Imported Upstream version 1.1.0
[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 setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
103             RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
104     {
105         SCOPE_LOG_F(DEBUG, TAG);
106
107         onRemoteAttributesSet(response.getAttributes(), eCode);
108     }
109
110     void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
111             RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
112     {
113         SCOPE_LOG_F(DEBUG, TAG);
114
115         onRemoteAttributesReceived(response.getAttributes(), eCode);
116     }
117 }
118
119 namespace OIC
120 {
121     namespace Service
122     {
123
124         RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
125         {
126             m_resourceInterface = std::move(resourceInterface);
127             return *this;
128         }
129
130         RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
131         {
132             m_resourceType = std::move(resourceType);
133             return *this;
134         }
135
136         RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
137         {
138             m_map[std::move(key)] = std::move(value);
139             return *this;
140         }
141
142         std::string RCSQueryParams::getResourceInterface() const
143         {
144             return m_resourceInterface;
145         }
146
147         std::string RCSQueryParams::getResourceType() const
148         {
149             return m_resourceType;
150         }
151
152         std::string RCSQueryParams::get(const std::string& key) const
153         {
154             try
155             {
156                 return m_map.at(key);
157             }
158             catch (const std::out_of_range&)
159             {
160                 throw RCSInvalidKeyException(key + " is an invalid key");
161             }
162         }
163
164         const RCSQueryParams::Map& RCSQueryParams::getAll() const
165         {
166             return m_map;
167         }
168
169
170         RCSRemoteResourceObject::RCSRemoteResourceObject(
171                 std::shared_ptr< PrimitiveResource > primtiveResource) :
172                 m_primitiveResource{ primtiveResource },
173                 m_cacheId{ },
174                 m_brokerId{ }
175         {
176         }
177
178         RCSRemoteResourceObject::~RCSRemoteResourceObject()
179         {
180             SCOPE_LOG_F(DEBUG, TAG);
181
182             try{
183                 stopCaching();
184                 stopMonitoring();
185             }
186             catch(std::exception &e){
187                 OIC_LOG_V(ERROR, TAG, "%s", e.what());
188             }
189
190         }
191
192         RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
193                 std::shared_ptr< OC::OCResource > ocResource)
194         {
195             if (!ocResource)
196             {
197                 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
198             }
199
200             return std::make_shared< RCSRemoteResourceObject >(
201                     PrimitiveResource::create(ocResource));
202         }
203
204         bool RCSRemoteResourceObject::isMonitoring() const
205         {
206             return m_brokerId != 0;
207         }
208
209         bool RCSRemoteResourceObject::isCaching() const
210         {
211             return m_cacheId != 0;
212         }
213
214         bool RCSRemoteResourceObject::isObservable() const
215         {
216             return m_primitiveResource->isObservable();
217         }
218
219         void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
220         {
221             SCOPE_LOG_F(DEBUG, TAG);
222
223             if (!cb)
224             {
225                 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
226             }
227
228             if (isMonitoring())
229             {
230                 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
231                 throw RCSBadRequestException{ "Monitoring already started." };
232             }
233
234             m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
235                     std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
236         }
237
238         void RCSRemoteResourceObject::stopMonitoring()
239         {
240             SCOPE_LOG_F(DEBUG, TAG);
241
242             if (!isMonitoring())
243             {
244                 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
245                 return;
246             }
247
248             ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
249             m_brokerId = 0;
250         }
251
252         ResourceState RCSRemoteResourceObject::getState() const
253         {
254             SCOPE_LOG_F(DEBUG, TAG);
255
256             if (!isMonitoring())
257             {
258                 return ResourceState::NONE;
259             }
260
261             return convertBrokerState(
262                     ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
263         }
264
265         void RCSRemoteResourceObject::startCaching()
266         {
267             startCaching({ });
268         }
269
270         void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
271         {
272             SCOPE_LOG_F(DEBUG, TAG);
273
274             if (isCaching())
275             {
276                 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
277                 throw RCSBadRequestException{ "Caching already started." };
278             }
279
280             if (cb)
281             {
282                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
283                         m_primitiveResource,
284                         std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
285                                 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
286             }
287             else
288             {
289                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
290                         m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
291             }
292
293             OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
294         }
295
296         void RCSRemoteResourceObject::stopCaching()
297         {
298             SCOPE_LOG_F(DEBUG, TAG);
299
300             if (!isCaching())
301             {
302                 OIC_LOG(DEBUG, TAG, "Caching already terminated");
303                 return;
304             }
305
306             ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
307             m_cacheId = 0;
308         }
309
310         CacheState RCSRemoteResourceObject::getCacheState() const
311         {
312             SCOPE_LOG_F(DEBUG, TAG);
313
314             if (!isCaching())
315             {
316                 return CacheState::NONE;
317             }
318
319             return convertCacheState(
320                     ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
321         }
322
323         bool RCSRemoteResourceObject::isCachedAvailable() const
324         {
325             if (!isCaching())
326             {
327                 return false;
328             }
329
330             return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
331         }
332
333         RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
334         {
335             SCOPE_LOG_F(DEBUG, TAG);
336
337             if (!isCaching())
338             {
339                 throw RCSBadRequestException{ "Caching not started." };
340             }
341
342             if (!isCachedAvailable())
343             {
344                 throw RCSBadRequestException{ "Cache data is not available." };
345             }
346
347             return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
348         }
349
350         RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
351                 const std::string& key) const
352         {
353             SCOPE_LOG_F(DEBUG, TAG);
354
355             return getCachedAttributes().at(key);
356         }
357
358         std::string RCSRemoteResourceObject::getUri() const
359         {
360             return m_primitiveResource->getUri();
361         }
362
363         std::string RCSRemoteResourceObject::getAddress() const
364         {
365             return m_primitiveResource->getHost();
366         }
367
368         std::vector< std::string > RCSRemoteResourceObject::getTypes() const
369         {
370             return m_primitiveResource->getTypes();
371         }
372
373         std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
374         {
375             return m_primitiveResource->getInterfaces();
376         }
377
378         void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
379         {
380             SCOPE_LOG_F(DEBUG, TAG);
381
382             if (!cb)
383             {
384                 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
385             }
386
387             m_primitiveResource->requestGet(
388                     std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
389                             std::placeholders::_3, std::move(cb)));
390         }
391
392         void RCSRemoteResourceObject::get(GetCallback cb)
393         {
394             SCOPE_LOG_F(DEBUG, TAG);
395
396             if (!cb)
397             {
398                 throw RCSInvalidParameterException{ "get : Callback is empty" };
399             }
400
401             m_primitiveResource->requestGet(std::move(cb));
402         }
403
404         void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
405         {
406             SCOPE_LOG_F(DEBUG, TAG);
407
408             if (!cb)
409             {
410                 throw RCSInvalidParameterException{ "get : Callback is empty" };
411             }
412
413             const auto& paramMap = queryParams.getAll();
414
415             m_primitiveResource->requestGetWith(
416                     queryParams.getResourceType(), queryParams.getResourceInterface(),
417                     OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
418                     std::move(cb));
419         }
420
421         void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
422                 RemoteAttributesSetCallback cb)
423         {
424             SCOPE_LOG_F(DEBUG, TAG);
425
426             if (!cb)
427             {
428                 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
429             }
430
431             m_primitiveResource->requestSet(attribute,
432                     std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
433                             std::placeholders::_3, cb));
434         }
435
436         void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
437         {
438             SCOPE_LOG_F(DEBUG, TAG);
439
440             if (!cb)
441             {
442                 throw RCSInvalidParameterException{ "set : Callback is empty" };
443             }
444
445             m_primitiveResource->requestSet(attributes, std::move(cb));
446         }
447
448         void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
449                 const RCSResourceAttributes& attributes, SetCallback cb)
450         {
451             SCOPE_LOG_F(DEBUG, TAG);
452
453             if (!cb)
454             {
455                 throw RCSInvalidParameterException{ "set : Callback is empty" };
456             }
457
458             const auto& paramMap = queryParams.getAll();
459
460             m_primitiveResource->requestSetWith(
461                     queryParams.getResourceType(), queryParams.getResourceInterface(),
462                     OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
463                     std::move(cb));
464         }
465
466     }
467 }