Added code for C++ implementation on Delete on both server and client ends.
authorSudarshan Prasad <sudarshan.prasad@intel.com>
Wed, 15 Oct 2014 03:02:27 +0000 (20:02 -0700)
committerSudarshan Prasad <sudarshan.prasad@intel.com>
Wed, 15 Oct 2014 06:23:55 +0000 (23:23 -0700)
Patch2 : Incorporated review comments
Change-Id: If666017890ec38890934bb01e53c9f89d49fee42

examples/fridgeclient.cpp
examples/fridgeserver.cpp
include/IClientWrapper.h
include/InProcClientWrapper.h
include/OCApi.h
include/OCResource.h
include/OutOfProcClientWrapper.h
src/InProcClientWrapper.cpp
src/InProcServerWrapper.cpp
src/OCResource.cpp
src/OCUtilities.cpp

index 2895ae1..fe9315b 100644 (file)
@@ -131,6 +131,10 @@ class ClientFridge
                 std::bind(&ClientFridge::getResponse, this, "Random Door", PH::_1,
                     PH::_2, PH::_3, randomdoor, 4)
                 ));
+        resource->deleteResource(DeleteCallback(
+                std::bind(&ClientFridge::deleteResponse, this, "Device", PH::_1,
+                    PH::_2, resource, 0)
+                ));
     }
 
     // Note that resourceName, resource, and getId are all bound via the std::bind mechanism.
@@ -140,19 +144,12 @@ class ClientFridge
     void getResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
                 const OCRepresentation rep, const int eCode, OCResource::Ptr resource, int getId)
     {
-        std::cout << "Got a response from get from the "<<resourceName<< std::endl;
-        std::cout << "Get ID is "<<getId<<" and resource URI is "<<resource->uri()<<std::endl;
+        std::cout << "Got a response from get from the " << resourceName << std::endl;
+        std::cout << "Get ID is "<<getId<<" and resource URI is " << resource->uri() << std::endl;
 
-        std::cout << "The Attribute Data is: "<<std::endl;
+        printHeaderOptions(headerOptions);
 
-        for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
-        {
-            if(it->getOptionID() == API_VERSION)
-            {
-                std::cout << "Server API version in GET response: " <<
-                        it->getOptionData() << std::endl;
-            }
-        }
+        std::cout << "The Attribute Data is: "<<std::endl;
 
         switch(getId)
         {
@@ -192,6 +189,28 @@ class ClientFridge
         }
     }
 
+    //Callback function to handle response for deleteResource call.
+    void deleteResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
+                const int eCode, OCResource::Ptr resource, int deleteId)
+    {
+        std::cout << "Got a response from delete from the "<< resourceName << std::endl;
+        std::cout << "Delete ID is "<<deleteId<<" and resource URI is "<<resource->uri()<<std::endl;
+        printHeaderOptions(headerOptions);
+    }
+
+    //Function to print the headerOptions received from the server
+    void printHeaderOptions(const HeaderOptions& headerOptions)
+    {
+        for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
+        {
+            if(it->getOptionID() == API_VERSION)
+            {
+                std::cout << "Server API version in GET response: " <<
+                        it->getOptionData() << std::endl;
+            }
+        }
+    }
+
     OCPlatform m_platform;
     std::mutex m_mutex;
     std::condition_variable m_cv;
index 3f80836..71354ab 100644 (file)
@@ -57,7 +57,9 @@ class Resource
 class DeviceResource : public Resource
 {
     public:
-    DeviceResource(OCPlatform& platform)
+    OCPlatform& m_platform;
+
+    DeviceResource(OCPlatform& platform): m_platform(platform)
     {
         std::string resourceURI = "/device";
         std::string resourceTypeName = "intel.fridge";
@@ -68,10 +70,10 @@ class DeviceResource : public Resource
                                                                          PH::_1, PH::_2);
 
         std::cout << "Setting device default entity handler\n";
-        platform.setDefaultDeviceEntityHandler(defaultEH);
+        m_platform.setDefaultDeviceEntityHandler(defaultEH);
 
         uint8_t resourceProperty = OC_DISCOVERABLE;
-        OCStackResult result = platform.registerResource(m_resourceHandle,
+        OCStackResult result = m_platform.registerResource(m_resourceHandle,
             resourceURI,
             resourceTypeName,
             resourceInterface,
@@ -91,6 +93,16 @@ class DeviceResource : public Resource
         return m_rep;
     }
 
+    void deleteDeviceResource()
+    {
+        OCStackResult result = m_platform.unregisterResource(m_resourceHandle);
+        if(OC_STACK_OK != result)
+        {
+            throw std::runtime_error(
+               std::string("Device Resource failed to unregister/delete") + std::to_string(result));
+        }
+    }
+
     std::string m_modelName;
     protected:
     virtual void entityHandler(std::shared_ptr<OCResourceRequest> request,
@@ -151,6 +163,16 @@ class DeviceResource : public Resource
                             response->setResourceRepresentation(get(), "");
                         }
                     }
+                    if(request->getRequestType() == "DELETE")
+                    {
+                        if(response)
+                        {
+                            std::cout<<"DeviceResource Delete Request"<<std::endl;
+                            deleteDeviceResource();
+                            response->setErrorCode(200);
+                            response->setHeaderOptions(serverHeaderOptions);
+                        }
+                    }
                     else
                     {
                         std::cout <<"DeviceResource unsupported request type "
index e3244c9..8140ff9 100644 (file)
@@ -61,6 +61,10 @@ namespace OC
                         const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
                         PostCallback& callback, QualityOfService QoS) = 0;
 
+        virtual OCStackResult DeleteResource(const std::string& host, const std::string& uri,
+                        const HeaderOptions& headerOptions, DeleteCallback& callback,
+                        QualityOfService QoS) = 0;
+
         virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle,
                         const std::string& host, const std::string& uri,
                         const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
index f702c98..49d80bf 100644 (file)
@@ -63,6 +63,9 @@ namespace OC
             const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
             PostCallback& callback, QualityOfService QoS);
 
+        virtual OCStackResult DeleteResource(const std::string& host, const std::string& uri,
+             const HeaderOptions& headerOptions, DeleteCallback& callback, QualityOfService QoS);
+
         virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle,
             const std::string& host, const std::string& uri, const QueryParamsMap& queryParams,
             const HeaderOptions& headerOptions, ObserveCallback& callback, QualityOfService QoS);
index 5bf0afe..5ea5321 100644 (file)
@@ -736,6 +736,8 @@ namespace OC
     typedef std::function<void(const HeaderOptions&,
                                 const OCRepresentation&, const int)> PutCallback;
 
+    typedef std::function<void(const HeaderOptions&, const int)> DeleteCallback;
+
     typedef std::function<void(const HeaderOptions&,
                                 const OCRepresentation&, const int, const int)> ObserveCallback;
 } // namespace OC
index ae52e14..db4f80c 100644 (file)
@@ -204,6 +204,17 @@ namespace OC
                         PostCallback attributeHandler, QualityOfService QoS);
 
         /**
+        * Function to perform DELETE operation
+        * @param observeHandler handles callback
+        *        The callback function will have headerOptions and result from this Delete
+        *        operation. This will have error codes
+        * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
+        * NOTE: OCStackResult is defined in ocstack.h.
+        */
+        OCStackResult deleteResource(DeleteCallback deleteHandler);
+        OCStackResult deleteResource(DeleteCallback deleteHandler, QualityOfService QoS);
+
+        /**
         * Function to set observation on the resource
         * @param observeType allows the client to specify how it wants to observe.
         * @param queryParametersMap map which can have the query parameter name and value
index ff7833a..8580d2b 100644 (file)
@@ -56,6 +56,10 @@ namespace OC
             PostCallback& callback, QualityOfService QoS)
             {return OC_STACK_NOTIMPL;}
 
+        virtual OCStackResult DeleteResource(const std::string& host, const std::string& uri,
+            const HeaderOptions& headerOptions, DeleteCallback& callback,
+            QualityOfService QoS) {return OC_STACK_NOTIMPL;}
+
         virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle,
             const std::string& host, const std::string& uri, const QueryParamsMap& queryParams,
             const HeaderOptions& headerOptions,
index 779caf2..63913f5 100644 (file)
@@ -599,6 +599,67 @@ namespace OC
         return result;
     }
 
+    struct DeleteContext
+    {
+        DeleteCallback callback;
+    };
+
+    OCStackApplicationResult deleteResourceCallback(void* ctx, OCDoHandle handle,
+        OCClientResponse* clientResponse)
+    {
+        DeleteContext* context = static_cast<DeleteContext*>(ctx);
+        OCRepresentation attrs;
+        HeaderOptions serverHeaderOptions;
+
+        if(clientResponse->result == OC_STACK_OK)
+        {
+            parseServerHeaderOptions(clientResponse, serverHeaderOptions);
+        }
+        std::thread exec(context->callback, serverHeaderOptions, clientResponse->result);
+        exec.detach();
+        return OC_STACK_DELETE_TRANSACTION;
+    }
+
+    OCStackResult InProcClientWrapper::DeleteResource(const std::string& host,
+        const std::string& uri, const HeaderOptions& headerOptions,
+         DeleteCallback& callback, QualityOfService QoS)
+    {
+        OCStackResult result;
+        OCCallbackData cbdata = {0};
+
+        DeleteContext* ctx = new DeleteContext();
+        ctx->callback = callback;
+        cbdata.cb = &deleteResourceCallback;
+        cbdata.cd = [](void* c){delete static_cast<DeleteContext*>(c);};
+        cbdata.context = static_cast<void*>(ctx);
+
+        ostringstream os;
+        os << host << uri;
+
+        auto cLock = m_csdkLock.lock();
+
+        if(cLock)
+        {
+            OCHeaderOption options[MAX_HEADER_OPTIONS];
+            OCDoHandle handle;
+
+            assembleHeaderOptions(options, headerOptions);
+
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+
+            result = OCDoResource(&handle, OC_REST_DELETE,
+                                  os.str().c_str(), nullptr,
+                                  nullptr, static_cast<OCQualityOfService>(m_cfg.QoS),
+                                  &cbdata, options, headerOptions.size());
+        }
+        else
+        {
+            result = OC_STACK_ERROR;
+        }
+
+        return result;
+    }
+
     struct ObserveContext
     {
         ObserveCallback callback;
index b28828e..12e222c 100644 (file)
@@ -104,6 +104,10 @@ void formResourceRequest(OCEntityHandlerFlag flag,
                 pRequest->setPayload(std::string(reinterpret_cast<const char*>
                                             (entityHandlerRequest->reqJSONPayload)));
             }
+            else if(OC_REST_DELETE == entityHandlerRequest->method)
+            {
+                pRequest->setRequestType("DELETE");
+            }
         }
     }
 
index e5321db..ee9cca7 100644 (file)
@@ -192,6 +192,20 @@ OCStackResult OCResource::post(const std::string& resourceType,
     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
 }
 
+OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
+{
+    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
+                         m_host, m_uri, m_headerOptions, deleteHandler, QoS);
+}
+
+OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
+{
+    QualityOfService defaultQos = OC::QualityOfService::NaQos;
+    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
+
+    return result_guard(deleteResource(deleteHandler, defaultQos));
+}
+
 OCStackResult OCResource::observe(ObserveType observeType,
         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
         QualityOfService QoS)
index 6916bb7..002d7fe 100644 (file)
@@ -67,7 +67,6 @@ namespace OC {
     }
 }
 // [TODO] remove this function
-// this function is just a temporary patch for Sudarshan
 // it seems that the C stack is parsing and giving out the query separately.
 // the entire URI need not be parsed
 static OC::Utilities::QueryParamsKeyVal tempPatch(const std::string& _uri)