test-socket-signal: add getopt_long support
authorRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 24 Oct 2010 07:29:56 +0000 (10:29 +0300)
committerRazvan Deaconescu <razvan.deaconescu@cs.pub.ro>
Sun, 24 Oct 2010 09:23:34 +0000 (12:23 +0300)
Utils/test-socket-signal/peer.c

index 78e98f4..54a9041 100644 (file)
@@ -1,5 +1,8 @@
 /*
- * inet (BSD) socket client application
+ * inet (BSD) socket peer application
+ *    starts a socket listener (server part) and connects to another
+ *    peer (client part)
+ *
  * 2010, Razvan Deaconescu
  */
 
@@ -11,6 +14,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <signal.h>
+#include <getopt.h>
 
 #include "sock_util.h"
 #include "utils.h"
@@ -172,25 +176,67 @@ static void print_buffer_meta(void)
 
 static void usage(const char *argv0)
 {
-       fprintf(stderr, "Usage: %s leader | follower\n", argv0);
+       fprintf(stderr, "Usage: %s [-l | --leader] [-p | --port <listen-port>] [-b | --buffer-size <buffer-size>] [-f | --frequency <frequency>] <peer-host> <peer-port>\n", argv0);
 }
 
 static void parse_args(int argc, char **argv)
 {
-       if (argc < 2) {
-               usage(argv[0]);
-               exit(EXIT_FAILURE);
+       int c;
+       int digit_optind = 0;
+
+       while (1) {
+               int this_option_optind = optind ? optind : 1;
+               int option_index = 0;
+               static struct option long_options[] = {
+                       {"leader", 0, NULL, 0},
+                       {"port", 1, NULL, 0},
+                       {"buffer-size", 1, NULL, 0},
+                       {"frequency", 1, NULL, 0},
+                       {0, 0, 0, 0}
+               };
+
+               c = getopt_long(argc, argv, "lp:b:f:",
+                               long_options, &option_index);
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 0:
+                       printf("option %s", long_options[option_index].name);
+                       if (optarg)
+                               printf(" with arg %s", optarg);
+                       printf("\n");
+                       break;
+
+               case 'l':
+                       printf("option l\n");
+                       break;
+
+               case 'p':
+                       printf("option l with value '%s'\n", optarg);
+                       break;
+
+               case 'b':
+                       printf("option b with value '%s'\n", optarg);
+                       break;
+
+               case 'f':
+                       printf("option f with value '%s'\n", optarg);
+                       break;
+
+               case '?':
+                       break;
+
+               default:
+                       printf("?? getopt returned character code 0%o ??\n", c);
+               }
        }
 
-       if (strcmp(argv[1], "leader") == 0)
-               client_type = TYPE_LEADER;
-       else if (strcmp(argv[1], "follower") == 0)
-               client_type = TYPE_FOLLOWER;
-       else if (strcmp(argv[1], "oldleader") == 0)
-               client_type = TYPE_OLD_LEADER;
-       else {
-               usage(argv[0]);
-               exit(EXIT_FAILURE);
+       if (optind < argc) {
+               printf("non-option ARGV-elements: ");
+               while (optind < argc)
+                       printf("%s ", argv[optind++]);
+               printf("\n");
        }
 }
 
@@ -203,6 +249,7 @@ int main(int argc, char **argv)
 
        parse_args(argc, argv);
 
+#if 0
        if (client_type == TYPE_LEADER) {
                listenfd = tcp_listen_connections(DEFAULT_LEADER_LISTEN_PORT,
                                DEFAULT_SERVER_BACKLOG);
@@ -276,5 +323,7 @@ int main(int argc, char **argv)
        close(sockfd);
        close(connectfd);
 
+#endif
+
        return 0;
 }