From 89a1837c668b681b877e721dd27a6b7f7508ee45 Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Thu, 2 Oct 2014 14:47:17 -0700 Subject: [PATCH] Changed std::mutex to std::recursive_mutex in the client/server wrappers. This will allow us to call create/get/put/etc from the entity handler\! Added the simpleclient/simpleserver changes from Sashi's review. Should be able to use them to test post. Change-Id: I3e96e0a1915e4da0e4ce5880db0b00c6dd73f247 --- examples/simpleclient.cpp | 40 ++++++++++++------ examples/simpleserver.cpp | 88 ++++++++++++++++++++++++++++++---------- include/InProcClientWrapper.h | 4 +- include/InProcServerWrapper.h | 5 ++- include/OCPlatform.h | 2 +- include/OutOfProcClientWrapper.h | 2 +- include/WrapperFactory.h | 12 ++++-- src/InProcClientWrapper.cpp | 19 +++++---- src/InProcServerWrapper.cpp | 16 ++++---- src/OCPlatform.cpp | 4 +- 10 files changed, 130 insertions(+), 62 deletions(-) diff --git a/examples/simpleclient.cpp b/examples/simpleclient.cpp index 7ac90e1..b692201 100644 --- a/examples/simpleclient.cpp +++ b/examples/simpleclient.cpp @@ -92,13 +92,21 @@ void onPost2(const OCRepresentation& rep, const int eCode) { std::cout << "POST request was successful" << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } if (OBSERVE_TYPE_TO_USE == ObserveType::Observe) std::cout << endl << "Observe is used." << endl << endl; @@ -121,13 +129,21 @@ void onPost(const OCRepresentation& rep, const int eCode) { std::cout << "POST request was successful" << std::endl; - rep.getValue("state", mylight.m_state); - rep.getValue("power", mylight.m_power); - rep.getValue("name", mylight.m_name); + if(rep.hasAttribute("createduri")) + { + std::cout << "\tUri of the created resource: " + << rep.getValue("createduri") << std::endl; + } + else + { + rep.getValue("state", mylight.m_state); + rep.getValue("power", mylight.m_power); + rep.getValue("name", mylight.m_name); - std::cout << "\tstate: " << mylight.m_state << std::endl; - std::cout << "\tpower: " << mylight.m_power << std::endl; - std::cout << "\tname: " << mylight.m_name << std::endl; + std::cout << "\tstate: " << mylight.m_state << std::endl; + std::cout << "\tpower: " << mylight.m_power << std::endl; + std::cout << "\tname: " << mylight.m_name << std::endl; + } OCRepresentation rep2; diff --git a/examples/simpleserver.cpp b/examples/simpleserver.cpp index ac1ae49..7ebcf77 100644 --- a/examples/simpleserver.cpp +++ b/examples/simpleserver.cpp @@ -52,6 +52,7 @@ class LightResource public: /// Access this property from a TB client + OCPlatform m_platform; std::string m_name; bool m_state; int m_power; @@ -62,7 +63,8 @@ public: public: /// Constructor - LightResource(): m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") { + LightResource(PlatformConfig& cfg): m_platform(cfg), + m_name("John's light"), m_state(false), m_power(0), m_lightUri("/a/light") { // Initialize representation m_lightRep.setUri(m_lightUri); @@ -75,7 +77,7 @@ public: access to, you can accomplish this with a free function: */ /// This function internally calls registerResource API. - void createResource(OC::OCPlatform& platform) + void createResource() { std::string resourceURI = m_lightUri; // URI of the resource std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light @@ -87,7 +89,7 @@ public: EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2); // This will internally create and register the resource. - OCStackResult result = platform.registerResource( + OCStackResult result = m_platform.registerResource( m_resourceHandle, resourceURI, resourceTypeName, resourceInterface, cb, resourceProperty); @@ -97,6 +99,32 @@ public: } } + OCStackResult createResource1() + { + std::string resourceURI = "/a/light1"; // URI of the resource + std::string resourceTypeName = "core.light"; // resource type name. In this case, it is light + std::string resourceInterface = DEFAULT_INTERFACE; // resource interface. + + // OCResourceProperty is defined ocstack.h + uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE; + + EntityHandler cb = std::bind(&LightResource::entityHandler, this,PH::_1, PH::_2); + + OCResourceHandle resHandle; + + // This will internally create and register the resource. + OCStackResult result = m_platform.registerResource( + resHandle, resourceURI, resourceTypeName, + resourceInterface, cb, resourceProperty); + + if (OC_STACK_OK != result) + { + cout << "Resource creation was unsuccessful\n"; + } + + return result; + } + OCResourceHandle getHandle() { return m_resourceHandle; @@ -139,8 +167,31 @@ public: // updates the internal state OCRepresentation post(OCRepresentation& rep) { - put(rep); - return m_lightRep; + static int first = 1; + + std::cout << "In POST\n"; + + // for the first time it tries to create a resource + if(first) + { + std::cout << "In POST/First\n"; + + first = 0; + + if(OC_STACK_OK == createResource1()) + { + std::cout << "Created a new resource\n"; + + OCRepresentation rep1; + rep1.setValue("createduri", std::string("/a/light1")); + + return rep1; + } + } + + // from second time onwards it just puts + put(rep); + return get(); } @@ -155,18 +206,18 @@ public: return m_lightRep; } - void addType(const OC::OCPlatform& platform, const std::string& type) const + void addType(const std::string& type) const { - OCStackResult result = platform.bindTypeToResource(m_resourceHandle, type); + OCStackResult result = m_platform.bindTypeToResource(m_resourceHandle, type); if (OC_STACK_OK != result) { cout << "Binding TypeName to Resource was unsuccessful\n"; } } - void addInterface(const OC::OCPlatform& platform, const std::string& interface) const + void addInterface(const std::string& interface) const { - OCStackResult result = platform.bindInterfaceToResource(m_resourceHandle, interface); + OCStackResult result = m_platform.bindInterfaceToResource(m_resourceHandle, interface); if (OC_STACK_OK != result) { cout << "Binding TypeName to Resource was unsuccessful\n"; @@ -237,15 +288,14 @@ void entityHandler(std::shared_ptr request, std::shared_ptrsetErrorCode(200); - response->setResourceRepresentation(get()); + response->setResourceRepresentation(rep_post); } // POST request operations @@ -333,7 +383,7 @@ void * ChangeLightRepresentation (void *param) } else { - result = OCPlatform::notifyAllObservers(lightPtr->getHandle()); + result = lightPtr->m_platform.notifyAllObservers(lightPtr->getHandle()); } if(OC_STACK_NO_OBSERVERS == result) @@ -386,20 +436,16 @@ int main(int argc, char* argv[1]) OC::QualityOfService::NonConfirmable }; - // Create a OCPlatform instance. - // Note: Platform creation is synchronous call. try { - OCPlatform platform(cfg); - // Create the instance of the resource class (in this case instance of class 'LightResource'). - LightResource myLight; + LightResource myLight(cfg); // Invoke createResource function of class light. - myLight.createResource(platform); + myLight.createResource(); - myLight.addType(platform, std::string("core.brightlight")); - myLight.addInterface(platform, std::string("oc.mi.ll")); + myLight.addType(std::string("core.brightlight")); + myLight.addInterface(std::string("oc.mi.ll")); // Perform app tasks while(true) { diff --git a/include/InProcClientWrapper.h b/include/InProcClientWrapper.h index 7765489..333b1e6 100644 --- a/include/InProcClientWrapper.h +++ b/include/InProcClientWrapper.h @@ -40,7 +40,7 @@ namespace OC class InProcClientWrapper : public IClientWrapper { public: - InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, + InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg); virtual ~InProcClientWrapper(); @@ -80,7 +80,7 @@ namespace OC std::string assembleSetResourcePayload(const OCRepresentation& attributes); std::thread m_listeningThread; bool m_threadRun; - std::weak_ptr m_csdkLock; + std::weak_ptr m_csdkLock; private: OC::OCPlatform& m_owner; diff --git a/include/InProcServerWrapper.h b/include/InProcServerWrapper.h index a53166b..23f1250 100644 --- a/include/InProcServerWrapper.h +++ b/include/InProcServerWrapper.h @@ -32,7 +32,8 @@ namespace OC class InProcServerWrapper : public IServerWrapper { public: - InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg); + InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, + PlatformConfig cfg); virtual ~InProcServerWrapper(); virtual OCStackResult registerResource( @@ -63,7 +64,7 @@ namespace OC void processFunc(); std::thread m_processThread; bool m_threadRun; - std::weak_ptr m_csdkLock; + std::weak_ptr m_csdkLock; }; } diff --git a/include/OCPlatform.h b/include/OCPlatform.h index 365bc09..cb2e96b 100644 --- a/include/OCPlatform.h +++ b/include/OCPlatform.h @@ -374,7 +374,7 @@ namespace OC std::unique_ptr m_WrapperInstance; IServerWrapper::Ptr m_server; IClientWrapper::Ptr m_client; - std::shared_ptr m_csdkLock; + std::shared_ptr m_csdkLock; private: /** diff --git a/include/OutOfProcClientWrapper.h b/include/OutOfProcClientWrapper.h index 463813d..18c3c04 100644 --- a/include/OutOfProcClientWrapper.h +++ b/include/OutOfProcClientWrapper.h @@ -28,7 +28,7 @@ namespace OC class OutOfProcClientWrapper : public IClientWrapper { public: - OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, + OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) : IClientWrapper(owner) {} diff --git a/include/WrapperFactory.h b/include/WrapperFactory.h index baf60d7..45e349c 100644 --- a/include/WrapperFactory.h +++ b/include/WrapperFactory.h @@ -38,8 +38,10 @@ namespace OC public: typedef std::shared_ptr Ptr; - virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) =0; - virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) =0; + virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, + std::weak_ptr csdkLock, PlatformConfig cfg) =0; + virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, + std::weak_ptr csdkLock, PlatformConfig cfg) =0; virtual ~IWrapperFactory(){} }; @@ -49,7 +51,8 @@ namespace OC public: WrapperFactory(){} - virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, + std::weak_ptr csdkLock, PlatformConfig cfg) { switch(cfg.serviceType) { @@ -63,7 +66,8 @@ namespace OC return nullptr; } - virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, + std::weak_ptr csdkLock, PlatformConfig cfg) { switch(cfg.serviceType) { diff --git a/src/InProcClientWrapper.cpp b/src/InProcClientWrapper.cpp index 457a8f0..44a3525 100644 --- a/src/InProcClientWrapper.cpp +++ b/src/InProcClientWrapper.cpp @@ -30,7 +30,8 @@ using namespace std; namespace OC { - InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr csdkLock, PlatformConfig cfg) + InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, + std::weak_ptr csdkLock, PlatformConfig cfg) : IClientWrapper(owner), m_threadRun(false), m_csdkLock(csdkLock), m_owner(owner), @@ -71,7 +72,7 @@ namespace OC auto cLock = m_csdkLock.lock(); if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCProcess(); } else @@ -229,7 +230,7 @@ namespace OC auto cLock = m_csdkLock.lock(); if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); OCDoHandle handle; result = OCDoResource(&handle, OC_REST_GET, resourceType.c_str(), @@ -399,7 +400,7 @@ namespace OC std::ostringstream os; os << host << assembleSetResourceUri(uri, queryParams).c_str(); - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); OCDoHandle handle; result = OCDoResource(&handle, OC_REST_GET, os.str().c_str(), nullptr, nullptr, @@ -493,7 +494,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); OCDoHandle handle; result = OCDoResource(&handle, OC_REST_POST, os.str().c_str(), nullptr, @@ -532,7 +533,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); OCDoHandle handle; result = OCDoResource(&handle, OC_REST_PUT, os.str().c_str(), nullptr, @@ -603,7 +604,7 @@ namespace OC std::ostringstream os; os << host << assembleSetResourceUri(uri, queryParams).c_str(); - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCDoResource(handle, method, os.str().c_str(), nullptr, nullptr, @@ -627,7 +628,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0); } else @@ -683,7 +684,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0); } else diff --git a/src/InProcServerWrapper.cpp b/src/InProcServerWrapper.cpp index 2808c39..71def86 100644 --- a/src/InProcServerWrapper.cpp +++ b/src/InProcServerWrapper.cpp @@ -232,7 +232,7 @@ OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag, namespace OC { InProcServerWrapper::InProcServerWrapper(OC::OCPlatform& owner, - std::weak_ptr csdkLock, PlatformConfig cfg) + std::weak_ptr csdkLock, PlatformConfig cfg) : IServerWrapper(owner), m_csdkLock(csdkLock) { @@ -271,7 +271,7 @@ namespace OC OCStackResult result; { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCProcess(); } @@ -298,7 +298,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); if(NULL != eHandler) { @@ -366,7 +366,7 @@ namespace OC if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCDeleteResource(resourceHandle); if(result == OC_STACK_OK) @@ -393,7 +393,7 @@ namespace OC OCStackResult result; if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str()); } else @@ -416,7 +416,7 @@ namespace OC OCStackResult result; if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCBindResourceInterfaceToResource(resourceHandle, resourceInterfaceName.c_str()); } @@ -438,7 +438,7 @@ namespace OC OCStackResult result = OC_STACK_ERROR; if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCStartPresence(seconds); } @@ -455,7 +455,7 @@ namespace OC OCStackResult result = OC_STACK_ERROR; if(cLock) { - std::lock_guard lock(*cLock); + std::lock_guard lock(*cLock); result = OCStopPresence(); } diff --git a/src/OCPlatform.cpp b/src/OCPlatform.cpp index 16e4dce..d05faa7 100644 --- a/src/OCPlatform.cpp +++ b/src/OCPlatform.cpp @@ -66,7 +66,7 @@ OCPlatform::OCPlatform(const PlatformConfig& config) : m_log_stream { std::move(oc_log_stream {oc_make_ostream_logger}) }, m_cfg { config }, m_WrapperInstance { make_unique() }, - m_csdkLock { make_shared() } + m_csdkLock { make_shared() } { init(m_cfg); } @@ -75,7 +75,7 @@ OCPlatform::OCPlatform(const PlatformConfig& config, OC::oc_log_stream& log_targ : m_log_stream { log_target }, m_cfg { config }, m_WrapperInstance { make_unique() }, - m_csdkLock { make_shared() } + m_csdkLock { make_shared() } { init(m_cfg); } -- 2.7.4