/*
- * 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
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
+#include <getopt.h>
#include "sock_util.h"
#include "utils.h"
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");
}
}
parse_args(argc, argv);
+#if 0
if (client_type == TYPE_LEADER) {
listenfd = tcp_listen_connections(DEFAULT_LEADER_LISTEN_PORT,
DEFAULT_SERVER_BACKLOG);
close(sockfd);
close(connectfd);
+#endif
+
return 0;
}