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,
96 RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
98 SCOPE_LOG_F(DEBUG, TAG);
100 onCacheUpdated(data);
104 void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
105 RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
107 SCOPE_LOG_F(DEBUG, TAG);
109 onRemoteAttributesSet(response.getAttributes(), eCode);
112 void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
113 RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
115 SCOPE_LOG_F(DEBUG, TAG);
117 onRemoteAttributesReceived(response.getAttributes(), eCode);
126 RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
128 m_resourceInterface = std::move(resourceInterface);
132 RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
134 m_resourceType = std::move(resourceType);
138 RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
140 m_map[std::move(key)] = std::move(value);
144 std::string RCSQueryParams::getResourceInterface() const
146 return m_resourceInterface;
149 std::string RCSQueryParams::getResourceType() const
151 return m_resourceType;
154 std::string RCSQueryParams::get(const std::string& key) const
158 return m_map.at(key);
160 catch (const std::out_of_range&)
162 throw RCSInvalidKeyException(key + " is an invalid key");
166 const RCSQueryParams::Map& RCSQueryParams::getAll() const
171 RCSRemoteResourceObject::RCSRemoteResourceObject(
172 std::shared_ptr< PrimitiveResource > primtiveResource) :
173 m_primitiveResource{ primtiveResource },
179 RCSRemoteResourceObject::~RCSRemoteResourceObject()
181 SCOPE_LOG_F(DEBUG, TAG);
187 catch(std::exception &e){
188 OIC_LOG_V(ERROR, TAG, "%s", e.what());
192 RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
193 std::shared_ptr< OC::OCResource > ocResource)
197 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
200 return std::make_shared< RCSRemoteResourceObject >(
201 PrimitiveResource::create(ocResource));
204 std::shared_ptr< OC::OCResource > RCSRemoteResourceObject::toOCResource(
205 RCSRemoteResourceObject::Ptr rcsResource)
209 throw RCSInvalidParameterException("the rcs resource must not be nullptr.");
212 OC::OCResource::Ptr ocResource = OC::OCPlatform::constructResourceObject(rcsResource->getAddress(),
213 rcsResource->getUri(),
215 rcsResource->isObservable(),
216 rcsResource->getTypes(),
217 rcsResource->getInterfaces());
222 bool RCSRemoteResourceObject::isMonitoring() const
224 return m_brokerId != 0;
227 bool RCSRemoteResourceObject::isCaching() const
229 return m_cacheId != 0;
232 bool RCSRemoteResourceObject::isObservable() const
234 return m_primitiveResource->isObservable();
237 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
239 SCOPE_LOG_F(DEBUG, TAG);
243 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
248 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
249 throw RCSBadRequestException{ "Monitoring already started." };
252 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
253 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
256 void RCSRemoteResourceObject::stopMonitoring()
258 SCOPE_LOG_F(DEBUG, TAG);
262 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
266 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
270 ResourceState RCSRemoteResourceObject::getState() const
272 SCOPE_LOG_F(DEBUG, TAG);
276 return ResourceState::NONE;
279 return convertBrokerState(
280 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
283 void RCSRemoteResourceObject::startCaching()
288 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb, CacheMode mode)
290 SCOPE_LOG_F(DEBUG, TAG);
294 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
295 throw RCSBadRequestException{ "Caching already started." };
298 if (mode == CacheMode::OBSERVE_ONLY)
300 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
302 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
303 std::move(cb)), CACHE_METHOD::OBSERVE_ONLY,
304 REPORT_FREQUENCY::UPTODATE, 0);
309 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
311 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
312 std::move(cb)), CACHE_METHOD::ITERATED_GET,
313 REPORT_FREQUENCY::UPTODATE, 0);
317 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
318 m_primitiveResource, { }, CACHE_METHOD::ITERATED_GET,
319 REPORT_FREQUENCY::NONE, 0);
322 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
325 void RCSRemoteResourceObject::stopCaching()
327 SCOPE_LOG_F(DEBUG, TAG);
331 OIC_LOG(DEBUG, TAG, "Caching already terminated");
335 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
339 CacheState RCSRemoteResourceObject::getCacheState() const
341 SCOPE_LOG_F(DEBUG, TAG);
345 return CacheState::NONE;
348 return convertCacheState(
349 ResourceCacheManager::getInstance()->getResourceCacheState(m_cacheId));
352 bool RCSRemoteResourceObject::isCachedAvailable() const
359 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
362 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
364 SCOPE_LOG_F(DEBUG, TAG);
368 throw RCSBadRequestException{ "Caching not started." };
371 if (!isCachedAvailable())
373 throw RCSBadRequestException{ "Cache data is not available." };
376 return ResourceCacheManager::getInstance()->getCachedData(m_cacheId);
379 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
380 const std::string& key) const
382 SCOPE_LOG_F(DEBUG, TAG);
384 return getCachedAttributes().at(key);
387 std::string RCSRemoteResourceObject::getUri() const
389 return m_primitiveResource->getUri();
392 std::string RCSRemoteResourceObject::getAddress() const
394 return m_primitiveResource->getHost();
397 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
399 return m_primitiveResource->getTypes();
402 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
404 return m_primitiveResource->getInterfaces();
407 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
409 SCOPE_LOG_F(DEBUG, TAG);
413 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
416 m_primitiveResource->requestGet(
417 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
418 std::placeholders::_3, std::move(cb)));
421 void RCSRemoteResourceObject::get(GetCallback cb)
423 SCOPE_LOG_F(DEBUG, TAG);
427 throw RCSInvalidParameterException{ "get : Callback is empty" };
430 m_primitiveResource->requestGet(std::move(cb));
433 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
435 SCOPE_LOG_F(DEBUG, TAG);
439 throw RCSInvalidParameterException{ "get : Callback is empty" };
442 const auto& paramMap = queryParams.getAll();
444 m_primitiveResource->requestGetWith(
445 queryParams.getResourceType(), queryParams.getResourceInterface(),
446 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
450 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
451 RemoteAttributesSetCallback cb)
453 SCOPE_LOG_F(DEBUG, TAG);
457 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
460 m_primitiveResource->requestSet(attribute,
461 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
462 std::placeholders::_3, cb));
465 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
467 SCOPE_LOG_F(DEBUG, TAG);
471 throw RCSInvalidParameterException{ "set : Callback is empty" };
474 m_primitiveResource->requestSet(attributes, std::move(cb));
477 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
478 const RCSResourceAttributes& attributes, SetCallback cb)
480 SCOPE_LOG_F(DEBUG, TAG);
484 throw RCSInvalidParameterException{ "set : Callback is empty" };
487 const auto& paramMap = queryParams.getAll();
489 m_primitiveResource->requestSetWith(
490 queryParams.getResourceType(), queryParams.getResourceInterface(),
491 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,
495 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
496 const RCSRepresentation& rep, SetCallback cb)
498 SCOPE_LOG_F(DEBUG, TAG);
502 throw RCSInvalidParameterException{ "set : Callback is empty" };
505 const auto& paramMap = queryParams.getAll();
507 m_primitiveResource->requestSetWith(
508 queryParams.getResourceType(), queryParams.getResourceInterface(),
509 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, rep,