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);
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::createRemoteResourceInfoWithStateCB(
76 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
77 RCSRemoteResourceObject::StateChangedCallback stateCB)
79 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
80 retRemoteResourceUnit->remoteObject = ptr;
81 retRemoteResourceUnit->pUpdatedCB = updatedCB;
83 retRemoteResourceUnit->pStateChangedCB = stateCB;
85 return retRemoteResourceUnit;
88 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB(
89 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
90 RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
92 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
93 retRemoteResourceUnit->remoteObject = ptr;
94 retRemoteResourceUnit->pUpdatedCB = updatedCB;
96 retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
98 return retRemoteResourceUnit;
101 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
106 std::string RemoteResourceUnit::getRemoteResourceUri() const
108 return remoteObject->getUri();
111 void RemoteResourceUnit::startCaching() const
113 remoteObject->startCaching(pCacheUpdateCB);
116 void RemoteResourceUnit::startMonitoring() const
118 remoteObject->startMonitoring(pStateChangedCB);
121 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
123 std::lock_guard<std::mutex> lock(m_mutex);
124 if(changedState == ResourceState::LOST_SIGNAL ||
125 changedState == ResourceState::DESTROYED)
127 pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
131 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const
133 std::lock_guard<std::mutex> lock(m_mutex);
135 pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);