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