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"
36 PrimitiveResourcePtr pResource,
40 ):sResource(pResource)
42 subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
43 data = std::make_shared<CachedData>();
45 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));
54 if(pResource->isObservable())
56 pResource->requestObserve(pObserveCB);
64 DataCache::~DataCache()
66 // TODO Auto-generated destructor stub
69 // TODO delete all node!!
70 subscriberList->clear();
71 subscriberList.release();
74 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
78 newItem.latestReportTime = 0l;
79 newItem.repeatTime = repeatTime;
82 newItem.reportID = rand();
86 if(findSubscriber(newItem.reportID).first == 0 || newItem.reportID == 0)
88 newItem.reportID = rand();
96 subscriberList->insert(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, (CacheCB)i.second.second));
130 CachedDataPtr DataCache::getCachedData()
132 if(state != CACHE_STATE::READY)
139 std::shared_ptr<PrimitiveResource> DataCache::getPrimitiveResource()
144 void DataCache::onObserve(
145 const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
148 if(_result != OC_STACK_OK)
154 if(_rep.getAttributes().empty())
159 ResourceAttributes att = _rep.getAttributes();
167 const std::string &key = i.key();
168 // std::string value = i.value();
170 data->insert(CachedData::value_type(key, val));
175 for(auto & i : * subscriberList)
177 if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
179 i.second.second(this->sResource, data);
184 void DataCache::onGet(const HeaderOptions& _hos,
185 const ResponseStatement& _rep, int _result)
190 CACHE_STATE DataCache::getCacheState()