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())
43 remoteObject->stopCaching();
45 catch(std::exception &e){
46 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
49 if(remoteObject->isMonitoring())
52 remoteObject->stopMonitoring();
54 catch(std::exception &e){
55 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
61 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo(
62 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB)
64 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
65 retRemoteResourceUnit->remoteObject = ptr;
66 retRemoteResourceUnit->pUpdatedCB = updatedCB;
68 return retRemoteResourceUnit;
71 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithStateCB(
72 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
73 RCSRemoteResourceObject::StateChangedCallback stateCB)
75 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
76 retRemoteResourceUnit->remoteObject = ptr;
77 retRemoteResourceUnit->pUpdatedCB = updatedCB;
79 retRemoteResourceUnit->pStateChangedCB = stateCB;
81 return retRemoteResourceUnit;
84 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB(
85 RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
86 RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
88 RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
89 retRemoteResourceUnit->remoteObject = ptr;
90 retRemoteResourceUnit->pUpdatedCB = updatedCB;
92 retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
94 return retRemoteResourceUnit;
97 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
102 std::string RemoteResourceUnit::getRemoteResourceUri() const
104 return remoteObject->getUri();
107 void RemoteResourceUnit::startCaching() const
109 remoteObject->startCaching(pCacheUpdateCB);
112 void RemoteResourceUnit::startMonitoring() const
114 remoteObject->startMonitoring(pStateChangedCB);
117 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
119 std::lock_guard<std::mutex> lock(m_mutex);
120 if(changedState == ResourceState::LOST_SIGNAL ||
121 changedState == ResourceState::DESTROYED)
123 pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
127 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const
129 std::lock_guard<std::mutex> lock(m_mutex);
131 pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);