Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSRemoteResourceObject.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 "RCSRemoteResourceObject.h"
22
23 #include "ResourceBroker.h"
24 #include "ResourceCacheManager.h"
25
26 #include "ScopeLogger.h"
27
28 #define TAG PCF("RCSRemoteResourceObject")
29
30 namespace
31 {
32     using namespace OIC::Service;
33
34     ResourceState convertBrokerState(BROKER_STATE state)
35     {
36         SCOPE_LOG_F(DEBUG, TAG);
37
38         switch (state)
39         {
40             case BROKER_STATE::ALIVE:
41                 return ResourceState::ALIVE;
42
43             case BROKER_STATE::REQUESTED:
44                 return ResourceState::REQUESTED;
45
46             case BROKER_STATE::LOST_SIGNAL:
47                 return ResourceState::LOST_SIGNAL;
48
49             case BROKER_STATE::DESTROYED:
50                 return ResourceState::DESTROYED;
51
52             case BROKER_STATE::NONE:
53                 return ResourceState::NONE;
54         }
55
56         return ResourceState::NONE;
57     }
58
59     CacheState convertCacheState(CACHE_STATE state)
60     {
61         SCOPE_LOG_F(DEBUG, TAG);
62
63         switch (state)
64         {
65             case CACHE_STATE::READY:
66                 return CacheState::READY;
67
68             case CACHE_STATE::READY_YET:
69             case CACHE_STATE::UPDATING:
70                 return CacheState::UNREADY;
71
72             case CACHE_STATE::LOST_SIGNAL:
73                 return CacheState::LOST_SIGNAL;
74
75             case CACHE_STATE::DESTROYED:
76             case CACHE_STATE::NONE:
77                 return CacheState::NONE;
78         }
79
80         return CacheState::NONE;
81     }
82
83     OCStackResult hostingCallback(BROKER_STATE state,
84             RCSRemoteResourceObject::StateChangedCallback onResourceStateChanged)
85     {
86         SCOPE_LOG_F(DEBUG, TAG);
87
88         onResourceStateChanged(convertBrokerState(state));
89         return OC_STACK_OK;
90     }
91
92     OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
93             const RCSResourceAttributes& data,
94             RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
95     {
96         SCOPE_LOG_F(DEBUG, TAG);
97
98         onCacheUpdated(data);
99         return OC_STACK_OK;
100     }
101
102     void setCallback(const HeaderOptions&, const ResponseStatement& response, int,
103             RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
104     {
105         SCOPE_LOG_F(DEBUG, TAG);
106
107         onRemoteAttributesSet(response.getAttributes());
108     }
109
110     void getCallback(const HeaderOptions&, const ResponseStatement& response, int,
111             RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
112     {
113         SCOPE_LOG_F(DEBUG, TAG);
114
115         onRemoteAttributesReceived(response.getAttributes());
116     }
117 }
118
119 namespace OIC
120 {
121     namespace Service
122     {
123         RCSRemoteResourceObject::RCSRemoteResourceObject(
124                 std::shared_ptr< PrimitiveResource > pResource) :
125                 m_primitiveResource{ pResource },
126                 m_cacheId{ },
127                 m_brokerId{ }
128         {
129         }
130
131         RCSRemoteResourceObject::~RCSRemoteResourceObject()
132         {
133             SCOPE_LOG_F(DEBUG, TAG);
134
135             stopCaching();
136             stopMonitoring();
137         }
138
139         bool RCSRemoteResourceObject::isMonitoring() const
140         {
141             return m_brokerId != 0;
142         }
143
144         bool RCSRemoteResourceObject::isCaching() const
145         {
146             return m_cacheId != 0;
147         }
148
149         bool RCSRemoteResourceObject::isObservable() const
150         {
151             return m_primitiveResource->isObservable();
152         }
153
154         void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
155         {
156             SCOPE_LOG_F(DEBUG, TAG);
157
158             if (!cb)
159             {
160                 throw InvalidParameterException{ "startMonitoring : Callback is NULL" };
161             }
162
163             if (isMonitoring())
164             {
165                 OC_LOG(DEBUG, TAG, "startMonitoring : already started");
166                 throw BadRequestException{ "Monitoring already started." };
167             }
168
169             m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
170                     std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
171         }
172
173         void RCSRemoteResourceObject::stopMonitoring()
174         {
175             SCOPE_LOG_F(DEBUG, TAG);
176
177             if (!isMonitoring())
178             {
179                 OC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
180                 return;
181             }
182
183             ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
184             m_brokerId = 0;
185         }
186
187         ResourceState RCSRemoteResourceObject::getState() const
188         {
189             SCOPE_LOG_F(DEBUG, TAG);
190
191             if (!isMonitoring())
192             {
193                 return ResourceState::NONE;
194             }
195
196             return convertBrokerState(
197                     ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
198         }
199
200         void RCSRemoteResourceObject::startCaching()
201         {
202             startCaching({ });
203         }
204
205         void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
206         {
207             SCOPE_LOG_F(DEBUG, TAG);
208
209             if (isCaching())
210             {
211                 OC_LOG(DEBUG, TAG, "startCaching : already Started");
212                 throw BadRequestException{ "Caching already started." };
213             }
214
215             if (cb)
216             {
217                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
218                         m_primitiveResource,
219                         std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
220                                 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
221             }
222             else
223             {
224                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
225                         m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
226             }
227
228             OC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
229         }
230
231         void RCSRemoteResourceObject::stopCaching()
232         {
233             SCOPE_LOG_F(DEBUG, TAG);
234
235             if (!isCaching())
236             {
237                 OC_LOG(DEBUG, TAG, "Caching already terminated");
238                 return;
239             }
240
241             ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
242             m_cacheId = 0;
243         }
244
245         CacheState RCSRemoteResourceObject::getCacheState() const
246         {
247             SCOPE_LOG_F(DEBUG, TAG);
248
249             if (!isCaching())
250             {
251                 return CacheState::NONE;
252             }
253
254             return convertCacheState(
255                     ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
256         }
257
258         bool RCSRemoteResourceObject::isCachedAvailable() const
259         {
260             if (!isCaching())
261             {
262                 return false;
263             }
264
265             return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
266         }
267
268         RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
269         {
270             SCOPE_LOG_F(DEBUG, TAG);
271
272             if (!isCaching())
273             {
274                 throw BadRequestException{ "Caching not started." };
275             }
276
277             if (!isCachedAvailable())
278             {
279                 throw BadRequestException{ "Cache data is not available." };
280             }
281
282             return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
283         }
284
285         RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
286                 const std::string& key) const
287         {
288             SCOPE_LOG_F(DEBUG, TAG);
289
290             return getCachedAttributes().at(key);
291         }
292
293         std::string RCSRemoteResourceObject::getUri() const
294         {
295             return m_primitiveResource->getUri();
296         }
297
298         std::string RCSRemoteResourceObject::getAddress() const
299         {
300             return m_primitiveResource->getHost();
301         }
302
303         std::vector< std::string > RCSRemoteResourceObject::getTypes() const
304         {
305             return m_primitiveResource->getTypes();
306         }
307
308         std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
309         {
310             return m_primitiveResource->getInterfaces();
311         }
312
313         void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
314         {
315             SCOPE_LOG_F(DEBUG, TAG);
316
317             if (!cb)
318             {
319                 throw InvalidParameterException{ "getRemoteAttributes : Callback is empty" };
320             }
321
322             m_primitiveResource->requestGet(
323                     std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
324                             std::placeholders::_3, std::move(cb)));
325         }
326
327         void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
328                 RemoteAttributesSetCallback cb)
329         {
330             SCOPE_LOG_F(DEBUG, TAG);
331
332             if (!cb)
333             {
334                 throw InvalidParameterException{ "setRemoteAttributes : Callback is empty" };
335             }
336
337             m_primitiveResource->requestSet(attribute,
338                     std::bind(setCallback, std::placeholders::_1, std::placeholders::_2,
339                             std::placeholders::_3, cb));
340         }
341     }
342 }