706a2a1bea2352c2c37675b8cac25fea1a647859
[platform/upstream/iotivity.git] / service / resource-manipulation / 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::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
30
31         ResourceCacheManager::~ResourceCacheManager()
32         {
33             if(s_cacheDataList != nullptr)
34             {
35                 s_cacheDataList->clear();
36             }
37         }
38
39         ResourceCacheManager * ResourceCacheManager::getInstance()
40         {
41             if(s_instance == nullptr)
42             {
43                 s_mutexForCreation.lock();
44                 if(s_instance == nullptr)
45                 {
46                     s_instance = new ResourceCacheManager();
47                     s_instance->initializeResourceCacheManager();
48                 }
49                 s_mutexForCreation.unlock();
50             }
51             return s_instance;
52         }
53
54         CacheID ResourceCacheManager::requestResourceCache(
55                 PrimitiveResourcePtr pResource, CacheCB func,
56                 REPORT_FREQUENCY rf, long reportTime)
57         {
58             if(pResource == nullptr)
59             {
60                 throw InvalidParameterException{"[requestResourceCache] Primitive Resource is invaild"};
61             }
62
63             CacheID retID = 0;
64
65             if(rf != REPORT_FREQUENCY::NONE)
66             {
67                 if(func == NULL || func == nullptr)
68                 {
69                     throw InvalidParameterException{"[requestResourceCache] CacheCB is invaild"};
70                 }
71                 if(!reportTime)
72                 {
73                     // default setting
74                     reportTime = CACHE_DEFAULT_REPORT_MILLITIME;
75                 }
76             }
77
78             DataCachePtr newHandler = findDataCache(pResource);
79             if(newHandler == nullptr)
80             {
81                 newHandler.reset(new DataCache());
82                 newHandler->initializeDataCache(pResource);
83                 s_cacheDataList->push_back(newHandler);
84             }
85             retID = newHandler->addSubscriber(func, rf, reportTime);
86
87             cacheIDmap.insert(std::make_pair(retID, newHandler));
88
89             return retID;
90         }
91
92         void ResourceCacheManager::cancelResourceCache(CacheID id)
93         {
94             if(id == 0 || cacheIDmap.find(id) == cacheIDmap.end())
95             {
96                 throw InvalidParameterException{"[cancelResourceCache] CacheID is invaild"};
97             }
98
99             DataCachePtr foundCacheHandler = findDataCache(id);
100             if(foundCacheHandler != nullptr)
101             {
102                 CacheID retID = foundCacheHandler->deleteSubscriber(id);
103                 if(retID == id)
104                 {
105                     cacheIDmap.erase(id);
106                 }
107                 if(foundCacheHandler->isEmptySubscriber())
108                 {
109                     s_cacheDataList->remove(foundCacheHandler);
110                     foundCacheHandler.reset();
111                 }
112             }
113         }
114
115         void ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource) const
116         {
117             if(pResource == nullptr)
118             {
119                 throw InvalidParameterException
120                 {"[updateResourceCache] Primitive Resource is invaild"};
121             }
122
123             DataCachePtr foundCache = findDataCache(pResource);
124             if(foundCache == nullptr)
125             {
126                 throw InvalidParameterException
127                 {"[updateResourceCache] Primitive Resource is invaild"};
128             }
129             foundCache->requestGet();
130         }
131         void ResourceCacheManager::updateResourceCache(CacheID updateId) const
132         {
133             if(updateId == 0)
134             {
135                 throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
136             }
137
138             DataCachePtr foundCache = findDataCache(updateId);
139             if(foundCache == nullptr)
140             {
141                 throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
142             }
143             foundCache->requestGet();
144         }
145
146         const ResourceAttributes ResourceCacheManager::getCachedData(
147                 PrimitiveResourcePtr pResource) const
148         {
149             if(pResource == nullptr)
150             {
151                 throw InvalidParameterException{"[getCachedData] Primitive Resource is invaild"};
152             }
153
154             DataCachePtr handler = findDataCache(pResource);
155             if(handler == nullptr)
156             {
157                 throw InvalidParameterException{"[getCachedData] Primitive Resource is invaild"};
158             }
159             return handler->getCachedData();
160         }
161
162         const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
163         {
164             if(id == 0)
165             {
166                 throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
167             }
168
169             DataCachePtr handler = findDataCache(id);
170             if(handler == nullptr)
171             {
172                 throw InvalidParameterException{"[getCachedData] CacheID is invaild"};
173             }
174             return handler->getCachedData();
175         }
176
177         CACHE_STATE ResourceCacheManager::getResourceCacheState(
178                 PrimitiveResourcePtr pResource) const
179         {
180             if(pResource == nullptr)
181             {
182                 throw InvalidParameterException{"[getResourceCacheState] Primitive Resource is invaild"};
183             }
184
185             DataCachePtr handler = findDataCache(pResource);
186             if(handler == nullptr)
187             {
188                 return CACHE_STATE::NONE;
189             }
190             return handler->getCacheState();
191         }
192
193         CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
194         {
195             if(id == 0)
196             {
197                 throw InvalidParameterException{"[getResourceCacheState] CacheID is invaild"};
198             }
199
200             DataCachePtr handler = findDataCache(id);
201             if(handler == nullptr)
202             {
203                 return CACHE_STATE::NONE;
204             }
205             return handler->getCacheState();
206         }
207
208         void ResourceCacheManager::initializeResourceCacheManager()
209         {
210             if(s_cacheDataList == nullptr)
211             {
212                 s_cacheDataList
213                 = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
214             }
215         }
216
217         DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
218         {
219             DataCachePtr retHandler = nullptr;
220             for (auto & i : * s_cacheDataList)
221             {
222                 if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
223                         i->getPrimitiveResource()->getHost() == pResource->getHost())
224                 {
225                     retHandler = i;
226                     break;
227                 }
228             }
229             return retHandler;
230         }
231
232         DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
233         {
234             DataCachePtr retHandler = nullptr;
235             for(auto it : cacheIDmap)
236             {
237                 if(it.first == id)
238                 {
239                     retHandler = it.second;
240                     break;
241                 }
242             }
243
244             return retHandler;
245         }
246     } // namespace Service
247 } // namespace OIC