diff --git a/TODO.t2t b/TODO.t2t index 0886348..f05fd58 100644 --- a/TODO.t2t +++ b/TODO.t2t @@ -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(). diff --git a/owlps-udp-to-http/owlps-udp-to-http.c b/owlps-udp-to-http/owlps-udp-to-http.c index 8152463..0d0f3b7 100644 --- a/owlps-udp-to-http/owlps-udp-to-http.c +++ b/owlps-udp-to-http/owlps-udp-to-http.c @@ -77,10 +77,12 @@ #include 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" diff --git a/owlps-udp-to-http/owlps-udp-to-http.h b/owlps-udp-to-http/owlps-udp-to-http.h index eaaa3e0..bef618e 100644 --- a/owlps-udp-to-http/owlps-udp-to-http.h +++ b/owlps-udp-to-http/owlps-udp-to-http.h @@ -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) ;