[IOT-1440] findResource() to return list of discovered resources.
authorHarish Kumara Marappa <h.marappa@samsung.com>
Tue, 11 Oct 2016 15:27:41 +0000 (00:27 +0900)
committerZiran Sun <ziran.sun@samsung.com>
Fri, 14 Oct 2016 08:53:46 +0000 (08:53 +0000)
OCPlatform::findResource() does not return all the discovered resources in single callback
even though it gets the whole discovery payload from RI C layer.

This change implements OCPlatform::findResourceList() API which does return all the parsed
resources in discovery payload in single callback.

Change-Id: Ic95541f7303a15eb676029e6f2ea4952e5e3ca3e
Signed-off-by: Harish Kumara Marappa <h.marappa@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13111
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
Reviewed-by: Abhishek Pandey <abhi.siso@samsung.com>
Reviewed-by: Ziran Sun <ziran.sun@samsung.com>
resource/include/IClientWrapper.h
resource/include/InProcClientWrapper.h
resource/include/OCApi.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 37adf61..b453068 100644 (file)
@@ -45,6 +45,12 @@ namespace OC
                         FindCallback& callback,
                         QualityOfService QoS) = 0;
 
+        virtual OCStackResult ListenForResource2(const std::string& serviceUrl,
+                        const std::string& resourceType,
+                        OCConnectivityType connectivityType,
+                        FindResListCallback& callback,
+                        QualityOfService QoS) = 0;
+
         virtual OCStackResult ListenErrorForResource(const std::string& serviceUrl,
                         const std::string& resourceType,
                         OCConnectivityType connectivityType,
index 9565d3b..bbf6a38 100644 (file)
@@ -56,6 +56,15 @@ namespace OC
                 : callback(cb), clientWrapper(cw){}
         };
 
+        struct ListenContext2
+        {
+            FindResListCallback callback;
+            std::weak_ptr<IClientWrapper> clientWrapper;
+
+            ListenContext2(FindResListCallback cb, std::weak_ptr<IClientWrapper> cw)
+                : callback(cb), clientWrapper(cw){}
+        };
+
         struct ListenErrorContext
         {
             FindCallback callback;
@@ -124,6 +133,10 @@ namespace OC
             const std::string& resourceType, OCConnectivityType transportFlags,
             FindCallback& callback, QualityOfService QoS);
 
+        virtual OCStackResult ListenForResource2(const std::string& serviceUrl,
+            const std::string& resourceType, OCConnectivityType transportFlags,
+            FindResListCallback& callback, QualityOfService QoS);
+
         virtual OCStackResult ListenErrorForResource(const std::string& serviceUrl,
             const std::string& resourceType, OCConnectivityType transportFlags,
             FindCallback& callback, FindErrorCallback& errorCallback, QualityOfService QoS);
index 2510f74..9094413 100644 (file)
@@ -275,6 +275,8 @@ namespace OC
 
     typedef std::function<void(const std::string&, const int)> FindErrorCallback;
 
+    typedef std::function<void(std::vector<std::shared_ptr<OCResource>>)> FindResListCallback;
+
     typedef std::function<void(const OCRepresentation&)> FindDeviceCallback;
 
     typedef std::function<void(const OCRepresentation&)> FindPlatformCallback;
index 257a5a7..305ad61 100644 (file)
@@ -173,6 +173,10 @@ namespace OC
                     OCConnectivityType connectivityType, FindCallback resourceHandler,
                     FindErrorCallback errorHandler, QualityOfService QoS);
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    QualityOfService QoS = QualityOfService::LowQos);
+
         /**
          * API for Device Discovery
          *
index 0a6c063..cffeeea 100644 (file)
@@ -92,6 +92,10 @@ namespace OC
                     OCConnectivityType connectivityType, FindCallback resourceHandler,
                     FindErrorCallback errorHandler, QualityOfService QoS);
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    QualityOfService QoS);
+
         OCStackResult getDeviceInfo(const std::string& host, const std::string& deviceURI,
                     OCConnectivityType connectivityType, FindDeviceCallback deviceInfoHandler);
 
index 8e7e13c..c32656c 100644 (file)
@@ -39,6 +39,13 @@ namespace OC
                                                 QualityOfService /*QoS*/)
             {return OC_STACK_NOTIMPL;}
 
+        virtual OCStackResult ListenForResource2(const std::string& /*servUrl*/,
+                                                const std::string& /*rsrcType*/,
+                                                OCConnectivityType /*connType*/,
+                                                FindResListCallback& /*callback*/,
+                                                QualityOfService /*QoS*/)
+            {return OC_STACK_NOTIMPL;}
+
         virtual OCStackResult ListenErrorForResource(const std::string& /*servUrl*/,
                                                      const std::string& /*rsrcType*/,
                                                      OCConnectivityType /*connType*/,
index 6283c72..e31ddf3 100644 (file)
@@ -327,6 +327,97 @@ namespace OC
         }
         return result;
     }
+
+    OCStackApplicationResult listenCallback2(void* ctx, OCDoHandle /*handle*/,
+        OCClientResponse* clientResponse)
+    {
+        ClientCallbackContext::ListenContext2* context =
+            static_cast<ClientCallbackContext::ListenContext2*>(ctx);
+
+        if (clientResponse->result != OC_STACK_OK)
+        {
+            oclog() << "listenCallback2(): failed to create resource. clientResponse: "
+                    << clientResponse->result
+                    << std::flush;
+
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
+        {
+            oclog() << "listenCallback2(): clientResponse payload was null or the wrong type"
+                << std::flush;
+            return OC_STACK_KEEP_TRANSACTION;
+        }
+
+        auto clientWrapper = context->clientWrapper.lock();
+
+        if (!clientWrapper)
+        {
+            oclog() << "listenCallback2(): 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 listCallback2, ignoring response: "
+                    << e.what() << std::flush;
+        }
+
+
+        return OC_STACK_KEEP_TRANSACTION;
+    }
+
+    OCStackResult InProcClientWrapper::ListenForResource2(
+            const std::string& serviceUrl,
+            const std::string& resourceType,
+            OCConnectivityType connectivityType,
+            FindResListCallback& callback, QualityOfService QoS)
+    {
+        if (!callback)
+        {
+            return OC_STACK_INVALID_PARAM;
+        }
+
+        OCStackResult result;
+        ostringstream resourceUri;
+        resourceUri << serviceUrl << resourceType;
+
+        ClientCallbackContext::ListenContext2* context =
+            new ClientCallbackContext::ListenContext2(callback, shared_from_this());
+        OCCallbackData cbdata;
+        cbdata.context = static_cast<void*>(context),
+        cbdata.cb      = listenCallback2;
+        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ListenContext2*)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;
+    }
+
 #ifdef WITH_MQ
     OCStackApplicationResult listenMQCallback(void* ctx, OCDoHandle /*handle*/,
                                               OCClientResponse* clientResponse)
index 21f8204..59e4fab 100644 (file)
@@ -123,6 +123,14 @@ namespace OC
                                     connectivityType, resourceHandler, errorHandler, QoS);
         }
 
+        OCStackResult findResourceList(const std::string& host, const std::string& resourceURI,
+                    OCConnectivityType connectivityType, FindResListCallback resourceHandler,
+                    QualityOfService QoS)
+        {
+            return OCPlatform_impl::Instance().findResourceList(host, resourceURI,
+                                    connectivityType, resourceHandler, QoS);
+        }
+
         OCStackResult getDeviceInfo(const std::string& host,
                                  const std::string& deviceURI,
                                  OCConnectivityType connectivityType,
index 547b4d9..7060a09 100644 (file)
@@ -209,6 +209,16 @@ namespace OC
                              errorHandler, QoS);
     }
 
+    OCStackResult OCPlatform_impl::findResourceList(const std::string& host,
+                                            const std::string& resourceName,
+                                            OCConnectivityType connectivityType,
+                                            FindResListCallback resourceHandler,
+                                            QualityOfService QoS)
+    {
+        return checked_guard(m_client, &IClientWrapper::ListenForResource2,
+                             host, resourceName, connectivityType, resourceHandler, QoS);
+    }
+
     OCStackResult OCPlatform_impl::getDeviceInfo(const std::string& host,
                                             const std::string& deviceURI,
                                             OCConnectivityType connectivityType,