#include <libwebsockets.h>
+#define WEBSOCKET_PORT 7681
+
#define RECVED_MESSAGE "recved"
#define MESSAGE_LEFT "left"
{ 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)
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);
}
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);
if (n < 0) {
LOGE("ERROR %d writing to di socket %d", n, pss->session_id);
- return -1;
}
if (messages.size() > 0) {
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);
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);
ret = false;
}
} else {
+ LOGE("Failed creating server context : %p", g_ws_server_context);
ret = false;
}
m_initialized = true;
+ test_client_connection();
+
return ret;
}
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;
}