caed5b4d4100f865d5dd3e5edd5690b7bc91b014
[platform/upstream/iotivity.git] / service / basis / resourceCache / src / DataCache.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "CacheHandler.h"
22
23 #include <memory>
24 #include <cstdlib>
25 #include <functional>
26 #include <map>
27 #include <utility>
28 #include <ctime>
29
30 #include "OCApi.h"
31
32 DataCache::DataCache(
33             PrimitiveResource & pResource,
34             CacheCB func,
35             REPORT_FREQUENCY rf,
36             long repeatTime
37             ):sResource(pResource)
38 {
39     subscriberList = new SubscriberInfo();
40     data = new CacheData();
41
42     state = CACHE_STATE::READY_YET;
43     updateTime = 0l;
44
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));
50
51     if(pResource.isObservable())
52     {
53         pResource.requestObserve(pObserveCB);
54     }
55     else
56     {
57         // TODO set timer
58     }
59 }
60
61 DataCache::~DataCache()
62 {
63     // TODO Auto-generated destructor stub
64     data.reset();
65
66     // TODO delete all node!!
67     subscriberList->clear();
68     delete subscriberList;
69 }
70
71 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
72 {
73     Report_Info newItem;
74     newItem.rf = rf;
75     newItem.latestReportTime = 0l;
76     newItem.repeatTime = repeatTime;
77
78     srand(time(NULL));
79     newItem.reportID = rand();
80
81     while(1)
82     {
83         if(findSubscriber(newItem.reportID) != nullptr || newItem.reportID == 0)
84         {
85             newItem.reportID = rand();
86         }
87         else
88         {
89             break;
90         }
91     }
92
93     subscriberList->insert(SubscriberInfoPair(newItem, func));
94
95     return newItem.reportID;
96 }
97
98 CacheID DataCache::deleteSubscriber(CacheID id)
99 {
100     CacheID ret = 0;
101
102     SubscriberInfoPair pair = findSubscriber(id);
103     if(pair != nullptr)
104     {
105         ret = pair.first.reportID;
106         subscriberList->erase(pair.first);
107     }
108
109     return ret;
110 }
111
112 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
113 {
114     SubscriberInfoPair ret = nullptr;
115
116     for(auto i : subscriberList)
117     {
118         if(i->first.reportID == id)
119         {
120             ret = i;
121         }
122     }
123
124     return ret;
125 }
126
127 std::shared_ptr<CacheData> DataCache::getCachedData()
128 {
129     if(state != CACHE_STATE::READY)
130     {
131         return NULL;
132     }
133     return data;
134 }
135
136 PrimitiveResource * DataCache::getPrimitiveResource()
137 {
138     PrimitiveResource ret = NULL;
139     if(sResource)
140     {
141         ret = sResource;
142     }
143
144     return ret;
145 }
146
147 void DataCache::onObserve(
148         const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
149 {
150
151     if(_result != OC_STACK_OK)
152     {
153         // TODO handle error
154         return;
155     }
156     if(_rep.getAttributes().isEmpty())
157     {
158         return;
159     }
160
161     ResourceAttributes att = _rep.getAttributes();
162
163     // set data
164     data->clear();
165     ResourceAttributes::iterator it = att.begin();
166     for(; att.end(); ++it)
167     {
168         std::string key = it->key();
169         // TODO change template or variant
170         std::string val = it->value();
171
172         data[key] = val;
173     }
174
175     // notify!!
176     std::map::iterator mapiter = subscriberList->begin();
177     for(; subscriberList->end(); ++mapiter)
178     {
179         if(mapiter->first().rf == REPORT_FREQUENCY::UPTODATE)
180         {
181             // TODO update report time
182 //            mapiter->first().latestReportTime = now;
183             mapiter->second()(this->sResource, data);
184         }
185     }
186 }