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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the RCSActiveDiscoveryManagerImpl class which provide APIs to discover the Resource in the network
25 * and discovery requests management.
29 #ifndef RCSDISCOVERYMANAGER_IMPL_H
30 #define RCSDISCOVERYMANAGER_IMPL_H
35 #include <unordered_map>
36 #include <unordered_set>
38 #include "RCSAddress.h"
39 #include "RCSDiscoveryManager.h"
40 #include "ExpiryTimer.h"
41 #include "PrimitiveResource.h"
48 * The class contains discovery request information
50 * @see RCSDiscoveryManager
52 class DiscoveryRequestInfo
55 DiscoveryRequestInfo(const RCSAddress&, const std::string&,
56 const std::vector< std::string >&, DiscoverCallback);
59 void discover() const;
60 bool isKnownResource(const std::shared_ptr< PrimitiveResource >&) const;
61 void addKnownResource(const std::shared_ptr< PrimitiveResource >&);
62 bool isMatchedAddress(const std::string&) const;
66 std::string m_relativeUri;
67 std::vector< std::string > m_resourceTypes;
68 std::unordered_set< std::string > m_knownResourceIds;
69 DiscoverCallback m_discoverCb;
73 * The class contains the resource discovery and management requests methods.
75 class RCSDiscoveryManagerImpl
80 * Typedef for discovery request ID
82 * @note This is generated for each discovery request
84 typedef unsigned int ID;
85 constexpr static char const* ALL_RESOURCE_TYPE = "";
89 static RCSDiscoveryManagerImpl* getInstance();
92 * Start discovery of resource
94 * @return DiscoverTask pointer
96 * @param address A RCSAddress object
97 * @param relativeURI The relative uri of resource to be searched
98 * @param resourceType Resource Type
99 * @param cb A callback to obtain discovered resource
101 * @throws InvalidParameterException If cb is empty
103 * @note If relativeURI is empty, will be discovered after be changed into
104 * "OC_RSRVD_WELL_KNOWN_URI"
105 * @note If resourceType is empty, will be discovered all resources in network
108 * @see RCSDiscoveryManager
110 RCSDiscoveryManager::DiscoveryTask::Ptr startDiscovery(const RCSAddress& address,
111 const std::string& relativeURI,
112 const std::vector< std::string >& resourceTypes,
113 RCSDiscoveryManager::ResourceDiscoveredCallback cb);
118 RCSDiscoveryManagerImpl();
119 ~RCSDiscoveryManagerImpl() = default;
121 void subscribePresenceWithMulticast();
124 * Check duplicated callback and invoke callback when resource is discovered
126 * @param resource A pointer of discovered resource
127 * @param discoverID The ID of discovery request
130 * @see PrimitiveResource
132 void onResourceFound(std::shared_ptr< PrimitiveResource > resource, ID discoveryId,
133 const RCSDiscoveryManager::ResourceDiscoveredCallback& cb);
136 * Discover resource on all requests and posting timer when timer is expired
141 * Discover resource on all requests when supporting presence function resource
144 void onPresence(OCStackResult, const unsigned int seq, const std::string& address);
149 * @return Returns the id
154 constexpr static ID INVALID_ID = 0;
159 std::unordered_map< ID, DiscoveryRequestInfo > m_discoveryMap;
161 mutable std::mutex m_mutex;
165 #endif // RCSDISCOVERYMANAGER_IMPL_H