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 "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 setCallback(const HeaderOptions&, const ResponseStatement& response, int,
103 RCSRemoteResourceObject::RemoteAttributesSetCallback onRemoteAttributesSet)
105 SCOPE_LOG_F(DEBUG, TAG);
107 onRemoteAttributesSet(response.getAttributes());
110 void getCallback(const HeaderOptions&, const ResponseStatement& response, int,
111 RCSRemoteResourceObject::RemoteAttributesGetCallback onRemoteAttributesReceived)
113 SCOPE_LOG_F(DEBUG, TAG);
115 onRemoteAttributesReceived(response.getAttributes());
123 RCSRemoteResourceObject::RCSRemoteResourceObject(
124 std::shared_ptr< PrimitiveResource > pResource) :
125 m_primitiveResource{ pResource },
131 RCSRemoteResourceObject::~RCSRemoteResourceObject()
133 SCOPE_LOG_F(DEBUG, TAG);
139 bool RCSRemoteResourceObject::isMonitoring() const
141 return m_brokerId != 0;
144 bool RCSRemoteResourceObject::isCaching() const
146 return m_cacheId != 0;
149 bool RCSRemoteResourceObject::isObservable() const
151 return m_primitiveResource->isObservable();
154 void RCSRemoteResourceObject::startMonitoring(StateChangedCallback cb)
156 SCOPE_LOG_F(DEBUG, TAG);
160 throw InvalidParameterException{ "startMonitoring : Callback is NULL" };
165 OC_LOG(DEBUG, TAG, "startMonitoring : already started");
166 throw BadRequestException{ "Monitoring already started." };
169 m_brokerId = ResourceBroker::getInstance()->hostResource(m_primitiveResource,
170 std::bind(hostingCallback, std::placeholders::_1, std::move(cb)));
173 void RCSRemoteResourceObject::stopMonitoring()
175 SCOPE_LOG_F(DEBUG, TAG);
179 OC_LOG(DEBUG, TAG, "stopMonitoring : Not started");
183 ResourceBroker::getInstance()->cancelHostResource(m_brokerId);
187 ResourceState RCSRemoteResourceObject::getState() const
189 SCOPE_LOG_F(DEBUG, TAG);
193 return ResourceState::NONE;
196 return convertBrokerState(
197 ResourceBroker::getInstance()->getResourceState(m_primitiveResource));
200 void RCSRemoteResourceObject::startCaching()
205 void RCSRemoteResourceObject::startCaching(CacheUpdatedCallback cb)
207 SCOPE_LOG_F(DEBUG, TAG);
211 OC_LOG(DEBUG, TAG, "startCaching : already Started");
212 throw BadRequestException{ "Caching already started." };
217 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
219 std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2,
220 std::move(cb)), REPORT_FREQUENCY::UPTODATE, 0);
224 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
225 m_primitiveResource, { }, REPORT_FREQUENCY::NONE, 0);
228 OC_LOG_V(DEBUG, TAG, "startCaching CACHE ID %d", m_cacheId);
231 void RCSRemoteResourceObject::stopCaching()
233 SCOPE_LOG_F(DEBUG, TAG);
237 OC_LOG(DEBUG, TAG, "Caching already terminated");
241 ResourceCacheManager::getInstance()->cancelResourceCache(m_cacheId);
245 CacheState RCSRemoteResourceObject::getCacheState() const
247 SCOPE_LOG_F(DEBUG, TAG);
251 return CacheState::NONE;
254 return convertCacheState(
255 ResourceCacheManager::getInstance()->getResourceCacheState(m_primitiveResource));
258 bool RCSRemoteResourceObject::isCachedAvailable() const
265 return ResourceCacheManager::getInstance()->isCachedData(m_cacheId);
268 RCSResourceAttributes RCSRemoteResourceObject::getCachedAttributes() const
270 SCOPE_LOG_F(DEBUG, TAG);
274 throw BadRequestException{ "Caching not started." };
277 if (!isCachedAvailable())
279 throw BadRequestException{ "Cache data is not available." };
282 return ResourceCacheManager::getInstance()->getCachedData(m_primitiveResource);
285 RCSResourceAttributes::Value RCSRemoteResourceObject::getCachedAttribute(
286 const std::string& key) const
288 SCOPE_LOG_F(DEBUG, TAG);
290 //check whether key is available or not
291 RCSResourceAttributes cachedAttributes= getCachedAttributes();
292 if(cachedAttributes.contains(key))
294 return getCachedAttributes().at(key);
298 throw BadRequestException{ "Requested Attribute is not present" };
302 std::string RCSRemoteResourceObject::getUri() const
304 return m_primitiveResource->getUri();
307 std::string RCSRemoteResourceObject::getAddress() const
309 return m_primitiveResource->getHost();
312 std::vector< std::string > RCSRemoteResourceObject::getTypes() const
314 return m_primitiveResource->getTypes();
317 std::vector< std::string > RCSRemoteResourceObject::getInterfaces() const
319 return m_primitiveResource->getInterfaces();
322 void RCSRemoteResourceObject::getRemoteAttributes(RemoteAttributesGetCallback cb)
324 SCOPE_LOG_F(DEBUG, TAG);
328 throw InvalidParameterException{ "getRemoteAttributes : Callback is empty" };
331 m_primitiveResource->requestGet(
332 std::bind(getCallback, std::placeholders::_1, std::placeholders::_2,
333 std::placeholders::_3, std::move(cb)));
336 void RCSRemoteResourceObject::setRemoteAttributes(const RCSResourceAttributes& attribute,
337 RemoteAttributesSetCallback cb)
339 SCOPE_LOG_F(DEBUG, TAG);
343 throw InvalidParameterException{ "setRemoteAttributes : Callback is empty" };
346 m_primitiveResource->requestSet(attribute,
347 std::bind(setCallback, std::placeholders::_1, std::placeholders::_2,
348 std::placeholders::_3, cb));