From 39c6ed5ebfde46e6e84bbd45cc9bc93ecdd5299b Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Sat, 27 Jun 2015 15:31:18 +0530 Subject: [PATCH] Updated Resource Client 1. Updated as per review comments. 2. Updated as per new folder structure. Change-Id: I8fa634cd4d6923fd424429b4d9304cb2449f4d86 Signed-off-by: Jay Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/1357 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka Reviewed-by: Uze Choi --- service/resource-manipulation/SConscript | 55 +++ .../include/ReportPolicyProxy.h | 86 ++++ .../resource-manipulation/include/ResourceClient.h | 428 ++++++++++++++++++ .../primitiveResource/include/PrimitiveResource.h | 1 + .../src/ReportPolicyProxy.cpp | 26 ++ .../resource-manipulation/src/ResourceClient.cpp | 477 +++++++++++++++++++++ 6 files changed, 1073 insertions(+) create mode 100644 service/resource-manipulation/include/ReportPolicyProxy.h create mode 100644 service/resource-manipulation/include/ResourceClient.h create mode 100644 service/resource-manipulation/src/ReportPolicyProxy.cpp create mode 100644 service/resource-manipulation/src/ResourceClient.cpp diff --git a/service/resource-manipulation/SConscript b/service/resource-manipulation/SConscript index 81085eb..0adb9a1 100644 --- a/service/resource-manipulation/SConscript +++ b/service/resource-manipulation/SConscript @@ -32,4 +32,59 @@ SConscript('modules/serverBuilder/SConscript') SConscript('modules/resourceContainer/SConscript') #SConscript('sdk/SConscript') +###################################################################### +#building Resource client +###################################################################### + +if env.get('RELEASE'): + env.AppendUnique(CCFLAGS = ['-Os']) + env.AppendUnique(CPPDEFINES = ['NDEBUG']) +else: + env.AppendUnique(CCFLAGS = ['-g']) + +if env.get('LOGGING'): + env.AppendUnique(CPPDEFINES = ['TB_LOG']) + +# Add third party libraries +lib_env = env.Clone() +SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') + +resourceClient_env = lib_env.Clone() +target_os = env.get('TARGET_OS') +###################################################################### +# Build flags +###################################################################### +resourceClient_env.AppendUnique(CPPPATH = ['include']) +resourceClient_env.AppendUnique(CPPPATH = ['modules/common/primitiveResource/include']) +resourceClient_env.AppendUnique(CPPPATH = ['modules/resourceBroker/include']) +resourceClient_env.AppendUnique(CPPPATH = ['modules/resourceCache/include']) + + +resourceClient_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')]) + +resourceClient_env.PrependUnique(LIBS = ['oc', 'ResourceBroker' , 'ResourceCache', 'service_common', 'octbstack', 'gnustl_shared','oc_logger', 'compatibility', 'log']) + + +if target_os not in ['windows', 'winrt']: + resourceClient_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) +if target_os == 'linux': + resourceClient_env.AppendUnique(LIBS = ['pthread']) + +###################################################################### +# Source files and Targets +###################################################################### +resourceClient_src = 'src/' +client_src = [ + resourceClient_src+ 'ReportPolicyProxy.cpp', + resourceClient_src+ 'ResourceClient.cpp'] + +ResourceClientsdk = resourceClient_env.StaticLibrary('ResourceClient', client_src) + +resourceClient_env.InstallTarget(ResourceClientsdk , 'libResourceClient') + +#Go to build sample apps +SConscript('examples/SConscript') + + + diff --git a/service/resource-manipulation/include/ReportPolicyProxy.h b/service/resource-manipulation/include/ReportPolicyProxy.h new file mode 100644 index 0000000..9d87628 --- /dev/null +++ b/service/resource-manipulation/include/ReportPolicyProxy.h @@ -0,0 +1,86 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * @file + * + * This file contains caching API, it binds the caching call with report policy object. + */ + +#ifndef REPORT_POLICY_PROXY_H_ +#define REPORT_POLICY_PROXY_H_ + +#include +#include +#include + +#include "CacheTypes.h" +#include "PrimitiveResource.h" + +/** + * @class ReportPolicyProxy + * @brief This class provides method for initiating caching with given caching method. + * Caching methods are defined in REPORT_FREQUENCY. + * + * NOTE: REPORT_FREQUENCY is enum class having values as the following: + * NONE(default), UPDATE and PERIODIC. +*/ +class ReportPolicyProxy +{ + public: + + /** + * Typedef to bind ProxyFunc to CacheCB method of PrimitiveResource. + */ + typedef std::function , CacheCB)> ProxyFunc; + + ReportPolicyProxy(ReportPolicyProxy &&reportPolicyProxy) = default; + + /** + * Constructor for ReportPolicyProxy + * + * @param func - Caching method so as to bind ReportPolicyProxy object to + * corresponding method of the PrimitiveResource class. + * + */ + + ReportPolicyProxy(ProxyFunc func) : m_proxyFunc(func) + { + } + + /** + * API for initiating caching on given resource and given caching method. + * Caching methods are defined in REPORT_FREQUENCY. + * + * @param res - resource to start caching for + * @param cb - callback to obtain caching data + * + * @return CacheID + */ + CacheID startProxyCaching(std::shared_ptr &res, CacheCB cb); + + private: + + /** + * proxy_binded binded to CacheCB method of PrimitiveResource. + */ + ProxyFunc m_proxyFunc; +}; +#endif //REPORT_POLICY_PROXY_H_ \ No newline at end of file diff --git a/service/resource-manipulation/include/ResourceClient.h b/service/resource-manipulation/include/ResourceClient.h new file mode 100644 index 0000000..63d3723 --- /dev/null +++ b/service/resource-manipulation/include/ResourceClient.h @@ -0,0 +1,428 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * @file + * + * This file contains the resource client APIs provided to the developer + */ + +#ifndef RESOURCE_CLIENT_H_ +#define RESOURCE_CLIENT_H_ + +#include +#include +#include +#include +#include + +#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 getProxy(); + + private: + + /** + * constructor for ReportPolicy + * + * @param reportPolicyProxy - object binded to startCaching method. + * + */ + explicit ReportPolicy(ReportPolicyProxy &&reportPolicyProxy); + + /** + * ReportPolicyProxy object to invoke startCaching method. + */ + std::shared_ptr m_proxy; + +}; + +/** + * @class PrimitiveClientResource + * @brief PrimitiveClientResource is an interaction point between Resource + * and the developer. + * + */ +class PrimitiveClientResource +{ + public: + + /** + * Constructor for PrimitiveClientResource. + */ + PrimitiveClientResource(std::shared_ptr 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 &); + + /** + * 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: + + /** + * 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 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(); + +}; +#endif //RESOURCE_CLIENT_H_ diff --git a/service/resource-manipulation/modules/common/primitiveResource/include/PrimitiveResource.h b/service/resource-manipulation/modules/common/primitiveResource/include/PrimitiveResource.h index 9f91b2c..9f2b460 100755 --- a/service/resource-manipulation/modules/common/primitiveResource/include/PrimitiveResource.h +++ b/service/resource-manipulation/modules/common/primitiveResource/include/PrimitiveResource.h @@ -26,6 +26,7 @@ #include #include +#include namespace OIC { diff --git a/service/resource-manipulation/src/ReportPolicyProxy.cpp b/service/resource-manipulation/src/ReportPolicyProxy.cpp new file mode 100644 index 0000000..2b46d66 --- /dev/null +++ b/service/resource-manipulation/src/ReportPolicyProxy.cpp @@ -0,0 +1,26 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "ReportPolicyProxy.h" + +CacheID ReportPolicyProxy::startProxyCaching(std::shared_ptr &res, CacheCB cb) +{ + return m_proxyFunc(res, cb); +} \ No newline at end of file diff --git a/service/resource-manipulation/src/ResourceClient.cpp b/service/resource-manipulation/src/ResourceClient.cpp new file mode 100644 index 0000000..025c95e --- /dev/null +++ b/service/resource-manipulation/src/ResourceClient.cpp @@ -0,0 +1,477 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "ResourceClient.h" +#include "ResourceBroker.h" +#include "ResourceCacheManager.h" +#include "ReportPolicyProxy.h" + +#define CLIENT_W_TAG PCF("ResourceClient") + +namespace +{ + ResourceState getResourceStateFromBrokerState(BROKER_STATE state) + { + + OC_LOG(DEBUG, CLIENT_W_TAG, "getResourceStateFromBrokerState entry"); + + if (state == BROKER_STATE::ALIVE) + { + return ResourceState::ALIVE; + } + else if (state == BROKER_STATE::REQUESTED) + { + return ResourceState::REQUESTED; + } + else if (state == BROKER_STATE::LOST_SIGNAL) + { + return ResourceState::LOST_SIGNAL; + } + else if (state == BROKER_STATE::DESTROYED) + { + return ResourceState::DESTROYED; + } + + OC_LOG(ERROR, CLIENT_W_TAG, "getResourceStateFromBrokerState ERROR"); + + //Default return value + return ResourceState::DESTROYED; + } + + CacheState getCacheState(CACHE_STATE state) + { + + OC_LOG(DEBUG, CLIENT_W_TAG, "getCacheState (from CACHE_STATE) entry"); + + if (state == CACHE_STATE::READY) + { + return CacheState::READY; + } + else if (state == CACHE_STATE::READY_YET) + { + return CacheState::READY_YET; + } + else if (state == CACHE_STATE::LOST_SIGNAL) + { + return CacheState::LOST_SIGNAL; + } + else if (state == CACHE_STATE::DESTROYED) + { + return CacheState::DESTROYED; + } + else if (state == CACHE_STATE::UPDATING) + { + return CacheState::UPDATING; + } + else if (state == CACHE_STATE::NONE) + { + return CacheState::NONE; + } + OC_LOG(ERROR, CLIENT_W_TAG, "getCacheState (from CACHE_STATE) ERROR"); + + //Default return value + return CacheState::NONE; + } + + OCStackResult hosting_cb(BROKER_STATE state, + PrimitiveClientResource::ResourceStateChangedCallback onResourceStateChanged) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "hosting_cb entry"); + + ResourceState rState = getResourceStateFromBrokerState(state); + + onResourceStateChanged(rState); //passing ResourceState to application + + OC_LOG(DEBUG, CLIENT_W_TAG, "hosting_cb exit"); + return OC_STACK_OK; + } + + OCStackResult caching_cb(std::shared_ptr resource, + const ResourceAttributes &data, + PrimitiveClientResource::CacheUpdatedCallback onCacheUpdated) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "caching_cb entry"); + + onCacheUpdated(data); //passing ResourceAttribute to application + + OC_LOG(DEBUG, CLIENT_W_TAG, "caching_cb exit"); + return OC_STACK_OK; + } + + void set_cb(const HeaderOptions &header, const ResponseStatement &response, int n) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "set_cb"); + + } + + void get_cb(const HeaderOptions &headerOption, const ResponseStatement &response, int n, + PrimitiveClientResource::RemoteAttributesReceivedCallback onRemoteAttributesReceived) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "get_cb entry"); + + const ResourceAttributes &attributes = response.getAttributes(); + onRemoteAttributesReceived(attributes); //passing ResourceAttribute to application + + OC_LOG(DEBUG, CLIENT_W_TAG, "get_cb exit"); + } + + void find_cb(std::shared_ptr primitiveResource, + PrimitiveClient::OnResourceDiscoveredCallback OnResourceDiscovered ) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "findcb entry"); + + if (nullptr == primitiveResource) + { + OC_LOG(ERROR, CLIENT_W_TAG, "find_cb::primitiveResource NULL Parameter"); + return ; + } + + std::shared_ptr< PrimitiveClientResource> primitiveClientResource = + std::shared_ptr< PrimitiveClientResource>(new PrimitiveClientResource(primitiveResource)); + + OnResourceDiscovered(primitiveClientResource); //passing PrimitiveClientResource to application + + OC_LOG(DEBUG, CLIENT_W_TAG, "findcb exit"); + } + +} + +//*******************************Primitive Client Resource************************************* + +PrimitiveClientResource:: PrimitiveClientResource(std::shared_ptr 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 +{ + return m_watchingFlag; +} + +bool PrimitiveClientResource::isCaching() const +{ + return m_cachingFlag; +} + +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) + { + m_watchingFlag = false; + throw BadRequestException { "Failed to start watching resource "}; + } + else + { + m_watchingFlag = true; + m_brokerId = brokerId; + } + } + + 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) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: Failed to terminate hosting"); + throw BadRequestException { "Failed to terminate hosting " }; + } + else + { + m_watchingFlag = false; + } + } + 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); +} + +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)); + + OC_LOG_V(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching CACHE ID %d", cacheId); + if (0 == cacheId) + { + m_cachingFlag = false; + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource::startCaching FAILED"); + throw BadRequestException { "Failed to generate Cache ID" }; + } + else + { + m_cacheId = cacheId; + m_cachingFlag = true; + } + } + + 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) + { + m_cachingFlag = false; + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: SUCCESS"); + } + else + { + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClientResource:: Failed to terminate Caching"); + throw BadRequestException { "Failed to terminate Caching " }; + } + } + 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))); +} + +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"); + + ReportPolicyProxy::ProxyFunc func = std::bind(&ResourceCacheManager::requestResourceCache, + ResourceCacheManager::getInstance(), std::placeholders::_1, std::placeholders::_2, + REPORT_FREQUENCY::UPTODATE, 0l); + ReportPolicy reportPolicy = ReportPolicy((ReportPolicyProxy(func))); + + OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::upToDate exit"); + return reportPolicy; +} + +ReportPolicy ReportPolicy::periodic(int interval, TimeUnit unit) +{ + + 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))); + + OC_LOG(DEBUG, CLIENT_W_TAG, "ReportPolicy::periodic exit"); + return reportPolicy; +} + +std::shared_ptr ReportPolicy::getProxy() +{ + return m_proxy; +} + +//*******************************primitive client********************************************* + +PrimitiveClient *PrimitiveClient:: getInstance() +{ + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient:: getInstance entry"); + + static PrimitiveClient *s_instance; + static std::mutex s_mutex; + if (!s_instance) + { + std::lock_guard lock(s_mutex); + if (!s_instance) + { + s_instance = new PrimitiveClient(); + } + } + + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient:: getInstance exit"); + return s_instance; +} + +void PrimitiveClient::discoverPrimitiveResource(std::string host, std::string resourceURI, + OCConnectivityType connectivityType, + OnResourceDiscoveredCallback cb) +{ + + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient::discoverResource entry"); + + 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'" }; + } + discoverResource(host, resourceURI, connectivityType, std::bind(find_cb, std::placeholders::_1, + cb)); + + OC_LOG(DEBUG, CLIENT_W_TAG, "PrimitiveClient::discoverResource exit"); +} -- 2.7.4