/**
* @file
*
- * This file contains the resource client APIs provided to the developer
+ * This file contains the resource client APIs provided to the developers
*/
#ifndef RESOURCE_CLIENT_H_
#define RESOURCE_CLIENT_H_
-#include<iostream>
-#include<string>
#include<vector>
-#include<mutex>
-#include<functional>
-
#include "ResourceAttributes.h"
-using namespace OIC::Service;
-
-/*
-* Forward Declaration of Classes
-*/
-
namespace OIC
{
namespace Service
{
- class PrimitiveResource;
- }
-}
-class ReportPolicyProxy;
-class PrimitiveClientResource;
-
-/**
-* Cache State enum specify the state of the Cache.
-*/
-enum class CacheState
-{
- READY = 0,
- READY_YET,
- LOST_SIGNAL,
- DESTROYED,
- UPDATING,
- NONE
-};
-
-/**
-* Resource State enum specify the state of the resource.
-*/
-enum class ResourceState
-{
- NOT_WATCHING,
- ALIVE, REQUESTED,
- LOST_SIGNAL,
- DESTROYED
-};
-
-/**
-* Time unit enum to indicate time duration in report cache policy.
-*/
-enum class TimeUnit
-{
- MILLISECOND, SECOND, MIN
-};
-
-/**
- * @class InvalidParameterException
- * @brief This class extends from PrimitiveException class. It is used to
- * throw exception to the upper layer if parameter is invalid.
- *
- */
-class BadRequestException: public OIC::Service::PrimitiveException
-{
- public:
- BadRequestException(const std::string &what) : PrimitiveException { what } {}
- BadRequestException(std::string &&what) : PrimitiveException { std::move(what) } {}
-};
-
-/**
- * @class InvalidParameterException
- * @brief This class extends from PrimitiveException class. It is used to
- * throw exception to the upper layer if parameter is invalid.
- *
- */
-class InvalidParameterException: public OIC::Service::PrimitiveException
-{
- public:
- InvalidParameterException(const std::string &what) : PrimitiveException { what } {}
- InvalidParameterException(std::string &&what) : PrimitiveException { std::move(what) } {}
-};
-
-/**
- * @class ReportPolicy
- * @brief This class provides a set of method for various caching policies.
- * Each method corresponds to REPORT_FREQUENCY values.
- *
- * NOTE: REPORT_FREQUENCY is enum class having values as the following:
- * NONE(default), UPDATE and PERIODIC.
- *
- */
-class ReportPolicy
-{
- public:
-
- /**
- * Destructor for ReportPolicy
- */
- ~ReportPolicy(void) {}
-
- /**
- * API for setting the caching policy to default.
- *
- * @return ReportPolicy - reportPolicy object corresponding to default caching method.
- *
- */
- static ReportPolicy none();
-
- /**
- * API for setting the caching policy to updated.
- *
- * @return ReportPolicy - reportPolicy object corresponding to updated caching method.
- *
- */
- static ReportPolicy upToDate();
-
- /**
- * API for setting the caching policy to periodic with tive interval given.
- *
- * @param interval - Duration for the periodic caching of data.
- * @param unit - unit of the time interval.
- *
- * @return ReportPolicy - reportPolicy object corresponding to periodic caching method.
- *
- */
- static ReportPolicy periodic(int interval, TimeUnit unit);
-
- /**
- * API for getting the m_proxy data member for the called object.
- *
- * @return ReportPolicyProxy - ReportPolicyProxy data member.
- *
- */
- std::shared_ptr<ReportPolicyProxy> getProxy();
-
- private:
/**
- * constructor for ReportPolicy
- *
- * @param reportPolicyProxy - object binded to startCaching method.
- *
+ * Cache State enum specify the state of the Cache.
*/
- explicit ReportPolicy(ReportPolicyProxy &&reportPolicyProxy);
-
- /**
- * ReportPolicyProxy object to invoke startCaching method.
- */
- std::shared_ptr<ReportPolicyProxy> m_proxy;
-
-};
-
-/**
- * @class PrimitiveClientResource
- * @brief PrimitiveClientResource is an interaction point between Resource
- * and the developer.
- *
- */
-class PrimitiveClientResource
-{
- public:
-
- /**
- * Constructor for PrimitiveClientResource.
+ enum class CacheState
+ {
+ READY = 0,
+ READY_YET,
+ LOST_SIGNAL,
+ DESTROYED,
+ UPDATING,
+ NONE
+ };
+
+ /**
+ * Resource State enum specify the state of the resource.
*/
- PrimitiveClientResource(std::shared_ptr<OIC::Service::PrimitiveResource> pResource);
-
- /**
- * Typedef for callbacks
- */
- typedef std::function< void(ResourceState) > ResourceStateChangedCallback;
- typedef std::function< void(const OIC::Service::ResourceAttributes &) > CacheUpdatedCallback;
- typedef std::function< void(const OIC::Service::ResourceAttributes &) >
- RemoteAttributesReceivedCallback;
-
- /**
- * Typedef for Broker and Cache ID
- */
- typedef int CacheID;
- typedef unsigned int BrokerID;
-
- /**
- * API to get watching state.
- *
- * @return bool - watching or not.
- */
- bool isWatching() const;
-
- /**
- * API to get Caching state.
- *
- * @return bool - caching or not.
- */
- bool isCaching() const;
-
- /**
- * API to get observable state.
- *
- * @return bool - observing or not.
- */
- bool isObservable() const;
-
- /**
- * API to start watching the resource.
- *
- * @param ResourceStateChangedCallback - callback to get changed resource state.
- */
- void startWatching(ResourceStateChangedCallback);
-
- /**
- * API to stop watching the resource.
- */
- void stopWatching();
-
- /**
- * API to get resource state.
- *
- * @return ResourceState - current state of the resource.
- */
- ResourceState getState() const ;
-
- /**
- * API to start caching for the resource.
- *
- * @param ReportPolicy - caching policy.
- *
- * @param CacheUpdatedCallback - callback to get updated resourceAttributes.
- */
- void startCaching(ReportPolicy , CacheUpdatedCallback);
-
- /**
- * API to get the cache State of the resource
- *
- *@return CacheState - current state of the Cache.
- */
- CacheState getResourceCacheState();
-
- /**
- * API to stop caching for the resource.
- */
- void stopCaching();
-
- /**
- * API to refresh the cache explicitly.
- */
- void refreshCache() ;
-
- /**
- * API to obtain cached ResourceAttributes data.
- *
- *@return ResourceAttributes - cached ResourceAttributes data
- */
- ResourceAttributes getCachedAttributes() const;
-
- /**
- * API to obtain current resource attributes data.
- *
- * @param RemoteAttributesReceivedCallback - callback to get resourceAttributes data.
- */
- void getRemoteAttributes(RemoteAttributesReceivedCallback);
-
- /**
- * API to Set resource attributes data.
- *
- * @param ResourceAttributes - resourceAttributes data to set for the resource.
- */
- void setRemoteAttributes(ResourceAttributes &);
+ enum class ResourceState
+ {
+ NOT_WATCHING,
+ ALIVE, REQUESTED,
+ LOST_SIGNAL,
+ DESTROYED
+ };
+
+ /*
+ * Forward Declaration of Classes
+ */
+ class PrimitiveResource;
+ class RemoteResourceObject;
/**
- * API to get resource uri.
+ * @class BadRequestException
+ * @brief This class inherited from PrimitiveException class. It is used to
+ * throw exception to the upper layer if request is invalid.
*
- * @return string - uri of the Resource
*/
- std::string getUri() const;
+ class BadRequestException: public PrimitiveException
+ {
+ public:
+ BadRequestException(const std::string &what) : PrimitiveException { what } {}
+ BadRequestException(std::string &&what) : PrimitiveException { std::move(what) } {}
+ };
/**
- * API to get resource address.
+ * @class InvalidParameterException
+ * @brief This class inherited from PrimitiveException class. It is used to
+ * throw exception to the upper layer if parameter is invalid.
*
- * @return string - address of the Resource
*/
- std::string getAddress() const;
+ class InvalidParameterException: public PrimitiveException
+ {
+ public:
+ InvalidParameterException(const std::string &what) : PrimitiveException { what } {}
+ InvalidParameterException(std::string &&what) : PrimitiveException { std::move(what) } {}
+ };
/**
- * API to get resource types.
+ * @class RemoteResourceObject
+ * @brief RemoteResourceObject is an interaction point between Resource
+ * and the developers.
*
- * @return vector - resource types
*/
- std::vector< std::string > getTypes() const;
-
- /**
- * API to get resource interfaces.
+ class RemoteResourceObject
+ {
+ public:
+
+ /**
+ * Constructor for RemoteResourceObject
+ */
+ RemoteResourceObject(std::shared_ptr<PrimitiveResource> pResource);
+
+ /**
+ * Typedef for callback of startWatching API
+ */
+ typedef std::function< void(ResourceState) > ResourceStateChangedCallback;
+
+ /**
+ * Typedef for callback of startCaching API
+ */
+ typedef std::function< void(const ResourceAttributes &) > CacheUpdatedCallback;
+
+ /**
+ * Typedef for callback of getRemoteAttributes API
+ */
+ typedef std::function< void(const ResourceAttributes &) >
+ RemoteAttributesReceivedCallback;
+
+
+ /**
+ * Typedef for callback of setRemoteAttributes API
+ */
+ typedef std::function< void(const ResourceAttributes &) >
+ RemoteAttributesSetCallback;
+
+ /**
+ * API to get watching state.
+ *
+ * @return bool - watching or not.
+ */
+ bool isWatching() const;
+
+ /**
+ * API to get Caching state.
+ *
+ * @return bool - caching or not.
+ */
+ bool isCaching() const;
+
+ /**
+ * API to get observable state.
+ *
+ * @return bool - observable or not.
+ */
+ bool isObservable() const;
+
+ /**
+ * API to start watching the resource.
+ *
+ * @param ResourceStateChangedCallback - callback to get changed resource state.
+ *
+ * @throw BadRequestException
+ *
+ */
+ void startWatching(ResourceStateChangedCallback);
+
+ /**
+ * API to stop watching the resource.
+ *
+ * @throw BadRequestException
+ */
+ void stopWatching();
+
+ /**
+ * API to get resource state.
+ *
+ * @return ResourceState - current state of the resource.
+ */
+ ResourceState getState() const ;
+
+ /**
+ * API to start caching for the resource.
+ *
+ * @throw BadRequestException
+ */
+ void startCaching();
+
+ /**
+ * API to start caching data of the resource.
+ *
+ * @param CacheUpdatedCallback - callback to get updated resourceAttributes.
+ *
+ * @throw BadRequestException
+ *
+ */
+ void startCaching(CacheUpdatedCallback);
+
+ /**
+ * API to get the cache State of the resource
+ *
+ * @return CacheState - Current state of the Cache.
+ */
+ CacheState getResourceCacheState();
+
+ /**
+ * API to stop caching data of the resource.
+ *
+ * @throw BadRequestException
+ */
+ void stopCaching();
+
+ /**
+ * API to refresh the cache explicitly.
+ */
+ void refreshCache() ;
+
+ /**
+ * API to get cached ResourceAttributes data.
+ *
+ * @return ResourceAttributes - cached ResourceAttributes data
+ */
+ ResourceAttributes getCachedAttributes() const;
+
+ /**
+ * API to get particular cached ResourceAttribute value
+ *
+ * @return Value - ResourceAttributes::Value class object
+ */
+ ResourceAttributes::Value getCachedAttribute( const std::string &) ;
+
+ /**
+ * API to get current resource attributes data.
+ *
+ * @param RemoteAttributesReceivedCallback - callback to get resourceAttributes data.
+ *
+ * @throw InvalidParameterException
+ *
+ */
+ void getRemoteAttributes(RemoteAttributesReceivedCallback);
+
+ /**
+ * API to set resource attributes data.
+ *
+ * @param ResourceAttributes - resourceAttributes data to set for the resource.
+ * @param RemoteAttributesSetCallback - callback on setting resourceAttributes data.
+ *
+ * @throw InvalidParameterException
+ *
+ */
+ void setRemoteAttributes(ResourceAttributes &, RemoteAttributesSetCallback );
+
+ /**
+ * API to get resource uri.
+ *
+ * @return string - uri of the Resource
+ */
+ std::string getUri() const;
+
+ /**
+ * API to get resource address.
+ *
+ * @return string - address of the Resource
+ */
+ std::string getAddress() const;
+
+ /**
+ * API to get resource types.
+ *
+ * @return vector - resource types
+ */
+ std::vector< std::string > getTypes() const;
+
+ /**
+ * API to get resource interfaces.
+ *
+ * @return vector - resource interfaces
+ */
+ std::vector< std::string > getInterfaces() const;
+
+ private:
+
+ /**
+ * Typedef for Cache ID
+ */
+ typedef int CacheID;
+
+ /**
+ * Typedef for Broker ID
+ */
+ typedef unsigned int BrokerID;
+
+ /**
+ * Flag to check watching state.
+ */
+ bool m_watchingFlag;
+
+ /**
+ * Flag to check caching state.
+ */
+ bool m_cachingFlag;
+
+ /**
+ * Flag to check observing state.
+ */
+ bool m_observableFlag;
+
+ /**
+ * PrimitiveResource
+ */
+ std::shared_ptr<PrimitiveResource> m_primitiveResource;
+
+ /**
+ * caching identification number.
+ */
+ CacheID m_cacheId;
+
+ /**
+ * Broker identification number.
+ */
+ BrokerID m_brokerId;
+
+ };
+
+ /**
+ * @class DiscoveryManager
+ * @brief It contains the resource discovery method.
*
- * @return vector - resource interfaces
- */
- std::vector< std::string > getInterfaces() const;
-
- private:
-
- /**
- * Flag to check watching state.
- */
- bool m_watchingFlag;
-
- /**
- * Flag to check caching state.
- */
- bool m_cachingFlag;
-
- /**
- * Flag to check observing state.
- */
- bool m_observableFlag;
-
- /**
- * resource uri.
- */
- std::string m_uri;
-
- /**
- * resource address.
- */
- std::string m_address;
-
- /**
- * Resource type(s).
- */
- std::vector< std::string > m_types;
-
- /**
- * Resource interface(s).
*/
- std::vector< std::string > m_interfaces;
-
- /**
- * PrimitiveResource.
- */
- std::shared_ptr<OIC::Service::PrimitiveResource> m_primitiveResource;
-
- /**
- * caching identification number.
- */
- CacheID m_cacheId;
-
- /**
- * Broker identification number.
- */
- BrokerID m_brokerId;
-
-};
-
-/**
- * @class PrimitiveClient
- * @brief It contains the resource discovery method.
- *
- */
-class PrimitiveClient
-{
- public:
-
- /**
- * Typedef for callback
- */
- typedef std::function< void( std::shared_ptr< PrimitiveClientResource>) >
- OnResourceDiscoveredCallback;
-
- /**
- * API to get Primitive Client instance.
- *
- * @return PrimitiveClient -instance.
- */
- static PrimitiveClient *getInstance();
-
- /**
- * API for discovey of resource of Interrest.
- *
- * @param host - host to search for
- * @param resourceURI - uri of resource to be searched
- * @param connectivityType - connection type
- * @param cb - callback to obtain discovered resource.
- *
- */
- void discoverPrimitiveResource(std::string host, std::string resourceURI,
- OCConnectivityType connectivityType,
- OnResourceDiscoveredCallback cb);
-
- private:
-
- /**
- * Constructor for PrimitiveClient.
- */
- PrimitiveClient() = default;
-
- /**
- * Destructor for PrimitiveClient.
- */
- ~PrimitiveClient();
-
-};
+ class DiscoveryManager
+ {
+ public:
+
+ /**
+ * Typedef for callback of discoverResource API
+ */
+ typedef std::function< void( std::shared_ptr< RemoteResourceObject>) >
+ OnResourceDiscoveredCallback;
+
+ /**
+ * API to get DiscoveryManager instance.
+ *
+ * @return DiscoveryManager -instance.
+ */
+ static DiscoveryManager *getInstance();
+
+ /**
+ * API for discovey of resource of Interest.
+ *
+ * @param host - host of the Resource
+ * @param resourceURI - uri of resource to be searched
+ * @param connectivityType - connection type
+ * @param cb - callback to obtain discovered resource.
+ *
+ * @throw InvalidParameterException
+ *
+ */
+ void discoverResource(std::string host, std::string resourceURI,
+ OCConnectivityType connectivityType,
+ OnResourceDiscoveredCallback cb);
+
+ private:
+
+ /**
+ * Constructor for DiscoveryManager.
+ */
+ DiscoveryManager() = default;
+
+ /**
+ * Destructor for DiscoveryManager.
+ */
+ ~DiscoveryManager() = default;
+
+ };
+ }
+}
#endif //RESOURCE_CLIENT_H_
#include "ResourceClient.h"
#include "ResourceBroker.h"
#include "ResourceCacheManager.h"
-#include "ReportPolicyProxy.h"
#define CLIENT_W_TAG PCF("ResourceClient")
return CacheState::NONE;
}
- OCStackResult hosting_cb(BROKER_STATE state,
- PrimitiveClientResource::ResourceStateChangedCallback onResourceStateChanged)
+ OCStackResult hostingCallback(BROKER_STATE state,
+ RemoteResourceObject::ResourceStateChangedCallback onResourceStateChanged)
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "hosting_cb entry");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "hostingCallback entry");
- ResourceState rState = getResourceStateFromBrokerState(state);
+ ResourceState resourceState = getResourceStateFromBrokerState(state);
+ onResourceStateChanged(resourceState); //passing ResourceState to application
- onResourceStateChanged(rState); //passing ResourceState to application
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "hosting_cb exit");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "hostingCallback exit");
return OC_STACK_OK;
}
- OCStackResult caching_cb(std::shared_ptr<PrimitiveResource> resource,
- const ResourceAttributes &data,
- PrimitiveClientResource::CacheUpdatedCallback onCacheUpdated)
+ OCStackResult cachingCallback(std::shared_ptr<PrimitiveResource> resource,
+ const ResourceAttributes &data,
+ RemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "caching_cb entry");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "cachingCallback entry");
- onCacheUpdated(data); //passing ResourceAttribute to application
+ onCacheUpdated(data); //passing ResourceAttributes to application
- OC_LOG(DEBUG, CLIENT_W_TAG, "caching_cb exit");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "cachingCallback exit");
return OC_STACK_OK;
}
- void set_cb(const HeaderOptions &header, const ResponseStatement &response, int n)
+ void setCallback(const HeaderOptions &header, const ResponseStatement &response, int n,
+ RemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "set_cb");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "setCallback entry");
+
+ const ResourceAttributes &attributes = response.getAttributes();
+ onRemoteAttributesSet(attributes); //passing ResourceAttributes to application
+ OC_LOG(DEBUG, CLIENT_W_TAG, "setCallback exit");
}
- void get_cb(const HeaderOptions &headerOption, const ResponseStatement &response, int n,
- PrimitiveClientResource::RemoteAttributesReceivedCallback onRemoteAttributesReceived)
+ void getCallback(const HeaderOptions &headerOption, const ResponseStatement &response, int n,
+ RemoteResourceObject::RemoteAttributesReceivedCallback onRemoteAttributesReceived)
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "get_cb entry");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "getCallback entry");
const ResourceAttributes &attributes = response.getAttributes();
- onRemoteAttributesReceived(attributes); //passing ResourceAttribute to application
+ onRemoteAttributesReceived(attributes); //passing ResourceAttributes to application
- OC_LOG(DEBUG, CLIENT_W_TAG, "get_cb exit");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "getCallback exit");
}
- void find_cb(std::shared_ptr<PrimitiveResource> primitiveResource,
- PrimitiveClient::OnResourceDiscoveredCallback OnResourceDiscovered )
+ void findCallback(std::shared_ptr<PrimitiveResource> primitiveResource,
+ DiscoveryManager::OnResourceDiscoveredCallback OnResourceDiscovered )
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "findcb entry");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "findCallback entry");
if (nullptr == primitiveResource)
{
- OC_LOG(ERROR, CLIENT_W_TAG, "find_cb::primitiveResource NULL Parameter");
+ OC_LOG(ERROR, CLIENT_W_TAG, "findCallback::primitiveResource NULL Parameter");
return ;
}
- std::shared_ptr< PrimitiveClientResource> primitiveClientResource =
- std::shared_ptr< PrimitiveClientResource>(new PrimitiveClientResource(primitiveResource));
+ std::shared_ptr< RemoteResourceObject> primitiveClientResource =
+ std::shared_ptr< RemoteResourceObject>(new RemoteResourceObject(primitiveResource));
OnResourceDiscovered(primitiveClientResource); //passing PrimitiveClientResource to application
//*******************************Primitive Client Resource*************************************
-PrimitiveClientResource:: PrimitiveClientResource(std::shared_ptr<PrimitiveResource> pResource) :
- m_primitiveResource(pResource), m_uri(pResource->getUri()),
- m_address(pResource->getHost()), m_types(pResource->getTypes()),
- m_interfaces(pResource->getInterfaces()), m_observableFlag(pResource->isObservable()) {}
-
-
-bool PrimitiveClientResource::isWatching() const
+namespace OIC
{
- return m_watchingFlag;
-}
+ namespace Service
+ {
-bool PrimitiveClientResource::isCaching() const
-{
- return m_cachingFlag;
-}
+ RemoteResourceObject:: RemoteResourceObject(std::shared_ptr<PrimitiveResource> pResource) :
+ m_watchingFlag(false), m_cachingFlag(false), m_observableFlag(pResource->isObservable()),
+ m_primitiveResource(pResource), m_cacheId(0), m_brokerId(0) {}
-void PrimitiveClientResource::startWatching(ResourceStateChangedCallback cb)
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startWatching entry");
- if (true == m_watchingFlag)
- {
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startWatching : Already started");
- }
- else
- {
- BrokerID brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
- std::bind(hosting_cb, std::placeholders::_1,
- cb));
- if (0 == brokerId)
+ bool RemoteResourceObject::isWatching() const
{
- m_watchingFlag = false;
- throw BadRequestException { "Failed to start watching resource "};
+ return m_watchingFlag;
}
- else
+
+ bool RemoteResourceObject::isCaching() const
{
- m_watchingFlag = true;
- m_brokerId = brokerId;
+ return m_cachingFlag;
}
- }
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startWatching exit");
-}
-void PrimitiveClientResource::stopWatching()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::stopWatching entry");
- if (true == m_watchingFlag)
- {
- BrokerID brokerId = ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
- if (0 == brokerId)
+ void RemoteResourceObject::startWatching(ResourceStateChangedCallback cb)
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: Failed to terminate hosting");
- throw BadRequestException { "Failed to terminate hosting " };
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startWatching entry");
+
+ if (true == m_watchingFlag)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startWatching : Already started");
+ throw BadRequestException { "Already started watching resource "};
+ }
+ else
+ {
+ BrokerID brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
+ std::bind(hostingCallback, std::placeholders::_1,
+ cb));
+ if (0 == brokerId)
+ {
+ m_watchingFlag = false;
+ throw BadRequestException { "Failed to start watching resource "};
+ }
+ else
+ {
+ m_watchingFlag = true;
+ m_brokerId = brokerId;
+ }
+ }
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startWatching exit");
}
- else
+
+ void RemoteResourceObject::stopWatching()
{
- m_watchingFlag = false;
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching entry");
+ if (true == m_watchingFlag)
+ {
+ BrokerID brokerId = ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
+ if (0 == brokerId)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: Failed to terminate hosting");
+ throw BadRequestException { "Failed to terminate hosting " };
+ }
+ else
+ {
+ m_watchingFlag = false;
+ }
+ }
+ else
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: stopWatching : already terminated");
+ throw BadRequestException { "Not watching" };
+ }
+
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching exit");
}
- }
- else
- {
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: stopWatching : already terminated");
- }
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::stopWatching exit");
-}
-
-ResourceState PrimitiveClientResource::getState() const
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, " PrimitiveClientResource::getState entry");
- BROKER_STATE brokerState = ResourceBroker::getInstance()->getResourceState(m_primitiveResource);
- return getResourceStateFromBrokerState(brokerState);
-}
+ ResourceState RemoteResourceObject::getState() const
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState entry");
+ BROKER_STATE brokerState = ResourceBroker::getInstance()->getResourceState(m_primitiveResource);
+ OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState exit");
-void PrimitiveClientResource::startCaching(ReportPolicy reportPolicy, CacheUpdatedCallback cb)
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching entry");
- if (true == m_cachingFlag)
- {
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching : already Started");
- }
- else
- {
- CacheID cacheId = reportPolicy.getProxy()->startProxyCaching(m_primitiveResource,
- std::bind(caching_cb, std::placeholders::_1, std::placeholders::_2, cb));
+ return getResourceStateFromBrokerState(brokerState);
+ }
- OC_LOG_V(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching CACHE ID %d", cacheId);
- if (0 == cacheId)
+ void RemoteResourceObject::startCaching()
{
- m_cachingFlag = false;
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching FAILED");
- throw BadRequestException { "Failed to generate Cache ID" };
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching entry");
+ if (true == m_cachingFlag)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching : already Started");
+ throw BadRequestException { "Already Started caching" };
+ }
+ else
+ {
+ CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource,
+ NULL, REPORT_FREQUENCY::NONE, 0);
+
+ OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId);
+ if (0 == cacheId)
+ {
+ m_cachingFlag = false;
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching FAILED");
+ throw BadRequestException { "Failed to Start Cache" };
+ }
+ else
+ {
+ m_cacheId = cacheId;
+ m_cachingFlag = true;
+ }
+ }
+
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching exit");
}
- else
+
+ void RemoteResourceObject::startCaching(CacheUpdatedCallback cb)
{
- m_cacheId = cacheId;
- m_cachingFlag = true;
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching entry");
+
+ if (true == m_cachingFlag)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching : already Started");
+ throw BadRequestException { "Already Started caching" };
+ }
+ else
+ {
+ CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource,
+ std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, cb),
+ REPORT_FREQUENCY::UPTODATE, 0);
+
+ OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId);
+ if (0 == cacheId)
+ {
+ m_cachingFlag = false;
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching FAILED");
+ throw BadRequestException { "Failed to Start Caching" };
+ }
+ else
+ {
+ m_cacheId = cacheId;
+ m_cachingFlag = true;
+ }
+ }
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching exit");
}
- }
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching exit");
-}
-
-void PrimitiveClientResource::stopCaching()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::stopCaching entry");
- OCStackResult result = OC_STACK_ERROR;
- if (true == m_cachingFlag)
- {
- result = ResourceCacheManager::getInstance()->cancelResourceCache(m_primitiveResource,
- m_cacheId);
- if (result == OC_STACK_OK)
+ void RemoteResourceObject::stopCaching()
{
- m_cachingFlag = false;
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: SUCCESS");
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopCaching entry");
+ CacheID cacheId;
+
+ if (true == m_cachingFlag)
+ {
+ cacheId = ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
+ if (0 != cacheId)
+ {
+ m_cachingFlag = false;
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: SUCCESS");
+ }
+ else
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: Failed to terminate Caching");
+ throw BadRequestException { "Failed to terminate Caching " };
+ }
+ }
+ else
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: Caching already terminated");
+ throw BadRequestException { "No Caching started " };
+ }
+
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopCaching exit");
}
- else
+
+ CacheState RemoteResourceObject::getResourceCacheState()
{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: Failed to terminate Caching");
- throw BadRequestException { "Failed to terminate Caching " };
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getResourceCacheState entry");
+ CACHE_STATE cacheState = ResourceCacheManager::getInstance()->getResourceCacheState(
+ m_primitiveResource);
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getResourceCacheState exit");
+ return getCacheState(cacheState);
}
- }
- else
- {
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: Caching already terminated");
- }
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::stopCaching exit");
-}
-
-CacheState PrimitiveClientResource::getResourceCacheState()
-{
- CACHE_STATE cacheState = ResourceCacheManager::getInstance()->getResourceCacheState(
- m_primitiveResource);
- return getCacheState(cacheState);
-}
-
-void PrimitiveClientResource::refreshCache()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::refreshCache entry");
-
- OCStackResult result = ResourceCacheManager::getInstance()->updateResourceCache(
- m_primitiveResource);
- if (result == OC_STACK_OK)
- {
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::refreshCache Success");
- }
- else
- {
- OC_LOG_V(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::refreshCache FAILED %d", result);
- throw BadRequestException { "Failed to refresh Caching " };
- }
-}
-
-ResourceAttributes PrimitiveClientResource:: getCachedAttributes() const
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "ResourceAttributes getCachedAttributes ");
- return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
-}
-
-std::string PrimitiveClientResource::getUri() const
-{
- return m_uri;
-}
-
-std::string PrimitiveClientResource::getAddress() const
-{
- return m_address;
-}
-
-bool PrimitiveClientResource::isObservable() const
-{
- return m_observableFlag;
-}
-std::vector < std::string > PrimitiveClientResource::getTypes() const
-{
- return m_types;
-}
-
-std::vector < std::string > PrimitiveClientResource::getInterfaces() const
-{
- return m_interfaces;
-}
-
-void PrimitiveClientResource::getRemoteAttributes(RemoteAttributesReceivedCallback cb)
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::getRemoteAttributes entry");
-
- m_primitiveResource->requestGet(std::bind(get_cb, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3, cb));
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::getRemoteAttributes exit");
-}
-
-void PrimitiveClientResource::setRemoteAttributes(ResourceAttributes &attribute)
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::setRemoteAttributes entry");
-
- m_primitiveResource->requestSet(attribute, std::bind(set_cb, std::placeholders::_1,
- std::placeholders::_2, std::placeholders::_3));
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::setRemoteAttributes exit");
-}
-
-//*******************************Report Policy**********************************************
-
-ReportPolicy::ReportPolicy(ReportPolicyProxy &&reportPolicyProxy)
-{
- m_proxy = std::shared_ptr< ReportPolicyProxy>(new ReportPolicyProxy(std::forward<ReportPolicyProxy>
- (reportPolicyProxy)));
-}
-
-ReportPolicy ReportPolicy::none()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::none entry");
-
- ReportPolicyProxy::ProxyFunc func = std::bind(&ResourceCacheManager::requestResourceCache,
- ResourceCacheManager::getInstance(), std::placeholders::_1, std::placeholders::_2,
- REPORT_FREQUENCY::NONE, 0l);
- ReportPolicy reportPolicy = ReportPolicy(ReportPolicyProxy(func));
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::none exit");
- return reportPolicy;
-}
-
-ReportPolicy ReportPolicy::upToDate()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::upToDate entry");
+ void RemoteResourceObject::refreshCache()
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache entry");
+
+ OCStackResult result = ResourceCacheManager::getInstance()->updateResourceCache(
+ m_primitiveResource);
+ if (result == OC_STACK_OK)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache Success");
+ }
+ else
+ {
+ OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache FAILED %d", result);
+ throw BadRequestException { "Failed to refresh Caching " };
+ }
+ }
- ReportPolicyProxy::ProxyFunc func = std::bind(&ResourceCacheManager::requestResourceCache,
- ResourceCacheManager::getInstance(), std::placeholders::_1, std::placeholders::_2,
- REPORT_FREQUENCY::UPTODATE, 0l);
- ReportPolicy reportPolicy = ReportPolicy((ReportPolicyProxy(func)));
+ ResourceAttributes RemoteResourceObject:: getCachedAttributes() const
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttributes ");
+ return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
+ }
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::upToDate exit");
- return reportPolicy;
-}
+ ResourceAttributes::Value RemoteResourceObject:: getCachedAttribute( const std::string &key)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttribute entry");
+ ResourceAttributes Cachedattributes = ResourceCacheManager::getInstance()->getCachedData(
+ m_primitiveResource);
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttribute exit");
+ return Cachedattributes[key];
+ }
-ReportPolicy ReportPolicy::periodic(int interval, TimeUnit unit)
-{
+ std::string RemoteResourceObject::getUri() const
+ {
+ return m_primitiveResource->getUri();
+ }
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::periodic entry");
- if (0 > interval)
- {
- OC_LOG(ERROR, CLIENT_W_TAG, "ReportPolicy::periodic Invalid interval ");
- throw InvalidParameterException { "Invalid interval value " };
- }
- long long timeInMillis;
- if (unit == TimeUnit::MILLISECOND)
- {
- timeInMillis = interval;
- }
- else if (unit == TimeUnit::SECOND)
- {
- timeInMillis = interval * 60;
- }
- else if (unit == TimeUnit::MIN)
- {
- timeInMillis = interval * 60 * 60;
- }
- ReportPolicyProxy::ProxyFunc func = std::bind(&ResourceCacheManager::requestResourceCache,
- ResourceCacheManager::getInstance(), std::placeholders::_1, std::placeholders::_2,
- REPORT_FREQUENCY::PERIODICTY, timeInMillis);
- ReportPolicy reportPolicy = ReportPolicy((ReportPolicyProxy(func)));
+ std::string RemoteResourceObject::getAddress() const
+ {
+ return m_primitiveResource->getHost();
+ }
- OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::periodic exit");
- return reportPolicy;
-}
+ std::vector < std::string > RemoteResourceObject::getTypes() const
+ {
+ return m_primitiveResource->getTypes();
+ }
-std::shared_ptr<ReportPolicyProxy> ReportPolicy::getProxy()
-{
- return m_proxy;
-}
+ std::vector < std::string > RemoteResourceObject::getInterfaces() const
+ {
+ return m_primitiveResource->getInterfaces();
+ }
-//*******************************primitive client*********************************************
+ void RemoteResourceObject::getRemoteAttributes(RemoteAttributesReceivedCallback cb)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getRemoteAttributes entry");
+ if (!cb)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getRemoteAttributes : InvalidParameter");
+ throw InvalidParameterException { "Callback is NULL "};
+ }
+ else
+ {
+ m_primitiveResource->requestGet(std::bind(getCallback, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3, cb));
+ }
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getRemoteAttributes exit");
+ }
-PrimitiveClient *PrimitiveClient:: getInstance()
-{
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient:: getInstance entry");
+ void RemoteResourceObject::setRemoteAttributes(ResourceAttributes &attribute,
+ RemoteAttributesSetCallback cb)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::setRemoteAttributes entry");
+ if (!cb)
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::setRemoteAttributes : InvalidParameter");
+ throw InvalidParameterException { "Callback is NULL "};
+ }
+ else
+ {
+ m_primitiveResource->requestSet(attribute, std::bind(setCallback, std::placeholders::_1,
+ std::placeholders::_2, std::placeholders::_3, cb));
+ }
+ OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::setRemoteAttributes exit");
+ }
- static PrimitiveClient *s_instance;
- static std::mutex s_mutex;
- if (!s_instance)
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- if (!s_instance)
+ bool RemoteResourceObject::isObservable() const
{
- s_instance = new PrimitiveClient();
+ return m_observableFlag;
}
- }
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient:: getInstance exit");
- return s_instance;
-}
+//*******************************Discovery Manager****************************************
-void PrimitiveClient::discoverPrimitiveResource(std::string host, std::string resourceURI,
- OCConnectivityType connectivityType,
- OnResourceDiscoveredCallback cb)
-{
+ DiscoveryManager *DiscoveryManager:: getInstance()
+ {
+ OC_LOG(DEBUG, CLIENT_W_TAG, "DiscoveryManager:: getInstance entry");
+ static DiscoveryManager *s_instance;
+ static std::mutex s_mutex;
+ if (!s_instance)
+ {
+ std::lock_guard<std::mutex> lock(s_mutex);
+ if (!s_instance)
+ {
+ s_instance = new DiscoveryManager();
+ }
+ }
+ OC_LOG(DEBUG, CLIENT_W_TAG, "DiscoveryManager:: getInstance exit");
+ return s_instance;
+ }
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient::discoverResource entry");
+ void DiscoveryManager::discoverResource(std::string host, std::string resourceURI,
+ OCConnectivityType connectivityType,
+ OnResourceDiscoveredCallback cb)
+ {
- if ( resourceURI.empty() )
- {
- OC_LOG(ERROR, CLIENT_W_TAG, "discoverPrimitiveResource NULL resourceURI");
- throw InvalidParameterException { "discoverPrimitiveResource NULL resourceURI'" };
- }
- else if ( !cb )
- {
- OC_LOG(ERROR, CLIENT_W_TAG, "discoverPrimitiveResource NULL Callback");
- throw InvalidParameterException { "discoverPrimitiveResource NULL Callback'" };
+ OC_LOG(DEBUG, CLIENT_W_TAG, "DiscoveryManager::discoverResource entry");
+
+ if ( resourceURI.empty() )
+ {
+ OC_LOG(ERROR, CLIENT_W_TAG, "discoverResource NULL resourceURI");
+ throw InvalidParameterException { "discoverResource NULL resourceURI'" };
+ }
+ else if ( !cb )
+ {
+ OC_LOG(ERROR, CLIENT_W_TAG, "discoverResource NULL Callback");
+ throw InvalidParameterException { "discoverResource NULL Callback'" };
+ }
+ OIC::Service::discoverResource(host, resourceURI, connectivityType, std::bind(findCallback,
+ std::placeholders::_1,
+ cb));
+
+ OC_LOG(DEBUG, CLIENT_W_TAG, "DiscoveryManager::discoverResource exit");
+ }
}
- discoverResource(host, resourceURI, connectivityType, std::bind(find_cb, std::placeholders::_1,
- cb));
-
- OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient::discoverResource exit");
}