Updated the name of the API to unregisterResource. And this API returns
authorErich Keane <erich.keane@intel.com>
Fri, 5 Sep 2014 17:35:33 +0000 (10:35 -0700)
committerErich Keane <erich.keane@intel.com>
Fri, 5 Sep 2014 20:06:17 +0000 (13:06 -0700)
whatever result it got from the stack. NOTE: Only this API has been changed.

Added C++ API for unregistering a resource
Patch 3: Rebase
Patch 4: added test code, fixed ocicuc bug

Change-Id: Ida95886fcbbaceab349bc127ae4b4e44ff823828

examples/ocicuc/light_resource.cpp
examples/ocicuc/light_resource.hpp
examples/ocicuc/server.cpp
include/IServerWrapper.h
include/InProcServerWrapper.h
include/OCPlatform.h
include/OutOfProcServerWrapper.h
src/InProcServerWrapper.cpp
src/OCPlatform.cpp

index 56a2a7f..370a54e 100644 (file)
@@ -44,14 +44,12 @@ void LightResource::createResource(OC::OCPlatform& platform, const unsigned int
 
  cout << "registering resource: " << resourceURI << '\n';
  cout << "registering type name \"" << resourceTypeName << "\".\n";
-
  // This will internally create and register the resource, binding the current instance's method as a callback:
  OCStackResult result = platform.registerResource(
                                             m_resourceHandle, resourceURI, resourceTypeName,
                                             DEFAULT_INTERFACE, 
                                             std::bind(&LightResource::entityHandler, this, std::placeholders::_1, std::placeholders::_2), 
                                             OC_DISCOVERABLE | OC_OBSERVABLE);
-
   if (OC_STACK_OK != result)
    std::cout << "Resource creation failed.\n";
 }
@@ -81,6 +79,22 @@ void LightResource::observe_function()
  cerr << "Observation thread is shutting down.\n";
 }
 
+void LightResource::unregisterResource(OC::OCPlatform& platform)
+{
+    std::cout << "Unregistering light resource"<<std::endl;
+
+    OCStackResult result = platform.unregisterResource(m_resourceHandle);
+
+    if(result == OC_STACK_OK)
+    {
+        std::cout << "Resource unregistered."<<std::endl;
+    }
+    else
+    {
+        cerr << "Unregister resource failed: "<<std::endl;
+    }
+}
+
 // This is just a sample implementation of entity handler.
 // Entity handler can be implemented in several ways by the manufacturer
 void LightResource::entityHandler(std::shared_ptr<OCResourceRequest> request, std::shared_ptr<OCResourceResponse> response)
index bfb1692..3cd2031 100644 (file)
@@ -62,7 +62,7 @@ class LightResource
     public:
     // This function internally calls registerResource API.
     void createResource(OC::OCPlatform& platform, const unsigned int resource_number);
-
+    void unregisterResource(OC::OCPlatform& platform);
     OCResourceHandle getHandle() const { return m_resourceHandle; }
 
     void setRepresentation(AttributeMap& attributeMap);
index a564624..3b175e5 100644 (file)
@@ -40,6 +40,7 @@ auto make_description()
     ("host_port",   po::value<uint16_t>()->default_value(56832),          "port of host")
     ("interface",   po::value<string>()->default_value("eth0"),           "network interface name") 
     ("uri",     po::value<vector<string>>(),                              "resource URI")
+    ("runtime", po::value<unsigned int>()->default_value(3600),             "time in seconds to keep the server alive")
     ;
 
  return desc;
@@ -67,8 +68,6 @@ int exec(const boost::program_options::variables_map& vm)
 
  const unsigned long& nresources = vm["nres"].as<unsigned long>();
 
- lights.resize(nresources);
-
  for(unsigned int resource_number = 1; nresources >= resource_number; resource_number++)
   {
         cout << "Registering resource " << resource_number << ": " << std::flush;
@@ -85,9 +84,12 @@ int exec(const boost::program_options::variables_map& vm)
   }
 
  // Perform app tasks
- while(true)
+ cout << "Sleeping for "<< vm["runtime"].as<unsigned int>()<<" seconds."<<endl;
+ std::this_thread::sleep_for(std::chrono::seconds( vm["runtime"].as<unsigned int>()));
+
+ for(auto light: lights)
  {
-    std::this_thread::sleep_for(std::chrono::seconds(5));
+    light->unregisterResource(platform);
  }
 
  return 1;
index b102329..4126a17 100644 (file)
@@ -47,7 +47,8 @@ namespace OC
                     const std::string& resourceInterface,
                     std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                     uint8_t resourceProperty) = 0;
-
+        virtual OCStackResult unregisterResource(
+                    const OCResourceHandle& resourceHandle) = 0;
         virtual OCStackResult bindTypeToResource(
                     const OCResourceHandle& resourceHandle,
                     const std::string& resourceTypeName) = 0;
index bcd85eb..f06c3db 100644 (file)
@@ -45,6 +45,9 @@ namespace OC
                     std::function<void(const OCResourceRequest::Ptr, const OCResourceResponse::Ptr)> entityHandler,
                     uint8_t resourceProperty);
 
+        virtual OCStackResult unregisterResource(
+                    const OCResourceHandle& resourceHandle);
+
         virtual OCStackResult bindTypeToResource(
                     const OCResourceHandle& resourceHandle,
                     const std::string& resourceTypeName);
index e39fae0..26ce6a6 100644 (file)
@@ -132,6 +132,17 @@ namespace OC
                         uint8_t resourceProperty);
 
         /**
+        * This API unregisters a resource with the server
+        * NOTE: This API applies to server side only.
+        *
+        * @param resourceHandle - This is the resource handle which we which to unregister from the server
+        *
+        * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
+        * NOTE: OCStackResult is defined in ocstack.h.
+        */
+        OCStackResult unregisterResource(const OCResourceHandle& resourceHandle) const;
+
+        /**
         * Add a resource to a collection resource.
         *
         * @param collectionHandle - handle to the collection resource
index 3c2108f..2503ee5 100644 (file)
@@ -42,7 +42,14 @@ namespace OC
             // Not implemented
             return OC_STACK_NOTIMPL;
         }
-        virtual OCStackResult bindTypeToResource(
+
+        virtual OCStackResult unregisterResource(
+                     const OCResourceHandle& resourceHandle)
+        {   //Not implemented yet
+            return OC_STACK_ERROR;
+        }
+
+       virtual OCStackResult bindTypeToResource(
                      const OCResourceHandle& resourceHandle,
                      const std::string& resourceTypeName)
         {   //Not implemented yet
index fea13c3..0be188c 100644 (file)
@@ -272,6 +272,19 @@ namespace OC
         return result;
     }
 
+    OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle)
+    {
+        cout << "Unregistering Resource: \n";
+
+        auto cLock = m_csdkLock.lock();
+        OCStackResult result = OC_STACK_ERROR;
+        if(cLock)
+        {
+            std::lock_guard<std::mutex> lock(*cLock);
+            result = OCDeleteResource(resourceHandle);
+        }
+        return result;
+    }
     OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle,
                      const std::string& resourceTypeName)
     {
index ec272e4..dbf5a14 100644 (file)
@@ -126,6 +126,20 @@ namespace OC
         return result;
     }
 
+
+    OCStackResult OCPlatform::unregisterResource(const OCResourceHandle& resourceHandle) const
+    {
+        OCStackResult result = OC_STACK_ERROR;
+
+        if(m_server)
+        {
+            result = m_server->unregisterResource(resourceHandle);
+        }
+        return result;
+    }
+
+
+
     OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
     {
         OCStackResult result = OC_STACK_OK;