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"
27 ResourceCacheManager *ResourceCacheManager::s_instance = NULL;
28 std::mutex ResourceCacheManager::s_mutexForCreation;
29 std::mutex ResourceCacheManager::s_mutex;
30 std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
32 ResourceCacheManager::~ResourceCacheManager()
34 std::lock_guard<std::mutex> lock(s_mutex);
35 if (s_cacheDataList != nullptr)
37 s_cacheDataList->clear();
41 ResourceCacheManager *ResourceCacheManager::getInstance()
43 if (s_instance == nullptr)
45 s_mutexForCreation.lock();
46 if (s_instance == nullptr)
48 s_instance = new ResourceCacheManager();
49 s_instance->initializeResourceCacheManager();
51 s_mutexForCreation.unlock();
56 CacheID ResourceCacheManager::requestResourceCache(
57 PrimitiveResourcePtr pResource, CacheCB func,
58 REPORT_FREQUENCY rf, long reportTime)
60 if (pResource == nullptr)
62 throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
67 if (rf != REPORT_FREQUENCY::NONE)
69 if (func == NULL || func == nullptr)
71 throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
76 reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
80 DataCachePtr newHandler = findDataCache(pResource);
81 if (newHandler == nullptr)
83 std::lock_guard<std::mutex> lock(s_mutex);
84 newHandler.reset(new DataCache());
85 newHandler->initializeDataCache(pResource);
86 s_cacheDataList->push_back(newHandler);
88 retID = newHandler->addSubscriber(func, rf, reportTime);
90 cacheIDmap.insert(std::make_pair(retID, newHandler));
95 void ResourceCacheManager::cancelResourceCache(CacheID id)
97 if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
99 throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
102 DataCachePtr foundCacheHandler = findDataCache(id);
103 if (foundCacheHandler != nullptr)
105 CacheID retID = foundCacheHandler->deleteSubscriber(id);
108 cacheIDmap.erase(id);
110 std::lock_guard<std::mutex> lock(s_mutex);
111 if (foundCacheHandler->isEmptySubscriber())
113 s_cacheDataList->remove(foundCacheHandler);
118 void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
120 if (pResource == nullptr)
122 throw InvalidParameterException
123 {"[updateResourceCache] Primitive Resource is invaild"};
126 DataCachePtr foundCache = findDataCache(pResource);
127 if (foundCache == nullptr)
129 throw InvalidParameterException
130 {"[updateResourceCache] Primitive Resource is invaild"};
132 foundCache->requestGet();
135 void ResourceCacheManager::updateResourceCache(CacheID updateId) const
139 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
142 DataCachePtr foundCache = findDataCache(updateId);
143 if (foundCache == nullptr)
145 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
147 foundCache->requestGet();
150 const RCSResourceAttributes ResourceCacheManager::getCachedData(
151 PrimitiveResourcePtr pResource) const
153 if (pResource == nullptr)
155 throw InvalidParameterException {"[getCachedData] Primitive Resource is nullptr"};
158 DataCachePtr handler = findDataCache(pResource);
159 if (handler == nullptr)
161 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
164 if (handler->isCachedData() == false)
166 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
169 return handler->getCachedData();
172 const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
176 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
179 DataCachePtr handler = findDataCache(id);
180 if (handler == nullptr)
182 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
185 if (handler->isCachedData() == false)
187 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
190 return handler->getCachedData();
193 CACHE_STATE ResourceCacheManager::getResourceCacheState(
194 PrimitiveResourcePtr pResource) const
196 if (pResource == nullptr)
198 throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is nullptr"};
201 DataCachePtr handler = findDataCache(pResource);
202 if (handler == nullptr)
204 return CACHE_STATE::NONE;
206 return handler->getCacheState();
209 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
213 throw InvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
216 DataCachePtr handler = findDataCache(id);
217 if (handler == nullptr)
219 return CACHE_STATE::NONE;
221 return handler->getCacheState();
224 bool ResourceCacheManager::isCachedData(PrimitiveResourcePtr pResource) const
226 if (pResource == nullptr)
228 throw InvalidParameterException {"[isCachedData] Primitive Resource is nullptr"};
231 DataCachePtr handler = findDataCache(pResource);
232 if (handler == nullptr)
234 throw InvalidParameterException {"[isCachedData] Primitive Resource is invaild"};
236 return handler->isCachedData();
239 bool ResourceCacheManager::isCachedData(CacheID id) const
243 throw InvalidParameterException {"[isCachedData] CacheID is NULL"};
246 DataCachePtr handler = findDataCache(id);
247 if (handler == nullptr)
249 throw InvalidParameterException {"[isCachedData] CacheID is invaild"};
251 return handler->isCachedData();
254 void ResourceCacheManager::initializeResourceCacheManager()
256 std::lock_guard<std::mutex> lock(s_mutex);
257 if (s_cacheDataList == nullptr)
260 = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
264 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
266 DataCachePtr retHandler = nullptr;
267 std::lock_guard<std::mutex> lock(s_mutex);
268 for (auto &i : * s_cacheDataList)
270 if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
271 i->getPrimitiveResource()->getHost() == pResource->getHost())
280 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
282 DataCachePtr retHandler = nullptr;
283 for (auto it : cacheIDmap)
287 retHandler = it.second;
294 } // namespace Service