replace : iotivity -> iotivity-sec
[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, std::placeholders::_2);
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::createRemoteResourceInfoWithCacheCB(
76     RCSRemoteResourceObject::Ptr ptr, UpdatedCBFromServer updatedCB,
77     RCSRemoteResourceObject::CacheUpdatedCallback cacheCB)
78 {
79     RemoteResourceUnit::Ptr retRemoteResourceUnit = std::make_shared<RemoteResourceUnit>();
80     retRemoteResourceUnit->remoteObject = ptr;
81     retRemoteResourceUnit->pUpdatedCB = updatedCB;
82
83     retRemoteResourceUnit->pCacheUpdateCB = cacheCB;
84
85     return retRemoteResourceUnit;
86 }
87
88 RCSRemoteResourceObject::Ptr RemoteResourceUnit::getRemoteResourceObject() const
89 {
90     return remoteObject;
91 }
92
93 std::string RemoteResourceUnit::getRemoteResourceUri() const
94 {
95     return remoteObject->getUri();
96 }
97
98 void RemoteResourceUnit::startCaching() const
99 {
100     remoteObject->startCaching(pCacheUpdateCB);
101 }
102
103 void RemoteResourceUnit::startMonitoring() const
104 {
105     remoteObject->startMonitoring(pStateChangedCB);
106 }
107
108 void RemoteResourceUnit::stateChangedCB(ResourceState changedState) const
109 {
110     std::lock_guard<std::mutex> lock(m_mutex);
111     if(changedState == ResourceState::LOST_SIGNAL ||
112        changedState == ResourceState::DESTROYED)
113     {
114         pUpdatedCB(UPDATE_MSG::RESOURCE_DELETED, remoteObject);
115     }
116 }
117
118 void RemoteResourceUnit::cacheUpdateCB(const RCSResourceAttributes & updatedAtt, int) const
119 {
120     std::lock_guard<std::mutex> lock(m_mutex);
121     (void)updatedAtt;
122     pUpdatedCB(UPDATE_MSG::DATA_UPDATED, remoteObject);
123 }