From 814ae39e882dd07f9a281cd7d9bd7f796f6f7ca1 Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Tue, 27 Dec 2016 19:07:32 +0530 Subject: [PATCH] Added a Variant of findResourceList API which support error callback. JIRA : IOT-1706 Change-Id: I85fea5c323746534b3f95515c9a7eb920ba7cbb3 Signed-off-by: Jay Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/15915 Tested-by: jenkins-iotivity Reviewed-by: Jihun Ha Reviewed-by: Abhishek Pandey Reviewed-by: Ashok Babu Channa --- resource/include/IClientWrapper.h | 9 +- resource/include/InProcClientWrapper.h | 28 ++++-- resource/include/OCPlatform.h | 4 + resource/include/OCPlatform_impl.h | 4 + resource/include/OutOfProcClientWrapper.h | 10 ++- resource/src/InProcClientWrapper.cpp | 143 ++++++++++++++++++++++++++---- resource/src/OCPlatform.cpp | 8 ++ resource/src/OCPlatform_impl.cpp | 14 ++- 8 files changed, 194 insertions(+), 26 deletions(-) diff --git a/resource/include/IClientWrapper.h b/resource/include/IClientWrapper.h index d1658c7..52a400f 100644 --- a/resource/include/IClientWrapper.h +++ b/resource/include/IClientWrapper.h @@ -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, diff --git a/resource/include/InProcClientWrapper.h b/resource/include/InProcClientWrapper.h index 530b076..4bfd512 100644 --- a/resource/include/InProcClientWrapper.h +++ b/resource/include/InProcClientWrapper.h @@ -56,22 +56,33 @@ namespace OC : callback(cb), clientWrapper(cw){} }; - struct ListenContext2 + struct ListenErrorContext + { + FindCallback callback; + FindErrorCallback errorCallback; + std::weak_ptr clientWrapper; + + ListenErrorContext(FindCallback cb1, FindErrorCallback cb2, + std::weak_ptr cw) + : callback(cb1), errorCallback(cb2), clientWrapper(cw){} + }; + + struct ListenResListContext { FindResListCallback callback; std::weak_ptr clientWrapper; - ListenContext2(FindResListCallback cb, std::weak_ptr cw) + ListenResListContext(FindResListCallback cb, std::weak_ptr cw) : callback(cb), clientWrapper(cw){} }; - struct ListenErrorContext + struct ListenResListWithErrorContext { - FindCallback callback; + FindResListCallback callback; FindErrorCallback errorCallback; std::weak_ptr clientWrapper; - ListenErrorContext(FindCallback cb1, FindErrorCallback cb2, + ListenResListWithErrorContext(FindResListCallback cb1, FindErrorCallback cb2, std::weak_ptr 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); diff --git a/resource/include/OCPlatform.h b/resource/include/OCPlatform.h index 1adcb82..ec8e3aa 100644 --- a/resource/include/OCPlatform.h +++ b/resource/include/OCPlatform.h @@ -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& value); OCStackResult getPropertyValue(OCPayloadType type, const std::string& tag, std::string& value); diff --git a/resource/include/OCPlatform_impl.h b/resource/include/OCPlatform_impl.h index e20dc7d..6b52693 100644 --- a/resource/include/OCPlatform_impl.h +++ b/resource/include/OCPlatform_impl.h @@ -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); diff --git a/resource/include/OutOfProcClientWrapper.h b/resource/include/OutOfProcClientWrapper.h index 0fb6fa4..1178367 100644 --- a/resource/include/OutOfProcClientWrapper.h +++ b/resource/include/OutOfProcClientWrapper.h @@ -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*/, diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 6a4b15c..9dccd59 100644 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -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(ctx); @@ -228,10 +233,6 @@ namespace OC ClientCallbackContext::ListenErrorContext* context = static_cast(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(ctx); + if (!ctx || !clientResponse) + { + return OC_STACK_KEEP_TRANSACTION; + } + + ClientCallbackContext::ListenResListContext* context = + static_cast(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(context), + cbdata.cb = listenResListCallback; + cbdata.cd = [](void* c){delete (ClientCallbackContext::ListenResListContext*)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; + } + + OCStackApplicationResult listenResListWithErrorCallback(void* ctx, OCDoHandle /*handle*/, + OCClientResponse* clientResponse) + { + if (!ctx || !clientResponse) + { + return OC_STACK_KEEP_TRANSACTION; + } + + ClientCallbackContext::ListenResListWithErrorContext* context = + static_cast(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(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(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) diff --git a/resource/src/OCPlatform.cpp b/resource/src/OCPlatform.cpp index c41d665..e05820b 100644 --- a/resource/src/OCPlatform.cpp +++ b/resource/src/OCPlatform.cpp @@ -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, diff --git a/resource/src/OCPlatform_impl.cpp b/resource/src/OCPlatform_impl.cpp index f23688f..0180b7f 100644 --- a/resource/src/OCPlatform_impl.cpp +++ b/resource/src/OCPlatform_impl.cpp @@ -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, -- 2.7.4