1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RCSRemoteResourceObject.h"
23 #include "OCPlatform.h"
25 #include "ResourceBroker.h"
26 #include "ResourceCacheManager.h"
28 #include "ScopeLogger.h"
30 #define TAG PCF("RCSRemoteResourceObject")
34 using namespace OIC::Service;
36 ResourceState convertBrokerState(BROKER_STATE state)
38 SCOPE_LOG_F(DEBUG, TAG);
42 case BROKER_STATE::ALIVE:
43 return ResourceState::ALIVE;
45 case BROKER_STATE::REQUESTED:
46 return ResourceState::REQUESTED;
48 case BROKER_STATE::LOST_SIGNAL:
49 return ResourceState::LOST_SIGNAL;
51 case BROKER_STATE::DESTROYED:
52 return ResourceState::DESTROYED;
54 case BROKER_STATE::NONE:
55 return ResourceState::NONE;
58 return ResourceState::NONE;
61 CacheState convertCacheState(CACHE_STATE state)
63 SCOPE_LOG_F(DEBUG, TAG);
67 case CACHE_STATE::READY:
68 return CacheState::READY;
70 case CACHE_STATE::READY_YET:
71 case CACHE_STATE::UPDATING:
72 return CacheState::UNREADY;
74 case CACHE_STATE::LOST_SIGNAL:
75 return CacheState::LOST_SIGNAL;
77 case CACHE_STATE::DESTROYED:
78 case CACHE_STATE::NONE:
79 return CacheState::NONE;
82 return CacheState::NONE;
85 OCStackResult hostingCallback(BROKER_STATE state,
86 RCSRemoteResourceObject::StateChangedCallback onResourceStateChanged)
88 SCOPE_LOG_F(DEBUG, TAG);
90 onResourceStateChanged(convertBrokerState(state));
94 OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
95 const RCSResourceAttributes& data, int eCode,
96 RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated,
97 std::weak_ptr<RCSRemoteResourceObject> resourcePtr)
99 SCOPE_LOG_F(DEBUG, TAG);
101 std::shared_ptr<RCSRemoteResourceObject> resource = resourcePtr.lock();
104 OIC_LOG(ERROR, TAG, "Resource object is null");
108 //If error code is failure then RE Cache module should
109 //do clean up for caching flags, maps etc.
112 OIC_LOG_V(ERROR, TAG, "Error code: %d", eCode);
115 resource->stopCaching();
119 //Exception will be thrown: stack will return OC_STACK_ERROR
120 //if it already stopped observe. This call is reqired for clearing
122 OIC_LOG(DEBUG, TAG, "Cleared Cache");
126 //Calling application callback
127 onCacheUpdated(data, eCode);
131 void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
132 RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
134 SCOPE_LOG_F(DEBUG, TAG);
136 onRemoteAttributesSet(response.getAttributes(), eCode);
139 void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
140 RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
142 SCOPE_LOG_F(DEBUG, TAG);
144 onRemoteAttributesReceived(response.getAttributes(), eCode);
153 RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
155 m_resourceInterface = std::move(resourceInterface);
159 RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
161 m_resourceType = std::move(resourceType);
165 RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
167 m_map[std::move(key)] = std::move(value);
171 std::string RCSQueryParams::getResourceInterface() const
173 return m_resourceInterface;
176 std::string RCSQueryParams::getResourceType() const
178 return m_resourceType;
181 std::string RCSQueryParams::get(const std::string& key) const
185 return m_map.at(key);
187 catch (const std::out_of_range&)
189 throw RCSInvalidKeyException(key + " is an invalid key");
193 const RCSQueryParams::Map& RCSQueryParams::getAll() const
198 RCSRemoteResourceObject::RCSRemoteResourceObject(
199 std::shared_ptr< PrimitiveResource > primtiveResource) :
200 m_primitiveResource{ primtiveResource },
206 RCSRemoteResourceObject::~RCSRemoteResourceObject()
208 SCOPE_LOG_F(DEBUG, TAG);
214 catch(std::exception &e){
215 OIC_LOG_V(ERROR, TAG, "%s", e.what());
219 std::weak_ptr< RCSRemoteResourceObject > RCSRemoteResourceObject::weakFromThis()
221 return shared_from_this();
224 RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
225 std::shared_ptr< OC::OCResource > ocResource)
229 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
232 return std::make_shared< RCSRemoteResourceObject >(
233 PrimitiveResource::create(ocResource));
236 std::shared_ptr< OC::OCResource > RCSRemoteResourceObject::toOCResource(
237 RCSRemoteResourceObject::Ptr rcsResource)
241 throw RCSInvalidParameterException("the rcs resource must not be nullptr.");
244 OC::OCResource::Ptr ocResource = OC::OCPlatform::constructResourceObject(rcsResource->getAddress(),
245 rcsResource->getUri(),
246 rcsResource->m_primitiveResource->getConnectivityType(),
247 rcsResource->isObservable(),
248 rcsResource->getTypes(),
249 rcsResource->getInterfaces());
254 bool RCSRemoteResourceObject::isMonitoring() const
256 return m_brokerId != 0;
259 bool RCSRemoteResourceObject::isCaching() const
261 return m_cacheId != 0;
264 bool RCSRemoteResourceObject::isObservable() const
266 return m_primitiveResource->isObservable();
269 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
271 SCOPE_LOG_F(DEBUG, TAG);
275 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
280 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
281 throw RCSBadRequestException{ "Monitoring already started." };
284 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
285 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
288 void RCSRemoteResourceObject::stopMonitoring()
290 SCOPE_LOG_F(DEBUG, TAG);
294 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
298 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
302 ResourceState RCSRemoteResourceObject::getState() const
304 SCOPE_LOG_F(DEBUG, TAG);
308 return ResourceState::NONE;
311 return convertBrokerState(
312 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
315 void RCSRemoteResourceObject::startCaching()
320 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb,
321 CacheMode mode, CacheReport reportType)
323 SCOPE_LOG_F(DEBUG, TAG);
327 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
328 throw RCSBadRequestException{ "Caching already started." };
331 REPORT_FREQUENCY freq;
332 if (reportType == CacheReport::REPORT_CHANGES)
334 freq = REPORT_FREQUENCY::UPTODATE;
336 else if(reportType == CacheReport::REPORT_ALL)
338 freq = REPORT_FREQUENCY::WHENEVER_NOTIFIED;
341 if (mode == CacheMode::OBSERVE_ONLY)
343 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
345 std::bind(cachingCallback, std::placeholders::_1,
346 std::placeholders::_2, std::placeholders::_3,
347 std::move(cb), weakFromThis()), CACHE_METHOD::OBSERVE_ONLY,
352 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
354 std::bind(cachingCallback, std::placeholders::_1,
355 std::placeholders::_2, std::placeholders::_3,
356 std::move(cb), weakFromThis()), CACHE_METHOD::ITERATED_GET,
361 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
362 m_primitiveResource, { }, CACHE_METHOD::ITERATED_GET,
363 REPORT_FREQUENCY::NONE, 0);
366 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
369 void RCSRemoteResourceObject::stopCaching()
371 SCOPE_LOG_F(DEBUG, TAG);
375 OIC_LOG(DEBUG, TAG, "Caching already terminated");
381 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
383 catch (const RCSInvalidParameterException &)
395 CacheState RCSRemoteResourceObject::getCacheState() const
397 SCOPE_LOG_F(DEBUG, TAG);
401 return CacheState::NONE;
404 return convertCacheState(
405 ResourceCacheManager::getInstance()->getResourceCacheState(m_cacheId));
408 bool RCSRemoteResourceObject::isCachedAvailable() const
415 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
418 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
420 SCOPE_LOG_F(DEBUG, TAG);
424 throw RCSBadRequestException{ "Caching not started." };
427 if (!isCachedAvailable())
429 throw RCSBadRequestException{ "Cache data is not available." };
432 return ResourceCacheManager::getInstance()->getCachedData(m_cacheId);
435 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
436 const std::string& key) const
438 SCOPE_LOG_F(DEBUG, TAG);
440 return getCachedAttributes().at(key);
443 std::string RCSRemoteResourceObject::getUri() const
445 return m_primitiveResource->getUri();
448 std::string RCSRemoteResourceObject::getAddress() const
450 return m_primitiveResource->getHost();
453 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
455 return m_primitiveResource->getTypes();
458 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
460 return m_primitiveResource->getInterfaces();
463 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
465 SCOPE_LOG_F(DEBUG, TAG);
469 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
472 m_primitiveResource->requestGet(
473 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
474 std::placeholders::_3, std::move(cb)));
477 void RCSRemoteResourceObject::get(GetCallback cb)
479 SCOPE_LOG_F(DEBUG, TAG);
483 throw RCSInvalidParameterException{ "get : Callback is empty" };
486 m_primitiveResource->requestGet(std::move(cb));
489 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
491 SCOPE_LOG_F(DEBUG, TAG);
495 throw RCSInvalidParameterException{ "get : Callback is empty" };
498 const auto& paramMap = queryParams.getAll();
500 m_primitiveResource->requestGetWith(
501 queryParams.getResourceType(), queryParams.getResourceInterface(),
502 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
506 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
507 RemoteAttributesSetCallback cb)
509 SCOPE_LOG_F(DEBUG, TAG);
513 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
516 m_primitiveResource->requestSet(attribute,
517 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
518 std::placeholders::_3, cb));
521 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
523 SCOPE_LOG_F(DEBUG, TAG);
527 throw RCSInvalidParameterException{ "set : Callback is empty" };
530 m_primitiveResource->requestSet(attributes, std::move(cb));
533 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
534 const RCSResourceAttributes& attributes, SetCallback cb)
536 SCOPE_LOG_F(DEBUG, TAG);
540 throw RCSInvalidParameterException{ "set : Callback is empty" };
543 const auto& paramMap = queryParams.getAll();
545 m_primitiveResource->requestSetWith(
546 queryParams.getResourceType(), queryParams.getResourceInterface(),
547 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
551 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
552 const RCSRepresentation& rep, SetCallback cb)
554 SCOPE_LOG_F(DEBUG, TAG);
558 throw RCSInvalidParameterException{ "set : Callback is empty" };
561 const auto& paramMap = queryParams.getAll();
563 m_primitiveResource->requestSetWith(
564 queryParams.getResourceType(), queryParams.getResourceInterface(),
565 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, rep,