Imported Upstream version 1.2.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             {
44                 remoteObject->stopCaching();
45             }
46             catch(std::exception &e)
47             {
48                 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
49             }
50         }
51         if(remoteObject->isMonitoring())
52         {
53             try
54             {
55                 remoteObject->stopMonitoring();
56             }
57             catch(std::exception &e)
58             {
59                 OIC_LOG_V(ERROR, CONTAINER_TAG, "%s", e.what());
60             }
61         }
62     }
63 }
64
65 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfo(
66         RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB)
67 {
68     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
69     retRemoteResourceUnit->remoteObject = ptr;
70     retRemoteResourceUnit->pUpdatedCB = updatedCB;
71
72     return retRemoteResourceUnit;
73 }
74
75 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithStateCB(
76     RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
77     RCSRemoteResourceObject::StateChangedCallback stateCB)
78 {
79     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
80     retRemoteResourceUnit->remoteObject = ptr;
81     retRemoteResourceUnit->pUpdatedCB = updatedCB;
82
83     retRemoteResourceUnit->pStateChangedCB = stateCB;
84
85     return retRemoteResourceUnit;
86 }
87
88 RemoteResourceUnit::Ptr RemoteResourceUnit::createRemoteResourceInfoWithCacheCB(
89     RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
90     RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
91 {
92     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
93     retRemoteResourceUnit->remoteObject = ptr;
94     retRemoteResourceUnit->pUpdatedCB = updatedCB;
95
96     retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
97
98     return retRemoteResourceUnit;
99 }
100
101 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
102 {
103     return remoteObject;
104 }
105
106 std::string RemoteResourceUnit::getRemoteResourceUri() const
107 {
108     return remoteObject->getUri();
109 }
110
111 void RemoteResourceUnit::startCaching() const
112 {
113     remoteObject->startCaching(pCacheUpdateCB);
114 }
115
116 void RemoteResourceUnit::startMonitoring() const
117 {
118     remoteObject->startMonitoring(pStateChangedCB);
119 }
120
121 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
122 {
123     std::lock_guard<std::mutex> lock(m_mutex);
124     if(changedState == ResourceState::LOST_SIGNAL ||
125        changedState == ResourceState::DESTROYED)
126     {
127         pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
128     }
129 }
130
131 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt) const
132 {
133     std::lock_guard<std::mutex> lock(m_mutex);
134     (void)updatedAtt;
135     pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);
136 }