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;
61 if(subscriberList != nullptr)
63 subscriberList->clear();
64 subscriberList.release();
68 void DataCache::initializeDataCache(PrimitiveResourcePtr pResource)
70 sResource = pResource;
72 sResource->requestGet(pGetCB);
73 if(sResource->isObservable())
75 sResource->requestObserve(pObserveCB);
77 networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
80 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
84 newItem.repeatTime = repeatTime;
87 newItem.reportID = generateCacheID();
89 if(subscriberList != nullptr)
91 subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
94 return newItem.reportID;
97 CacheID DataCache::deleteSubscriber(CacheID id)
101 SubscriberInfoPair pair = findSubscriber(id);
105 subscriberList->erase(pair.first);
111 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
113 SubscriberInfoPair ret;
115 for(auto & i : *subscriberList)
119 ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first,
120 (CacheCB)i.second.second));
128 const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
130 return (sResource!=nullptr)?sResource:nullptr;
133 const ResourceAttributes DataCache::getCachedData() const
135 if(state != CACHE_STATE::READY || attributes.empty())
137 return ResourceAttributes();
139 const ResourceAttributes retAtt = attributes;
143 void DataCache::onObserve(
144 const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
147 if(_result != OC_STACK_OK || _rep.getAttributes().empty())
152 if(state != CACHE_STATE::READY)
154 state = CACHE_STATE::READY;
157 networkTimer.cancelTimer(networkTimeOutHandle);
158 networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
160 notifyObservers(_rep.getAttributes());
163 void DataCache::onGet(const HeaderOptions& _hos,
164 const ResponseStatement& _rep, int _result)
166 if(_result != OC_STACK_OK || _rep.getAttributes().empty())
171 if(state != CACHE_STATE::READY)
173 state = CACHE_STATE::READY;
176 if(!sResource->isObservable())
178 networkTimer.cancelTimer(networkTimeOutHandle);
179 networkTimeOutHandle = networkTimer.requestTimer(DEFAULT_EXPIRED_TIME, pTimerCB);
181 pollingHandle = pollingTimer.requestTimer(DEFAULT_REPORT_TIME, pPollingCB);
184 notifyObservers(_rep.getAttributes());
187 void DataCache::notifyObservers(ResourceAttributes Att)
189 if(attributes == Att)
196 ResourceAttributes retAtt = Att;
197 for(auto & i : * subscriberList)
199 if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
201 i.second.second(this->sResource, retAtt);
206 CACHE_STATE DataCache::getCacheState() const
211 void *DataCache::onTimeOut(unsigned int timerID)
213 state = CACHE_STATE::LOST_SIGNAL;
216 void *DataCache::onPollingOut(const unsigned int timerID)
218 if(sResource != nullptr)
220 sResource->requestGet(pGetCB);
225 CacheID DataCache::generateCacheID()
232 if(findSubscriber(retID).first == 0 && retID != 0)
242 void DataCache::requestGet()
244 state = CACHE_STATE::UPDATING;
245 if(sResource != nullptr)
247 sResource->requestGet(pGetCB);
251 bool DataCache::isEmptySubscriber() const
253 return (subscriberList!=nullptr)?subscriberList->empty():true;
255 } // namespace Service