Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / src / ResourceCacheManager.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ResourceCacheManager.h"
22
23 namespace OIC
24 {
25     namespace Service
26     {
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);
31
32         ResourceCacheManager::~ResourceCacheManager()
33         {
34             std::lock_guard<std::mutex> lock(s_mutex);
35             if (s_cacheDataList != nullptr)
36             {
37                 s_cacheDataList->clear();
38             }
39         }
40
41         ResourceCacheManager *ResourceCacheManager::getInstance()
42         {
43             if (s_instance == nullptr)
44             {
45                 s_mutexForCreation.lock();
46                 if (s_instance == nullptr)
47                 {
48                     s_instance = new ResourceCacheManager();
49                     s_instance->initializeResourceCacheManager();
50                 }
51                 s_mutexForCreation.unlock();
52             }
53             return s_instance;
54         }
55
56         CacheID ResourceCacheManager::requestResourceCache(
57             PrimitiveResourcePtr pResource, CacheCB func,
58             REPORT_FREQUENCY rf, long reportTime)
59         {
60             if (pResource == nullptr)
61             {
62                 throw InvalidParameterException {"[requestResourceCache] Primitive Resource is invaild"};
63             }
64
65             CacheID retID = 0;
66
67             if (rf != REPORT_FREQUENCY::NONE)
68             {
69                 if (func == NULL || func == nullptr)
70                 {
71                     throw InvalidParameterException {"[requestResourceCache] CacheCB is invaild"};
72                 }
73                 if (!reportTime)
74                 {
75                     // default setting
76                     reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
77                 }
78             }
79
80             DataCachePtr newHandler = findDataCache(pResource);
81             if (newHandler == nullptr)
82             {
83                 std::lock_guard<std::mutex> lock(s_mutex);
84                 newHandler.reset(new DataCache());
85                 newHandler->initializeDataCache(pResource);
86                 s_cacheDataList->push_back(newHandler);
87             }
88             retID = newHandler->addSubscriber(func, rf, reportTime);
89
90             cacheIDmap.insert(std::make_pair(retID, newHandler));
91
92             return retID;
93         }
94
95         void ResourceCacheManager::cancelResourceCache(CacheID id)
96         {
97             if (id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
98             {
99                 throw InvalidParameterException {"[cancelResourceCache] CacheID is invaild"};
100             }
101
102             DataCachePtr foundCacheHandler = findDataCache(id);
103             if (foundCacheHandler != nullptr)
104             {
105                 CacheID retID = foundCacheHandler->deleteSubscriber(id);
106                 if (retID == id)
107                 {
108                     cacheIDmap.erase(id);
109                 }
110                 std::lock_guard<std::mutex> lock(s_mutex);
111                 if (foundCacheHandler->isEmptySubscriber())
112                 {
113                     s_cacheDataList->remove(foundCacheHandler);
114                 }
115             }
116         }
117
118         void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
119         {
120             if (pResource == nullptr)
121             {
122                 throw InvalidParameterException
123                 {"[updateResourceCache] Primitive Resource is invaild"};
124             }
125
126             DataCachePtr foundCache = findDataCache(pResource);
127             if (foundCache == nullptr)
128             {
129                 throw InvalidParameterException
130                 {"[updateResourceCache] Primitive Resource is invaild"};
131             }
132             foundCache->requestGet();
133         }
134
135         void ResourceCacheManager::updateResourceCache(CacheID updateId) const
136         {
137             if (updateId == 0)
138             {
139                 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
140             }
141
142             DataCachePtr foundCache = findDataCache(updateId);
143             if (foundCache == nullptr)
144             {
145                 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
146             }
147             foundCache->requestGet();
148         }
149
150         const RCSResourceAttributes ResourceCacheManager::getCachedData(
151             PrimitiveResourcePtr pResource) const
152         {
153             if (pResource == nullptr)
154             {
155                 throw InvalidParameterException {"[getCachedData] Primitive Resource is nullptr"};
156             }
157
158             DataCachePtr handler = findDataCache(pResource);
159             if (handler == nullptr)
160             {
161                 throw InvalidParameterException {"[getCachedData] Primitive Resource is invaild"};
162             }
163
164             if (handler->isCachedData() == false)
165             {
166                 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
167             }
168
169             return handler->getCachedData();
170         }
171
172         const RCSResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
173         {
174             if (id == 0)
175             {
176                 throw InvalidParameterException {"[getCachedData] CacheID is NULL"};
177             }
178
179             DataCachePtr handler = findDataCache(id);
180             if (handler == nullptr)
181             {
182                 throw InvalidParameterException {"[getCachedData] CacheID is invaild"};
183             }
184
185             if (handler->isCachedData() == false)
186             {
187                 throw HasNoCachedDataException {"[getCachedData] Cached Data is not stored"};
188             }
189
190             return handler->getCachedData();
191         }
192
193         CACHE_STATE ResourceCacheManager::getResourceCacheState(
194             PrimitiveResourcePtr pResource) const
195         {
196             if (pResource == nullptr)
197             {
198                 throw InvalidParameterException {"[getResourceCacheState] Primitive Resource is nullptr"};
199             }
200
201             DataCachePtr handler = findDataCache(pResource);
202             if (handler == nullptr)
203             {
204                 return CACHE_STATE::NONE;
205             }
206             return handler->getCacheState();
207         }
208
209         CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
210         {
211             if (id == 0)
212             {
213                 throw InvalidParameterException {"[getResourceCacheState] CacheID is NULL"};
214             }
215
216             DataCachePtr handler = findDataCache(id);
217             if (handler == nullptr)
218             {
219                 return CACHE_STATE::NONE;
220             }
221             return handler->getCacheState();
222         }
223
224         bool ResourceCacheManager::isCachedData(PrimitiveResourcePtr pResource) const
225         {
226             if (pResource == nullptr)
227             {
228                 throw InvalidParameterException {"[isCachedData] Primitive Resource is nullptr"};
229             }
230
231             DataCachePtr handler = findDataCache(pResource);
232             if (handler == nullptr)
233             {
234                 throw InvalidParameterException {"[isCachedData] Primitive Resource is invaild"};
235             }
236             return handler->isCachedData();
237         }
238
239         bool ResourceCacheManager::isCachedData(CacheID id) const
240         {
241             if (id == 0)
242             {
243                 throw InvalidParameterException {"[isCachedData] CacheID is NULL"};
244             }
245
246             DataCachePtr handler = findDataCache(id);
247             if (handler == nullptr)
248             {
249                 throw InvalidParameterException {"[isCachedData] CacheID is invaild"};
250             }
251             return handler->isCachedData();
252         }
253
254         void ResourceCacheManager::initializeResourceCacheManager()
255         {
256             std::lock_guard<std::mutex> lock(s_mutex);
257             if (s_cacheDataList == nullptr)
258             {
259                 s_cacheDataList
260                     = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
261             }
262         }
263
264         DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
265         {
266             DataCachePtr retHandler = nullptr;
267             std::lock_guard<std::mutex> lock(s_mutex);
268             for (auto &i : * s_cacheDataList)
269             {
270                 if (i->getPrimitiveResource()->getUri() == pResource->getUri() &&
271                     i->getPrimitiveResource()->getHost() == pResource->getHost())
272                 {
273                     retHandler = i;
274                     break;
275                 }
276             }
277             return retHandler;
278         }
279
280         DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
281         {
282             DataCachePtr retHandler = nullptr;
283             for (auto it : cacheIDmap)
284             {
285                 if (it.first == id)
286                 {
287                     retHandler = it.second;
288                     break;
289                 }
290             }
291
292             return retHandler;
293         }
294     } // namespace Service
295 } // namespace OIC