void parse_server_options(int argc, char ** argv, dtnperf_global_options_t * perf_g_opt) { char c, done = 0; boolean_t output_set = FALSE; dtnperf_options_t * perf_opt = perf_g_opt->perf_opt; dtnperf_connection_options_t * conn_opt = perf_g_opt->conn_opt; // kill daemon variables int pid; char cmd[256]; while (!done) { static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, {"memory", no_argument, 0, 'M'}, {"lifetime", required_argument, 0, 'l'}, {"debug", optional_argument, 0, 33}, // 33 because D is for data mode {"priority", required_argument, 0, 'p'}, {"ddir", required_argument, 0, 34}, {"fdir", required_argument, 0, 39}, {"acks-to-mon", no_argument, 0, 35}, // server only option {"ip-addr", required_argument, 0, 37}, {"ip-port", required_argument, 0, 38}, {"daemon", no_argument, 0, 'a'}, {"output", required_argument, 0, 'o'}, {"stop", no_argument, 0, 's'}, {0,0,0,0} // The last element of the array has to be filled with zeros. }; int option_index = 0; c = getopt_long(argc, argv, "hvMl:p:ao:s", long_options, &option_index); switch (c) { case 'h': print_server_usage(argv[0]); exit(0); return ; case 'v': perf_opt->verbose = TRUE; break; case 'M': perf_opt->use_file = 0; perf_opt->payload_type = BP_PAYLOAD_MEM; break; case 'l': conn_opt->expiration = atoi(optarg); break; case 'p': if (!strcasecmp(optarg, "bulk")) { conn_opt->priority.priority = BP_PRIORITY_BULK; } else if (!strcasecmp(optarg, "normal")) { conn_opt->priority.priority = BP_PRIORITY_NORMAL; } else if (!strcasecmp(optarg, "expedited")) { conn_opt->priority.priority = BP_PRIORITY_EXPEDITED; } else if (!strcasecmp(optarg, "reserved")) { conn_opt->priority.priority = BP_PRIORITY_RESERVED; } else { fprintf(stderr, "Invalid priority value %s\n", optarg); exit(1); } break; case 33: // debug perf_opt->debug = TRUE; if (optarg != NULL){ int debug_level = atoi(optarg); if (debug_level >= 1 && debug_level <= 2) perf_opt->debug_level = atoi(optarg) - 1; else { fprintf(stderr, "wrong --debug argument\n"); exit(1); return; } } else perf_opt->debug_level = 2; break; case 34: //incoming bundles destination directory perf_opt->dest_dir = strdup(optarg); break; case 35: //server send acks to monitor perf_opt->acks_to_mon = TRUE; break; case 36: //server do not send acks perf_opt->no_acks = TRUE; break; case 37: perf_opt->ip_addr = strdup(optarg); perf_opt->use_ip = TRUE; break; case 38: perf_opt->ip_port = atoi(optarg); perf_opt->use_ip = TRUE; break; case 39: perf_opt->file_dir = strdup(optarg); break; case 'a': perf_opt->daemon = TRUE; break; case 'o': perf_opt->server_output_file = strdup(optarg); output_set = TRUE; break; case 's': memset(cmd, 0, sizeof(cmd)); sprintf(cmd, "%s %s", argv[0], SERVER_STRING); pid = find_proc(cmd); if (pid) { printf("Closing dtnperf server pid: %d\n", pid); kill(pid, SIGINT); } else { fprintf(stderr, "ERROR: cannot find a running instance of dtnperf server\n"); } exit(0); break; case '?': fprintf(stderr, "Unknown option: %c\n", optopt); exit(1); break; case (char)(-1): done = 1; break; default: // getopt already prints an error message for unknown option characters print_server_usage(argv[0]); exit(1); } } if (output_set && !perf_opt->daemon) { fprintf(stderr, "\nSYNTAX ERROR: -o option can be used only with -a option\n"); \ print_server_usage(argv[0]); \ exit(1); } }
int main(int argc, char* argv[]) { int port = DEFAULT_PORT; char* calendarName = DEFAULT_CALENDAR_NAME; int create = 0; // Process command line options char c; while ((c = getopt (argc, argv, "cp:n:")) != -1) switch (c) { case 'c': create = 1; break; case 'p': port = atoi(optarg); break; case 'n': calendarName = optarg; break; default: print_server_usage(argv[0]); return 1; } // Create or open calendar database if (create) { if (new_calendar_with_name(calendarName) == -1) { printf("Error creating calendar %s: %s\n", calendarName, calendar_error_msg()); print_server_usage(argv[0]); return 1; } } else { if (calendar_open_with_name(calendarName) == -1) { printf("Error opening calendar %s: %s\n", calendarName, calendar_error_msg()); print_server_usage(argv[0]); return 1; } } // Creating the socket to receive incoming connections server_socket = myServerSocket(port); if (server_socket == -1) { perror(""); return -1; } // Client connection handling loop while (1) { int client = myAcceptServerSocket(server_socket); if (client == -1) perror("Error accepting client"); else { // NOT IMPLEMENTED - START pthread_t t; pthread_create(&t, NULL, handleClient, (void*)(&client)); } // NOT IMPLEMENTED - END //handleClient(client); // handle the client } return 0; }