Use `URI` all caps in documentation
[iotivity.git] / service / resource-encapsulation / src / resourceClient / RCSDiscoveryManagerImpl.h
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 /**
22  * @file
23  *
24  * This file contains the RCSActiveDiscoveryManagerImpl class which provide APIs to discover the Resource in the network
25  * and discovery requests management.
26  *
27  */
28
29 #ifndef RCSDISCOVERYMANAGER_IMPL_H
30 #define RCSDISCOVERYMANAGER_IMPL_H
31
32 #include <memory>
33 #include <functional>
34 #include <mutex>
35 #include <unordered_map>
36 #include <unordered_set>
37
38 #include "RCSAddress.h"
39 #include "RCSDiscoveryManager.h"
40 #include "ExpiryTimer.h"
41 #include "PrimitiveResource.h"
42
43 namespace OIC
44 {
45     namespace Service
46     {
47         /**
48          * The class contains discovery request information
49          *
50          * @see RCSDiscoveryManager
51          */
52         class DiscoveryRequestInfo
53         {
54             public:
55                 DiscoveryRequestInfo(const RCSAddress&, const std::string&,
56                         const std::vector< std::string >&, DiscoverCallback);
57
58             public:
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;
63
64             private:
65                 RCSAddress m_address;
66                 std::string m_relativeUri;
67                 std::vector< std::string > m_resourceTypes;
68                 std::unordered_set< std::string > m_knownResourceIds;
69                 DiscoverCallback m_discoverCb;
70         };
71
72         /**
73          * The class contains the resource discovery and management requests methods.
74          */
75         class RCSDiscoveryManagerImpl
76         {
77             public:
78
79                 /*
80                  * Typedef for discovery request ID
81                  *
82                  * @note This is generated for each discovery request
83                  */
84                 typedef unsigned int ID;
85                 constexpr static char const* ALL_RESOURCE_TYPE = "";
86
87             public:
88
89                 static RCSDiscoveryManagerImpl* getInstance();
90
91                 /**
92                  * Start discovery of resource
93                  *
94                  * @return DiscoverTask pointer
95                  *
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
100                  *
101                  * @throws InvalidParameterException If cb is empty
102                  *
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
106                  *
107                  * @see RCSAddress
108                  * @see RCSDiscoveryManager
109                  */
110                 RCSDiscoveryManager::DiscoveryTask::Ptr startDiscovery(const RCSAddress& address,
111                         const std::string& relativeURI,
112                         const std::vector< std::string >& resourceTypes,
113                         RCSDiscoveryManager::ResourceDiscoveredCallback cb);
114
115                 void cancel(ID);
116
117             private:
118                 RCSDiscoveryManagerImpl();
119                 ~RCSDiscoveryManagerImpl() = default;
120
121                 void subscribePresenceWithMulticast();
122
123                 /**
124                  * Check duplicated callback and invoke callback when resource is discovered
125                  *
126                  * @param resource     A pointer of discovered resource
127                  * @param discoverID   The ID of discovery request
128                  * @param cb           Callback
129                  *
130                  * @see PrimitiveResource
131                  */
132                 void onResourceFound(std::shared_ptr< PrimitiveResource > resource, ID discoveryId,
133                         const RCSDiscoveryManager::ResourceDiscoveredCallback& cb,
134                         const std::string& uri);
135
136                 /**
137                  * Discover resource on all requests and posting timer when timer is expired
138                  */
139                 void onPolling();
140
141                 /**
142                  * Discover resource on all requests when supporting presence function resource
143                  * enter into network
144                  */
145                 void onPresence(OCStackResult, const unsigned int seq, const std::string& address);
146
147                 /**
148                  * Create unique id
149                  *
150                  * @return Returns the id
151                  */
152                 ID createId() const;
153
154             public:
155                 constexpr static ID INVALID_ID = 0;
156
157             private:
158                 ExpiryTimer m_timer;
159
160                 std::unordered_map< ID, DiscoveryRequestInfo > m_discoveryMap;
161
162                 mutable std::mutex m_mutex;
163         };
164     }
165 }
166 #endif // RCSDISCOVERYMANAGER_IMPL_H