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;
46 networkTimeOutHandle = 0;
49 pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
50 std::placeholders::_1, std::placeholders::_2,
51 std::placeholders::_3, std::placeholders::_4));
52 pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
53 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
54 pTimerCB = (TimerCB)(std::bind(&DataCache::onTimeOut, this, std::placeholders::_1));
55 pPollingCB = (TimerCB)(std::bind(&DataCache::onPollingOut, this, std::placeholders::_1));
59 DataCache::~DataCache()
61 state = CACHE_STATE::DESTROYED;
63 sResource->cancelObserve();
64 if (subscriberList != nullptr)
66 subscriberList->clear();
67 subscriberList.release();
71 void DataCache::initializeDataCache(PrimitiveResourcePtr pResource)
73 sResource = pResource;
75 sResource->requestGet(pGetCB);
76 if (sResource->isObservable())
78 sResource->requestObserve(pObserveCB);
80 networkTimeOutHandle = networkTimer.postTimer(CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
83 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
87 newItem.repeatTime = repeatTime;
90 newItem.reportID = generateCacheID();
92 if (subscriberList != nullptr)
94 subscriberList->insert(
95 std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
98 return newItem.reportID;
101 CacheID DataCache::deleteSubscriber(CacheID id)
105 SubscriberInfoPair pair = findSubscriber(id);
109 subscriberList->erase(pair.first);
115 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
117 SubscriberInfoPair ret;
119 for (auto &i : *subscriberList)
123 ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
124 (CacheCB)i.second.second));
132 const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
134 return (sResource != nullptr) ? sResource : nullptr;
137 const RCSResourceAttributes DataCache::getCachedData() const
139 if (state != CACHE_STATE::READY || attributes.empty())
141 return RCSResourceAttributes();
143 const RCSResourceAttributes retAtt = attributes;
147 void DataCache::onObserve(
148 const HeaderOptions &_hos, const ResponseStatement &_rep, int _result, int _seq)
151 if (_result != OC_STACK_OK || _rep.getAttributes().empty())
156 if (state != CACHE_STATE::READY)
158 state = CACHE_STATE::READY;
161 networkTimer.cancelTimer(networkTimeOutHandle);
162 networkTimeOutHandle = networkTimer.postTimer(CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
164 notifyObservers(_rep.getAttributes());
167 void DataCache::onGet(const HeaderOptions &_hos,
168 const ResponseStatement &_rep, int _result)
170 if (_result != OC_STACK_OK || _rep.getAttributes().empty())
175 if (state != CACHE_STATE::READY)
177 state = CACHE_STATE::READY;
180 if (!sResource->isObservable())
182 networkTimer.cancelTimer(networkTimeOutHandle);
183 networkTimeOutHandle = networkTimer.postTimer(
184 CACHE_DEFAULT_EXPIRED_MILLITIME, pTimerCB);
186 pollingHandle = pollingTimer.postTimer(CACHE_DEFAULT_REPORT_MILLITIME, pPollingCB);
189 notifyObservers(_rep.getAttributes());
192 void DataCache::notifyObservers(RCSResourceAttributes Att)
194 if (attributes == Att)
201 RCSResourceAttributes retAtt = Att;
202 for (auto &i : * subscriberList)
204 if (i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
206 i.second.second(this->sResource, retAtt);
211 CACHE_STATE DataCache::getCacheState() const
216 void DataCache::onTimeOut(unsigned int timerID)
218 state = CACHE_STATE::LOST_SIGNAL;
221 void DataCache::onPollingOut(const unsigned int timerID)
223 if (sResource != nullptr)
225 sResource->requestGet(pGetCB);
230 CacheID DataCache::generateCacheID()
237 if (findSubscriber(retID).first == 0 && retID != 0)
247 void DataCache::requestGet()
249 state = CACHE_STATE::UPDATING;
250 if (sResource != nullptr)
252 sResource->requestGet(pGetCB);
256 bool DataCache::isEmptySubscriber() const
258 return (subscriberList != nullptr) ? subscriberList->empty() : true;
260 } // namespace Service