From 8ab52ca609bee1c479c618f10e205413344609be Mon Sep 17 00:00:00 2001 From: Sudarshan Prasad Date: Mon, 12 Jan 2015 22:46:14 -0800 Subject: [PATCH] Added lock logic in findResource callback for C++ samples 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/126 Tested-by: jenkins-iotivity Reviewed-by: Erich Keane Reviewed-by: Sashi Penta --- resource/examples/garageclient.cpp | 3 +++ resource/examples/groupclient.cpp | 14 +++++++++++--- resource/examples/presenceclient.cpp | 4 +++- resource/examples/roomclient.cpp | 2 ++ resource/examples/simpleclientHQ.cpp | 3 +++ resource/examples/threadingsample.cpp | 3 ++- 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/resource/examples/garageclient.cpp b/resource/examples/garageclient.cpp index ba8a09a..4aade76 100644 --- a/resource/examples/garageclient.cpp +++ b/resource/examples/garageclient.cpp @@ -32,6 +32,7 @@ using namespace OC; const int SUCCESS_RESPONSE = 0; std::shared_ptr curResource; +std::mutex curResourceLock; class Garage { @@ -211,9 +212,11 @@ void getLightRepresentation(std::shared_ptr resource) // Callback to found resources void foundResource(std::shared_ptr resource) { + std::lock_guard lock(curResourceLock); if(curResource) { std::cout << "Found another resource, ignoring"< #include #include +#include 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 lock(resourceLock); + if(g_resource) + { + std::cout << "Found another resource, ignoring"< 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) diff --git a/resource/examples/presenceclient.cpp b/resource/examples/presenceclient.cpp index 857d0f4..c8cd9b8 100644 --- a/resource/examples/presenceclient.cpp +++ b/resource/examples/presenceclient.cpp @@ -32,7 +32,7 @@ using namespace OC; std::shared_ptr 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 resource) { + std::lock_guard lock(curResourceLock); if(curResource) { std::cout << "Found another resource, ignoring"< 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 resource) { + std::lock_guard lock(curResourceLock); if(curResource) { std::cout << "Found another resource, ignoring"< curResource; +std::mutex curResourceLock; static ObserveType OBSERVE_TYPE_TO_USE = ObserveType::Observe; class Light @@ -285,9 +286,11 @@ void getLightRepresentation(std::shared_ptr resource) // Callback to found resources void foundResource(std::shared_ptr resource) { + std::lock_guard lock(curResourceLock); if(curResource) { std::cout << "Found another resource, ignoring"< #include #include + #include "OCPlatform.h" #include "OCApi.h" -using namespace OC; +using namespace OC; struct FooResource { -- 2.7.4