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