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