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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #include "DataCache.h"
32 #include "ResponseStatement.h"
33 #include "ResourceAttributes.h"
34 #include "ExpiryTimer.h"
40 DataCache::DataCache()
42 subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
46 state = CACHE_STATE::READY_YET;
48 pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
49 std::placeholders::_1, std::placeholders::_2,
50 std::placeholders::_3, std::placeholders::_4));
51 pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
52 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
53 pTimerCB = (TimerCB)(std::bind(&DataCache::onTimeOut, this, std::placeholders::_1));
54 pPollingCB = (TimerCB)(std::bind(&DataCache::onPollingOut, this, std::placeholders::_1));
58 DataCache::~DataCache()
60 state = CACHE_STATE::DESTROYED;
62 sResource->cancelObserve();
63 if(subscriberList != nullptr)
65 subscriberList->clear();
66 subscriberList.release();
70 void DataCache::initializeDataCache(PrimitiveResourcePtr pResource)
72 sResource = pResource;
74 sResource->requestGet(pGetCB);
75 if(sResource->isObservable())
77 sResource->requestObserve(pObserveCB);
79 networkTimeOutHandle = networkTimer.postTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
82 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
86 newItem.repeatTime = repeatTime;
89 newItem.reportID = generateCacheID();
91 if(subscriberList != nullptr)
93 subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
96 return newItem.reportID;
99 CacheID DataCache::deleteSubscriber(CacheID id)
103 SubscriberInfoPair pair = findSubscriber(id);
107 subscriberList->erase(pair.first);
113 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
115 SubscriberInfoPair ret;
117 for(auto & i : *subscriberList)
121 ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
122 (CacheCB)i.second.second));
130 const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
132 return (sResource!=nullptr)?sResource:nullptr;
135 const ResourceAttributes DataCache::getCachedData() const
137 if(state != CACHE_STATE::READY || attributes.empty())
139 return ResourceAttributes();
141 const ResourceAttributes retAtt = attributes;
145 void DataCache::onObserve(
146 const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
149 if(_result != OC_STACK_OK || _rep.getAttributes().empty())
154 if(state != CACHE_STATE::READY)
156 state = CACHE_STATE::READY;
159 networkTimer.cancelTimer(networkTimeOutHandle);
160 networkTimeOutHandle = networkTimer.postTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
162 notifyObservers(_rep.getAttributes());
165 void DataCache::onGet(const HeaderOptions& _hos,
166 const ResponseStatement& _rep, int _result)
168 if(_result != OC_STACK_OK || _rep.getAttributes().empty())
173 if(state != CACHE_STATE::READY)
175 state = CACHE_STATE::READY;
178 if(!sResource->isObservable())
180 networkTimer.cancelTimer(networkTimeOutHandle);
181 networkTimeOutHandle = networkTimer.postTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
183 pollingHandle = pollingTimer.postTimer(DEFAULT_REPORT_TIME, pPollingCB);
186 notifyObservers(_rep.getAttributes());
189 void DataCache::notifyObservers(ResourceAttributes Att)
191 if(attributes == Att)
198 ResourceAttributes retAtt = Att;
199 for(auto & i : * subscriberList)
201 if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
203 i.second.second(this->sResource, retAtt);
208 CACHE_STATE DataCache::getCacheState() const
213 void *DataCache::onTimeOut(unsigned int timerID)
215 state = CACHE_STATE::LOST_SIGNAL;
218 void *DataCache::onPollingOut(const unsigned int timerID)
220 if(sResource != nullptr)
222 sResource->requestGet(pGetCB);
227 CacheID DataCache::generateCacheID()
234 if(findSubscriber(retID).first == 0 && retID != 0)
244 void DataCache::requestGet()
246 state = CACHE_STATE::UPDATING;
247 if(sResource != nullptr)
249 sResource->requestGet(pGetCB);
253 bool DataCache::isEmptySubscriber() const
255 return (subscriberList!=nullptr)?subscriberList->empty():true;
257 } // namespace Service