Fixed problem while transfering binary message 07/101807/3
authorkmook <kmook.choi@samsung.com>
Fri, 2 Dec 2016 07:19:55 +0000 (16:19 +0900)
committerkmook <kmook.choi@samsung.com>
Fri, 2 Dec 2016 08:47:45 +0000 (17:47 +0900)
Change-Id: Idf671284cbbd08e64afd333e449b4d4a20de466f
Signed-off-by: kmook <kmook.choi@samsung.com>
daemon/service_provider/AppCommServiceApplication.cpp
daemon/service_provider/AppCommServiceProvider.cpp
lib/conv_lib_payload.cpp
msf_tizen_client/include/Channel.h
msf_tizen_client/include/Service.h
msf_tizen_client/src/Channel.cpp
msf_tizen_client/src/Service.cpp

index ea314a2..ab601cd 100755 (executable)
@@ -168,7 +168,10 @@ void conv::AppCommServiceApplication::onMessage(Message message)
 
                result.set(NULL, CONV_JSON_DESCRIPTION, description);
                result.set(NULL, CONV_JSON_PAYLOAD, payload);
+
+               _D("size of msg:%d", result.str().length());
                (*requestObj)->publish(CONV_ERROR_NONE, result);
+               _D("publishing done");
        }
 }
 
index f2c85b2..c3bd805 100755 (executable)
@@ -197,8 +197,10 @@ int conv::AppCommServiceProvider::startRequest(Request* requestObj)
        if ( svcInfo->isLocal ) {
                _D("COMMUNCATION_START : local channel. channel_id : %s", channelId.c_str());
 
-               appInfo->localService = Service::getLocal();
+               int error = 0;
+               appInfo->localService = Service::getLocal(&error);
 
+               IF_FAIL_RETURN_TAG(error == 0, CONV_ERROR_INVALID_OPERATION, _E, "get local service failed");
                IF_FAIL_RETURN_TAG(secureMode.compare("1") || appInfo->localService.IsSecureConnectionSupport(), CONV_ERROR_INVALID_PARAMETER, _E, "service does not support secure mode");
 
                Channel* application = appInfo->localService.createChannel(channelId);
@@ -500,7 +502,14 @@ int conv::AppCommServiceProvider::getServiceInfoForDiscovery(Json* jsonObj)
                return CONV_ERROR_NOT_SUPPORTED;
        } else {
                char uri[200];
-               Service localService = Service::getLocal();
+               int error = 0;
+               Service localService = Service::getLocal(&error);
+
+               if (error != 0) {
+                       _E("get local service failed");
+                       g_free(address);
+                       return CONV_ERROR_INVALID_OPERATION;
+               }
 
                if (localService.getUri().empty()) {
                        g_free(address);
index aa8332c..54709f1 100755 (executable)
@@ -106,7 +106,7 @@ EXTAPI int conv_payload_set_app_control(conv_payload_h handle, const char* key,
        std::string appctl_str = reinterpret_cast<const char*>(appctl_raw);
        handle->jpayload.set(NULL, key, appctl_str);
 
-       bundle_free(appctl_bundle);
+       bundle_free_encoded_rawdata(&appctl_raw);
 
        return CONV_ERROR_NONE;
 }
@@ -152,8 +152,7 @@ EXTAPI int conv_payload_set_byte(conv_payload_h handle, const char* key, int len
        ASSERT_NOT_NULL(key);
        ASSERT_NOT_NULL(value);
 
-       std::string str_value(reinterpret_cast<const char*>(value), length);
-
+       std::string str_value = g_base64_encode(value, length);
        handle->jpayload.set(NULL, key, str_value);
 
        return CONV_ERROR_NONE;
@@ -173,9 +172,10 @@ EXTAPI int conv_payload_get_byte(conv_payload_h handle, const char* key, int* le
        // Check Invalid record key
        std::string str;
        IF_FAIL_RETURN_TAG(handle->jpayload.get(NULL, key, &str), CONV_ERROR_INVALID_PARAMETER, _E, "Value load failed");
+       unsigned int unsigned_len;
 
-       *value = reinterpret_cast<unsigned char*>(g_strdup(str.c_str()));
-       *length = str.size();
+       *value = g_base64_decode(g_strdup(str.c_str()), &unsigned_len);
+       *length = (int)unsigned_len;
 
        ASSERT_ALLOC(*value);
 
index f6f7ef2..9f4df29 100755 (executable)
@@ -267,8 +267,9 @@ private:
        //bool binary_message;
        bool disconnecting;
        //long buflen;
-       unsigned char cl_payload[1000];
+       unsigned char *cl_payload;
        int cl_payload_size;
+       bool is_header_parsed;
        int was_closed;
        Client client;
        long long clientconnectTime;
index e0231c1..4f26512 100755 (executable)
@@ -79,7 +79,7 @@ public:
        string getVersion();
        bool IsSecureConnectionSupport();
        static string getUniqueId(string address);
-       static Service getLocal(void);
+       static Service getLocal(int* error);
        static void getByURI(string, Result_Base*);
        static void getByURI(string, long, Result_Base *result);
        static void getById(string id, Result_Base *result);
index f1cd636..bfe5f50 100755 (executable)
@@ -92,6 +92,9 @@ Channel::Channel() {
        write_buf_count = 0;
        write_buf_index = 0;
        write_buf_last_sent_index = 0;
+       cl_payload = NULL;
+       is_header_parsed = false;
+       eventType = "";
 }
 
 Channel::Channel(Service *service1, string uri1) {
@@ -117,6 +120,9 @@ Channel::Channel(Service *service1, string uri1) {
        write_buf_count = 0;
        write_buf_index = 0;
        write_buf_last_sent_index = 0;
+       cl_payload = NULL;
+       is_header_parsed = false;
+       eventType = "";
 }
 
 Channel *Channel::create(Service *service, string uri) {
@@ -146,6 +152,11 @@ Channel::~Channel() {
                delete connectionHandler;
                connectionHandler = NULL;
        }
+
+       if (cl_payload != NULL) {
+               free(cl_payload);
+               cl_payload = NULL;
+       }
 }
 
 void Channel::init_json_key_map() {
@@ -649,7 +660,6 @@ void Channel::disconnect(Result_Base *result1) {
                }
 
                connectionHandler->stopPing();
-
                was_closed = true;
                lws_callback_on_writable(wsi_mirror);
 
@@ -861,49 +871,86 @@ int Channel::callback_lws_mirror(struct lws *wsi,
                        return -1;
                } else {
                }
-
-               this_ptr->eventType = "";
-
                if (lws_frame_is_binary(wsi)) {
-                       MSF_DBG("BINARY MESSAGE ARRIVED");
+                       MSF_DBG("BINARY MESSAGE ARRIVED. len:%d", len);
+                       // header needs to be parsed on first chunk
+                       if (this_ptr->is_header_parsed == false) {
+                               MSF_DBG("first chunk. parsing header");
+                               int header_size = 0;
 
-                       int header_size = 0;
+                               header_size = (int)((unsigned char *)in)[0];
+                               header_size = header_size << 8;
+                               header_size = header_size & 0xff00;
+                               header_size = header_size | (int)((unsigned char *)in)[1];
+                               MSF_DBG("header_size = %d", header_size);
 
-                       header_size = (int)((unsigned char *)in)[0];
-                       header_size = header_size << 8;
-                       header_size = header_size & 0xff00;
-                       header_size = header_size | (int)((unsigned char *)in)[1];
-                       MSF_DBG("header_size = %d", header_size);
+                               char header_json[header_size + 1] = {0};
 
-                       char header_json[header_size + 1] = {0};
+                               memcpy(&header_json[0], &(((unsigned char *)in)[2]), header_size);
+                               header_json[header_size + 1] = 0;
 
-                       memcpy(&header_json[0], &(((unsigned char *)in)[2]), header_size);
-                       header_json[header_size + 1] = 0;
+                               MSF_DBG("in = %s", &header_json[0]);
 
-                       MSF_DBG("in = %s", &header_json[0]);
+                               this_ptr->json_parse(header_json);
 
-                       this_ptr->json_parse(header_json);
-                       memcpy(&(this_ptr->cl_payload),
-                                       &(((unsigned char *)in)[2 + header_size]),
-                                       len - 2 - header_size);
+                               this_ptr->cl_payload = (unsigned char*)calloc(len - 2 - header_size, sizeof(unsigned char));
+                               memcpy(this_ptr->cl_payload, &(((unsigned char*)in)[2 + header_size]), len - 2 - header_size);
 
-                       this_ptr->cl_payload[len - 2 - header_size] = 0;
-                       this_ptr->cl_payload_size = len - 2 - header_size;
+                               this_ptr->cl_payload_size = len - 2 - header_size;
+                               this_ptr->connectionHandler->resetLastPingReceived();
+                               this_ptr->is_header_parsed = true;
 
-/*
-                       MSF_DBG("payload = %s",
-                                               &(this_ptr->cl_payload[0]));
-*/
-                       this_ptr->connectionHandler->resetLastPingReceived();
+                               MSF_DBG("type:%s", this_ptr->eventType.c_str());
 
-                       if (this_ptr->eventType == CONNECT_EVENT) {
-                               this_ptr->handleConnectMessage(this_ptr->UID);
+                               if (lws_remaining_packet_payload(wsi) == 0) {
+                                       MSF_DBG("there's no remaining packet");
+                                       if (this_ptr->eventType == CONNECT_EVENT) {
+                                               MSF_DBG("handle connect message");
+                                               this_ptr->handleConnectMessage(this_ptr->UID);
+                                       } else {
+                                               MSF_DBG("handle user message");
+                                               this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+                                       }
+
+                                       if (this_ptr->cl_payload) {
+                                               free(this_ptr->cl_payload);
+                                               this_ptr->cl_payload = NULL;
+                                               this_ptr->cl_payload_size = 0;
+                                               this_ptr->is_header_parsed = false;
+                                               this_ptr->eventType.clear();
+                                       }
+                               }
                        } else {
-                               // this_ptr->handleMessage(this_ptr->UID);
-                               this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+                               MSF_DBG("reallocating to copy payload");
+
+                               this_ptr->cl_payload = (unsigned char*)realloc(this_ptr->cl_payload, this_ptr->cl_payload_size + len);
+                               memcpy(&(this_ptr->cl_payload[this_ptr->cl_payload_size]), (char*)in, len);
+
+                               this_ptr->cl_payload_size += len;
+
+                               this_ptr->connectionHandler->resetLastPingReceived();
+
+                               if (lws_remaining_packet_payload(wsi) == 0) {
+                                       MSF_DBG("there's no remaining packet");
+                                       if (this_ptr->eventType == CONNECT_EVENT) {
+                                               MSF_DBG("handle connect message");
+                                               this_ptr->handleConnectMessage(this_ptr->UID);
+                                       } else {
+                                               MSF_DBG("handle user message");
+                                               this_ptr->handleMessage(this_ptr->UID, this_ptr->cl_payload);
+                                       }
+
+                                       if (this_ptr->cl_payload) {
+                                               free(this_ptr->cl_payload);
+                                               this_ptr->cl_payload = NULL;
+                                               this_ptr->cl_payload_size = 0;
+                                               this_ptr->is_header_parsed = false;
+                                               this_ptr->eventType.clear();
+                                       }
+                               }
                        }
                } else {
-                       MSF_DBG("TEXT MESSAGE ARRIVED");
+                       MSF_DBG("TEXT MESSAGE ARRIVED. len:%d", len);
                        this_ptr->json_parse((char *)in);
                        this_ptr->connectionHandler->resetLastPingReceived();
 
@@ -914,7 +961,6 @@ int Channel::callback_lws_mirror(struct lws *wsi,
                                this_ptr->handleMessage(this_ptr->UID, NULL);
                        }
                }
-
                break;
 
        case LWS_CALLBACK_CLIENT_WRITEABLE:
@@ -1319,10 +1365,6 @@ void Channel::create_websocket(void *att) {
 
        // loop until socket closed
        while (n >= 0 && !was_closed) {
-               if (was_closed) {
-                       break;
-               }
-
                if (wsi_mirror == NULL) {
                        wsi_mirror = lws_client_connect_via_info(&connect_info);
                        if (wsi_mirror == NULL) {
index 176e767..dfd3f3c 100755 (executable)
@@ -207,24 +207,32 @@ string Service::getUniqueId(string address)
        return ret_id;
 }
 
-Service Service::getLocal(void)
+Service Service::getLocal(int* error)
 {
        class GetLocalServiceCallback : public Result_Base
        {
                public:
+               int* error;
                        void onSuccess(Service service)
                        {
                                MSF_DBG("service name : %s\n", service.getName().c_str() ? service.getName().c_str() : "name is NULL");
                                local_service = service;
+                               if (error != NULL) {
+                                       *error = 0;
+                               }
                        }
 
                        void onError(Error)
                        {
                                MSF_DBG("getLocal() : Fail to get local service info");
+                               if (error != NULL) {
+                                       *error = -1;
+                               }
                        }
        };
 
        GetLocalServiceCallback r1Service;
+       r1Service.error = error;
        string uri = "http://127.0.0.1:8001/api/v2/";
        getByURI(uri, 2000, &r1Service);