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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "CacheHandler.h"
33 PrimitiveResource & pResource,
37 ):sResource(pResource)
39 subscriberList = new SubscriberInfo();
40 data = new CacheData();
42 state = CACHE_STATE::READY_YET;
45 pObserveCB = (ObserveCB)(std::bind(&DataCache::onObserve, this,
46 std::placeholders::_1, std::placeholders::_2,
47 std::placeholders::_3, std::placeholders::_4));
48 pGetCB = (GetCB)(std::bind(&DataCache::onGet, this,
49 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
51 if(pResource.isObservable())
53 pResource.requestObserve(pObserveCB);
61 DataCache::~DataCache()
63 // TODO Auto-generated destructor stub
66 // TODO delete all node!!
67 subscriberList->clear();
68 delete subscriberList;
71 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
75 newItem.latestReportTime = 0l;
76 newItem.repeatTime = repeatTime;
79 newItem.reportID = rand();
83 if(findSubscriber(newItem.reportID) != nullptr || newItem.reportID == 0)
85 newItem.reportID = rand();
93 subscriberList->insert(SubscriberInfoPair(newItem, func));
95 return newItem.reportID;
98 CacheID DataCache::deleteSubscriber(CacheID id)
102 SubscriberInfoPair pair = findSubscriber(id);
105 ret = pair.first.reportID;
106 subscriberList->erase(pair.first);
112 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
114 SubscriberInfoPair ret = nullptr;
116 for(auto i : subscriberList)
118 if(i->first.reportID == id)
127 std::shared_ptr<CacheData> DataCache::getCachedData()
129 if(state != CACHE_STATE::READY)
136 PrimitiveResource * DataCache::getPrimitiveResource()
138 PrimitiveResource ret = NULL;
147 void DataCache::onObserve(
148 const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
151 if(_result != OC_STACK_OK)
156 if(_rep.getAttributes().isEmpty())
161 ResourceAttributes att = _rep.getAttributes();
165 ResourceAttributes::iterator it = att.begin();
166 for(; att.end(); ++it)
168 std::string key = it->key();
169 // TODO change template or variant
170 std::string val = it->value();
176 std::map::iterator mapiter = subscriberList->begin();
177 for(; subscriberList->end(); ++mapiter)
179 if(mapiter->first().rf == REPORT_FREQUENCY::UPTODATE)
181 // TODO update report time
182 // mapiter->first().latestReportTime = now;
183 mapiter->second()(this->sResource, data);