Added get/set apis with query params in RCSRemoteResourceObject.
[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             stopCaching();
206             stopMonitoring();
207         }
208
209         RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
210                 std::shared_ptr< OC::OCResource > ocResource)
211         {
212             if (!ocResource)
213             {
214                 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
215             }
216
217             return std::make_shared< RCSRemoteResourceObject >(
218                     PrimitiveResource::create(ocResource));
219         }
220
221         bool RCSRemoteResourceObject::isMonitoring() const
222         {
223             return m_brokerId != 0;
224         }
225
226         bool RCSRemoteResourceObject::isCaching() const
227         {
228             return m_cacheId != 0;
229         }
230
231         bool RCSRemoteResourceObject::isObservable() const
232         {
233             return m_primitiveResource->isObservable();
234         }
235
236         void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
237         {
238             SCOPE_LOG_F(DEBUG, TAG);
239
240             if (!cb)
241             {
242                 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
243             }
244
245             if (isMonitoring())
246             {
247                 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
248                 throw RCSBadRequestException{ "Monitoring already started." };
249             }
250
251             m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
252                     std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
253         }
254
255         void RCSRemoteResourceObject::stopMonitoring()
256         {
257             SCOPE_LOG_F(DEBUG, TAG);
258
259             if (!isMonitoring())
260             {
261                 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
262                 return;
263             }
264
265             ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
266             m_brokerId = 0;
267         }
268
269         ResourceState RCSRemoteResourceObject::getState() const
270         {
271             SCOPE_LOG_F(DEBUG, TAG);
272
273             if (!isMonitoring())
274             {
275                 return ResourceState::NONE;
276             }
277
278             return convertBrokerState(
279                     ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
280         }
281
282         void RCSRemoteResourceObject::startCaching()
283         {
284             startCaching({ });
285         }
286
287         void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
288         {
289             SCOPE_LOG_F(DEBUG, TAG);
290
291             if (isCaching())
292             {
293                 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
294                 throw RCSBadRequestException{ "Caching already started." };
295             }
296
297             if (cb)
298             {
299                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
300                         m_primitiveResource,
301                         std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
302                                 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
303             }
304             else
305             {
306                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
307                         m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
308             }
309
310             OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
311         }
312
313         void RCSRemoteResourceObject::stopCaching()
314         {
315             SCOPE_LOG_F(DEBUG, TAG);
316
317             if (!isCaching())
318             {
319                 OIC_LOG(DEBUG, TAG, "Caching already terminated");
320                 return;
321             }
322
323             ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
324             m_cacheId = 0;
325         }
326
327         CacheState RCSRemoteResourceObject::getCacheState() const
328         {
329             SCOPE_LOG_F(DEBUG, TAG);
330
331             if (!isCaching())
332             {
333                 return CacheState::NONE;
334             }
335
336             return convertCacheState(
337                     ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
338         }
339
340         bool RCSRemoteResourceObject::isCachedAvailable() const
341         {
342             if (!isCaching())
343             {
344                 return false;
345             }
346
347             return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
348         }
349
350         RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
351         {
352             SCOPE_LOG_F(DEBUG, TAG);
353
354             if (!isCaching())
355             {
356                 throw RCSBadRequestException{ "Caching not started." };
357             }
358
359             if (!isCachedAvailable())
360             {
361                 throw RCSBadRequestException{ "Cache data is not available." };
362             }
363
364             return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
365         }
366
367         RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
368                 const std::string& key) const
369         {
370             SCOPE_LOG_F(DEBUG, TAG);
371
372             return getCachedAttributes().at(key);
373         }
374
375         std::string RCSRemoteResourceObject::getUri() const
376         {
377             return m_primitiveResource->getUri();
378         }
379
380         std::string RCSRemoteResourceObject::getAddress() const
381         {
382             return m_primitiveResource->getHost();
383         }
384
385         std::vector< std::string > RCSRemoteResourceObject::getTypes() const
386         {
387             return m_primitiveResource->getTypes();
388         }
389
390         std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
391         {
392             return m_primitiveResource->getInterfaces();
393         }
394
395         void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
396         {
397             SCOPE_LOG_F(DEBUG, TAG);
398
399             if (!cb)
400             {
401                 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
402             }
403
404             m_primitiveResource->requestGet(
405                     std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
406                             std::placeholders::_3, std::move(cb)));
407         }
408
409         void RCSRemoteResourceObject::get(GetCallback cb)
410         {
411             SCOPE_LOG_F(DEBUG, TAG);
412
413             if (!cb)
414             {
415                 throw RCSInvalidParameterException{ "get : Callback is empty" };
416             }
417
418             m_primitiveResource->requestGet(std::move(cb));
419         }
420
421         void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
422         {
423             SCOPE_LOG_F(DEBUG, TAG);
424
425             if (!cb)
426             {
427                 throw RCSInvalidParameterException{ "get : Callback is empty" };
428             }
429
430             const auto& paramMap = queryParams.getAll();
431
432             std::cout << queryParams.getResourceInterface() << "??\n";
433
434             m_primitiveResource->requestGetWith(
435                     queryParams.getResourceType(), queryParams.getResourceInterface(),
436                     OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
437                     std::move(cb));
438         }
439
440         void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
441                 RemoteAttributesSetCallback cb)
442         {
443             SCOPE_LOG_F(DEBUG, TAG);
444
445             if (!cb)
446             {
447                 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
448             }
449
450             m_primitiveResource->requestSet(attribute,
451                     std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
452                             std::placeholders::_3, cb));
453         }
454
455         void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
456         {
457             SCOPE_LOG_F(DEBUG, TAG);
458
459             if (!cb)
460             {
461                 throw RCSInvalidParameterException{ "set : Callback is empty" };
462             }
463
464             m_primitiveResource->requestSet(attributes, std::move(cb));
465         }
466
467         void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
468                 const RCSResourceAttributes& attributes, SetCallback cb)
469         {
470             SCOPE_LOG_F(DEBUG, TAG);
471
472             if (!cb)
473             {
474                 throw RCSInvalidParameterException{ "set : Callback is empty" };
475             }
476
477             const auto& paramMap = queryParams.getAll();
478
479             m_primitiveResource->requestSetWith(
480                     queryParams.getResourceType(), queryParams.getResourceInterface(),
481                     OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
482                     std::move(cb));
483         }
484
485     }
486 }