[UDP-HTTP] Verbose/quiet command-line options

Add options -v and -q to handle verbose and quiet mode.
This commit is contained in:
Matteo Cypriani 2013-09-13 10:15:46 -04:00
parent d6fb1c04fc
commit 647b025840
3 changed files with 35 additions and 10 deletions

View File

@ -164,8 +164,6 @@ Work to do in OwlPS
= UDP to HTTP =
- Command-line options (-v/-q).
- Delete inactive clients after a given amount of time.
- Refactor prepare_answer().

View File

@ -77,10 +77,12 @@
#include <assert.h>
struct {
bool verbose ;
uint_fast16_t result_port ;
uint_fast16_t http_port ;
int nb_connections ;
} options = {
true, // verbose
OWL_DEFAULT_RESULT_PORT, // result_port
DEFAULT_HTTP_PORT, // http_port
DEFAULT_NB_CONNECTIONS // nb_connections
@ -88,9 +90,6 @@ struct {
char *program_name = NULL ;
// This needs to become an option when we introduce options
#define verbose true
results_list *results = NULL ;
unsigned int nb_results = 0 ;
sem_t lock_results ;
@ -165,7 +164,7 @@ int main(int argc, char *argv[])
free_results_list() ;
sem_destroy(&lock_results) ;
if (verbose)
if (options.verbose)
fprintf(stderr, "%s: exiting.\n", program_name) ;
return ret ;
@ -176,6 +175,9 @@ void parse_command_line(int argc, char **argv)
{
parse_options(argc, argv) ;
check_configuration() ;
if (options.verbose)
print_configuration() ;
}
@ -196,6 +198,12 @@ void parse_options(int argc, char **argv)
case 't' :
options.http_port = strtoul(optarg, NULL, 0) ;
break ;
case 'q' :
options.verbose = false ;
break ;
case 'v' :
options.verbose = true ;
break ;
case 'V' :
print_version() ;
exit(0) ;
@ -225,6 +233,20 @@ void check_configuration()
}
void print_configuration()
{
fprintf(stderr, "Options:\n"
"\tVerbose: %s\n"
"\tResult port: %"PRIuFAST16"\n"
"\tHTTP port: %"PRIuFAST16"\n"
,
OWL_BOOL_TO_STRING(options.verbose),
options.result_port,
options.http_port
) ;
}
/*
* Opens the UDP socket and reads from it the results sent by OwlPS
@ -395,7 +417,7 @@ void* tcp_server(void *NULL_value)
}
client_message[nbytes] = '\0' ;
if (verbose)
if (options.verbose)
printf("Got a message from the client:\n"
"\"%s\"\n", client_message) ;
@ -403,7 +425,7 @@ void* tcp_server(void *NULL_value)
extract_request_from_message(client_request, client_message) ;
prepare_answer(request_id) ;
if (verbose)
if (options.verbose)
printf("Answer to send:\n\"%s\"\n", answer) ;
/* Send the answer */
@ -624,13 +646,15 @@ void print_usage()
{
printf("Usage:\n"
"\t%s"
/* " [-v | -q]" */
" [-v | -q]"
" [-l result_port]"
" [-t http_port]\n"
"Options:\n"
"\t-h\t\tPrint this help.\n"
"\t-V\t\tPrint version information.\n"
"\t-v\t\tTurn on verbose mode (default).\n"
"\t-q\t\tDo not print informational messages.\n"
"\t-l result_port\tPort on which the results are received"
" (default: %d).\n"
"\t-t http_port\tPort on which the HTTP server listens"

View File

@ -23,7 +23,7 @@
/* Arguments & program configuration */
#define OPTIONS "hl:t:V" // getopt string
#define OPTIONS "hl:qt:vV" // getopt string
#define DEFAULT_HTTP_PORT 8080
#define DEFAULT_NB_CONNECTIONS 1
@ -58,6 +58,8 @@ typedef struct _results_list
void parse_command_line(int argc, char **argv) ;
void parse_options(int argc, char **argv) ;
void check_configuration(void) ;
void print_configuration(void) ;
int receive_udp(void) ;
void store_result(owl_result *result) ;
int init_tcp_socket(void) ;
@ -68,6 +70,7 @@ extract_request_from_message(char client_request[CLIENT_REQUEST_STRLEN],
void prepare_answer(int request_id) ;
void realloc_answer(size_t new_size) ;
void free_results_list(void) ;
void print_usage(void) ;
void print_version(void) ;