examples/coap-server : add flag to prevent duplicate run of coap-server
authorJin-Seong Kim <jseong82.kim@samsung.com>
Wed, 31 May 2017 02:09:33 +0000 (11:09 +0900)
committerEunBong Song <eunb.song@samsung.com>
Tue, 11 Jul 2017 01:26:57 +0000 (10:26 +0900)
This commit is patch to add flag to prevent duplicate run of coap-server

Change-Id: I8259a8e0a2708785d53870b42a2b971dcc4baf27
Signed-off-by: Jin-Seong Kim <jseong82.kim@samsung.com>
apps/examples/libcoap_test/coap-server.c

index 743ca89..1145f28 100644 (file)
 #endif
 #endif
 
+enum server_status {
+       SERVER_STOPPED = 0,
+       SERVER_RUNNING,
+       SERVER_UNKNOWN
+};
+static enum server_status g_operating_status;
+
 /* temporary storage for dynamic resource representations */
 static int quit = 0;
 
@@ -60,11 +67,13 @@ struct coap_resource_t *time_resource = NULL;
 static coap_async_state_t *async = NULL;
 #endif                                                 /* WITHOUT_ASYNC */
 
+#if !defined (__TINYARA__)
 /* SIGINT handler: set quit to 1 for graceful termination */
 static void handle_sigint(int signum)
 {
        quit = 1;
 }
+#endif
 
 #define INDEX "This is a test server made with libcoap (see http://libcoap.sf.net)\n" \
              "Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>\n\n"
@@ -354,16 +363,20 @@ int coap_server_test_main(int argc, char **argv)
                        }
                        strncpy(addr_str, optarg, NI_MAXHOST - 1);
                        addr_str[NI_MAXHOST - 1] = '\0';
+                       printf("coap-server : addresss %s\n", addr_str);
                        break;
                case 'p':
                        strncpy(port_str, optarg, NI_MAXSERV - 1);
                        port_str[NI_MAXSERV - 1] = '\0';
+                       printf("coap-server : port %s\n", port_str);
                        break;
                case 'v':
                        log_level = strtol(optarg, NULL, 10);
+                       printf("coap-server : log_level %d\n", log_level);
                        break;
                case 'Q':
                        quit = 1;
+                       printf("coap-server : set quit flag %d\n", quit);
                        return 0;
                default:
                        usage(argv[0], PACKAGE_VERSION);
@@ -371,6 +384,11 @@ int coap_server_test_main(int argc, char **argv)
                }
        }
 
+       if (g_operating_status == SERVER_RUNNING) {
+               fprintf(stderr, "coap-server : error, another coap_server is running\n");
+               return 0;
+       }
+
        coap_set_log_level(log_level);
 
        ctx = get_context(addr_str, port_str);
@@ -382,10 +400,13 @@ int coap_server_test_main(int argc, char **argv)
        /* initialize global variables */
        quit = 0;
 
-#if 0
+#if !defined (__TINYARA__)
        signal(SIGINT, handle_sigint);
 #endif
 
+       g_operating_status = SERVER_RUNNING;
+       printf("coap-server : coap_server is started\n");
+
        while (!quit) {
                FD_ZERO(&readfds);
                FD_SET(ctx->sockfd, &readfds);
@@ -441,5 +462,9 @@ int coap_server_test_main(int argc, char **argv)
                coap_free(addr_str);
        }
 
+       g_operating_status = SERVER_STOPPED;
+
+       printf("coap-server : good bye\n");
+
        return 0;
 }