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