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();
45 ResourceCacheManager * ResourceCacheManager::getInstance()
47 if(s_instance == nullptr)
49 s_mutexForCreation.lock();
50 if(s_instance == nullptr)
52 s_instance = new ResourceCacheManager();
54 s_mutexForCreation.unlock();
59 CacheID ResourceCacheManager::requestResourceCache(
60 PrimitiveResourcePtr pResource, CacheCB func,
61 REPORT_FREQUENCY rf, long reportTime)
65 if(rf != REPORT_FREQUENCY::NONE)
74 reportTime = DEFAULT_REPORT_TIME;
78 DataCachePtr newHandler = findDataCache(pResource);
79 if(newHandler == nullptr)
81 newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
82 s_cacheDataList->push_back(newHandler);
84 retID = newHandler->addSubscriber(func, rf, reportTime);
89 CacheID ResourceCacheManager::cancelResourceCache(CacheID id)
97 DataCachePtr foundCacheHandler = findDataCache(id);
98 if(foundCacheHandler == nullptr)
104 retID = foundCacheHandler->deleteSubscriber(id);
110 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
112 DataCachePtr retHandler = nullptr;
113 for (auto & i : * s_cacheDataList)
115 if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
116 i->getPrimitiveResource()->getHost() == pResource->getHost())
125 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
127 DataCachePtr retHandler = nullptr;
128 for (auto & i : * s_cacheDataList)
130 SubscriberInfoPair pair = i->findSubscriber(id);
140 OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
142 OCStackResult ret = OC_STACK_ERROR;
144 // TODO update now (request get)
149 const ResourceAttributes ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource) const
151 DataCachePtr handler = findDataCache(pResource);
152 if(handler == nullptr)
154 return ResourceAttributes();
156 return handler->getCachedData();
159 const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
161 DataCachePtr handler = findDataCache(id);
162 if(handler == nullptr)
164 return ResourceAttributes();
166 return handler->getCachedData();
169 CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource) const
171 DataCachePtr handler = findDataCache(pResource);
172 if(handler == nullptr)
174 return CACHE_STATE::NONE;
176 return handler->getCacheState();
178 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
180 DataCachePtr handler = findDataCache(id);
181 if(handler == nullptr)
183 return CACHE_STATE::NONE;
185 return handler->getCacheState();