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 "ResourceBroker.h"
24 #include "ResourceCacheManager.h"
26 #include "ScopeLogger.h"
28 #define TAG PCF("RCSRemoteResourceObject")
32 using namespace OIC::Service;
34 ResourceState convertBrokerState(BROKER_STATE state)
36 SCOPE_LOG_F(DEBUG, TAG);
40 case BROKER_STATE::ALIVE:
41 return ResourceState::ALIVE;
43 case BROKER_STATE::REQUESTED:
44 return ResourceState::REQUESTED;
46 case BROKER_STATE::LOST_SIGNAL:
47 return ResourceState::LOST_SIGNAL;
49 case BROKER_STATE::DESTROYED:
50 return ResourceState::DESTROYED;
52 case BROKER_STATE::NONE:
53 return ResourceState::NONE;
56 return ResourceState::NONE;
59 CacheState convertCacheState(CACHE_STATE state)
61 SCOPE_LOG_F(DEBUG, TAG);
65 case CACHE_STATE::READY:
66 return CacheState::READY;
68 case CACHE_STATE::READY_YET:
69 case CACHE_STATE::UPDATING:
70 return CacheState::UNREADY;
72 case CACHE_STATE::LOST_SIGNAL:
73 return CacheState::LOST_SIGNAL;
75 case CACHE_STATE::DESTROYED:
76 case CACHE_STATE::NONE:
77 return CacheState::NONE;
80 return CacheState::NONE;
83 OCStackResult hostingCallback(BROKER_STATE state,
84 RCSRemoteResourceObject::StateChangedCallback onResourceStateChanged)
86 SCOPE_LOG_F(DEBUG, TAG);
88 onResourceStateChanged(convertBrokerState(state));
92 OCStackResult cachingCallback(std::shared_ptr< PrimitiveResource >,
93 const RCSResourceAttributes& data,
94 RCSRemoteResourceObject::CacheUpdatedCallback onCacheUpdated)
96 SCOPE_LOG_F(DEBUG, TAG);
102 void setRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
103 RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
105 SCOPE_LOG_F(DEBUG, TAG);
107 onRemoteAttributesSet(response.getAttributes(), eCode);
110 void getRemoteAttributesCb(const HeaderOptions&, const ResponseStatement& response, int eCode,
111 RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
113 SCOPE_LOG_F(DEBUG, TAG);
115 onRemoteAttributesReceived(response.getAttributes(), eCode);
124 RCSQueryParams& RCSQueryParams::setResourceInterface(std::string resourceInterface)
126 m_resourceInterface = std::move(resourceInterface);
130 RCSQueryParams& RCSQueryParams::setResourceType(std::string resourceType)
132 m_resourceType = std::move(resourceType);
136 RCSQueryParams& RCSQueryParams::put(std::string key, std::string value)
138 m_map[std::move(key)] = std::move(value);
142 std::string RCSQueryParams::getResourceInterface() const
144 return m_resourceInterface;
147 std::string RCSQueryParams::getResourceType() const
149 return m_resourceType;
152 std::string RCSQueryParams::get(const std::string& key) const
156 return m_map.at(key);
158 catch (const std::out_of_range&)
160 throw RCSInvalidKeyException(key + " is an invalid key");
164 const RCSQueryParams::Map& RCSQueryParams::getAll() const
170 RCSRemoteResourceObject::RCSRemoteResourceObject(
171 std::shared_ptr< PrimitiveResource > primtiveResource) :
172 m_primitiveResource{ primtiveResource },
178 RCSRemoteResourceObject::~RCSRemoteResourceObject()
180 SCOPE_LOG_F(DEBUG, TAG);
186 catch(std::exception &e){
187 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 bool RCSRemoteResourceObject::isMonitoring() const
206 return m_brokerId != 0;
209 bool RCSRemoteResourceObject::isCaching() const
211 return m_cacheId != 0;
214 bool RCSRemoteResourceObject::isObservable() const
216 return m_primitiveResource->isObservable();
219 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
221 SCOPE_LOG_F(DEBUG, TAG);
225 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
230 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
231 throw RCSBadRequestException{ "Monitoring already started." };
234 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
235 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
238 void RCSRemoteResourceObject::stopMonitoring()
240 SCOPE_LOG_F(DEBUG, TAG);
244 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
248 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
252 ResourceState RCSRemoteResourceObject::getState() const
254 SCOPE_LOG_F(DEBUG, TAG);
258 return ResourceState::NONE;
261 return convertBrokerState(
262 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
265 void RCSRemoteResourceObject::startCaching()
270 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
272 SCOPE_LOG_F(DEBUG, TAG);
276 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
277 throw RCSBadRequestException{ "Caching already started." };
282 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
284 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
285 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
289 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
290 m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
293 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
296 void RCSRemoteResourceObject::stopCaching()
298 SCOPE_LOG_F(DEBUG, TAG);
302 OIC_LOG(DEBUG, TAG, "Caching already terminated");
306 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
310 CacheState RCSRemoteResourceObject::getCacheState() const
312 SCOPE_LOG_F(DEBUG, TAG);
316 return CacheState::NONE;
319 return convertCacheState(
320 ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
323 bool RCSRemoteResourceObject::isCachedAvailable() const
330 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
333 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
335 SCOPE_LOG_F(DEBUG, TAG);
339 throw RCSBadRequestException{ "Caching not started." };
342 if (!isCachedAvailable())
344 throw RCSBadRequestException{ "Cache data is not available." };
347 return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
350 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
351 const std::string& key) const
353 SCOPE_LOG_F(DEBUG, TAG);
355 return getCachedAttributes().at(key);
358 std::string RCSRemoteResourceObject::getUri() const
360 return m_primitiveResource->getUri();
363 std::string RCSRemoteResourceObject::getAddress() const
365 return m_primitiveResource->getHost();
368 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
370 return m_primitiveResource->getTypes();
373 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
375 return m_primitiveResource->getInterfaces();
378 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
380 SCOPE_LOG_F(DEBUG, TAG);
384 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
387 m_primitiveResource->requestGet(
388 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
389 std::placeholders::_3, std::move(cb)));
392 void RCSRemoteResourceObject::get(GetCallback cb)
394 SCOPE_LOG_F(DEBUG, TAG);
398 throw RCSInvalidParameterException{ "get : Callback is empty" };
401 m_primitiveResource->requestGet(std::move(cb));
404 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
406 SCOPE_LOG_F(DEBUG, TAG);
410 throw RCSInvalidParameterException{ "get : Callback is empty" };
413 const auto& paramMap = queryParams.getAll();
415 m_primitiveResource->requestGetWith(
416 queryParams.getResourceType(), queryParams.getResourceInterface(),
417 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
421 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
422 RemoteAttributesSetCallback cb)
424 SCOPE_LOG_F(DEBUG, TAG);
428 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
431 m_primitiveResource->requestSet(attribute,
432 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
433 std::placeholders::_3, cb));
436 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
438 SCOPE_LOG_F(DEBUG, TAG);
442 throw RCSInvalidParameterException{ "set : Callback is empty" };
445 m_primitiveResource->requestSet(attributes, std::move(cb));
448 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
449 const RCSResourceAttributes& attributes, SetCallback cb)
451 SCOPE_LOG_F(DEBUG, TAG);
455 throw RCSInvalidParameterException{ "set : Callback is empty" };
458 const auto& paramMap = queryParams.getAll();
460 m_primitiveResource->requestSetWith(
461 queryParams.getResourceType(), queryParams.getResourceInterface(),
462 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,