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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
28 #include "DataCache.h"
30 #include "ResponseStatement.h"
31 #include "RCSResourceAttributes.h"
32 #include "ExpiryTimer.h"
38 DataCache::DataCache()
40 subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
44 state = CACHE_STATE::READY_YET;
45 mode = CACHE_MODE::FREQUENCY;
47 networkTimeOutHandle = 0;
50 pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
51 std::placeholders::_1, std::placeholders::_2,
52 std::placeholders::_3, std::placeholders::_4));
53 pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
54 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
55 pTimerCB = (TimerCB)(std::bind(&DataCache::onTimeOut, this, std::placeholders::_1));
56 pPollingCB = (TimerCB)(std::bind(&DataCache::onPollingOut, this, std::placeholders::_1));
60 DataCache::~DataCache()
62 state = CACHE_STATE::DESTROYED;
64 if (subscriberList != nullptr)
66 subscriberList->clear();
67 subscriberList.release();
70 if(mode == CACHE_MODE::OBSERVE)
74 sResource->cancelObserve();
77 // ignore the exception because data cache was released.
82 void DataCache::initializeDataCache(PrimitiveResourcePtr pResource)
84 sResource = pResource;
86 sResource->requestGet(pGetCB);
87 if (sResource->isObservable())
89 sResource->requestObserve(pObserveCB);
91 networkTimeOutHandle = networkTimer.postTimer(CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
94 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
98 newItem.repeatTime = repeatTime;
101 newItem.reportID = generateCacheID();
103 if (subscriberList != nullptr)
105 subscriberList->insert(
106 std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
109 return newItem.reportID;
112 CacheID DataCache::deleteSubscriber(CacheID id)
116 SubscriberInfoPair pair = findSubscriber(id);
120 subscriberList->erase(pair.first);
126 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
128 SubscriberInfoPair ret;
130 for (auto &i : *subscriberList)
134 ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
135 (CacheCB)i.second.second));
143 const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
145 return (sResource != nullptr) ? sResource : nullptr;
148 const RCSResourceAttributes DataCache::getCachedData() const
150 if (state != CACHE_STATE::READY || attributes.empty())
152 return RCSResourceAttributes();
154 const RCSResourceAttributes retAtt = attributes;
158 void DataCache::onObserve(
159 const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, int _seq)
162 if (_result != OC_STACK_OK || _rep.getAttributes().empty())
167 if (state != CACHE_STATE::READY)
169 state = CACHE_STATE::READY;
172 if (mode != CACHE_MODE::OBSERVE)
174 mode = CACHE_MODE::OBSERVE;
177 networkTimer.cancelTimer(networkTimeOutHandle);
178 networkTimeOutHandle = networkTimer.postTimer(CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
180 notifyObservers(_rep.getAttributes());
183 void DataCache::onGet(const HeaderOptions &_hos,
184 const ResponseStatement &_rep, int _result)
186 if (_result != OC_STACK_OK || _rep.getAttributes().empty())
191 if (state != CACHE_STATE::READY)
193 state = CACHE_STATE::READY;
196 if (!sResource->isObservable())
198 networkTimer.cancelTimer(networkTimeOutHandle);
199 networkTimeOutHandle = networkTimer.postTimer(
200 CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
202 pollingHandle = pollingTimer.postTimer(CACHE_DEFAULT_REPORT_MILLITIME, pPollingCB);
205 notifyObservers(_rep.getAttributes());
208 void DataCache::notifyObservers(RCSResourceAttributes Att)
210 if (attributes == Att)
217 RCSResourceAttributes retAtt = Att;
218 for (auto &i : * subscriberList)
220 if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
222 i.second.second(this->sResource, retAtt);
227 CACHE_STATE DataCache::getCacheState() const
232 void DataCache::onTimeOut(unsigned int timerID)
234 state = CACHE_STATE::LOST_SIGNAL;
237 void DataCache::onPollingOut(const unsigned int timerID)
239 if (sResource != nullptr)
241 mode = CACHE_MODE::FREQUENCY;
242 sResource->requestGet(pGetCB);
247 CacheID DataCache::generateCacheID()
254 if (findSubscriber(retID).first == 0 && retID != 0)
264 void DataCache::requestGet()
266 state = CACHE_STATE::UPDATING;
267 if (sResource != nullptr)
269 sResource->requestGet(pGetCB);
273 bool DataCache::isEmptySubscriber() const
275 return (subscriberList != nullptr) ? subscriberList->empty() : true;
277 } // namespace Service