1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RCSRemoteResourceObject.h"
22 #include "DiscoverResourceUnit.h"
23 #include "RCSAddress.h"
25 using namespace OIC::Service;
27 DiscoverResourceUnit::DiscoverResourceUnit(const std::string &bundleId)
28 : m_bundleId(bundleId)
31 isStartedDiscovery = false;
33 pUpdatedCBFromServer = std::bind(&DiscoverResourceUnit::onUpdate, this,
34 std::placeholders::_1, std::placeholders::_2);
37 DiscoverResourceUnit::~DiscoverResourceUnit()
40 pUpdatedCBFromServer = nullptr;
42 m_vecRemoteResource.clear();
45 void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB)
47 if (isStartedDiscovery)
49 // Already start Discovery
53 m_Uri = info.resourceUri;
54 m_ResourceType = info.resourceType;
55 m_AttrubuteName = info.attributeName;
56 pUpdatedCB = updatedCB;
60 // TODO may be will changed active discovery
63 pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1,
68 pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1, m_Uri);
71 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
72 RCSAddress::multicast(), m_ResourceType, pDiscoveredCB);
74 catch (RCSInvalidParameterException &e)
76 // TODO Handle Exception
80 isStartedDiscovery = true;
83 void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject, std::string uri)
85 if (remoteObject && !isAlreadyDiscoveredResource(remoteObject))
87 RemoteResourceUnit::Ptr newDiscoveredResource =
88 RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer);
90 if (uri.empty() || uri.compare(remoteObject->getUri()) == 0)
92 m_vecRemoteResource.push_back(newDiscoveredResource);
93 newDiscoveredResource->startMonitoring();
94 newDiscoveredResource->startCaching();
99 // Already Discovered Resource
103 void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource)
105 if (msg == REMOTE_MSG::DATA_UPDATED)
107 if (updatedResource == nullptr)
113 updatedResource->getCachedAttribute(m_AttrubuteName);
115 catch (RCSInvalidKeyException &e)
117 // TODO Handle Exception
120 catch (std::exception &e)
125 std::vector<RCSResourceAttributes::Value> retVector
126 = buildInputResourceData(updatedResource);
127 if (!retVector.empty() && pUpdatedCB != nullptr)
129 pUpdatedCB(m_AttrubuteName, retVector);
134 // TODO find & delete
138 std::vector<RCSResourceAttributes::Value> DiscoverResourceUnit::buildInputResourceData(
139 RCSRemoteResourceObject::Ptr updatedResource)
141 (void)updatedResource;
142 std::vector<RCSResourceAttributes::Value> retVector = {};
143 for (auto iter : m_vecRemoteResource)
145 if (iter->getRemoteResourceObject()->getCacheState() != CacheState::READY)
152 RCSResourceAttributes::Value value =
153 iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName);
154 retVector.push_back(value);
157 catch (RCSInvalidKeyException &e)
159 // TODO Handle Exception
166 bool DiscoverResourceUnit::isAlreadyDiscoveredResource(
167 RCSRemoteResourceObject::Ptr discoveredResource)
169 for (auto iter : m_vecRemoteResource)
171 if (discoveredResource->getUri().compare(iter->getRemoteResourceUri()) == 0 &&
172 discoveredResource->getAddress().compare(iter->getRemoteResourceObject()->getAddress()) == 0 )