c17491936b279145062873d62ae4134c026c892a
[platform/upstream/iotivity.git] / service / resource-manipulation / modules / 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 ResourceCacheManager * ResourceCacheManager::s_instance = NULL;
24 std::mutex ResourceCacheManager::s_mutexForCreation;
25 std::unique_ptr<std::list<DataCachePtr>> ResourceCacheManager::s_cacheDataList(nullptr);
26
27 ResourceCacheManager::ResourceCacheManager()
28 {
29     // TODO Auto-generated constructor stub
30     if(!s_cacheDataList)
31     {
32         s_cacheDataList = std::unique_ptr<std::list<DataCachePtr>>(new std::list<DataCachePtr>);
33     }
34 }
35
36 ResourceCacheManager::~ResourceCacheManager()
37 {
38     // TODO Auto-generated destructor stub
39     if(s_cacheDataList)
40     {
41         s_cacheDataList->clear();
42     }
43 }
44
45
46 ResourceCacheManager * ResourceCacheManager::getInstance()
47 {
48     if(s_instance == nullptr)
49     {
50         s_mutexForCreation.lock();
51         if(s_instance == nullptr)
52         {
53             s_instance = new ResourceCacheManager();
54         }
55         s_mutexForCreation.unlock();
56     }
57     return s_instance;
58 }
59
60 CacheID ResourceCacheManager::requestResourceCache(
61         PrimitiveResourcePtr pResource, CacheCB func,
62         REPORT_FREQUENCY rf, long reportTime)
63 {
64     CacheID ret = 0;
65
66     if(rf != REPORT_FREQUENCY::NONE)
67     {
68         if(func == NULL)
69         {
70             return ret;
71         }
72         if(!reportTime)
73         {
74             // default setting
75             reportTime = DEFAULT_REPORT_TIME;
76         }
77     }
78
79     DataCachePtr newHandler = findDataCache(pResource);
80     if(newHandler == nullptr)
81     {
82         newHandler = std::make_shared<DataCache>(pResource, func, rf, reportTime);
83         s_cacheDataList->push_back(newHandler);
84     }
85     ret = newHandler->addSubscriber(func, rf, reportTime);
86
87     return ret;
88 }
89
90 OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id)
91 {
92     OCStackResult ret = OC_STACK_ERROR;
93
94     if(id == 0)
95     {
96         return ret;
97     }
98
99     // TODO cancel cache
100     CacheID retID = 0;
101     DataCachePtr deleteCacheHandler = findDataCache(pResource);
102     if(deleteCacheHandler == nullptr)
103     {
104         return ret;
105     }
106     else
107     {
108         retID = deleteCacheHandler->deleteSubscriber(id);
109     }
110
111     if(retID == id)
112     {
113         ret = OC_STACK_OK;
114     }
115
116     return ret;
117 }
118
119 DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
120 {
121     DataCachePtr retHandler = nullptr;
122     for (auto & i : * s_cacheDataList)
123     {
124         if(i->getPrimitiveResource()->getUri() == pResource->getUri() &&
125                 i->getPrimitiveResource()->getHost() == pResource->getHost())
126         {
127             retHandler = i;
128             break;
129         }
130     }
131     return retHandler;
132 }
133
134 DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
135 {
136     DataCachePtr retHandler = nullptr;
137     for (auto & i : * s_cacheDataList)
138     {
139         SubscriberInfoPair pair = i->findSubscriber(id);
140         if(pair.first != 0)
141         {
142             retHandler = i;
143             break;
144         }
145     }
146     return retHandler;
147 }
148
149 OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pResource)
150 {
151     OCStackResult ret = OC_STACK_ERROR;
152
153     // TODO update now (request get)
154
155     return ret;
156 }
157
158 const ResourceAttributes ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource) const
159 {
160     DataCachePtr handler = findDataCache(pResource);
161     if(handler == nullptr)
162     {
163         return ResourceAttributes();
164     }
165     return handler->getCachedData();
166 }
167
168 const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
169 {
170     DataCachePtr handler = findDataCache(id);
171     if(handler == nullptr)
172     {
173         return ResourceAttributes();
174     }
175     return handler->getCachedData();
176 }
177
178 CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource) const
179 {
180     DataCachePtr handler = findDataCache(pResource);
181     if(handler == nullptr)
182     {
183         return CACHE_STATE::NONE;
184     }
185     return handler->getCacheState();
186 }
187 CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
188 {
189     DataCachePtr handler = findDataCache(id);
190     if(handler == nullptr)
191     {
192         return CACHE_STATE::NONE;
193     }
194     return handler->getCacheState();
195 }