Fixed potential race condition on getByURI 13/108613/2
authorkmook <kmook.choi@samsung.com>
Thu, 5 Jan 2017 08:03:31 +0000 (17:03 +0900)
committerKyoung-Mook Choi <kmook.choi@samsung.com>
Thu, 5 Jan 2017 08:03:54 +0000 (00:03 -0800)
Change-Id: I445765de9552039bb9daf4f31412d8409cd4ca6c
Signed-off-by: kmook <kmook.choi@samsung.com>
msf_tizen_client/include/Service.h
msf_tizen_client/src/Service.cpp

index 4f26512..4986fc7 100755 (executable)
@@ -66,7 +66,6 @@ class Service
 public:
        Service();
        Service(string, string, string, string, string);
-       static Result_Base *Resulturi;
        Result_Base *Resultdevice;
 
        position findServiceValue(string, char *);
@@ -83,11 +82,11 @@ public:
        static void getByURI(string, Result_Base*);
        static void getByURI(string, long, Result_Base *result);
        static void getById(string id, Result_Base *result);
-       static int curl_service_calling(string uri, long, void *);
+       static int curl_service_calling(string uri, long, void *, Result_Base *);
        //static Search *search();
        static size_t createdata(char *buf, size_t size, size_t nmemb, void *up);
-       static void createdata_process(string data,  void *);
-       static int json_parse_service(const char *in,  void *);
+       static void createdata_process(string data,  void *, Result_Base *);
+       static int json_parse_service(const char *in,  void *, Result_Base *);
        static Service create(ServiceInfo);
        static Service create(map<string, string>);
        Channel *createChannel(string uri);
index dfd3f3c..579abd0 100755 (executable)
@@ -30,8 +30,6 @@ string Service::NAME_PROPERTY       = "fn";
 string Service::TYPE_PROPERTY       = "md";
 string Service::ENDPOINT_PROPERTY   = "se";
 
-Result_Base *Service::Resulturi = NULL;
-
 Service Service::local_service;
 
 bool Service::success_get_id = false;
@@ -168,7 +166,7 @@ position Service::findServiceValue(string substrng, char *strng)
 
 void Service::getDeviceInfo(Result_Base *dev_result)
 {
-       curl_service_calling(uri, 5000, dev_result);
+       curl_service_calling(uri, 5000, dev_result, NULL);
 }
 
 string Service::getUniqueId(string address)
@@ -195,7 +193,9 @@ string Service::getUniqueId(string address)
        GetLocalServiceCallback r1Service;
        string remote_uri = "http://" + address + "/api/v2/";
        success_get_id = false;
+       MSF_DBG("calling getByUri");
        getByURI(remote_uri, 5000, &r1Service);
+       MSF_DBG("called getByUri");
 
        if (success_get_id) {
                MSF_DBG("remote_device_id[%s] for address[%s]", remote_device_id.c_str(), address.c_str());
@@ -248,14 +248,16 @@ void Service::getByURI(string uri, long timeout, Result_Base *result)
 {
        MSF_DBG("getByURI() uri = %s", uri.c_str());
 
-       Resulturi = result;
+       MSF_DBG("calling curl_service_calling");
        //Resultdevice = NULL;
-       curl_service_calling(uri, timeout, NULL);
-       Resulturi = NULL;
+       curl_service_calling(uri, timeout, NULL, result);
+       MSF_DBG("called curl_service_calling");
+       MSF_DBG("getByURI end");
 }
 
-int Service::curl_service_calling(string uri, long timeout, void *dev_result_ptr)
+int Service::curl_service_calling(string uri, long timeout, void *dev_result_ptr, Result_Base *result)
 {
+       MSF_DBG("curl_service_calling start");
        CURL *curl;
        CURLcode res;
        struct curl_slist *headers = NULL;
@@ -278,18 +280,19 @@ int Service::curl_service_calling(string uri, long timeout, void *dev_result_ptr
 
         if ((res != CURLE_OK) || (read_data == NULL)) {
             MSF_DBG("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
-            createdata_process("", dev_result_ptr);
+            createdata_process("", dev_result_ptr, result);
             if (read_data) {
                 free(read_data);
             }
         } else {
-            createdata_process(string(read_data), dev_result_ptr);
+               MSF_DBG("calling createdata_process");
+            createdata_process(string(read_data), dev_result_ptr, result);
             free(read_data);
         }
 
                curl_easy_cleanup(curl);
        }
-
+       MSF_DBG("curl_service_calling end");
        return 0;
 }
 
@@ -306,13 +309,13 @@ size_t Service::createdata(char *buf, size_t size, size_t nmemb, void *up)
     return size*nmemb;
 }
 
-void Service::createdata_process(string data, void *dev_result_ptr)
+void Service::createdata_process(string data, void *dev_result_ptr, Result_Base* result)
 {
        if (data.length() != 0) {
-               json_parse_service(data.c_str(), dev_result_ptr);
+               json_parse_service(data.c_str(), dev_result_ptr, result);
        } else {
-               if (Resulturi != NULL) {
-                       Resulturi->onError(Error::create("Timeout"));
+               if (result != NULL) {
+                       result->onError(Error::create("Timeout"));
                }
                if (dev_result_ptr != NULL) {
                        (static_cast<Result_Base*> (dev_result_ptr))->onError(Error::create("Not Found"));
@@ -320,8 +323,9 @@ void Service::createdata_process(string data, void *dev_result_ptr)
        }
 }
 
-int Service::json_parse_service(const char *in, void *ptr)
+int Service::json_parse_service(const char *in, void *ptr, Result_Base* result)
 {
+       MSF_DBG("json_parse_service start");
        JsonParser *parser = json_parser_new();
 
        if (json_parser_load_from_data(parser, in, -1, NULL)) {
@@ -367,10 +371,13 @@ int Service::json_parse_service(const char *in, void *ptr)
                retService.SecureConnectionSupport = true;
        }
 
-       if ((Resulturi!= NULL)) {
-               Resulturi->onSuccess(retService);
+       if ((result != NULL)) {
+               MSF_DBG("json_parse_service");
+               result->onSuccess(retService);
        }
 
+       MSF_DBG("json_parse_service end");
+
        return 0;
 }