*
*/
-#ifndef RCSDISCOVERYMANAGER_H
-#define RCSDISCOVERYMANAGER_H
+#ifndef RCS_DISCOVERYMANAGER_H_
+#define RCS_DISCOVERYMANAGER_H_
#include <memory>
#include <functional>
{
namespace Service
{
- class RCSRemoteResourceObject;
class RCSAddress;
-
+ class RCSRemoteResourceObject;
/**
* This class contains the resource discovery methods.
*
*/
class RCSDiscoveryManager
{
- public:
+ public:
/**
* This class represents a discovery task.
*
- * @note A discovery task will be automatically canceled when destroyed.
*/
- class DiscoveryTask
- {
- public:
- typedef std::unique_ptr<DiscoveryTask> Ptr;
+ class DiscoveryTask
+ {
+ public:
+ typedef std::unique_ptr< DiscoveryTask > Ptr;
- ~DiscoveryTask();
+ ~DiscoveryTask() = default;
- DiscoveryTask(const DiscoveryTask&) = delete;
- DiscoveryTask(DiscoveryTask&&) = delete;
- DiscoveryTask& operator = (const DiscoveryTask&) const = delete;
- DiscoveryTask& operator = (DiscoveryTask&&) const = delete;
+ DiscoveryTask(const DiscoveryTask&) = delete;
+ DiscoveryTask(DiscoveryTask&&) = delete;
+ DiscoveryTask& operator =(const DiscoveryTask&) const = delete;
+ DiscoveryTask& operator =(DiscoveryTask&&) const = delete;
- /**
- * Cancel the task for discovery request. If cancel is called in duplicate, the request is ignored.
- */
- void cancel();
+ /**
+ * It is cancelling the task of discovery.
+ * If it is already canceled, the operation is ignored.
+ */
+ void cancel();
- /**
- * Return a boolean value whether the discovery request is canceled or not.
- */
- bool isCanceled();
+ /**
+ * Returns whether the discovery request is canceled or not.
+ */
+ bool isCanceled();
- private:
- explicit DiscoveryTask(unsigned int);
+ private:
+ explicit DiscoveryTask(unsigned int);
- private:
- unsigned int m_id;
- friend class RCSDiscoveryManagerImpl;
- };
+ private:
+ unsigned int m_id;
- public:
+ friend class RCSDiscoveryManagerImpl;
+ };
- /**
- * Typedef for callback of discoverResource APIs
- *
- * @see discoverResource
- */
- typedef std::function< void(std::shared_ptr< RCSRemoteResourceObject >) >
- ResourceDiscoveredCallback;
+ public:
- /**
- * @return RCSDiscoveryManager instance.
- *
- */
- static RCSDiscoveryManager* getInstance();
+ /**
+ * Typedef for callback of discoverResource APIs
+ *
+ * @see discoverResource
+ */
+ typedef std::function< void(std::shared_ptr< RCSRemoteResourceObject >)
+ > ResourceDiscoveredCallback;
- /**
- * Discovering the resource of interest, regardless of uri and resource type.
- * Find resource matching request periodically until returned resource is disappeared or destroyed.
- *
- * @return Returned object must be received.
- *
- * @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.
- *
- */
- DiscoveryTask::Ptr discoverResource(const RCSAddress& address,
- ResourceDiscoveredCallback cb);
+ /**
+ * @return RCSDiscoveryManager instance.
+ *
+ */
+ static RCSDiscoveryManager* getInstance();
- /**
- * Discovering the resource of Interest, regardless of resource type.
- * Find resource matching request periodically until returned resource is disappeared or destroyed.
- *
- * @return Returned object must be received.
- *
- * @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
- *
- */
- DiscoveryTask::Ptr discoverResource(const RCSAddress& address,
- const std::string& relativeURI, ResourceDiscoveredCallback cb);
+ /**
+ * Discovers resources of interest, regardless of uri and resource type.
+ * It is Consistently discovering resources until the discovery task is canceled.
+ *
+ * @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.
+ *
+ */
+ DiscoveryTask::Ptr discoverResource(const RCSAddress& address,
+ ResourceDiscoveredCallback cb);
- /**
- * Discovering the resource of Interest by Resource type.
- * Find resource matching request periodically until returned resource is disappeared or destroyed.
- *
- * @return Returned object must be received.
- *
- * @param address A RCSAddress object
- * @param resourceType Resource 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
- *
- */
- DiscoveryTask::Ptr discoverResourceByType(const RCSAddress& address,
- const std::string& resourceType, ResourceDiscoveredCallback cb);
+ /**
+ * Discovers resources of interest, regardless of resource type.
+ * It is Consistently discovering resources until the discovery task is canceled.
+ *
+ * @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.
+ *
+ */
+ DiscoveryTask::Ptr discoverResource(const RCSAddress& address,
+ const std::string& relativeUri, ResourceDiscoveredCallback cb);
- /**
- * Discovering the resource of Interest by Resource type with provided relativeURI.
- * Find resource matching request periodically until returned resource is disappeared or destroyed.
- *
- * @return Returned object must be received.
- *
- * @param address A RCSAddress object
- * @param relativeURI The relative uri of resource to be searched
- * @param resourceType Resource 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
- *
- */
- DiscoveryTask::Ptr discoverResourceByType(const RCSAddress& address,
- const std::string& relativeURI, const std::string& resourceType,
- ResourceDiscoveredCallback cb);
+ /**
+ * Discovers resources of interest by resource type.
+ * It is Consistently discovering resources until the discovery task is canceled.
+ *
+ * @param address A RCSAddress object
+ * @param resourceType Resource 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.
+ *
+ */
+ DiscoveryTask::Ptr discoverResourceByType(const RCSAddress& address,
+ const std::string& resourceType, ResourceDiscoveredCallback cb);
- private:
- RCSDiscoveryManager() = default;
- ~RCSDiscoveryManager()= default;
+ /**
+ * Discovers resources of interest by resource type with provided relativeUri.
+ * It is Consistently discovering resources until the discovery task is canceled.
+ *
+ * @param address A RCSAddress object
+ * @param relativeUri The relative uri of resource to be searched
+ * @param resourceType Resource 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.
+ *
+ */
+ DiscoveryTask::Ptr discoverResourceByType(const RCSAddress& address,
+ const std::string& relativeUri, const std::string& resourceType,
+ ResourceDiscoveredCallback cb);
- friend class DiscoveryTask;
+ private:
+ RCSDiscoveryManager() = default;
+ ~RCSDiscoveryManager() = default;
};
}
}
#include "RCSDiscoveryManager.h"
#include "RCSDiscoveryManagerImpl.h"
-#define TAG "RCSDiscoveryManager"
-
-namespace OIC {
- namespace Service {
-
+namespace OIC
+{
+ namespace Service
+ {
RCSDiscoveryManager::DiscoveryTask::DiscoveryTask(unsigned int id) :
- m_id{ id }
+ m_id { id }
{
}
- RCSDiscoveryManager::DiscoveryTask::~DiscoveryTask()
- {
- cancel();
- }
-
bool RCSDiscoveryManager::DiscoveryTask::isCanceled()
{
return RCSDiscoveryManagerImpl::getInstance()->isCanceled(m_id);
RCSDiscoveryManagerImpl::getInstance()->cancel(m_id);
}
- RCSDiscoveryManager* RCSDiscoveryManager::getInstance() {
+ RCSDiscoveryManager* RCSDiscoveryManager::getInstance()
+ {
static RCSDiscoveryManager instance;
return &instance;
}
- std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> RCSDiscoveryManager::discoverResource
- (const RCSAddress& address, ResourceDiscoveredCallback cb) {
- return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, "",
- std::move(cb));
+ RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResource(
+ const RCSAddress& address, ResourceDiscoveredCallback cb)
+ {
+ return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, "", std::move(cb));
}
- std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> RCSDiscoveryManager::discoverResource
- (const RCSAddress& address, const std::string& relativeURI, ResourceDiscoveredCallback cb) {
- return discoverResourceByType(address, relativeURI, "", std::move(cb));
+ RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResource(
+ const RCSAddress& address, const std::string& relativeUri,
+ ResourceDiscoveredCallback cb)
+ {
+ return discoverResourceByType(address, relativeUri, "", std::move(cb));
}
- std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> RCSDiscoveryManager::discoverResourceByType(
- const RCSAddress& address, const std::string& resourceType, ResourceDiscoveredCallback cb) {
- return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI,
- resourceType, std::move(cb));
+ RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResourceByType(
+ const RCSAddress& address, const std::string& resourceType,
+ ResourceDiscoveredCallback cb)
+ {
+ return discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, resourceType,
+ std::move(cb));
}
- std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> RCSDiscoveryManager::discoverResourceByType(
- const RCSAddress& address, const std::string& relativeURI,
- const std::string& resourceType, ResourceDiscoveredCallback cb) {
- return RCSDiscoveryManagerImpl::getInstance()->startDiscovery(address,
- relativeURI.empty() ? OC_RSRVD_WELL_KNOWN_URI : relativeURI,
- resourceType, std::move(cb));
+ RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManager::discoverResourceByType(
+ const RCSAddress& address, const std::string& relativeUri,
+ const std::string& resourceType, ResourceDiscoveredCallback cb)
+ {
+ return RCSDiscoveryManagerImpl::getInstance()->startDiscovery(address,
+ relativeUri.empty() ? OC_RSRVD_WELL_KNOWN_URI : relativeUri, resourceType,
+ std::move(cb));
}
}
}
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-#include <limits>
-
#include "RCSDiscoveryManagerImpl.h"
#include "OCPlatform.h"
namespace
{
- constexpr unsigned int LIMITNUMBER = std::numeric_limits<unsigned int>::max();
- constexpr unsigned int INTERVALTIME = 60000;
+ constexpr unsigned int POLLING_INTERVAL_TIME = 60000;
}
namespace OIC
{
RCSDiscoveryManagerImpl::RCSDiscoveryManagerImpl()
{
- srand(time(NULL));
+ srand (time(NULL));
requestMulticastPresence();
- m_timer.post(INTERVALTIME, std::bind(&RCSDiscoveryManagerImpl::onPolling, this));
+ m_timer.post(POLLING_INTERVAL_TIME, std::bind(&RCSDiscoveryManagerImpl::onPolling, this));
}
RCSDiscoveryManagerImpl* RCSDiscoveryManagerImpl::getInstance()
return &instance;
}
- void RCSDiscoveryManagerImpl::onResourceFound(std::shared_ptr< PrimitiveResource > resource,
- RCSDiscoveryManagerImpl::ID discoveryId,
- const RCSDiscoveryManager::ResourceDiscoveredCallback& discoverCB)
+ void RCSDiscoveryManagerImpl::onResourceFound(std::shared_ptr<PrimitiveResource> resource,
+ RCSDiscoveryManagerImpl::ID discoveryId,
+ const RCSDiscoveryManager::ResourceDiscoveredCallback& discoverCB)
{
{
- std::lock_guard<std::mutex> lock(m_mutex);
+ std::lock_guard < std::mutex > lock(m_mutex);
auto it = m_discoveryMap.find(discoveryId);
- if(it == m_discoveryMap.end()) return;
- if(it->second.isKnownResource(resource)) return;
+ if (it == m_discoveryMap.end()) return;
+ if (it->second.isKnownResource(resource)) return;
}
- discoverCB(std::make_shared<RCSRemoteResourceObject>(resource));
+ discoverCB(std::make_shared < RCSRemoteResourceObject > (resource));
}
- RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManagerImpl::startDiscovery
- (const RCSAddress& address, const std::string& relativeUri, const std::string& resourceType,
- RCSDiscoveryManager::ResourceDiscoveredCallback cb)
+ RCSDiscoveryManager::DiscoveryTask::Ptr RCSDiscoveryManagerImpl::startDiscovery(
+ const RCSAddress& address, const std::string& relativeUri,
+ const std::string& resourceType, RCSDiscoveryManager::ResourceDiscoveredCallback cb)
{
if (!cb)
{
ID discoveryId = createId();
auto discoverCb = std::bind(&RCSDiscoveryManagerImpl::onResourceFound, this,
std::placeholders::_1, discoveryId, std::move(cb));
- DiscoveryRequestInfo discoveryItem(RCSAddressDetail::getDetail(address)->getAddress(), relativeUri,
- resourceType, std::move(discoverCb));
- discoveryItem.discoverRequest();
+ DiscoveryRequestInfo discoveryInfo(RCSAddressDetail::getDetail(address)->getAddress(),
+ relativeUri, resourceType, std::move(discoverCb));
+ discoveryInfo.discover();
- std::lock_guard<std::mutex> lock(m_mutex);
- m_discoveryMap.insert(std::make_pair(discoveryId, std::move(discoveryItem)));
+ {
+ std::lock_guard < std::mutex > lock(m_mutex);
+ m_discoveryMap.insert(std::make_pair(discoveryId, std::move(discoveryInfo)));
+ }
- return std::unique_ptr<RCSDiscoveryManager::DiscoveryTask>(
+ return std::unique_ptr < RCSDiscoveryManager::DiscoveryTask> (
new RCSDiscoveryManager::DiscoveryTask(discoveryId));
}
void RCSDiscoveryManagerImpl::requestMulticastPresence()
{
- static constexpr char MULTICAST_PRESENCE_ADDRESS[] = "coap://" OC_MULTICAST_PREFIX;
+ constexpr char MULTICAST_PRESENCE_ADDRESS[] = "coap://" OC_MULTICAST_PREFIX;
OCDoHandle presenceHandle;
- subscribePresence(presenceHandle, MULTICAST_PRESENCE_ADDRESS, OCConnectivityType::CT_DEFAULT,
+ subscribePresence(presenceHandle, MULTICAST_PRESENCE_ADDRESS,
+ OCConnectivityType::CT_DEFAULT,
std::move(std::bind(&RCSDiscoveryManagerImpl::onPresence, this,
- std::placeholders::_1, std::placeholders::_2,std::placeholders::_3)));
+ std::placeholders::_1, std::placeholders::_2,
+ std::placeholders::_3)));
}
void RCSDiscoveryManagerImpl::onPolling()
{
- std::lock_guard<std::mutex> lock(m_mutex);
+ std::lock_guard < std::mutex > lock(m_mutex);
- for(const auto& it : m_discoveryMap)
+ for (const auto& it : m_discoveryMap)
{
- it.second.discoverRequest();
+ it.second.discover();
}
- m_timer.post(INTERVALTIME, std::bind(&RCSDiscoveryManagerImpl::onPolling, this));
+ m_timer.post(POLLING_INTERVAL_TIME, std::bind(&RCSDiscoveryManagerImpl::onPolling, this));
}
- void RCSDiscoveryManagerImpl::onPresence(OCStackResult ret,
- const unsigned int /*seq*/, const std::string& address)
+ void RCSDiscoveryManagerImpl::onPresence(OCStackResult ret, const unsigned int /*seq*/,
+ const std::string& address)
{
- if(ret != OC_STACK_OK && ret != OC_STACK_RESOURCE_CREATED) return;
+ if (ret != OC_STACK_OK && ret != OC_STACK_RESOURCE_CREATED) return;
- std::lock_guard<std::mutex> lock(m_mutex);
- for(const auto& it : m_discoveryMap)
+ std::lock_guard < std::mutex > lock(m_mutex);
+ for (const auto& it : m_discoveryMap)
{
- if(it.second.isMatchingAddress(address))
+ if (it.second.isMatchingAddress(address))
{
- it.second.discoverRequest();
+ it.second.discover();
}
}
}
RCSDiscoveryManagerImpl::ID RCSDiscoveryManagerImpl::createId()
{
- std::lock_guard<std::mutex> lock(m_mutex);
- static unsigned int s_uniqueId;
+ std::lock_guard < std::mutex > lock(m_mutex);
+ static ID s_uniqueId;
- if(m_discoveryMap.size() >= LIMITNUMBER)
- {
- throw RCSException { "Discovery request is full!" };
- }
s_uniqueId++;
- while(m_discoveryMap.find(s_uniqueId) != m_discoveryMap.end())
+ while (m_discoveryMap.find(s_uniqueId) != m_discoveryMap.end())
{
s_uniqueId++;
}
return s_uniqueId;
}
- void RCSDiscoveryManagerImpl::cancel(unsigned int id)
+ void RCSDiscoveryManagerImpl::cancel(ID id)
{
- std::lock_guard<std::mutex> lock(m_mutex);
+ std::lock_guard < std::mutex > lock(m_mutex);
m_discoveryMap.erase(id);
}
- bool RCSDiscoveryManagerImpl::isCanceled(unsigned int id)
+ bool RCSDiscoveryManagerImpl::isCanceled(ID id)
{
- std::lock_guard<std::mutex> lock(m_mutex);
+ std::lock_guard < std::mutex > lock(m_mutex);
auto it = m_discoveryMap.find(id);
- if(it == m_discoveryMap.end()) return true;
+ if (it == m_discoveryMap.end()) return true;
return false;
}
- DiscoveryRequestInfo::DiscoveryRequestInfo(const std::string &address, const std::string &relativeUri,
- const std::string &resourceType, DiscoverCallback cb) : m_address(address),
- m_relativeUri(relativeUri), m_resourceType(resourceType), m_discoverCB(cb) {}
+ DiscoveryRequestInfo::DiscoveryRequestInfo(const std::string &address,
+ const std::string &relativeUri, const std::string &resourceType,
+ DiscoverCallback cb) :
+ m_address(address), m_relativeUri(relativeUri), m_resourceType(resourceType),
+ m_discoverCb(std::move(cb))
+ {
+ }
- void DiscoveryRequestInfo::discoverRequest() const
+ void DiscoveryRequestInfo::discover() const
{
OIC::Service::discoverResource(m_address, m_relativeUri + "?rt=" + m_resourceType,
- OCConnectivityType::CT_DEFAULT, m_discoverCB);
+ OCConnectivityType::CT_DEFAULT, m_discoverCb);
}
- bool DiscoveryRequestInfo::isKnownResource(const std::shared_ptr<PrimitiveResource>& resource)
+ bool DiscoveryRequestInfo::isKnownResource(
+ const std::shared_ptr<PrimitiveResource>& resource)
{
std::string resourceId = resource->getSid() + resource->getUri();
auto it = std::find(m_receivedIds.begin(), m_receivedIds.end(), resourceId);
- if(it != m_receivedIds.end()) return true;
- m_receivedIds.push_back(resourceId);
+ if (it != m_receivedIds.end()) return true;
+ m_receivedIds.insert(resourceId);
return false;
}
#include <memory>
#include <functional>
-#include <list>
#include <mutex>
#include <unordered_map>
+#include <unordered_set>
#include "RCSDiscoveryManager.h"
#include "ExpiryTimer.h"
DiscoveryRequestInfo(const std::string &, const std::string &,
const std::string &, DiscoverCallback);
+ public:
+ void discover() const;
+ bool isKnownResource(const std::shared_ptr<PrimitiveResource>&);
+ bool isMatchingAddress(const std::string&) const;
+
private:
std::string m_address;
std::string m_relativeUri;
std::string m_resourceType;
- std::list<std::string> m_receivedIds;
- DiscoverCallback m_discoverCB;
- public:
- void discoverRequest() const;
- bool isKnownResource(const std::shared_ptr<PrimitiveResource>&);
- bool isMatchingAddress(const std::string&) const;
+ std::unordered_set<std::string> m_receivedIds;
+ DiscoverCallback m_discoverCb;
};
-
/**
* The class contains the resource discovery and management requests methods.
*/
class RCSDiscoveryManagerImpl
{
- static unsigned int s_uniqueId;
-
public:
- /*
- * Typedef for callback of requesting presence API
- *
- * @see requestMulticastPresence
- */
- typedef std::function<void(OCStackResult, const unsigned int,
- const std::string&)> PresenceCallback;
-
/*
* Typedef for discovery request ID
*
public:
- /*
- * @return Returns RCSDiscoveryManagerImpl instance.
- */
static RCSDiscoveryManagerImpl* getInstance();
/**
- * Starting discovery of resource
+ * Start discovery of resource
*
* @return DiscoverTask pointer
*
*
* @throws InvalidParameterException If cb is empty
*
- * @note If relativeURI is empty, will be discovered after be changed into "OC_RSRVD_WELL_KNOWN_URI"
+ * @note If relativeURI is empty, will be discovered after be changed into
+ * "OC_RSRVD_WELL_KNOWN_URI"
* @note If resourceType is empty, will be discovered all resources in network
*
* @see RCSAddress
* @see RCSDiscoveryManager
*/
- std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> startDiscovery(const RCSAddress& address,
+ RCSDiscoveryManager::DiscoveryTask::Ptr startDiscovery(const RCSAddress& address,
const std::string& relativeURI,const std::string& resourceType,
RCSDiscoveryManager::ResourceDiscoveredCallback cb);
~RCSDiscoveryManagerImpl() = default;
/**
- * Requesting presence by multicast
+ * Request presence by multicast
*/
void requestMulticastPresence();
/**
- * Checking duplicated callback and invoking callback when resource is discovered
+ * Check duplicated callback and invoke callback when resource is discovered
*
* @param resource A pointer of discovered resource
* @param discoverID The ID of discovery request
const RCSDiscoveryManager::ResourceDiscoveredCallback& cb);
/**
- * Discovering resource on all requests and posting timer when timer is expired
+ * Discover resource on all requests and posting timer when timer is expired
*/
void onPolling();
/**
- * Discovering resource on all requests when supporting presence function resource enter into network
- *
- * @param ret Not used in this class
- * @param seq Not used in this class
- * @param address A address of supporting presence function resource
+ * Discover resource on all requests when supporting presence function resource
+ * enter into network
*/
void onPresence(OCStackResult ret, const unsigned int seq, const std::string& address);
/**
- * Creating unique id
+ * Create unique id
*
* @return Returns the id
*/