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;
32 discoveryTask = nullptr;
34 pUpdatedCBFromServer = std::bind(&DiscoverResourceUnit::onUpdate, this,
35 std::placeholders::_1, std::placeholders::_2);
38 DiscoverResourceUnit::~DiscoverResourceUnit()
41 discoveryTask = nullptr;
42 pUpdatedCBFromServer = nullptr;
44 m_vecRemoteResource.clear();
47 void DiscoverResourceUnit::startDiscover(DiscoverResourceInfo info, UpdatedCB updatedCB)
49 if (isStartedDiscovery)
51 // Already start Discovery
55 m_Uri = info.resourceUri;
56 m_ResourceType = info.resourceType;
57 m_AttrubuteName = info.attributeName;
58 pUpdatedCB = updatedCB;
62 // TODO may be will changed active discovery
65 pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1,
70 pDiscoveredCB = std::bind(&DiscoverResourceUnit::discoverdCB, this, std::placeholders::_1, m_Uri);
73 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
74 RCSAddress::multicast(), m_ResourceType, pDiscoveredCB);
76 catch (RCSInvalidParameterException &e)
78 // TODO Handle Exception
82 isStartedDiscovery = true;
85 void DiscoverResourceUnit::discoverdCB(RCSRemoteResourceObject::Ptr remoteObject, std::string uri)
87 if (remoteObject && !isAlreadyDiscoveredResource(remoteObject))
89 RemoteResourceUnit::Ptr newDiscoveredResource =
90 RemoteResourceUnit::createRemoteResourceInfo(remoteObject, pUpdatedCBFromServer);
92 if (uri.empty() || uri.compare(remoteObject->getUri()) == 0)
94 m_vecRemoteResource.push_back(newDiscoveredResource);
95 newDiscoveredResource->startMonitoring();
96 newDiscoveredResource->startCaching();
101 // Already Discovered Resource
105 void DiscoverResourceUnit::onUpdate(REMOTE_MSG msg, RCSRemoteResourceObject::Ptr updatedResource)
107 if (msg == REMOTE_MSG::DATA_UPDATED)
109 if (updatedResource == nullptr)
115 updatedResource->getCachedAttribute(m_AttrubuteName);
117 catch (RCSInvalidKeyException &e)
119 // TODO Handle Exception
122 catch (std::exception &e)
127 std::vector<RCSResourceAttributes::Value> retVector
128 = buildInputResourceData(updatedResource);
129 if (!retVector.empty() && pUpdatedCB != nullptr)
131 pUpdatedCB(m_AttrubuteName, retVector);
136 // TODO find & delete
140 std::vector<RCSResourceAttributes::Value> DiscoverResourceUnit::buildInputResourceData(
141 RCSRemoteResourceObject::Ptr updatedResource)
143 (void)updatedResource;
144 std::vector<RCSResourceAttributes::Value> retVector = {};
145 for (auto iter : m_vecRemoteResource)
147 if (iter->getRemoteResourceObject()->getCacheState() != CacheState::READY)
154 RCSResourceAttributes::Value value =
155 iter->getRemoteResourceObject()->getCachedAttribute(m_AttrubuteName);
156 retVector.push_back(value);
159 catch (RCSInvalidKeyException &e)
161 // TODO Handle Exception
168 bool DiscoverResourceUnit::isAlreadyDiscoveredResource(
169 RCSRemoteResourceObject::Ptr discoveredResource)
171 for (auto iter : m_vecRemoteResource)
173 if (discoveredResource->getUri().compare(iter->getRemoteResourceUri()) == 0 &&
174 discoveredResource->getAddress().compare(iter->getRemoteResourceObject()->getAddress()) == 0 )