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(const std::string& resourceInterface)
126 m_resourceInterface = resourceInterface;
130 RCSQueryParams& RCSQueryParams::setResourceInterface(std::string&& resourceInterface)
132 m_resourceInterface = std::move(resourceInterface);
136 RCSQueryParams& RCSQueryParams::setResuorceType(const std::string& resourceType)
138 m_resourceType = resourceType;
142 RCSQueryParams& RCSQueryParams::setResuorceType(std::string&& resourceType)
144 m_resourceType = std::move(resourceType);
148 RCSQueryParams& RCSQueryParams::put(const std::string& key, const std::string& value)
154 RCSQueryParams& RCSQueryParams::put(std::string&& key, std::string&& value)
156 m_map[std::move(key)] = std::move(value);
160 RCSQueryParams& RCSQueryParams::put(const std::string& key, std::string&& value)
162 m_map[key] = std::move(value);
166 RCSQueryParams& RCSQueryParams::put(std::string&& key, const std::string& value)
168 m_map[std::move(key)] = value;
172 std::string RCSQueryParams::getResourceInterface() const
174 return m_resourceInterface;
177 std::string RCSQueryParams::getResourceType() const
179 return m_resourceType;
182 std::string RCSQueryParams::get(const std::string& key) const
184 return m_map.at(key);
187 const RCSQueryParams::Map& RCSQueryParams::getAll() const
193 RCSRemoteResourceObject::RCSRemoteResourceObject(
194 std::shared_ptr< PrimitiveResource > pResource) :
195 m_primitiveResource{ pResource },
201 RCSRemoteResourceObject::~RCSRemoteResourceObject()
203 SCOPE_LOG_F(DEBUG, TAG);
209 catch(std::exception &e){
210 OIC_LOG_V(ERROR, TAG, "%s", e.what());
215 RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
216 std::shared_ptr< OC::OCResource > ocResource)
220 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
223 return std::make_shared< RCSRemoteResourceObject >(
224 PrimitiveResource::create(ocResource));
227 bool RCSRemoteResourceObject::isMonitoring() const
229 return m_brokerId != 0;
232 bool RCSRemoteResourceObject::isCaching() const
234 return m_cacheId != 0;
237 bool RCSRemoteResourceObject::isObservable() const
239 return m_primitiveResource->isObservable();
242 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
244 SCOPE_LOG_F(DEBUG, TAG);
248 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
253 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
254 throw RCSBadRequestException{ "Monitoring already started." };
257 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
258 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
261 void RCSRemoteResourceObject::stopMonitoring()
263 SCOPE_LOG_F(DEBUG, TAG);
267 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
271 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
275 ResourceState RCSRemoteResourceObject::getState() const
277 SCOPE_LOG_F(DEBUG, TAG);
281 return ResourceState::NONE;
284 return convertBrokerState(
285 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
288 void RCSRemoteResourceObject::startCaching()
293 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
295 SCOPE_LOG_F(DEBUG, TAG);
299 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
300 throw RCSBadRequestException{ "Caching already started." };
305 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
307 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
308 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
312 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
313 m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
316 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
319 void RCSRemoteResourceObject::stopCaching()
321 SCOPE_LOG_F(DEBUG, TAG);
325 OIC_LOG(DEBUG, TAG, "Caching already terminated");
329 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
333 CacheState RCSRemoteResourceObject::getCacheState() const
335 SCOPE_LOG_F(DEBUG, TAG);
339 return CacheState::NONE;
342 return convertCacheState(
343 ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
346 bool RCSRemoteResourceObject::isCachedAvailable() const
353 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
356 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
358 SCOPE_LOG_F(DEBUG, TAG);
362 throw RCSBadRequestException{ "Caching not started." };
365 if (!isCachedAvailable())
367 throw RCSBadRequestException{ "Cache data is not available." };
370 return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
373 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
374 const std::string& key) const
376 SCOPE_LOG_F(DEBUG, TAG);
378 return getCachedAttributes().at(key);
381 std::string RCSRemoteResourceObject::getUri() const
383 return m_primitiveResource->getUri();
386 std::string RCSRemoteResourceObject::getAddress() const
388 return m_primitiveResource->getHost();
391 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
393 return m_primitiveResource->getTypes();
396 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
398 return m_primitiveResource->getInterfaces();
401 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
403 SCOPE_LOG_F(DEBUG, TAG);
407 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
410 m_primitiveResource->requestGet(
411 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
412 std::placeholders::_3, std::move(cb)));
415 void RCSRemoteResourceObject::get(GetCallback cb)
417 SCOPE_LOG_F(DEBUG, TAG);
421 throw RCSInvalidParameterException{ "get : Callback is empty" };
424 m_primitiveResource->requestGet(std::move(cb));
427 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
429 SCOPE_LOG_F(DEBUG, TAG);
433 throw RCSInvalidParameterException{ "get : Callback is empty" };
436 const auto& paramMap = queryParams.getAll();
438 std::cout << queryParams.getResourceInterface() << "??\n";
440 m_primitiveResource->requestGetWith(
441 queryParams.getResourceType(), queryParams.getResourceInterface(),
442 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
446 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
447 RemoteAttributesSetCallback cb)
449 SCOPE_LOG_F(DEBUG, TAG);
453 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
456 m_primitiveResource->requestSet(attribute,
457 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
458 std::placeholders::_3, cb));
461 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
463 SCOPE_LOG_F(DEBUG, TAG);
467 throw RCSInvalidParameterException{ "set : Callback is empty" };
470 m_primitiveResource->requestSet(attributes, std::move(cb));
473 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
474 const RCSResourceAttributes& attributes, SetCallback cb)
476 SCOPE_LOG_F(DEBUG, TAG);
480 throw RCSInvalidParameterException{ "set : Callback is empty" };
483 const auto& paramMap = queryParams.getAll();
485 m_primitiveResource->requestSetWith(
486 queryParams.getResourceType(), queryParams.getResourceInterface(),
487 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,