Added a Variant of findResourceList API which support error callback.
authorJay Sharma <jay.sharma@samsung.com>
Tue, 27 Dec 2016 13:37:32 +0000 (19:07 +0530)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 28 Dec 2016 08:52:36 +0000 (08:52 +0000)
JIRA : IOT-1706

Change-Id: I85fea5c323746534b3f95515c9a7eb920ba7cbb3
Signed-off-by: Jay Sharma <jay.sharma@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/15915
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-by: Abhishek Pandey <abhi.siso@samsung.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/include/IClientWrapper.h
resource/include/InProcClientWrapper.h
resource/include/OCPlatform.h
resource/include/OCPlatform_impl.h
resource/include/OutOfProcClientWrapper.h
resource/src/InProcClientWrapper.cpp
resource/src/OCPlatform.cpp
resource/src/OCPlatform_impl.cpp

index d1658c7..52a400f 100644 (file)
@@ -45,12 +45,19 @@ namespace OC
                         FindCallback& callback,
                         QualityOfService QoS) = 0;
 
-        virtual OCStackResult ListenForResource2(const std::string& serviceUrl,
+        virtual OCStackResult ListenForResourceList(const std::string& serviceUrl,
                         const std::string& resourceType,
                         OCConnectivityType connectivityType,
                         FindResListCallback& callback,
                         QualityOfService QoS) = 0;
 
+        virtual OCStackResult ListenForResourceListWithError(const std::string& serviceUrl,
+                        const std::string& resourceType,
+                        OCConnectivityType connectivityType,
+                        FindResListCallback& callback,
+                        FindErrorCallback& errorCallback,
+                        QualityOfService QoS) = 0;
+
         virtual OCStackResult ListenErrorForResource(const std::string& serviceUrl,
                         const std::string& resourceType,
                         OCConnectivityType connectivityType,
index 530b076..4bfd512 100644 (file)
@@ -56,22 +56,33 @@ namespace OC
                 : callback(cb), clientWrapper(cw){}
         };
 
-        struct ListenContext2
+        struct ListenErrorContext
+        {
+            FindCallback callback;
+            FindErrorCallback errorCallback;
+            std::weak_ptr<IClientWrapper> clientWrapper;
+
+            ListenErrorContext(FindCallback cb1, FindErrorCallback cb2,
+                               std::weak_ptr<IClientWrapper> cw)
+                : callback(cb1), errorCallback(cb2), clientWrapper(cw){}
+        };
+
+        struct ListenResListContext
         {
             FindResListCallback callback;
             std::weak_ptr<IClientWrapper> clientWrapper;
 
-            ListenContext2(FindResListCallback cb, std::weak_ptr<IClientWrapper> cw)
+            ListenResListContext(FindResListCallback cb, std::weak_ptr<IClientWrapper> cw)
                 : callback(cb), clientWrapper(cw){}
         };
 
-        struct ListenErrorContext
+        struct ListenResListWithErrorContext
         {
-            FindCallback callback;
+            FindResListCallback callback;
             FindErrorCallback errorCallback;
             std::weak_ptr<IClientWrapper> clientWrapper;
 
-            ListenErrorContext(FindCallback cb1, FindErrorCallback cb2,
+            ListenResListWithErrorContext(FindResListCallback cb1, FindErrorCallback cb2,
                                std::weak_ptr<IClientWrapper> cw)
                 : callback(cb1), errorCallback(cb2), clientWrapper(cw){}
         };
@@ -133,10 +144,15 @@ namespace OC
             const std::string& resourceType, OCConnectivityType transportFlags,
             FindCallback& callback, QualityOfService QoS);
 
-        virtual OCStackResult ListenForResource2(const std::string& serviceUrl,
+        virtual OCStackResult ListenForResourceList(const std::string& serviceUrl,
             const std::string& resourceType, OCConnectivityType transportFlags,
             FindResListCallback& callback, QualityOfService QoS);
 
+        virtual OCStackResult ListenForResourceListWithError(const std::string& serviceUrl,
+            const std::string& resourceType, OCConnectivityType connectivityType,
+            FindResListCallback& callback, FindErrorCallback& errorCallback,
+            QualityOfService QoS);
+
         virtual OCStackResult ListenErrorForResource(const std::string& serviceUrl,
             const std::string& resourceType, OCConnectivityType transportFlags,
             FindCallback& callback, FindErrorCallback& errorCallback, QualityOfService QoS);
index 1adcb82..ec8e3aa 100644 (file)
@@ -192,6 +192,10 @@ namespace OC
                     OCConnectivityType connectivityType, FindResListCallback resourceHandler,
                     QualityOfService QoS = QualityOfService::LowQos);
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    FindErrorCallback errorHandler, QualityOfService QoS = QualityOfService::LowQos);
+
         OCStackResult setPropertyValue(OCPayloadType type, const std::string& tag, const std::string& value);
         OCStackResult setPropertyValue(OCPayloadType type, const std::string& tag, const std::vector<std::string>& value);
         OCStackResult getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value);
index e20dc7d..6b52693 100644 (file)
@@ -96,6 +96,10 @@ namespace OC
                     OCConnectivityType connectivityType, FindResListCallback resourceHandler,
                     QualityOfService QoS);
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    FindErrorCallback errorHandler, QualityOfService Qos);
+
         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
 
index 0fb6fa4..1178367 100644 (file)
@@ -49,13 +49,21 @@ namespace OC
                                                 QualityOfService /*QoS*/)
             {return OC_STACK_NOTIMPL;}
 
-        virtual OCStackResult ListenForResource2(const std::string& /*servUrl*/,
+        virtual OCStackResult ListenForResourceList(const std::string& /*servUrl*/,
                                                 const std::string& /*rsrcType*/,
                                                 OCConnectivityType /*connType*/,
                                                 FindResListCallback& /*callback*/,
                                                 QualityOfService /*QoS*/)
             {return OC_STACK_NOTIMPL;}
 
+        virtual OCStackResult ListenForResourceListWithError(const std::string& /*servUrl*/,
+                                                const std::string&  /*rsrcType*/,
+                                                OCConnectivityType /*connType*/,
+                                                FindResListCallback& /*callback*/,
+                                                FindErrorCallback&  /*errorCallback*/,
+                                                QualityOfService /*QoS*/)
+            {return OC_STACK_NOTIMPL;}
+
         virtual OCStackResult ListenErrorForResource(const std::string& /*servUrl*/,
                                                      const std::string& /*rsrcType*/,
                                                      OCConnectivityType /*connType*/,
index 6a4b15c..9dccd59 100644 (file)
@@ -168,6 +168,11 @@ namespace OC
     OCStackApplicationResult listenCallback(void* ctx, OCDoHandle /*handle*/,
         OCClientResponse* clientResponse)
     {
+        if (!ctx || !clientResponse)
+        {
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
         ClientCallbackContext::ListenContext* context =
             static_cast<ClientCallbackContext::ListenContext*>(ctx);
 
@@ -228,10 +233,6 @@ namespace OC
 
         ClientCallbackContext::ListenErrorContext* context =
             static_cast<ClientCallbackContext::ListenErrorContext*>(ctx);
-        if (!context)
-        {
-            return OC_STACK_KEEP_TRANSACTION;
-        }
 
         OCStackResult result = clientResponse->result;
         if (result == OC_STACK_OK)
@@ -359,15 +360,20 @@ namespace OC
         return result;
     }
 
-    OCStackApplicationResult listenCallback2(void* ctx, OCDoHandle /*handle*/,
+    OCStackApplicationResult listenResListCallback(void* ctx, OCDoHandle /*handle*/,
         OCClientResponse* clientResponse)
     {
-        ClientCallbackContext::ListenContext2* context =
-            static_cast<ClientCallbackContext::ListenContext2*>(ctx);
+        if (!ctx || !clientResponse)
+        {
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        ClientCallbackContext::ListenResListContext* context =
+            static_cast<ClientCallbackContext::ListenResListContext*>(ctx);
 
         if (clientResponse->result != OC_STACK_OK)
         {
-            oclog() << "listenCallback2(): failed to create resource. clientResponse: "
+            oclog() << "listenResListCallback(): failed to create resource. clientResponse: "
                     << clientResponse->result
                     << std::flush;
 
@@ -376,7 +382,7 @@ namespace OC
 
         if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
         {
-            oclog() << "listenCallback2(): clientResponse payload was null or the wrong type"
+            oclog() << "listenResListCallback(): clientResponse payload was null or the wrong type"
                 << std::flush;
             return OC_STACK_KEEP_TRANSACTION;
         }
@@ -385,7 +391,7 @@ namespace OC
 
         if (!clientWrapper)
         {
-            oclog() << "listenCallback2(): failed to get a shared_ptr to the client wrapper"
+            oclog() << "listenResListCallback(): failed to get a shared_ptr to the client wrapper"
                     << std::flush;
             return OC_STACK_KEEP_TRANSACTION;
         }
@@ -400,15 +406,14 @@ namespace OC
         }
         catch (std::exception &e)
         {
-            oclog() << "Exception in listCallback2, ignoring response: "
+            oclog() << "Exception in listenResListCallback(), ignoring response: "
                     << e.what() << std::flush;
         }
 
-
         return OC_STACK_KEEP_TRANSACTION;
     }
 
-    OCStackResult InProcClientWrapper::ListenForResource2(
+    OCStackResult InProcClientWrapper::ListenForResourceList(
             const std::string& serviceUrl,
             const std::string& resourceType,
             OCConnectivityType connectivityType,
@@ -423,12 +428,116 @@ namespace OC
         ostringstream resourceUri;
         resourceUri << serviceUrl << resourceType;
 
-        ClientCallbackContext::ListenContext2* context =
-            new ClientCallbackContext::ListenContext2(callback, shared_from_this());
+        ClientCallbackContext::ListenResListContext* context =
+            new ClientCallbackContext::ListenResListContext(callback, shared_from_this());
+        OCCallbackData cbdata;
+        cbdata.context = static_cast<void*>(context),
+        cbdata.cb      = listenResListCallback;
+        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ListenResListContext*)c;};
+
+        auto cLock = m_csdkLock.lock();
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCDoResource(nullptr, OC_REST_DISCOVER,
+                                  resourceUri.str().c_str(),
+                                  nullptr, nullptr, connectivityType,
+                                  static_cast<OCQualityOfService>(QoS),
+                                  &cbdata,
+                                  nullptr, 0);
+        }
+        else
+        {
+            delete context;
+            result = OC_STACK_ERROR;
+        }
+        return result;
+    }
+
+    OCStackApplicationResult listenResListWithErrorCallback(void* ctx, OCDoHandle /*handle*/,
+        OCClientResponse* clientResponse)
+    {
+        if (!ctx || !clientResponse)
+        {
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        ClientCallbackContext::ListenResListWithErrorContext* context =
+                static_cast<ClientCallbackContext::ListenResListWithErrorContext*>(ctx);
+
+        OCStackResult result = clientResponse->result;
+        if (result != OC_STACK_OK)
+        {
+            oclog() << "listenResListWithErrorCallback(): failed to create resource. clientResponse: "
+                    << result << std::flush;
+
+             //send the error callback
+            std::thread exec(context->errorCallback, clientResponse->resourceUri, result);
+            exec.detach();
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
+        {
+            oclog() << "listenResListWithErrorCallback(): clientResponse payload was null or the wrong type"
+                << std::flush;
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        auto clientWrapper = context->clientWrapper.lock();
+
+        if (!clientWrapper)
+        {
+            oclog() << "listenResListWithErrorCallback(): failed to get a shared_ptr to the client wrapper"
+                    << std::flush;
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        try
+        {
+            ListenOCContainer container(clientWrapper, clientResponse->devAddr,
+                            reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
+
+            std::thread exec(context->callback, container.Resources());
+            exec.detach();
+        }
+        catch (std::exception &e)
+        {
+            oclog() << "Exception in listenResListWithErrorCallback(), ignoring response: "
+            << e.what() << std::flush;
+        }
+
+        return OC_STACK_KEEP_TRANSACTION;
+    }
+
+    OCStackResult InProcClientWrapper::ListenForResourceListWithError(
+            const std::string& serviceUrl,
+            const std::string& resourceType,
+            OCConnectivityType connectivityType,
+            FindResListCallback& callback,
+            FindErrorCallback& errorCallback, QualityOfService QoS)
+    {
+        if (!callback)
+        {
+            return OC_STACK_INVALID_PARAM;
+        }
+
+        OCStackResult result;
+        ostringstream resourceUri;
+        resourceUri << serviceUrl << resourceType;
+
+        ClientCallbackContext::ListenResListWithErrorContext* context =
+            new ClientCallbackContext::ListenResListWithErrorContext(callback, errorCallback,
+                                                          shared_from_this());
+        if (!context)
+        {
+            return OC_STACK_ERROR;
+        }
+
         OCCallbackData cbdata;
         cbdata.context = static_cast<void*>(context),
-        cbdata.cb      = listenCallback2;
-        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ListenContext2*)c;};
+        cbdata.cb      = listenResListWithErrorCallback;
+        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ListenResListWithErrorContext*)c;};
 
         auto cLock = m_csdkLock.lock();
         if (cLock)
index c41d665..e05820b 100644 (file)
@@ -141,6 +141,14 @@ namespace OC
                                     connectivityType, resourceHandler, QoS);
         }
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    FindErrorCallback errorHandler, QualityOfService QoS)
+        {
+            return OCPlatform_impl::Instance().findResourceList(host, resourceURI,
+                                    connectivityType, resourceHandler, errorHandler, QoS);
+        }
+
         OCStackResult getDeviceInfo(const std::string& host,
                                  const std::string& deviceURI,
                                  OCConnectivityType connectivityType,
index f23688f..0180b7f 100644 (file)
@@ -301,10 +301,22 @@ namespace OC
                                             FindResListCallback resourceHandler,
                                             QualityOfService QoS)
     {
-        return checked_guard(m_client, &IClientWrapper::ListenForResource2,
+        return checked_guard(m_client, &IClientWrapper::ListenForResourceList,
                              host, resourceName, connectivityType, resourceHandler, QoS);
     }
 
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
+                                            const std::string& resourceName,
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            FindErrorCallback errorHandler,
+                                            QualityOfService Qos)
+    {
+        return checked_guard(m_client, &IClientWrapper::ListenForResourceListWithError,
+                             host, resourceName, connectivityType, resourceHandler,
+                             errorHandler, Qos);
+    }
+
     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
                                             const std::string& deviceURI,
                                             OCConnectivityType connectivityType,