Add connection test client that checks libwebsocket server is alive 94/175394/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 9 Aug 2017 13:29:08 +0000 (22:29 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Tue, 10 Apr 2018 05:14:29 +0000 (14:14 +0900)
Change-Id: I3597d74988c98b5fb0154ed02106ad677f091ae0

src/legacy_support/websocket.cpp

index f3946d6..8d13e4d 100644 (file)
@@ -35,6 +35,8 @@
 
 #include <libwebsockets.h>
 
+#define WEBSOCKET_PORT 7681
+
 #define RECVED_MESSAGE "recved"
 #define MESSAGE_LEFT "left"
 
@@ -99,6 +101,82 @@ static struct lws_protocols protocols[] = {
     { NULL, NULL, 0, 0 }
 };
 
+
+static int callback_client(struct lws *wsi,
+    enum lws_callback_reasons reason,
+    void *user, void *in, size_t len)
+{
+    switch (reason) {
+    case LWS_CALLBACK_CLIENT_ESTABLISHED:
+        LOGD("[ClientTest] Connection established");
+        break;
+
+    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
+        LOGD("[ClientTest] Connection error");
+        break;
+
+    case LWS_CALLBACK_CLOSED:
+        LOGD("[ClientTest] Connection closed");
+        break;
+
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+int test_client_connection(void)
+{
+    struct lws_context *context = NULL;
+    struct lws_context_creation_info context_info;
+    struct lws_client_connect_info connect_info;
+    struct lws *wsi = NULL;
+
+    memset(&context_info, 0, sizeof context_info);
+    memset(&connect_info, 0, sizeof(connect_info));
+
+    const int protocols_num = sizeof(protocols) / sizeof(lws_protocols);
+    static struct lws_protocols client_protocols[protocols_num];
+
+    memcpy(&client_protocols, protocols, sizeof(protocols));
+    for (int loop = 0; loop < protocols_num - 1; loop++) {
+        client_protocols[loop].callback = callback_client;
+    }
+
+    context_info.port = CONTEXT_PORT_NO_LISTEN;
+    context_info.protocols = protocols;
+    context_info.gid = -1;
+    context_info.uid = -1;
+
+    context = lws_create_context(&context_info);
+    LOGD("[ClientTest] create_context : %p", context);
+    if (context == NULL) {
+        return -1;
+    }
+
+    connect_info.address = "localhost";
+    connect_info.port = WEBSOCKET_PORT;
+    connect_info.path = "/";
+    connect_info.context = context;
+    connect_info.ssl_connection = 0;
+    connect_info.host = connect_info.address;
+    connect_info.origin = connect_info.address;
+    connect_info.ietf_version_or_minus_one = -1;
+    connect_info.protocol = "keyboard-protocol";
+
+    wsi = lws_client_connect_via_info(&connect_info);
+    LOGD("[ClientTest] wsi created : %p", wsi);
+
+    if (wsi) {
+        lws_service(context, 50);
+    }
+
+    lws_context_destroy(context);
+
+    return 0;
+}
+
 static int callback_keyboard(struct lws *wsi,
         enum lws_callback_reasons reason,
         void *user, void *in, size_t len)
@@ -143,7 +221,7 @@ static int callback_keyboard(struct lws *wsi,
                     message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_PLAIN];
                     message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT];
                     std::string str = CISEMessageSerializer::serialize(message);
-                    LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
+                    SECURE_LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
                     n = snprintf((char *)p, bufsize, "%s", str.c_str());
                     /* too small for partial */
                     n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
@@ -163,7 +241,7 @@ static int callback_keyboard(struct lws *wsi,
                         }
                         if (!drop) {
                             std::string str = CISEMessageSerializer::serialize(message);
-                            LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
+                            SECURE_LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
                             n = snprintf((char *)p, bufsize, "%s", str.c_str());
                             /* too small for partial */
                             n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
@@ -178,7 +256,6 @@ static int callback_keyboard(struct lws *wsi,
 
                 if (n < 0) {
                     LOGE("ERROR %d writing to di socket %d", n, pss->session_id);
-                    return -1;
                 }
 
                 if (messages.size() > 0) {
@@ -312,7 +389,7 @@ bool CWebHelperAgentWebSocket::init()
     struct lws_context_creation_info info;
 
     memset(&info, 0, sizeof info);
-    info.port = 7681;
+    info.port = WEBSOCKET_PORT;
 
     int log_level = LLL_ERR | LLL_WARN | LLL_DEBUG;
     lws_set_log_level(log_level, log_func);
@@ -333,13 +410,13 @@ bool CWebHelperAgentWebSocket::init()
     m_message_pipe = ecore_pipe_add(message_pipe_handler, NULL);
 
     /* Let's retry creating server context for a certain number of times */
-    const int max_retry_num = 10;
+    const int max_retry_num = 30;
     int retry_num = 0;
 
     do {
         g_ws_server_context = lws_create_context(&info);
         LOGD("libwebsocket context : %p", g_ws_server_context);
-        sleep(1);
+        usleep(100 * 1000);
     } while (g_ws_server_context == NULL && retry_num++ < max_retry_num);
 
     pthread_mutex_init(&g_ws_server_mutex, NULL);
@@ -353,6 +430,7 @@ bool CWebHelperAgentWebSocket::init()
             ret = false;
         }
     } else {
+        LOGE("Failed creating server context : %p", g_ws_server_context);
         ret = false;
     }
 
@@ -360,6 +438,8 @@ bool CWebHelperAgentWebSocket::init()
 
     m_initialized = true;
 
+    test_client_connection();
+
     return ret;
 }
 
@@ -384,6 +464,7 @@ bool CWebHelperAgentWebSocket::exit()
     pthread_mutex_destroy(&g_ws_server_mutex);
 
     if (g_ws_server_context) {
+        lws_cancel_service(g_ws_server_context);
         lws_context_destroy(g_ws_server_context);
         g_ws_server_context = NULL;
     }