Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / src / RemoteResourceUnit.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "RemoteResourceUnit.h"
22 #include "InternalTypes.h"
23
24 #include <exception>
25
26 using namespace OIC::Service;
27
28 RemoteResourceUnit::RemoteResourceUnit()
29 {
30     pStateChangedCB = std::bind(&RemoteResourceUnit::stateChangedCB, this,
31                         std::placeholders::_1);
32     pCacheUpdateCB = std::bind(&RemoteResourceUnit::cacheUpdateCB, this,
33                     std::placeholders::_1);
34 }
35
36 RemoteResourceUnit::~RemoteResourceUnit()
37 {
38     if (remoteObject)
39     {
40         if(remoteObject->isCaching())
41         {
42             try{
43                 remoteObject->stopCaching();
44             }
45             catch(std::exception &e){
46                 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
47             }
48         }
49         if(remoteObject->isMonitoring())
50         {
51             try{
52                 remoteObject->stopMonitoring();
53             }
54             catch(std::exception &e){
55                 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
56             }
57         }
58     }
59 }
60
61 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo(
62         RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB)
63 {
64     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
65     retRemoteResourceUnit->remoteObject = ptr;
66     retRemoteResourceUnit->pUpdatedCB = updatedCB;
67
68     return retRemoteResourceUnit;
69 }
70
71 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithStateCB(
72     RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
73     RCSRemoteResourceObject::StateChangedCallback stateCB)
74 {
75     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
76     retRemoteResourceUnit->remoteObject = ptr;
77     retRemoteResourceUnit->pUpdatedCB = updatedCB;
78
79     retRemoteResourceUnit->pStateChangedCB = stateCB;
80
81     return retRemoteResourceUnit;
82 }
83
84 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB(
85     RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
86     RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
87 {
88     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
89     retRemoteResourceUnit->remoteObject = ptr;
90     retRemoteResourceUnit->pUpdatedCB = updatedCB;
91
92     retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
93
94     return retRemoteResourceUnit;
95 }
96
97 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
98 {
99     return remoteObject;
100 }
101
102 std::string RemoteResourceUnit::getRemoteResourceUri() const
103 {
104     return remoteObject->getUri();
105 }
106
107 void RemoteResourceUnit::startCaching() const
108 {
109     remoteObject->startCaching(pCacheUpdateCB);
110 }
111
112 void RemoteResourceUnit::startMonitoring() const
113 {
114     remoteObject->startMonitoring(pStateChangedCB);
115 }
116
117 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
118 {
119     std::lock_guard<std::mutex> lock(m_mutex);
120     if(changedState == ResourceState::LOST_SIGNAL ||
121        changedState == ResourceState::DESTROYED)
122     {
123         pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
124     }
125 }
126
127 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const
128 {
129     std::lock_guard<std::mutex> lock(m_mutex);
130     (void)updatedAtt;
131     pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);
132 }