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::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
31 ResourceCacheManager::~ResourceCacheManager()
33 if (s_cacheDataList != nullptr)
35 s_cacheDataList->clear();
39 ResourceCacheManager *ResourceCacheManager::getInstance()
41 if (s_instance == nullptr)
43 s_mutexForCreation.lock();
44 if (s_instance == nullptr)
46 s_instance = new ResourceCacheManager();
47 s_instance->initializeResourceCacheManager();
49 s_mutexForCreation.unlock();
54 CacheID ResourceCacheManager::requestResourceCache(
55 PrimitiveResourcePtr pResource, CacheCB func,
56 REPORT_FREQUENCY rf, long reportTime)
58 if (pResource == nullptr)
60 throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
65 if (rf != REPORT_FREQUENCY::NONE)
67 if (func == NULL || func == nullptr)
69 throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
74 reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
78 DataCachePtr newHandler = findDataCache(pResource);
79 if (newHandler == nullptr)
81 newHandler.reset(new DataCache());
82 newHandler->initializeDataCache(pResource);
83 s_cacheDataList->push_back(newHandler);
85 retID = newHandler->addSubscriber(func, rf, reportTime);
87 cacheIDmap.insert(std::make_pair(retID, newHandler));
92 void ResourceCacheManager::cancelResourceCache(CacheID id)
94 if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
96 throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
99 DataCachePtr foundCacheHandler = findDataCache(id);
100 if (foundCacheHandler != nullptr)
102 CacheID retID = foundCacheHandler->deleteSubscriber(id);
105 cacheIDmap.erase(id);
107 if (foundCacheHandler->isEmptySubscriber())
109 s_cacheDataList->remove(foundCacheHandler);
114 void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
116 if (pResource == nullptr)
118 throw InvalidParameterException
119 {"[updateResourceCache] Primitive Resource is invaild"};
122 DataCachePtr foundCache = findDataCache(pResource);
123 if (foundCache == nullptr)
125 throw InvalidParameterException
126 {"[updateResourceCache] Primitive Resource is invaild"};
128 foundCache->requestGet();
130 void ResourceCacheManager::updateResourceCache(CacheID updateId) const
134 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
137 DataCachePtr foundCache = findDataCache(updateId);
138 if (foundCache == nullptr)
140 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
142 foundCache->requestGet();
145 const RCSResourceAttributes ResourceCacheManager::getCachedData(
146 PrimitiveResourcePtr pResource) const
148 if (pResource == nullptr)
150 throw InvalidParameterException {"[getCachedData] Primitive Resource is nullptr"};
153 DataCachePtr handler = findDataCache(pResource);
154 if (handler == nullptr)
156 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
158 return handler->getCachedData();
161 const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
165 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
168 DataCachePtr handler = findDataCache(id);
169 if (handler == nullptr)
171 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
173 return handler->getCachedData();
176 CACHE_STATE ResourceCacheManager::getResourceCacheState(
177 PrimitiveResourcePtr pResource) const
179 if (pResource == nullptr)
181 throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is nullptr"};
184 DataCachePtr handler = findDataCache(pResource);
185 if (handler == nullptr)
187 return CACHE_STATE::NONE;
189 return handler->getCacheState();
192 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
196 throw InvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
199 DataCachePtr handler = findDataCache(id);
200 if (handler == nullptr)
202 return CACHE_STATE::NONE;
204 return handler->getCacheState();
207 void ResourceCacheManager::initializeResourceCacheManager()
209 if (s_cacheDataList == nullptr)
212 = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
216 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
218 DataCachePtr retHandler = nullptr;
219 for (auto &i : * s_cacheDataList)
221 if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
222 i->getPrimitiveResource()->getHost() == pResource->getHost())
231 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
233 DataCachePtr retHandler = nullptr;
234 for (auto it : cacheIDmap)
238 retHandler = it.second;
245 } // namespace Service