From 657365d989719ebf799646ef1fa960d5a6402c0b Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Sun, 16 Aug 2015 19:22:51 +0530 Subject: [PATCH] [Resource Encapsulation] Updated DiscoverResource APIs - Updated RCSDiscoveryManager class - Updated Linux Sample App - Updated UnitTestCase Change-Id: Iaaf72b85335df2507543ccc16de6c094d4c1fe1f Signed-off-by: Jay Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/2158 Reviewed-by: JungHo Kim Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../NotificationManager/src/ResourceHosting.cpp | 4 +- .../examples/linux/SampleResourceClient.cpp | 8 +- .../include/RCSDiscoveryManager.h | 120 +++++++++++++++------ .../src/resourceClient/RCSDiscoveryManager.cpp | 33 ++++-- .../unittests/ResourceClientTest.cpp | 9 +- 5 files changed, 123 insertions(+), 51 deletions(-) diff --git a/service/notification-manager/NotificationManager/src/ResourceHosting.cpp b/service/notification-manager/NotificationManager/src/ResourceHosting.cpp index b41bfe9..a85a853 100644 --- a/service/notification-manager/NotificationManager/src/ResourceHosting.cpp +++ b/service/notification-manager/NotificationManager/src/ResourceHosting.cpp @@ -184,9 +184,9 @@ void ResourceHosting::requestMulticastDiscovery() void ResourceHosting::requestDiscovery(std::string address) { std::string host = address; - std::string uri = OC_RSRVD_WELL_KNOWN_URI + std::string("?rt=") + HOSTING_RESOURSE_TYPE; RCSAddress rcsAddress = RCSAddress::unicast(host); - discoveryManager->discoverResource(rcsAddress, uri, pDiscoveryCB); + discoveryManager->discoverResourceByType(rcsAddress, OC_RSRVD_WELL_KNOWN_URI, + HOSTING_RESOURSE_TYPE, pDiscoveryCB); } void ResourceHosting::discoverHandler(RemoteObjectPtr remoteResource) diff --git a/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp b/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp index 316042e..b905977 100644 --- a/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp +++ b/service/resource-encapsulation/examples/linux/SampleResourceClient.cpp @@ -39,8 +39,8 @@ constexpr int QUIT_INPUT = 3; std::shared_ptr resource; const std::string defaultKey = "Temperature"; -const std::string resourceType = "?rt=core.TemperatureSensor"; -const std::string targetUri = OC_RSRVD_WELL_KNOWN_URI + resourceType; +const std::string resourceType = "core.TemperatureSensor"; +const std::string relativetUri = OC_RSRVD_WELL_KNOWN_URI; std::mutex mtx; std::condition_variable cond; @@ -397,8 +397,8 @@ bool discoverResource() { std::cout << "Wait 2 seconds until discovered." << std::endl; - RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), targetUri, - &onResourceDiscovered); + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), + relativetUri, resourceType, &onResourceDiscovered); std::unique_lock lck(mtx); cond.wait_for(lck,std::chrono::seconds(2)); diff --git a/service/resource-encapsulation/include/RCSDiscoveryManager.h b/service/resource-encapsulation/include/RCSDiscoveryManager.h index 53ad2b1..2105647 100644 --- a/service/resource-encapsulation/include/RCSDiscoveryManager.h +++ b/service/resource-encapsulation/include/RCSDiscoveryManager.h @@ -21,7 +21,7 @@ /** * @file * - * This file contains the RCSDiscoveryManager class which provide API to discover the Resource in the network + * This file contains the RCSDiscoveryManager class which provide APIs to discover the Resource in the network * */ @@ -35,54 +35,104 @@ namespace OIC { namespace Service { - class RCSRemoteResourceObject; class RCSAddress; /** - * This class contains the resource discovery method. + * This class contains the resource discovery methods. * * @see RCSRemoteResourceObject */ class RCSDiscoveryManager { - public: + public: + + /** + * Typedef for callback of discoverResource APIs + * + * @see discoverResource + */ + typedef std::function< void(std::shared_ptr< RCSRemoteResourceObject >) > + ResourceDiscoveredCallback; + + /** + * Returns RCSDiscoveryManager instance. + * + */ + static RCSDiscoveryManager* getInstance(); + + /** + * API for discovering the resource of Interest, regardless of URI and resource type + * + * @param address A RCSAddress object + * @param cb A callback to obtain discovered resource + * + * @throws InvalidParameterException If cb is empty. + * + * @note The callback will be invoked in an internal thread. + * + * @see RCSAddress + * + */ + void discoverResource(const RCSAddress& address, ResourceDiscoveredCallback cb); - /** - * Typedef for callback of discoverResource API - * - * @see discoverResource - */ - typedef std::function< void(std::shared_ptr< RCSRemoteResourceObject >) > - ResourceDiscoveredCallback; + /** + * API for discovering the resource of Interest, regardless of resource type + * + * @param address A RCSAddress object + * @param relativeURI The relative uri of resource to be searched + * @param cb A callback to obtain discovered resource + * + * @throws InvalidParameterException If cb is empty. + * + * @note The callback will be invoked in an internal thread. + * + * @see RCSAddress + * + */ + void discoverResource(const RCSAddress& address, const std::string& relativeURI, + ResourceDiscoveredCallback cb); - /** - * Returns RCSDiscoveryManager instance. - * - */ - static RCSDiscoveryManager* getInstance(); + /** + * API for discovering the resource of Interest by Resource type. + * + * @param address A RCSAddress object + * @param resourceType Ressource Type + * @param cb A callback to obtain discovered resource + * + * @throws InvalidParameterException If cb is empty. + * + * @note The callback will be invoked in an internal thread. + * + * @see RCSAddress + * + */ + void discoverResourceByType(const RCSAddress& address, const std::string& resourceType, + ResourceDiscoveredCallback cb); - /** - * API for discovering the resource of Interest. - * - * @param address A RCSAddress object - * @param resourceURI The uri of resource to be searched - * @param cb A callback to obtain discovered resource - * - * @throws InvalidParameterException If cb is empty. - * - * @note The callback will be invoked in an internal thread. - * - * @see RCSAddress - * - */ - void discoverResource(const RCSAddress& address, const std::string& resourceURI, - ResourceDiscoveredCallback cb); + /** + * API for discovering the resource of Interest by Resource type with provided relativeURI + * + * @param address A RCSAddress object + * @param relativeURI The relative uri of resource to be searched + * @param resourceType Ressource Type + * @param cb A callback to obtain discovered resource + * + * @throws InvalidParameterException If cb is empty. + * + * @note The callback will be invoked in an internal thread. + * + * @see RCSAddress + * + */ + void discoverResourceByType(const RCSAddress& address, const std::string& relativeURI, + const std::string& resourceType, + ResourceDiscoveredCallback cb); - private: - RCSDiscoveryManager() = default; - ~RCSDiscoveryManager() = default; + private: + RCSDiscoveryManager() = default; + ~RCSDiscoveryManager() = default; }; } } diff --git a/service/resource-encapsulation/src/resourceClient/RCSDiscoveryManager.cpp b/service/resource-encapsulation/src/resourceClient/RCSDiscoveryManager.cpp index c51b733..7e19d4d 100644 --- a/service/resource-encapsulation/src/resourceClient/RCSDiscoveryManager.cpp +++ b/service/resource-encapsulation/src/resourceClient/RCSDiscoveryManager.cpp @@ -57,18 +57,39 @@ namespace OIC } void RCSDiscoveryManager::discoverResource(const RCSAddress& address, - const std::string& resourceURI, ResourceDiscoveredCallback cb) + ResourceDiscoveredCallback cb) { - SCOPE_LOG_F(DEBUG, TAG); + discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, "", std::move(cb)); + } + + void RCSDiscoveryManager::discoverResource(const RCSAddress& address, + const std::string& relativeURI, ResourceDiscoveredCallback cb) + { + discoverResourceByType(address, relativeURI, "", std::move(cb)); + } + + void RCSDiscoveryManager::discoverResourceByType(const RCSAddress& address, + const std::string& resourceType, + ResourceDiscoveredCallback cb) + { + discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, resourceType, std::move(cb)); + } + void RCSDiscoveryManager::discoverResourceByType(const RCSAddress& address, + const std::string& relativeURI, const std::string& resourceType, + ResourceDiscoveredCallback cb) + { if (!cb) { - OC_LOG(ERROR, TAG, "discoverResource NULL Callback"); - throw InvalidParameterException{ "discoverResource NULL Callback'" }; + OC_LOG(ERROR, TAG, "discoverResourceByType NULL Callback"); + throw InvalidParameterException { "discoverResourceByType NULL Callback'" }; } - - OIC::Service::discoverResource(address, resourceURI, + else + { + std::string resourceURI = relativeURI + "?rt=" + resourceType; + OIC::Service::discoverResource(address, resourceURI, std::bind(findCallback, std::placeholders::_1, std::move(cb))); + } } } } diff --git a/service/resource-encapsulation/unittests/ResourceClientTest.cpp b/service/resource-encapsulation/unittests/ResourceClientTest.cpp index c51d1a5..08b58cd 100644 --- a/service/resource-encapsulation/unittests/ResourceClientTest.cpp +++ b/service/resource-encapsulation/unittests/ResourceClientTest.cpp @@ -102,10 +102,11 @@ private: { while (checkObject()) { - RCSDiscoveryManager::getInstance()->discoverResource(RCSAddress::multicast(), - "/oic/res?rt=Resource.Hosting", std::bind(resourceDiscovered, this, finished, - std::placeholders::_1)); - + const std::string uri = "/oic/res"; + const std::string type = "Resource.Hosting"; + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), + uri, type, std::bind(resourceDiscovered, this, finished, + std::placeholders::_1)); Wait(1000); } } -- 2.7.4