1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ResourceCacheManager.h"
22 #include "RCSException.h"
25 #include "ScopeLogger.h"
27 #define TAG PCF("RCSResourceCacheManager")
33 ResourceCacheManager *ResourceCacheManager::s_instance = NULL;
34 std::mutex ResourceCacheManager::s_mutexForCreation;
35 std::mutex ResourceCacheManager::s_mutex;
36 std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
38 ResourceCacheManager::~ResourceCacheManager()
40 std::lock_guard<std::mutex> lock(s_mutex);
41 if (s_cacheDataList != nullptr)
43 s_cacheDataList->clear();
47 ResourceCacheManager *ResourceCacheManager::getInstance()
49 if (s_instance == nullptr)
51 s_mutexForCreation.lock();
52 if (s_instance == nullptr)
54 s_instance = new ResourceCacheManager();
55 s_instance->initializeResourceCacheManager();
57 s_mutexForCreation.unlock();
62 CacheID ResourceCacheManager::requestResourceCache(
63 PrimitiveResourcePtr pResource, CacheCB func, CACHE_METHOD cm,
64 REPORT_FREQUENCY rf, long reportTime)
66 SCOPE_LOG_F(DEBUG, TAG);
68 if (pResource == nullptr)
70 throw RCSInvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
75 if (cm == CACHE_METHOD::OBSERVE_ONLY)
77 if (func == NULL || func == nullptr)
79 throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
82 std::lock_guard<std::mutex> lock(s_mutex);
83 retID = OCGetRandom();
84 while(observeCacheIDmap.find(retID) != observeCacheIDmap.end())
86 retID = OCGetRandom();
89 auto newHandler = std::make_shared<ObserveCache>(pResource);
90 newHandler->startCache(std::move(func));
91 m_observeCacheList.push_back(newHandler);
93 observeCacheIDmap.insert(std::make_pair(retID, newHandler));
97 if (rf != REPORT_FREQUENCY::NONE)
99 if (func == NULL || func == nullptr)
101 throw RCSInvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
106 reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
110 DataCachePtr newHandler = findDataCache(pResource);
111 if (newHandler == nullptr)
113 std::lock_guard<std::mutex> lock(s_mutex);
114 newHandler.reset(new DataCache());
115 newHandler->initializeDataCache(pResource);
116 s_cacheDataList->push_back(newHandler);
119 std::lock_guard<std::mutex> lock(s_mutex);
120 retID = newHandler->addSubscriber(func, rf, reportTime);
122 cacheIDmap.insert(std::make_pair(retID, newHandler));
127 void ResourceCacheManager::cancelResourceCache(CacheID id)
129 SCOPE_LOG_F(DEBUG, TAG);
130 std::lock_guard<std::mutex> lock(s_mutex);
132 auto observeIns = observeCacheIDmap.find(id);
133 auto dataCacheIns = cacheIDmap.find(id);
134 if ((dataCacheIns == cacheIDmap.end() && observeIns == observeCacheIDmap.end())
138 throw RCSInvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
141 if (observeIns != observeCacheIDmap.end())
145 (observeIns->second)->stopCache();
149 (observeIns->second).reset();
150 observeCacheIDmap.erase(id);
154 (observeIns->second).reset();
155 observeCacheIDmap.erase(id);
159 DataCachePtr foundCacheHandler = findDataCache(id);
160 if (foundCacheHandler != nullptr)
162 CacheID retID = foundCacheHandler->deleteSubscriber(id);
165 cacheIDmap.erase(id);
167 if (foundCacheHandler->isEmptySubscriber())
169 s_cacheDataList->remove(foundCacheHandler);
174 void ResourceCacheManager::updateResourceCache(CacheID updateId) const
176 SCOPE_LOG_F(DEBUG, TAG);
180 throw RCSInvalidParameterException {"[updateResourceCache] CacheID is NULL"};
183 DataCachePtr foundCache = findDataCache(updateId);
184 if (foundCache == nullptr)
186 throw RCSInvalidParameterException {"[updateResourceCache] CacheID is invaild"};
188 foundCache->requestGet();
191 const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
193 SCOPE_LOG_F(DEBUG, TAG);
197 throw RCSInvalidParameterException {"[getCachedData] CacheID is NULL"};
200 auto observePtr = observeCacheIDmap.find(id);
201 if (observePtr != observeCacheIDmap.end())
203 return (observePtr->second)->getCachedData();
206 DataCachePtr handler = findDataCache(id);
207 if (handler == nullptr)
209 throw RCSInvalidParameterException {"[getCachedData] CacheID is invaild"};
212 if (handler->isCachedData() == false)
214 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
217 return handler->getCachedData();
220 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
222 SCOPE_LOG_F(DEBUG, TAG);
226 throw RCSInvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
229 auto observePtr = observeCacheIDmap.find(id);
230 if (observePtr != observeCacheIDmap.end())
232 return (observePtr->second)->getCacheState();
235 DataCachePtr handler = findDataCache(id);
236 if (handler == nullptr)
238 return CACHE_STATE::NONE;
240 return handler->getCacheState();
243 bool ResourceCacheManager::isCachedData(CacheID id) const
245 SCOPE_LOG_F(DEBUG, TAG);
249 throw RCSInvalidParameterException {"[isCachedData] CacheID is NULL"};
252 auto observePtr = observeCacheIDmap.find(id);
253 if (observePtr != observeCacheIDmap.end())
255 return (observePtr->second)->isCachedData();
258 DataCachePtr handler = findDataCache(id);
259 if (handler == nullptr)
261 throw RCSInvalidParameterException {"[isCachedData] CacheID is invaild"};
263 return handler->isCachedData();
266 void ResourceCacheManager::initializeResourceCacheManager()
268 SCOPE_LOG_F(DEBUG, TAG);
270 std::lock_guard<std::mutex> lock(s_mutex);
271 if (s_cacheDataList == nullptr)
274 = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
278 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
280 SCOPE_LOG_F(DEBUG, TAG);
282 DataCachePtr retHandler = nullptr;
283 std::lock_guard<std::mutex> lock(s_mutex);
284 for (auto &i : * s_cacheDataList)
286 if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
287 i->getPrimitiveResource()->getHost() == pResource->getHost())
296 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
298 SCOPE_LOG_F(DEBUG, TAG);
300 DataCachePtr retHandler = nullptr;
301 for (auto it : cacheIDmap)
305 retHandler = it.second;
312 } // namespace Service