Handle the case WebIME reloads and losts init information 91/143391/2
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 9 Aug 2017 12:39:46 +0000 (21:39 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Wed, 9 Aug 2017 12:53:45 +0000 (21:53 +0900)
Change-Id: I62e9dc511919cdb5752e3a528d8c907406644231

src/legacy_support/websocket.cpp
src/legacy_support/websocket.h

index 507f3b8..6b3ea7f 100644 (file)
@@ -72,6 +72,8 @@ static int callback_http(struct lws *wsi,
 struct per_session_data__keyboard {
     int session_id;
     int valid;
+    int need_init;
+    int initialized;
 };
 
 static int callback_keyboard(struct lws *wsi,
@@ -112,6 +114,8 @@ static int callback_keyboard(struct lws *wsi,
         pss->session_id = ++last_session_id;
         LOGD("LWS_CALLBACK_ESTABLISHED : %p %d", g_ws_server_context, pss->session_id);
         pss->valid = false;
+        pss->need_init = false;
+        pss->initialized = false;
         if (g_ws_server_context) {
             lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
         }
@@ -123,31 +127,58 @@ static int callback_keyboard(struct lws *wsi,
 
     case LWS_CALLBACK_SERVER_WRITEABLE:
         if (agent) {
+            /* Ignore valid check since the magic key is not used anymore */
+            if (!pss->valid) pss->valid = true;
+
             /* We allow data tranmission only if this client is guaranteed to be valid */
             if (pss->valid) {
                 pthread_mutex_lock(&g_ws_server_mutex);
                 std::queue<ISE_MESSAGE>& messages = agent->get_send_message_queue();
-                if (messages.size() > 0) {
-                    ISE_MESSAGE &message = messages.front();
+
+                if (pss->need_init && !pss->initialized) {
+                    ISE_MESSAGE message;
+                    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 : %s", str.c_str());
+                    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);
-                    messages.pop();
+                    pss->need_init = false;
+                    pss->initialized = true;
+                } else {
+                    if (messages.size() > 0) {
+                        ISE_MESSAGE &message = messages.front();
+                        bool drop = false;
+                        if (message.command.compare(ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT]) == 0) {
+                            if (pss->initialized) {
+                                drop = true;
+                            } else {
+                                pss->initialized = true;
+                            }
+                        }
+                        if (!drop) {
+                            std::string str = CISEMessageSerializer::serialize(message);
+                            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);
+                        }
+                        messages.pop();
+                    }
                 }
-                pthread_mutex_unlock(&g_ws_server_mutex);
 
                 if (n < 0) {
-                    LOGE("ERROR %d writing to di socket\n", n);
+                    LOGE("ERROR %d writing to di socket %d", n, pss->session_id);
                     return -1;
                 }
 
                 if (messages.size() > 0) {
                     lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
                 }
+                pthread_mutex_unlock(&g_ws_server_mutex);
             } else {
-                LOGD("Rejecting data transmission since client is not valid");
+                LOGD("Rejecting data transmission since client is not valid : %d", pss->session_id);
             }
         }
         break;
@@ -168,8 +199,16 @@ static int callback_keyboard(struct lws *wsi,
                 }
                 */
                 pss->valid = true;
+
+                if (agent->initalized()) {
+                    pss->need_init = true;
+                    lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
+                }
             }
 
+            /* Ignore valid check since the magic key is not used anymore */
+            if (!pss->valid) pss->valid = true;
+
             if (pss->valid) {
                 pthread_mutex_lock(&g_ws_server_mutex);
                 std::queue<ISE_MESSAGE>& messages = agent->get_recv_message_queue();
@@ -186,7 +225,7 @@ static int callback_keyboard(struct lws *wsi,
                     pthread_mutex_unlock(&g_ws_query_mutex);
                 }
             } else {
-                LOGD("Ignoring data received since client is not valid");
+                LOGD("Ignoring data received since client is not valid %d", pss->session_id);
             }
         }
 
@@ -227,6 +266,7 @@ CWebHelperAgentWebSocket::CWebHelperAgentWebSocket()
     }
     m_current_instance = this;
     m_recv_message_pipe = NULL;
+    m_initialized = false;
 }
 
 CWebHelperAgentWebSocket::~CWebHelperAgentWebSocket()
@@ -302,6 +342,8 @@ bool CWebHelperAgentWebSocket::init()
 
     on_init();
 
+    m_initialized = true;
+
     return ret;
 }
 
index 811b3f9..1e86416 100644 (file)
@@ -205,6 +205,8 @@ public:
     bool init();
     bool exit();
 
+    bool initalized() { return m_initialized; }
+
     void signal(int sig);
 
     void on_init();
@@ -258,6 +260,8 @@ protected:
     std::queue<ISE_MESSAGE> m_recv_message_queue;
 
     Ecore_Pipe *m_recv_message_pipe;
+
+    bool m_initialized;
 };
 
 #endif // _WEB_HELPER_AGENT_WEBSOCKET_H_