From 6ae44f1897e512cb12969a821256d9c65a1ec97c Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Thu, 9 Jul 2015 20:01:26 +0530 Subject: [PATCH] Resource Client updated - Updated Doxygen comments - Updated ResourceClient APIs - Updated Exception handling in Resource Client Change-Id: Ie299c18d47732c21588dbba8678bb3d3e4b905ac Signed-off-by: Jay Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/1605 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../resource-manipulation/include/ResourceClient.h | 29 +--- .../resource-manipulation/src/ResourceClient.cpp | 187 ++++++++++----------- 2 files changed, 102 insertions(+), 114 deletions(-) diff --git a/service/resource-manipulation/include/ResourceClient.h b/service/resource-manipulation/include/ResourceClient.h index 5dd4c96..4916142 100644 --- a/service/resource-manipulation/include/ResourceClient.h +++ b/service/resource-manipulation/include/ResourceClient.h @@ -67,8 +67,8 @@ namespace OIC /** * @class BadRequestException - * @brief This class inherited from PrimitiveException class. It is used to - * throw exception to the upper layer if request is invalid. + * @brief It is used to throw exception to the upper layer if request is invalid. + * This class inherited from PrimitiveException class. * */ class BadRequestException: public PrimitiveException @@ -80,9 +80,8 @@ namespace OIC /** * @class InvalidParameterException - * @brief This class inherited from PrimitiveException class. It is used to - * throw exception to the upper layer if parameter is invalid. - * + * @brief It is used to throw exception to the upper layer if parameter is invalid. + * This class inherited from PrimitiveException class. */ class InvalidParameterException: public PrimitiveException { @@ -93,7 +92,7 @@ namespace OIC /** * @class RemoteResourceObject - * @brief RemoteResourceObject is an interaction point between Resource + * @brief It is an interaction point between Resource * and the developers. * */ @@ -155,15 +154,13 @@ namespace OIC * * @param ResourceStateChangedCallback - callback to get changed resource state. * - * @throw BadRequestException + * @throw InvalidParameterException * */ void startWatching(ResourceStateChangedCallback); /** * API to stop watching the resource. - * - * @throw BadRequestException */ void stopWatching(); @@ -176,8 +173,6 @@ namespace OIC /** * API to start caching for the resource. - * - * @throw BadRequestException */ void startCaching(); @@ -186,7 +181,7 @@ namespace OIC * * @param CacheUpdatedCallback - callback to get updated resourceAttributes. * - * @throw BadRequestException + * @throw InvalidParameterException * */ void startCaching(CacheUpdatedCallback); @@ -199,9 +194,7 @@ namespace OIC CacheState getResourceCacheState(); /** - * API to stop caching data of the resource. - * - * @throw BadRequestException + * API to stop caching the data for the resource */ void stopCaching(); @@ -229,19 +222,15 @@ namespace OIC * * @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 ResourceAttributes - resourceAttributes data to set * @param RemoteAttributesSetCallback - callback on setting resourceAttributes data. * - * @throw InvalidParameterException - * */ void setRemoteAttributes(ResourceAttributes &, RemoteAttributesSetCallback ); diff --git a/service/resource-manipulation/src/ResourceClient.cpp b/service/resource-manipulation/src/ResourceClient.cpp index 63c2ef0..545f079 100644 --- a/service/resource-manipulation/src/ResourceClient.cpp +++ b/service/resource-manipulation/src/ResourceClient.cpp @@ -24,6 +24,8 @@ #define CLIENT_W_TAG PCF("ResourceClient") +using namespace OIC::Service; + namespace { ResourceState getResourceStateFromBrokerState(BROKER_STATE state) @@ -180,27 +182,24 @@ namespace OIC void RemoteResourceObject::startWatching(ResourceStateChangedCallback cb) { 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 + try { + BrokerID brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource, + std::bind(hostingCallback, std::placeholders::_1, + cb)); m_watchingFlag = true; m_brokerId = brokerId; } + catch (InvalidParameterException exception ) + { + throw InvalidParameterException {exception.what()}; + } } OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startWatching exit"); } @@ -210,21 +209,19 @@ namespace OIC OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching entry"); if (true == m_watchingFlag) { - BrokerID brokerId = ResourceBroker::getInstance()->cancelHostResource(m_brokerId); - if (0 == brokerId) + try { - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: Failed to terminate hosting"); - throw BadRequestException { "Failed to terminate hosting " }; + ResourceBroker::getInstance()->cancelHostResource(m_brokerId); + m_watchingFlag = false; } - else + catch (InvalidParameterException exception ) { - m_watchingFlag = false; + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching InvalidParameterException"); } } else { OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: stopWatching : already terminated"); - throw BadRequestException { "Not watching" }; } OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopWatching exit"); @@ -233,10 +230,16 @@ namespace OIC 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"); - - return getResourceStateFromBrokerState(brokerState); + try + { + BROKER_STATE brokerState = ResourceBroker::getInstance()->getResourceState(m_primitiveResource); + OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState exit"); + return getResourceStateFromBrokerState(brokerState); + } + catch (InvalidParameterException exception) + { + OC_LOG(DEBUG, CLIENT_W_TAG, " RemoteResourceObject::getState InvalidParameterException"); + } } void RemoteResourceObject::startCaching() @@ -245,84 +248,71 @@ namespace OIC 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 + try { + CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource, + NULL, REPORT_FREQUENCY::NONE, 0); + m_cacheId = cacheId; m_cachingFlag = true; + OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId); + } + catch (InvalidParameterException e) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching InvalidParameterException"); } } - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching exit"); } void RemoteResourceObject::startCaching(CacheUpdatedCallback cb) { 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 + try { + CacheID cacheId = ResourceCacheManager::getInstance()->requestResourceCache(m_primitiveResource, + std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, cb), + REPORT_FREQUENCY::UPTODATE, 0); + m_cacheId = cacheId; m_cachingFlag = true; + OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching CACHE ID %d", cacheId); + } + catch (InvalidParameterException e) + { + throw InvalidParameterException { e.what() }; } } - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::startCaching exit"); } void RemoteResourceObject::stopCaching() { OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopCaching entry"); - CacheID cacheId; if (true == m_cachingFlag) { - cacheId = ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId); - if (0 != cacheId) + try { + ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId); m_cachingFlag = false; - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: SUCCESS"); } - else + catch (InvalidParameterException exception) { - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject:: Failed to terminate Caching"); - throw BadRequestException { "Failed to terminate Caching " }; + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::stopCaching InvalidParameterException"); } } 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"); @@ -331,42 +321,63 @@ namespace OIC CacheState RemoteResourceObject::getResourceCacheState() { 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); + try + { + CACHE_STATE cacheState = ResourceCacheManager::getInstance()->getResourceCacheState( + m_primitiveResource); + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getResourceCacheState exit"); + return getCacheState(cacheState); + } + catch (InvalidParameterException exception) + { + OC_LOG(DEBUG, CLIENT_W_TAG, + "RemoteResourceObject::getResourceCacheState InvalidParameterException"); + } } void RemoteResourceObject::refreshCache() { OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache entry"); - - OCStackResult result = ResourceCacheManager::getInstance()->updateResourceCache( - m_primitiveResource); - if (result == OC_STACK_OK) + try { - OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache Success"); + + ResourceCacheManager::getInstance()->updateResourceCache( + m_primitiveResource); } - else + catch (InvalidParameterException exception) { - OC_LOG_V(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache FAILED %d", result); - throw BadRequestException { "Failed to refresh Caching " }; + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache InvalidParameterException"); } + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::refreshCache exit"); } ResourceAttributes RemoteResourceObject:: getCachedAttributes() const { OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttributes "); - return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource); + try + { + return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource); + } + catch (InvalidParameterException e) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getCachedAttributes InvalidParameterException"); + } } ResourceAttributes::Value RemoteResourceObject:: getCachedAttribute( const std::string &key) { OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttribute entry"); - ResourceAttributes Cachedattributes = ResourceCacheManager::getInstance()->getCachedData( - m_primitiveResource); + try + { + ResourceAttributes Cachedattributes = ResourceCacheManager::getInstance()->getCachedData( + m_primitiveResource); + return Cachedattributes[key]; + } + catch (InvalidParameterException e) + { + OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject::getCachedAttribute InvalidParameterException"); + } OC_LOG(DEBUG, CLIENT_W_TAG, "RemoteResourceObject :: getCachedAttribute exit"); - return Cachedattributes[key]; } std::string RemoteResourceObject::getUri() const @@ -392,16 +403,10 @@ namespace OIC 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)); - } + + 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"); } @@ -409,16 +414,10 @@ namespace OIC 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)); - } + + 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"); } -- 2.7.4