Added lock logic in findResource callback for C++ samples
authorSudarshan Prasad <sudarshan.prasad@intel.com>
Tue, 13 Jan 2015 06:46:14 +0000 (22:46 -0800)
committerSashi Penta <sashi.kumar.penta@intel.com>
Fri, 6 Feb 2015 00:24:22 +0000 (00:24 +0000)
As part of Gerrit review #101,(83fed5a6c6b01d32de3e47c669652a1aa79ebb49)
there were changes required in other samples to prevent a crash in the
findResource callback.

Additionally, made minor correction in groupclient by changing
printf to cout in one of the function

Change-Id: I524f5d862c82011e5d008e686cc1d6e41ac3f170
Signed-Off-by:Sudarshan Prasad<sudarshan.prasad@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/126
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
Reviewed-by: Sashi Penta <sashi.kumar.penta@intel.com>
resource/examples/garageclient.cpp
resource/examples/groupclient.cpp
resource/examples/presenceclient.cpp
resource/examples/roomclient.cpp
resource/examples/simpleclientHQ.cpp
resource/examples/threadingsample.cpp

index ba8a09a..4aade76 100644 (file)
@@ -32,6 +32,7 @@ using namespace OC;
 
 const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
 
 class Garage
 {
@@ -211,9 +212,11 @@ void getLightRepresentation(std::shared_ptr<OCResource> resource)
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
     }
 
     std::string resourceURI;
index 34f325f..e6724a0 100644 (file)
 #include <mutex>
 #include <condition_variable>
 #include <iostream>
+#include <mutex>
 
 using namespace std;
 using namespace OC;
 namespace PH = std::placeholders;
+std::mutex resourceLock;
 
 OCResourceHandle resourceHandle;
 shared_ptr< OCResource > g_resource;
@@ -48,6 +50,13 @@ void onPost(const HeaderOptions& headerOptions, const OCRepresentation& rep, con
 
 void foundResource(std::shared_ptr< OCResource > resource)
 {
+    std::lock_guard<std::mutex> lock(resourceLock);
+    if(g_resource)
+    {
+        std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
+    }
+
     std::string resourceURI;
     std::string hostAddress;
 
@@ -59,13 +68,12 @@ void foundResource(std::shared_ptr< OCResource > resource)
         {
             string resourceURI = resource->uri();
             cout << resourceURI << endl;
+            cout << "HOST :: " << resource->host() << endl;
             if (resourceURI == "/core/a/collection")
             {
                 g_resource = resource;
+                resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
             }
-
-            g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet);
-            printf("HOST :: %s\n", resource->host().c_str());
         }
     }
     catch (std::exception& e)
index 857d0f4..c8cd9b8 100644 (file)
@@ -32,7 +32,7 @@
 using namespace OC;
 
 std::shared_ptr<OCResource> curResource;
-
+std::mutex curResourceLock;
 static int TEST_CASE = 0;
 
 /**
@@ -92,9 +92,11 @@ void presenceHandler(OCStackResult result, const unsigned int nonce, const std::
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
     }
 
     std::string resourceURI;
index 7e73597..e4148d7 100644 (file)
@@ -33,6 +33,7 @@ using namespace OC;
 
 const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
 
 int observe_count()
 {
@@ -161,6 +162,7 @@ void onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, cons
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
index bc29da0..53ba62f 100644 (file)
@@ -33,6 +33,7 @@ using namespace OC;
 
 const int SUCCESS_RESPONSE = 0;
 std::shared_ptr<OCResource> curResource;
+std::mutex curResourceLock;
 static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe;
 
 class Light
@@ -285,9 +286,11 @@ void getLightRepresentation(std::shared_ptr<OCResource> resource)
 // Callback to found resources
 void foundResource(std::shared_ptr<OCResource> resource)
 {
+    std::lock_guard<std::mutex> lock(curResourceLock);
     if(curResource)
     {
         std::cout << "Found another resource, ignoring"<<std::endl;
+        return;
     }
 
     std::string resourceURI;
index ff556e2..2adfab4 100644 (file)
 #include <condition_variable>
 #include <map>
 #include <vector>
+
 #include "OCPlatform.h"
 #include "OCApi.h"
-using namespace OC;
 
+using namespace OC;
 
 struct FooResource
 {