[UDP-HTTP] Verbose/quiet command-line options
Add options -v and -q to handle verbose and quiet mode.
This commit is contained in:
parent
d6fb1c04fc
commit
647b025840
2
TODO.t2t
2
TODO.t2t
|
@ -164,8 +164,6 @@ Work to do in OwlPS
|
||||||
|
|
||||||
= UDP to HTTP =
|
= UDP to HTTP =
|
||||||
|
|
||||||
- Command-line options (-v/-q).
|
|
||||||
|
|
||||||
- Delete inactive clients after a given amount of time.
|
- Delete inactive clients after a given amount of time.
|
||||||
|
|
||||||
- Refactor prepare_answer().
|
- Refactor prepare_answer().
|
||||||
|
|
|
@ -77,10 +77,12 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
bool verbose ;
|
||||||
uint_fast16_t result_port ;
|
uint_fast16_t result_port ;
|
||||||
uint_fast16_t http_port ;
|
uint_fast16_t http_port ;
|
||||||
int nb_connections ;
|
int nb_connections ;
|
||||||
} options = {
|
} options = {
|
||||||
|
true, // verbose
|
||||||
OWL_DEFAULT_RESULT_PORT, // result_port
|
OWL_DEFAULT_RESULT_PORT, // result_port
|
||||||
DEFAULT_HTTP_PORT, // http_port
|
DEFAULT_HTTP_PORT, // http_port
|
||||||
DEFAULT_NB_CONNECTIONS // nb_connections
|
DEFAULT_NB_CONNECTIONS // nb_connections
|
||||||
|
@ -88,9 +90,6 @@ struct {
|
||||||
|
|
||||||
char *program_name = NULL ;
|
char *program_name = NULL ;
|
||||||
|
|
||||||
// This needs to become an option when we introduce options
|
|
||||||
#define verbose true
|
|
||||||
|
|
||||||
results_list *results = NULL ;
|
results_list *results = NULL ;
|
||||||
unsigned int nb_results = 0 ;
|
unsigned int nb_results = 0 ;
|
||||||
sem_t lock_results ;
|
sem_t lock_results ;
|
||||||
|
@ -165,7 +164,7 @@ int main(int argc, char *argv[])
|
||||||
free_results_list() ;
|
free_results_list() ;
|
||||||
sem_destroy(&lock_results) ;
|
sem_destroy(&lock_results) ;
|
||||||
|
|
||||||
if (verbose)
|
if (options.verbose)
|
||||||
fprintf(stderr, "%s: exiting.\n", program_name) ;
|
fprintf(stderr, "%s: exiting.\n", program_name) ;
|
||||||
|
|
||||||
return ret ;
|
return ret ;
|
||||||
|
@ -176,6 +175,9 @@ void parse_command_line(int argc, char **argv)
|
||||||
{
|
{
|
||||||
parse_options(argc, argv) ;
|
parse_options(argc, argv) ;
|
||||||
check_configuration() ;
|
check_configuration() ;
|
||||||
|
|
||||||
|
if (options.verbose)
|
||||||
|
print_configuration() ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -196,6 +198,12 @@ void parse_options(int argc, char **argv)
|
||||||
case 't' :
|
case 't' :
|
||||||
options.http_port = strtoul(optarg, NULL, 0) ;
|
options.http_port = strtoul(optarg, NULL, 0) ;
|
||||||
break ;
|
break ;
|
||||||
|
case 'q' :
|
||||||
|
options.verbose = false ;
|
||||||
|
break ;
|
||||||
|
case 'v' :
|
||||||
|
options.verbose = true ;
|
||||||
|
break ;
|
||||||
case 'V' :
|
case 'V' :
|
||||||
print_version() ;
|
print_version() ;
|
||||||
exit(0) ;
|
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
|
* 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' ;
|
client_message[nbytes] = '\0' ;
|
||||||
|
|
||||||
if (verbose)
|
if (options.verbose)
|
||||||
printf("Got a message from the client:\n"
|
printf("Got a message from the client:\n"
|
||||||
"\"%s\"\n", client_message) ;
|
"\"%s\"\n", client_message) ;
|
||||||
|
|
||||||
|
@ -403,7 +425,7 @@ void* tcp_server(void *NULL_value)
|
||||||
extract_request_from_message(client_request, client_message) ;
|
extract_request_from_message(client_request, client_message) ;
|
||||||
prepare_answer(request_id) ;
|
prepare_answer(request_id) ;
|
||||||
|
|
||||||
if (verbose)
|
if (options.verbose)
|
||||||
printf("Answer to send:\n\"%s\"\n", answer) ;
|
printf("Answer to send:\n\"%s\"\n", answer) ;
|
||||||
|
|
||||||
/* Send the answer */
|
/* Send the answer */
|
||||||
|
@ -624,13 +646,15 @@ void print_usage()
|
||||||
{
|
{
|
||||||
printf("Usage:\n"
|
printf("Usage:\n"
|
||||||
"\t%s"
|
"\t%s"
|
||||||
/* " [-v | -q]" */
|
" [-v | -q]"
|
||||||
" [-l result_port]"
|
" [-l result_port]"
|
||||||
" [-t http_port]\n"
|
" [-t http_port]\n"
|
||||||
|
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
"\t-h\t\tPrint this help.\n"
|
"\t-h\t\tPrint this help.\n"
|
||||||
"\t-V\t\tPrint version information.\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"
|
"\t-l result_port\tPort on which the results are received"
|
||||||
" (default: %d).\n"
|
" (default: %d).\n"
|
||||||
"\t-t http_port\tPort on which the HTTP server listens"
|
"\t-t http_port\tPort on which the HTTP server listens"
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
|
|
||||||
/* Arguments & program configuration */
|
/* Arguments & program configuration */
|
||||||
#define OPTIONS "hl:t:V" // getopt string
|
#define OPTIONS "hl:qt:vV" // getopt string
|
||||||
|
|
||||||
#define DEFAULT_HTTP_PORT 8080
|
#define DEFAULT_HTTP_PORT 8080
|
||||||
#define DEFAULT_NB_CONNECTIONS 1
|
#define DEFAULT_NB_CONNECTIONS 1
|
||||||
|
@ -58,6 +58,8 @@ typedef struct _results_list
|
||||||
void parse_command_line(int argc, char **argv) ;
|
void parse_command_line(int argc, char **argv) ;
|
||||||
void parse_options(int argc, char **argv) ;
|
void parse_options(int argc, char **argv) ;
|
||||||
void check_configuration(void) ;
|
void check_configuration(void) ;
|
||||||
|
void print_configuration(void) ;
|
||||||
|
|
||||||
int receive_udp(void) ;
|
int receive_udp(void) ;
|
||||||
void store_result(owl_result *result) ;
|
void store_result(owl_result *result) ;
|
||||||
int init_tcp_socket(void) ;
|
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 prepare_answer(int request_id) ;
|
||||||
void realloc_answer(size_t new_size) ;
|
void realloc_answer(size_t new_size) ;
|
||||||
void free_results_list(void) ;
|
void free_results_list(void) ;
|
||||||
|
|
||||||
void print_usage(void) ;
|
void print_usage(void) ;
|
||||||
void print_version(void) ;
|
void print_version(void) ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue