From: Ji-hoon Lee Date: Wed, 9 Aug 2017 13:29:08 +0000 (+0900) Subject: Add connection test client that checks libwebsocket server is alive X-Git-Tag: accepted/tizen/3.0/common/20170810.115428~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0f42da1134d6e74f3575ca55cef0ee4618cf8ab1;p=platform%2Fcore%2Fuifw%2Flibscl-core.git Add connection test client that checks libwebsocket server is alive Change-Id: I7b668aca4e3b1afb5b47bb249d9c28916af0d6ec --- diff --git a/src/legacy_support/websocket.cpp b/src/legacy_support/websocket.cpp index 6b3ea7f..33d0c81 100644 --- a/src/legacy_support/websocket.cpp +++ b/src/legacy_support/websocket.cpp @@ -35,6 +35,8 @@ #include +#define WEBSOCKET_PORT 7681 + pthread_t g_ws_server_thread = (pthread_t)NULL; pthread_mutex_t g_ws_server_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -96,6 +98,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) @@ -140,7 +218,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); @@ -159,7 +237,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); @@ -170,7 +248,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) { @@ -296,7 +373,7 @@ bool CWebHelperAgentWebSocket::init() struct lws_context_creation_info info; memset(&info, 0, sizeof info); - info.port = 7681; + info.port = WEBSOCKET_PORT; int debug_level = LLL_DEBUG; lws_set_log_level(debug_level, log_func); @@ -317,13 +394,13 @@ bool CWebHelperAgentWebSocket::init() m_recv_message_pipe = ecore_pipe_add(recv_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); @@ -337,6 +414,7 @@ bool CWebHelperAgentWebSocket::init() ret = false; } } else { + LOGE("Failed creating server context : %p", g_ws_server_context); ret = false; } @@ -344,6 +422,8 @@ bool CWebHelperAgentWebSocket::init() m_initialized = true; + test_client_connection(); + return ret; } @@ -368,6 +448,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; }