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"
23 ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
24 std::mutex ResourceCacheManager::s_mutexForCreation;
25 std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
27 ResourceCacheManager::ResourceCacheManager()
29 // TODO Auto-generated constructor stub
32 s_cacheDataList = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
36 ResourceCacheManager::~ResourceCacheManager()
38 // TODO Auto-generated destructor stub
41 s_cacheDataList->clear();
46 ResourceCacheManager * ResourceCacheManager::getInstance()
48 if(s_instance == nullptr)
50 s_mutexForCreation.lock();
51 if(s_instance == nullptr)
53 s_instance = new ResourceCacheManager();
55 s_mutexForCreation.unlock();
60 CacheID ResourceCacheManager::requestResourceCache(
61 PrimitiveResourcePtr pResource, CacheCB func,
62 REPORT_FREQUENCY rf, long reportTime)
66 if(rf != REPORT_FREQUENCY::NONE)
75 reportTime = DEFAULT_REPORT_TIME;
79 DataCachePtr newHandler = findDataCache(pResource);
80 if(newHandler == nullptr)
82 newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
83 s_cacheDataList->push_back(newHandler);
85 ret = newHandler->addSubscriber(func, rf, reportTime);
90 OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id)
92 OCStackResult ret = OC_STACK_ERROR;
101 DataCachePtr deleteCacheHandler = findDataCache(pResource);
102 if(deleteCacheHandler == nullptr)
108 retID = deleteCacheHandler->deleteSubscriber(id);
119 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
121 DataCachePtr retHandler = nullptr;
122 for (auto & i : * s_cacheDataList)
124 if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
125 i->getPrimitiveResource()->getHost() == pResource->getHost())
134 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
136 DataCachePtr retHandler = nullptr;
137 for (auto & i : * s_cacheDataList)
139 SubscriberInfoPair pair = i->findSubscriber(id);
149 OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
151 OCStackResult ret = OC_STACK_ERROR;
153 // TODO update now (request get)
158 const ResourceAttributes ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource) const
160 DataCachePtr handler = findDataCache(pResource);
161 if(handler == nullptr)
163 return ResourceAttributes();
165 return handler->getCachedData();
168 const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
170 DataCachePtr handler = findDataCache(id);
171 if(handler == nullptr)
173 return ResourceAttributes();
175 return handler->getCachedData();
178 CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource) const
180 DataCachePtr handler = findDataCache(pResource);
181 if(handler == nullptr)
183 return CACHE_STATE::NONE;
185 return handler->getCacheState();
187 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
189 DataCachePtr handler = findDataCache(id);
190 if(handler == nullptr)
192 return CACHE_STATE::NONE;
194 return handler->getCacheState();