From 5d94fe8cc47596b3e6f7dea06247948e5f356b88 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 29 Aug 2012 12:20:34 +0200 Subject: [PATCH] [Client] Add option -N (number of requests) New option to use in conjunction with -F, in order to stop after a given number of requests. --- owlps-client/owlps-client.c | 45 ++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/owlps-client/owlps-client.c b/owlps-client/owlps-client.c index af55e55..58918df 100644 --- a/owlps-client/owlps-client.c +++ b/owlps-client/owlps-client.c @@ -39,7 +39,7 @@ #define MAX_PKT_SIZE 1450u /* Program arguments (getopt string) */ -#define OPTIONS "DF::hi:I:l::n:p:s:t:V" +#define OPTIONS "DF::hi:I:l::n:N:p:s:t:V" /* Function headers */ @@ -74,6 +74,7 @@ struct { uint_fast16_t nb_pkt ; // Number of packets to send uint_fast16_t pkt_size ; // Size of the packet to send int_fast32_t flood_delay ; // Time between two request transmissions + uint_fast16_t nb_requests ; // Number of requests to send uint_fast16_t listening_port ; // Calibration data: owl_direction direction ; @@ -89,6 +90,7 @@ struct { 0, // nb_pkt 0, // pkt_size -1, // flood_delay + 0, // nb_requests 0, // listening_port 0, 0, 0, 0 // Calibration data } ; @@ -110,6 +112,8 @@ int main(int argc, char *argv[]) { struct sigaction action ; // Signal handler structure int ret = 0 ; + // Number of requests we still have to transmit: + uint_fast16_t nb_requests_left = 1 ; program_name = argv[0] ; parse_command_line(argc, argv) ; @@ -132,11 +136,17 @@ int main(int argc, char *argv[]) create_socket() ; send_request() ; - while (owl_run && options.flood_delay >= 0) + if (options.nb_requests) + nb_requests_left = options.nb_requests - 1 ; + while (owl_run && options.flood_delay >= 0 && nb_requests_left > 0) { owl_msleep(options.flood_delay) ; if (owl_run) // owl_run can have been set to false during the sleep - send_request() ; + { + send_request() ; + if (options.nb_requests) + --nb_requests_left ; + } } close(sockfd) ; @@ -238,6 +248,9 @@ void parse_main_options(int argc, char **argv) case 'n' : options.nb_pkt = strtoul(optarg, NULL, 0) ; break ; + case 'N' : + options.nb_requests = strtoul(optarg, NULL, 0) ; + break ; case 'p' : options.dest_port = strtoul(optarg, NULL, 0) ; break ; @@ -386,15 +399,25 @@ void check_configuration() } } else // Flood is unactivated - if (options.daemon) - { + { + if (options.nb_requests) + { +#ifdef DEBUG + fprintf(stderr, "Warning! The -N option can be used only along" + " with -F. Option -N ignored.\n") ; +#endif // DEBUG + options.nb_requests = 0 ; + } + if (options.daemon) + { #ifdef DEBUG fprintf(stderr, "Warning! It is useless to detach from" " the foreground if the flood mode is not activated" " Option -D ignored.\n") ; #endif // DEBUG options.daemon = owl_false ; - } + } + } } @@ -409,6 +432,7 @@ void print_configuration() "\tDelay (ms): %"PRIdFAST32"\n" "\tNumber of packets: %"PRIuFAST16"\n" "\tFlood delay (ms): %"PRIdFAST32"\n" + "\tNumber of requests: %"PRIuFAST16"\n" "\tListening port: %"PRIuFAST16"\n" "\tDirection: %"PRIu8"\n" "\tX: %f\n" @@ -421,6 +445,7 @@ void print_configuration() options.delay, options.nb_pkt, options.flood_delay, + options.nb_requests, options.listening_port, options.direction, options.x, @@ -623,7 +648,8 @@ void print_usage() "\n\t" " [-n nb_packets]" " [-s packet_size]" - " [-F [delay] [-D]]" + " [-F [delay] [-N nb_requests] [-D]]" + "\n\t" " [-l [port]]\n" "Calibration request:\n" "\t%s" @@ -634,7 +660,8 @@ void print_usage() "\n\t" " [-n nb_packets]" " [-s packet_size]" - " [-F [delay] [-D]]" + " [-F [delay] [-N nb_requests] [-D]]" + "\n\t" " direction x y z\n" "Options:\n" @@ -663,6 +690,8 @@ void print_usage() "\t-F [delay]\t\"Flood mode\": loop indefinitely, sending a" " new request\n\t\t\tevery milliseconds (default:" " %d ms).\n" + "\t-N nb_requests\tWith -F, stop after requests" + " transmitted\n\t\t\tinstead of looping indefinitely.\n" "\t-D\t\tDaemon mode. Useful only in flood mode.\n" "\t-l [port]\tWait for the computed position and display it." " The\n\t\t\toptional argument allows to specify the"