Changed std::mutex to std::recursive_mutex in the client/server wrappers. This will...
authorErich Keane <erich.keane@intel.com>
Thu, 2 Oct 2014 21:47:17 +0000 (14:47 -0700)
committerErich Keane <erich.keane@intel.com>
Mon, 6 Oct 2014 18:19:46 +0000 (11:19 -0700)
Added the simpleclient/simpleserver changes from Sashi's review.  Should be able to use them to test post.

Change-Id: I3e96e0a1915e4da0e4ce5880db0b00c6dd73f247

examples/simpleclient.cpp
examples/simpleserver.cpp
include/InProcClientWrapper.h
include/InProcServerWrapper.h
include/OCPlatform.h
include/OutOfProcClientWrapper.h
include/WrapperFactory.h
src/InProcClientWrapper.cpp
src/InProcServerWrapper.cpp
src/OCPlatform.cpp

index 7ac90e1..b692201 100644 (file)
@@ -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<std::string>("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<std::string>("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;
 
index ac1ae49..7ebcf77 100644 (file)
@@ -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<OCResourceRequest> request, std::shared_ptr<O
 
                 // Do related operations related to POST request
 
-                // Update the lightResource
-                post(rep);
+                OCRepresentation rep_post = post(rep);
 
                 if(response)
                 {
                     // TODO Error Code
                     response->setErrorCode(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)
         {
index 7765489..333b1e6 100644 (file)
@@ -40,7 +40,7 @@ namespace OC
     class InProcClientWrapper : public IClientWrapper
     {
     public:
-        InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock,
+        InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> 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<std::mutex> m_csdkLock;
+        std::weak_ptr<std::recursive_mutex> m_csdkLock;
 
     private:
         OC::OCPlatform& m_owner;
index a53166b..23f1250 100644 (file)
@@ -32,7 +32,8 @@ namespace OC
     class InProcServerWrapper : public IServerWrapper
     {
     public:
-        InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg);
+        InProcServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> 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<std::mutex> m_csdkLock;
+        std::weak_ptr<std::recursive_mutex> m_csdkLock;
     };
 }
 
index 365bc09..cb2e96b 100644 (file)
@@ -374,7 +374,7 @@ namespace OC
         std::unique_ptr<WrapperFactory> m_WrapperInstance;
         IServerWrapper::Ptr m_server;
         IClientWrapper::Ptr m_client;
-        std::shared_ptr<std::mutex> m_csdkLock;
+        std::shared_ptr<std::recursive_mutex> m_csdkLock;
 
     private:
         /**
index 463813d..18c3c04 100644 (file)
@@ -28,7 +28,7 @@ namespace OC
     class OutOfProcClientWrapper : public IClientWrapper
     {
     public:
-        OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock,
+        OutOfProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::recursive_mutex> csdkLock,
                                 PlatformConfig cfg)
          : IClientWrapper(owner)
         {}
index baf60d7..45e349c 100644 (file)
@@ -38,8 +38,10 @@ namespace OC
     public:
         typedef std::shared_ptr<IWrapperFactory> Ptr;
 
-        virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
-        virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg) =0;
+        virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner,
+            std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg) =0;
+        virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner,
+            std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg) =0;
         virtual ~IWrapperFactory(){}
     };
 
@@ -49,7 +51,8 @@ namespace OC
     public:
         WrapperFactory(){}
 
-        virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+        virtual IClientWrapper::Ptr CreateClientWrapper(OC::OCPlatform& owner,
+            std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
         {
             switch(cfg.serviceType)
             {
@@ -63,7 +66,8 @@ namespace OC
                        return nullptr;
         }
 
-        virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+        virtual IServerWrapper::Ptr CreateServerWrapper(OC::OCPlatform& owner,
+            std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
         {
             switch(cfg.serviceType)
             {
index 457a8f0..44a3525 100644 (file)
@@ -30,7 +30,8 @@ using namespace std;
 
 namespace OC
 {
-    InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner, std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+    InProcClientWrapper::InProcClientWrapper(OC::OCPlatform& owner,
+        std::weak_ptr<std::recursive_mutex> 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<std::mutex> lock(*cLock);
+                std::lock_guard<std::recursive_mutex> lock(*cLock);
                 result = OCProcess();
             }
             else
@@ -229,7 +230,7 @@ namespace OC
         auto cLock = m_csdkLock.lock();
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> 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<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> 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<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> 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<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> 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<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCDoResource(handle, method,
                                   os.str().c_str(), nullptr,
                                   nullptr,
@@ -627,7 +628,7 @@ namespace OC
 
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0);
         }
         else
@@ -683,7 +684,7 @@ namespace OC
 
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCCancel(handle, OC_NON_CONFIRMABLE, NULL, 0);
         }
         else
index 2808c39..71def86 100644 (file)
@@ -232,7 +232,7 @@ OCEntityHandlerResult EntityHandlerWrapper(OCEntityHandlerFlag flag,
 namespace OC
 {
     InProcServerWrapper::InProcServerWrapper(OC::OCPlatform& owner,
-        std::weak_ptr<std::mutex> csdkLock, PlatformConfig cfg)
+        std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
      : IServerWrapper(owner),
        m_csdkLock(csdkLock)
     {
@@ -271,7 +271,7 @@ namespace OC
             OCStackResult result;
 
             {
-                std::lock_guard<std::mutex> lock(*cLock);
+                std::lock_guard<std::recursive_mutex> lock(*cLock);
                 result = OCProcess();
             }
 
@@ -298,7 +298,7 @@ namespace OC
 
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
 
             if(NULL != eHandler)
             {
@@ -366,7 +366,7 @@ namespace OC
 
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCDeleteResource(resourceHandle);
 
             if(result == OC_STACK_OK)
@@ -393,7 +393,7 @@ namespace OC
         OCStackResult result;
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str());
         }
         else
@@ -416,7 +416,7 @@ namespace OC
         OCStackResult result;
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCBindResourceInterfaceToResource(resourceHandle,
                         resourceInterfaceName.c_str());
         }
@@ -438,7 +438,7 @@ namespace OC
         OCStackResult result = OC_STACK_ERROR;
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCStartPresence(seconds);
         }
 
@@ -455,7 +455,7 @@ namespace OC
         OCStackResult result = OC_STACK_ERROR;
         if(cLock)
         {
-            std::lock_guard<std::mutex> lock(*cLock);
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
             result = OCStopPresence();
         }
 
index 16e4dce..d05faa7 100644 (file)
@@ -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<WrapperFactory>() },
-   m_csdkLock        { make_shared<std::mutex>() }
+   m_csdkLock        { make_shared<std::recursive_mutex>() }
 {
     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<WrapperFactory>() },
-    m_csdkLock        { make_shared<std::mutex>() }
+    m_csdkLock        { make_shared<std::recursive_mutex>() }
 {
     init(m_cfg);
 }