Add ResourceCache Sconscript
[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 <memory>
22 #include <cstdlib>
23 #include <functional>
24 #include <map>
25 #include <utility>
26 #include <ctime>
27
28 #include "OCApi.h"
29
30 #include "DataCache.h"
31
32 #include "ResponseStatement.h"
33 #include "ResourceAttributes.h"
34
35 DataCache::DataCache(
36             PrimitiveResourcePtr pResource,
37             CacheCB func,
38             REPORT_FREQUENCY rf,
39             long repeatTime
40             ):sResource(pResource)
41 {
42     subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
43     data = std::make_shared<CachedData>();
44
45     state = CACHE_STATE::READY_YET;
46     updateTime = 0l;
47
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
54     if(pResource->isObservable())
55     {
56         pResource->requestObserve(pObserveCB);
57     }
58     else
59     {
60         // TODO set timer
61     }
62 }
63
64 DataCache::~DataCache()
65 {
66     // TODO Auto-generated destructor stub
67     data.reset();
68
69     // TODO delete all node!!
70     subscriberList->clear();
71     subscriberList.release();
72 }
73
74 CacheID DataCache::addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime)
75 {
76     Report_Info newItem;
77     newItem.rf = rf;
78     newItem.latestReportTime = 0l;
79     newItem.repeatTime = repeatTime;
80
81     srand(time(NULL));
82     newItem.reportID = rand();
83
84     while(1)
85     {
86         if(findSubscriber(newItem.reportID).first == 0 || newItem.reportID == 0)
87         {
88             newItem.reportID = rand();
89         }
90         else
91         {
92             break;
93         }
94     }
95
96     subscriberList->insert(std::make_pair(newItem.reportID, std::make_pair(newItem, func)));
97
98     return newItem.reportID;
99 }
100
101 CacheID DataCache::deleteSubscriber(CacheID id)
102 {
103     CacheID ret = 0;
104
105     SubscriberInfoPair pair = findSubscriber(id);
106     if(pair.first != 0)
107     {
108         ret = pair.first;
109         subscriberList->erase(pair.first);
110     }
111
112     return ret;
113 }
114
115 SubscriberInfoPair DataCache::findSubscriber(CacheID id)
116 {
117     SubscriberInfoPair ret;
118
119     for(auto & i : *subscriberList)
120     {
121         if(i.first == id)
122         {
123             ret = std::make_pair(i.first, std::make_pair((Report_Info)i.second.first, (CacheCB)i.second.second));
124         }
125     }
126
127     return ret;
128 }
129
130 CachedDataPtr DataCache::getCachedData()
131 {
132     if(state != CACHE_STATE::READY)
133     {
134         return nullptr;
135     }
136     return data;
137 }
138
139 std::shared_ptr<PrimitiveResource> DataCache::getPrimitiveResource()
140 {
141     return sResource;
142 }
143
144 void DataCache::onObserve(
145         const HeaderOptions& _hos, const ResponseStatement& _rep, int _result, int _seq)
146 {
147
148     if(_result != OC_STACK_OK)
149     {
150         // TODO handle error
151         return;
152     }
153
154     if(_rep.getAttributes().empty())
155     {
156         return;
157     }
158
159     ResourceAttributes att = _rep.getAttributes();
160
161     // set data
162     data->clear();
163
164
165     for(auto & i : att)
166     {
167         const std::string &key = i.key();
168 //        std::string value = i.value();
169         std::string val;
170         data->insert(CachedData::value_type(key, val));
171     }
172
173     // notify!!
174
175     for(auto & i : * subscriberList)
176     {
177         if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
178         {
179             i.second.second(this->sResource, data);
180         }
181     }
182 }
183
184 void DataCache::onGet(const HeaderOptions& _hos,
185         const ResponseStatement& _rep, int _result)
186 {
187
188 }
189
190 CACHE_STATE DataCache::getCacheState()
191 {
192     return state;
193 }