From 34e0c46505b1031815d3962c17a05d8d814fe19f Mon Sep 17 00:00:00 2001 From: Harish Kumara Marappa Date: Wed, 12 Oct 2016 00:27:41 +0900 Subject: [PATCH] [IOT-1440] findResource() to return list of discovered resources. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/13113 Tested-by: jenkins-iotivity Reviewed-by: Abhishek Pandey Reviewed-by: Uze Choi Reviewed-by: Ashok Babu Channa --- resource/include/IClientWrapper.h | 6 ++ resource/include/InProcClientWrapper.h | 13 +++++ resource/include/OCApi.h | 2 + resource/include/OCPlatform.h | 4 ++ resource/include/OCPlatform_impl.h | 4 ++ resource/include/OutOfProcClientWrapper.h | 7 +++ resource/src/InProcClientWrapper.cpp | 91 +++++++++++++++++++++++++++++++ resource/src/OCPlatform.cpp | 8 +++ resource/src/OCPlatform_impl.cpp | 10 ++++ 9 files changed, 145 insertions(+) diff --git a/resource/include/IClientWrapper.h b/resource/include/IClientWrapper.h index 37adf61..b453068 100644 --- a/resource/include/IClientWrapper.h +++ b/resource/include/IClientWrapper.h @@ -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, diff --git a/resource/include/InProcClientWrapper.h b/resource/include/InProcClientWrapper.h index 9565d3b..bbf6a38 100644 --- a/resource/include/InProcClientWrapper.h +++ b/resource/include/InProcClientWrapper.h @@ -56,6 +56,15 @@ namespace OC : callback(cb), clientWrapper(cw){} }; + struct ListenContext2 + { + FindResListCallback callback; + std::weak_ptr clientWrapper; + + ListenContext2(FindResListCallback cb, std::weak_ptr 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); diff --git a/resource/include/OCApi.h b/resource/include/OCApi.h index 2510f74..9094413 100644 --- a/resource/include/OCApi.h +++ b/resource/include/OCApi.h @@ -275,6 +275,8 @@ namespace OC typedef std::function FindErrorCallback; + typedef std::function>)> FindResListCallback; + typedef std::function FindDeviceCallback; typedef std::function FindPlatformCallback; diff --git a/resource/include/OCPlatform.h b/resource/include/OCPlatform.h index 257a5a7..305ad61 100644 --- a/resource/include/OCPlatform.h +++ b/resource/include/OCPlatform.h @@ -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 * diff --git a/resource/include/OCPlatform_impl.h b/resource/include/OCPlatform_impl.h index 0a6c063..cffeeea 100644 --- a/resource/include/OCPlatform_impl.h +++ b/resource/include/OCPlatform_impl.h @@ -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); diff --git a/resource/include/OutOfProcClientWrapper.h b/resource/include/OutOfProcClientWrapper.h index 8e7e13c..c32656c 100644 --- a/resource/include/OutOfProcClientWrapper.h +++ b/resource/include/OutOfProcClientWrapper.h @@ -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*/, diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 494e5df..91a5708 100644 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -327,6 +327,97 @@ namespace OC } return result; } + + OCStackApplicationResult listenCallback2(void* ctx, OCDoHandle /*handle*/, + OCClientResponse* clientResponse) + { + ClientCallbackContext::ListenContext2* context = + static_cast(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(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(context), + cbdata.cb = listenCallback2; + cbdata.cd = [](void* c){delete (ClientCallbackContext::ListenContext2*)c;}; + + auto cLock = m_csdkLock.lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCDoResource(nullptr, OC_REST_DISCOVER, + resourceUri.str().c_str(), + nullptr, nullptr, connectivityType, + static_cast(QoS), + &cbdata, + nullptr, 0); + } + else + { + delete context; + result = OC_STACK_ERROR; + } + return result; + } + #ifdef WITH_MQ OCStackApplicationResult listenMQCallback(void* ctx, OCDoHandle /*handle*/, OCClientResponse* clientResponse) diff --git a/resource/src/OCPlatform.cpp b/resource/src/OCPlatform.cpp index 21f8204..59e4fab 100644 --- a/resource/src/OCPlatform.cpp +++ b/resource/src/OCPlatform.cpp @@ -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, diff --git a/resource/src/OCPlatform_impl.cpp b/resource/src/OCPlatform_impl.cpp index 547b4d9..7060a09 100644 --- a/resource/src/OCPlatform_impl.cpp +++ b/resource/src/OCPlatform_impl.cpp @@ -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, -- 2.7.4