//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include "ResourceCacheManager.h"
+#include "RCSException.h"
+#include "ocrandom.h"
+
+#include "ScopeLogger.h"
+
+#define TAG PCF("RCSResourceCacheManager")
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<std::mutex> lock(s_mutex);
+ retID = OCGetRandom();
+ while(observeCacheIDmap.find(retID) != observeCacheIDmap.end())
+ {
+ retID = OCGetRandom();
+ }
+
+ auto newHandler = std::make_shared<ObserveCache>(pResource);
+ newHandler->startCache(std::move(func));
+
+ 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)
{
newHandler->initializeDataCache(pResource);
s_cacheDataList->push_back(newHandler);
}
+
+ std::lock_guard<std::mutex> lock(s_mutex);
retID = newHandler->addSubscriber(func, rf, reportTime);
cacheIDmap.insert(std::make_pair(retID, newHandler));
void ResourceCacheManager::cancelResourceCache(CacheID id)
{
- if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
+ SCOPE_LOG_F(DEBUG, TAG);
+ std::lock_guard<std::mutex> 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);
{
cacheIDmap.erase(id);
}
- std::lock_guard<std::mutex> lock(s_mutex);
if (foundCacheHandler->isEmptySubscriber())
{
s_cacheDataList->remove(foundCacheHandler);
}
}
- 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)
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);
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<std::mutex> lock(s_mutex);
if (s_cacheDataList == nullptr)
{
DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
{
+ SCOPE_LOG_F(DEBUG, TAG);
+
DataCachePtr retHandler = nullptr;
std::lock_guard<std::mutex> lock(s_mutex);
for (auto &i : * s_cacheDataList)
DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
{
+ SCOPE_LOG_F(DEBUG, TAG);
+
DataCachePtr retHandler = nullptr;
for (auto it : cacheIDmap)
{