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>
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,
: 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;
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);
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;
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
*
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);
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*/,
}
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)
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,
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,