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 "RemoteResourceUnit.h"
22 #include "InternalTypes.h"
26 using namespace OIC::Service;
28 RemoteResourceUnit::RemoteResourceUnit()
30 pStateChangedCB = std::bind(&RemoteResourceUnit::stateChangedCB, this,
31 std::placeholders::_1);
32 pCacheUpdateCB = std::bind(&RemoteResourceUnit::cacheUpdateCB, this,
33 std::placeholders::_1, std::placeholders::_2);
36 RemoteResourceUnit::~RemoteResourceUnit()
40 if(remoteObject->isCaching())
44 remoteObject->stopCaching();
46 catch(std::exception &e)
48 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
51 if(remoteObject->isMonitoring())
55 remoteObject->stopMonitoring();
57 catch(std::exception &e)
59 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
65 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo(
66 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB)
68 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
69 retRemoteResourceUnit->remoteObject = ptr;
70 retRemoteResourceUnit->pUpdatedCB = updatedCB;
72 return retRemoteResourceUnit;
75 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB(
76 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
77 RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
79 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
80 retRemoteResourceUnit->remoteObject = ptr;
81 retRemoteResourceUnit->pUpdatedCB = updatedCB;
83 retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
85 return retRemoteResourceUnit;
88 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
93 std::string RemoteResourceUnit::getRemoteResourceUri() const
95 return remoteObject->getUri();
98 void RemoteResourceUnit::startCaching() const
100 remoteObject->startCaching(pCacheUpdateCB);
103 void RemoteResourceUnit::startMonitoring() const
105 remoteObject->startMonitoring(pStateChangedCB);
108 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
110 std::lock_guard<std::mutex> lock(m_mutex);
111 if(changedState == ResourceState::LOST_SIGNAL ||
112 changedState == ResourceState::DESTROYED)
114 pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
118 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt, int) const
120 std::lock_guard<std::mutex> lock(m_mutex);
122 pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);