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);
110 foundCacheHandler.reset();
115 void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
117 if (pResource == nullptr)
119 throw InvalidParameterException
120 {"[updateResourceCache] Primitive Resource is invaild"};
123 DataCachePtr foundCache = findDataCache(pResource);
124 if (foundCache == nullptr)
126 throw InvalidParameterException
127 {"[updateResourceCache] Primitive Resource is invaild"};
129 foundCache->requestGet();
131 void ResourceCacheManager::updateResourceCache(CacheID updateId) const
135 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
138 DataCachePtr foundCache = findDataCache(updateId);
139 if (foundCache == nullptr)
141 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
143 foundCache->requestGet();
146 const RCSResourceAttributes ResourceCacheManager::getCachedData(
147 PrimitiveResourcePtr pResource) const
149 if (pResource == nullptr)
151 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
154 DataCachePtr handler = findDataCache(pResource);
155 if (handler == nullptr)
157 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
159 return handler->getCachedData();
162 const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
166 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
169 DataCachePtr handler = findDataCache(id);
170 if (handler == nullptr)
172 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
174 return handler->getCachedData();
177 CACHE_STATE ResourceCacheManager::getResourceCacheState(
178 PrimitiveResourcePtr pResource) const
180 if (pResource == nullptr)
182 throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is invaild"};
185 DataCachePtr handler = findDataCache(pResource);
186 if (handler == nullptr)
188 return CACHE_STATE::NONE;
190 return handler->getCacheState();
193 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
197 throw InvalidParameterException {"[getResourceCacheState] CacheID is invaild"};
200 DataCachePtr handler = findDataCache(id);
201 if (handler == nullptr)
203 return CACHE_STATE::NONE;
205 return handler->getCacheState();
208 void ResourceCacheManager::initializeResourceCacheManager()
210 if (s_cacheDataList == nullptr)
213 = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
217 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
219 DataCachePtr retHandler = nullptr;
220 for (auto &i : * s_cacheDataList)
222 if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
223 i->getPrimitiveResource()->getHost() == pResource->getHost())
232 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
234 DataCachePtr retHandler = nullptr;
235 for (auto it : cacheIDmap)
239 retHandler = it.second;
246 } // namespace Service