X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fresource-encapsulation%2Fsrc%2FresourceCache%2Fsrc%2FResourceCacheManager.cpp;h=812c52ea6f8d7c5639d3fa709d0bb6460e6c8aba;hb=45053652a06c2c7939af3433d59d4dc6a5eedb26;hp=e53608baa95139fc93a3136e88aebb3078ac0191;hpb=068b9d878cead4b54ceaba89cb9d9c19b1c5dcb1;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp b/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp index e53608b..812c52e 100644 --- a/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp +++ b/service/resource-encapsulation/src/resourceCache/src/ResourceCacheManager.cpp @@ -19,6 +19,12 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "ResourceCacheManager.h" +#include "RCSException.h" +#include "ocrandom.h" + +#include "ScopeLogger.h" + +#define TAG PCF("RCSResourceCacheManager") namespace OIC { @@ -54,21 +60,44 @@ namespace OIC } CacheID ResourceCacheManager::requestResourceCache( - PrimitiveResourcePtr pResource, CacheCB func, + PrimitiveResourcePtr pResource, CacheCB func, CACHE_METHOD cm, REPORT_FREQUENCY rf, long reportTime) { + SCOPE_LOG_F(DEBUG, TAG); + if (pResource == nullptr) { - throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"}; + throw RCSInvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"}; } CacheID retID = 0; + if (cm == CACHE_METHOD::OBSERVE_ONLY) + { + if (func == NULL || func == nullptr) + { + throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"}; + } + + std::lock_guard lock(s_mutex); + retID = OCGetRandom(); + while(observeCacheIDmap.find(retID) != observeCacheIDmap.end()) + { + retID = OCGetRandom(); + } + + auto newHandler = std::make_shared(pResource); + newHandler->startCache(std::move(func), (rf == REPORT_FREQUENCY::WHENEVER_NOTIFIED)); + + observeCacheIDmap.insert(std::make_pair(retID, newHandler)); + return retID; + } + if (rf != REPORT_FREQUENCY::NONE) { if (func == NULL || func == nullptr) { - throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"}; + throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"}; } if (!reportTime) { @@ -85,6 +114,8 @@ namespace OIC newHandler->initializeDataCache(pResource); s_cacheDataList->push_back(newHandler); } + + std::lock_guard lock(s_mutex); retID = newHandler->addSubscriber(func, rf, reportTime); cacheIDmap.insert(std::make_pair(retID, newHandler)); @@ -94,9 +125,32 @@ namespace OIC void ResourceCacheManager::cancelResourceCache(CacheID id) { - if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end()) + SCOPE_LOG_F(DEBUG, TAG); + std::lock_guard lock(s_mutex); + + auto observeIns = observeCacheIDmap.find(id); + auto dataCacheIns = cacheIDmap.find(id); + if ((dataCacheIns == cacheIDmap.end() && observeIns == observeCacheIDmap.end()) + || id == 0) { - throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"}; + throw RCSInvalidParameterException {"[cancelResourceCache] CacheID is invaild"}; + } + + if (observeIns != observeCacheIDmap.end()) + { + try + { + (observeIns->second)->stopCache(); + } + catch (...) + { + (observeIns->second).reset(); + observeCacheIDmap.erase(id); + throw; + } + (observeIns->second).reset(); + observeCacheIDmap.erase(id); + return; } DataCachePtr foundCacheHandler = findDataCache(id); @@ -107,7 +161,6 @@ namespace OIC { cacheIDmap.erase(id); } - std::lock_guard lock(s_mutex); if (foundCacheHandler->isEmptySubscriber()) { s_cacheDataList->remove(foundCacheHandler); @@ -115,71 +168,42 @@ namespace OIC } } - void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const - { - if (pResource == nullptr) - { - throw InvalidParameterException - {"[updateResourceCache] Primitive Resource is invaild"}; - } - - DataCachePtr foundCache = findDataCache(pResource); - if (foundCache == nullptr) - { - throw InvalidParameterException - {"[updateResourceCache] Primitive Resource is invaild"}; - } - foundCache->requestGet(); - } - void ResourceCacheManager::updateResourceCache(CacheID updateId) const { + SCOPE_LOG_F(DEBUG, TAG); + if (updateId == 0) { - throw InvalidParameterException {"[getCachedData] CacheID is NULL"}; + throw RCSInvalidParameterException {"[updateResourceCache] CacheID is NULL"}; } DataCachePtr foundCache = findDataCache(updateId); if (foundCache == nullptr) { - throw InvalidParameterException {"[getCachedData] CacheID is invaild"}; + throw RCSInvalidParameterException {"[updateResourceCache] CacheID is invaild"}; } foundCache->requestGet(); } - const RCSResourceAttributes ResourceCacheManager::getCachedData( - PrimitiveResourcePtr pResource) const + const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const { - if (pResource == nullptr) - { - throw InvalidParameterException {"[getCachedData] Primitive Resource is nullptr"}; - } + SCOPE_LOG_F(DEBUG, TAG); - DataCachePtr handler = findDataCache(pResource); - if (handler == nullptr) + if (id == 0) { - throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"}; + throw RCSInvalidParameterException {"[getCachedData] CacheID is NULL"}; } - if (handler->isCachedData() == false) + auto observePtr = observeCacheIDmap.find(id); + if (observePtr != observeCacheIDmap.end()) { - throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"}; - } - - return handler->getCachedData(); - } - - const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const - { - if (id == 0) - { - throw InvalidParameterException {"[getCachedData] CacheID is NULL"}; + return (observePtr->second)->getCachedData(); } DataCachePtr handler = findDataCache(id); if (handler == nullptr) { - throw InvalidParameterException {"[getCachedData] CacheID is invaild"}; + throw RCSInvalidParameterException {"[getCachedData] CacheID is invaild"}; } if (handler->isCachedData() == false) @@ -190,27 +214,19 @@ namespace OIC return handler->getCachedData(); } - CACHE_STATE ResourceCacheManager::getResourceCacheState( - PrimitiveResourcePtr pResource) const + CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const { - if (pResource == nullptr) - { - throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is nullptr"}; - } + SCOPE_LOG_F(DEBUG, TAG); - DataCachePtr handler = findDataCache(pResource); - if (handler == nullptr) + if (id == 0) { - return CACHE_STATE::NONE; + throw RCSInvalidParameterException {"[getResourceCacheState] CacheID is NULL"}; } - return handler->getCacheState(); - } - CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const - { - if (id == 0) + auto observePtr = observeCacheIDmap.find(id); + if (observePtr != observeCacheIDmap.end()) { - throw InvalidParameterException {"[getResourceCacheState] CacheID is NULL"}; + return (observePtr->second)->getCacheState(); } DataCachePtr handler = findDataCache(id); @@ -221,38 +237,33 @@ namespace OIC return handler->getCacheState(); } - bool ResourceCacheManager::isCachedData(PrimitiveResourcePtr pResource) const + bool ResourceCacheManager::isCachedData(CacheID id) const { - if (pResource == nullptr) - { - throw InvalidParameterException {"[isCachedData] Primitive Resource is nullptr"}; - } + SCOPE_LOG_F(DEBUG, TAG); - DataCachePtr handler = findDataCache(pResource); - if (handler == nullptr) + if (id == 0) { - throw InvalidParameterException {"[isCachedData] Primitive Resource is invaild"}; + throw RCSInvalidParameterException {"[isCachedData] CacheID is NULL"}; } - return handler->isCachedData(); - } - bool ResourceCacheManager::isCachedData(CacheID id) const - { - if (id == 0) + auto observePtr = observeCacheIDmap.find(id); + if (observePtr != observeCacheIDmap.end()) { - throw InvalidParameterException {"[isCachedData] CacheID is NULL"}; + return (observePtr->second)->isCachedData(); } DataCachePtr handler = findDataCache(id); if (handler == nullptr) { - throw InvalidParameterException {"[isCachedData] CacheID is invaild"}; + throw RCSInvalidParameterException {"[isCachedData] CacheID is invaild"}; } return handler->isCachedData(); } void ResourceCacheManager::initializeResourceCacheManager() { + SCOPE_LOG_F(DEBUG, TAG); + std::lock_guard lock(s_mutex); if (s_cacheDataList == nullptr) { @@ -263,6 +274,8 @@ namespace OIC DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const { + SCOPE_LOG_F(DEBUG, TAG); + DataCachePtr retHandler = nullptr; std::lock_guard lock(s_mutex); for (auto &i : * s_cacheDataList) @@ -279,6 +292,8 @@ namespace OIC DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const { + SCOPE_LOG_F(DEBUG, TAG); + DataCachePtr retHandler = nullptr; for (auto it : cacheIDmap) {