Merge "Merge branch 'security-CKM' into 'master'"
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / src / DiscoverResourceUnit.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 "RCSRemoteResourceObject.h"
22 #include "DiscoverResourceUnit.h"
23 #include "RCSAddress.h"
24
25 using namespace OIC::Service;
26
27 DiscoverResourceUnit::DiscoverResourceUnit(const std::string &bundleId)
28     : m_bundleId(bundleId)
29 {
30     pUpdatedCB = nullptr;
31     isStartedDiscovery = false;
32
33     pUpdatedCBFromServer = std::bind(&DiscoverResourceUnit::onUpdate, this,
34                                      std::placeholders::_1, std::placeholders::_2);
35 }
36
37 DiscoverResourceUnit::~DiscoverResourceUnit()
38 {
39     pUpdatedCB = nullptr;
40     pUpdatedCBFromServer = nullptr;
41
42     m_vecRemoteResource.clear();
43 }
44
45 void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB)
46 {
47     if (isStartedDiscovery)
48     {
49         // Already start Discovery
50         return;
51     }
52
53     m_Uri = info.resourceUri;
54     m_ResourceType = info.resourceType;
55     m_AttrubuteName = info.attributeName;
56     pUpdatedCB = updatedCB;
57
58     try
59     {
60         // TODO may be will changed active discovery
61         if (m_Uri.empty())
62         {
63             pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1,
64                                       std::string(""));
65         }
66         else
67         {
68             pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1, m_Uri);
69         }
70
71         discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
72                 RCSAddress::multicast(), m_ResourceType, pDiscoveredCB);
73     }
74     catch (InvalidParameterException &e)
75     {
76         // TODO Handle Exception
77         return;
78     }
79
80     isStartedDiscovery = true;
81 }
82
83 void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject, std::string uri)
84 {
85     if (remoteObject && !isAlreadyDiscoveredResource(remoteObject))
86     {
87         RemoteResourceUnit::Ptr newDiscoveredResource =
88             RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer);
89
90         if (uri.empty() || uri.compare(remoteObject->getUri()) == 0)
91         {
92             m_vecRemoteResource.push_back(newDiscoveredResource);
93             newDiscoveredResource->startMonitoring();
94             newDiscoveredResource->startCaching();
95         }
96     }
97     else
98     {
99         // Already Discovered Resource
100     }
101 }
102
103 void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource)
104 {
105     if (msg == REMOTE_MSG::DATA_UPDATED)
106     {
107         if (updatedResource == nullptr)
108         {
109             return;
110         }
111         try
112         {
113             updatedResource->getCachedAttribute(m_AttrubuteName);
114         }
115         catch (InvalidKeyException &e)
116         {
117             // TODO Handle Exception
118             return;
119         }
120         catch (std::exception &e)
121         {
122             return;
123         }
124
125         std::vector<RCSResourceAttributes::Value> retVector
126             = buildInputResourceData(updatedResource);
127         if (!retVector.empty() && pUpdatedCB != nullptr)
128         {
129             pUpdatedCB(m_AttrubuteName, retVector);
130         }
131     }
132     else
133     {
134         // TODO find & delete
135     }
136 }
137
138 std::vector<RCSResourceAttributes::Value> DiscoverResourceUnit::buildInputResourceData(
139     RCSRemoteResourceObject::Ptr updatedResource)
140 {
141     (void)updatedResource;
142     std::vector<RCSResourceAttributes::Value> retVector = {};
143     for (auto iter : m_vecRemoteResource)
144     {
145         if (iter->getRemoteResourceObject()->getCacheState() != CacheState::READY)
146         {
147             continue;
148         }
149
150         try
151         {
152             RCSResourceAttributes::Value value =
153                 iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName);
154             retVector.push_back(value);
155
156         }
157         catch (InvalidKeyException &e)
158         {
159             // TODO Handle Exception
160         }
161     }
162
163     return retVector;
164 }
165
166 bool DiscoverResourceUnit::isAlreadyDiscoveredResource(
167     RCSRemoteResourceObject::Ptr discoveredResource)
168 {
169     for (auto iter : m_vecRemoteResource)
170     {
171         if (discoveredResource->getUri().compare(iter->getRemoteResourceUri()) == 0 &&
172             discoveredResource->getAddress().compare(iter->getRemoteResourceObject()->getAddress()) == 0 )
173         {
174             return true;
175         }
176     }
177     return false;
178 }