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 RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
210 std::shared_ptr< OC::OCResource > ocResource)
214 throw RCSInvalidParameterException("the oc resource must not be nullptr.");
217 return std::make_shared< RCSRemoteResourceObject >(
218 PrimitiveResource::create(ocResource));
221 bool RCSRemoteResourceObject::isMonitoring() const
223 return m_brokerId != 0;
226 bool RCSRemoteResourceObject::isCaching() const
228 return m_cacheId != 0;
231 bool RCSRemoteResourceObject::isObservable() const
233 return m_primitiveResource->isObservable();
236 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
238 SCOPE_LOG_F(DEBUG, TAG);
242 throw RCSInvalidParameterException{ "startMonitoring : Callback is NULL" };
247 OIC_LOG(DEBUG, TAG, "startMonitoring : already started");
248 throw RCSBadRequestException{ "Monitoring already started." };
251 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
252 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
255 void RCSRemoteResourceObject::stopMonitoring()
257 SCOPE_LOG_F(DEBUG, TAG);
261 OIC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
265 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
269 ResourceState RCSRemoteResourceObject::getState() const
271 SCOPE_LOG_F(DEBUG, TAG);
275 return ResourceState::NONE;
278 return convertBrokerState(
279 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
282 void RCSRemoteResourceObject::startCaching()
287 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
289 SCOPE_LOG_F(DEBUG, TAG);
293 OIC_LOG(DEBUG, TAG, "startCaching : already Started");
294 throw RCSBadRequestException{ "Caching already started." };
299 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
301 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
302 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
306 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
307 m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
310 OIC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
313 void RCSRemoteResourceObject::stopCaching()
315 SCOPE_LOG_F(DEBUG, TAG);
319 OIC_LOG(DEBUG, TAG, "Caching already terminated");
323 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
327 CacheState RCSRemoteResourceObject::getCacheState() const
329 SCOPE_LOG_F(DEBUG, TAG);
333 return CacheState::NONE;
336 return convertCacheState(
337 ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
340 bool RCSRemoteResourceObject::isCachedAvailable() const
347 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
350 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
352 SCOPE_LOG_F(DEBUG, TAG);
356 throw RCSBadRequestException{ "Caching not started." };
359 if (!isCachedAvailable())
361 throw RCSBadRequestException{ "Cache data is not available." };
364 return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
367 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
368 const std::string& key) const
370 SCOPE_LOG_F(DEBUG, TAG);
372 return getCachedAttributes().at(key);
375 std::string RCSRemoteResourceObject::getUri() const
377 return m_primitiveResource->getUri();
380 std::string RCSRemoteResourceObject::getAddress() const
382 return m_primitiveResource->getHost();
385 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
387 return m_primitiveResource->getTypes();
390 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
392 return m_primitiveResource->getInterfaces();
395 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
397 SCOPE_LOG_F(DEBUG, TAG);
401 throw RCSInvalidParameterException{ "getRemoteAttributes : Callback is empty" };
404 m_primitiveResource->requestGet(
405 std::bind(getRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
406 std::placeholders::_3, std::move(cb)));
409 void RCSRemoteResourceObject::get(GetCallback cb)
411 SCOPE_LOG_F(DEBUG, TAG);
415 throw RCSInvalidParameterException{ "get : Callback is empty" };
418 m_primitiveResource->requestGet(std::move(cb));
421 void RCSRemoteResourceObject::get(const RCSQueryParams& queryParams, GetCallback cb)
423 SCOPE_LOG_F(DEBUG, TAG);
427 throw RCSInvalidParameterException{ "get : Callback is empty" };
430 const auto& paramMap = queryParams.getAll();
432 std::cout << queryParams.getResourceInterface() << "??\n";
434 m_primitiveResource->requestGetWith(
435 queryParams.getResourceType(), queryParams.getResourceInterface(),
436 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() },
440 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
441 RemoteAttributesSetCallback cb)
443 SCOPE_LOG_F(DEBUG, TAG);
447 throw RCSInvalidParameterException{ "setRemoteAttributes : Callback is empty" };
450 m_primitiveResource->requestSet(attribute,
451 std::bind(setRemoteAttributesCb, std::placeholders::_1, std::placeholders::_2,
452 std::placeholders::_3, cb));
455 void RCSRemoteResourceObject::set(const RCSResourceAttributes& attributes, SetCallback cb)
457 SCOPE_LOG_F(DEBUG, TAG);
461 throw RCSInvalidParameterException{ "set : Callback is empty" };
464 m_primitiveResource->requestSet(attributes, std::move(cb));
467 void RCSRemoteResourceObject::set(const RCSQueryParams& queryParams,
468 const RCSResourceAttributes& attributes, SetCallback cb)
470 SCOPE_LOG_F(DEBUG, TAG);
474 throw RCSInvalidParameterException{ "set : Callback is empty" };
477 const auto& paramMap = queryParams.getAll();
479 m_primitiveResource->requestSetWith(
480 queryParams.getResourceType(), queryParams.getResourceInterface(),
481 OC::QueryParamsMap{ paramMap.begin(), paramMap.end() }, attributes,