Add API explained comments for RCSDiscoveryManager and RCSDiscoveryManagerImpl
[platform/upstream/iotivity.git] / service / resource-encapsulation / include / 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 <list>
35 #include <mutex>
36 #include <unordered_map>
37 #include <algorithm>
38
39 #include "octypes.h"
40
41 #include "RCSDiscoveryManager.h"
42 #include "RCSAddress.h"
43 #include "ExpiryTimer.h"
44 #include "PrimitiveResource.h"
45 #include "RCSRemoteResourceObject.h"
46
47 namespace OIC
48 {
49     namespace Service
50     {
51         class RCSDiscoveryManager;
52         class PrimitiveResource;
53         class RCSAddress;
54
55         /**
56          * The class contains discovery request information
57          *
58          * @see RCSDiscoveryManager
59          */
60         class DiscoverRequestInfo
61         {
62             public:
63
64                 std::string m_address;
65                 std::string m_relativeUri;
66                 std::string m_resourceType;
67                 std::list<std::string> m_receivedIds;
68                 bool m_isReceivedFindCallback;
69                 DiscoverCallback m_findCB;
70                 RCSDiscoveryManager::ResourceDiscoveredCallback m_discoverCB;
71         };
72
73         /**
74          * The class contains the resource discovery and management requests methods.
75          */
76         class RCSDiscoveryManagerImpl
77         {
78             static unsigned int s_uniqueId;
79
80             public:
81
82                 /*
83                  * Typedef for callback of requesting presence API
84                  *
85                  * @see requestMulticastPresence
86                  */
87                 typedef std::function<void(OCStackResult, const unsigned int,
88                         const std::string&)> PresenceCallback;
89
90                 /*
91                  * Typedef for discovery request ID
92                  *
93                  * @note This is generated for each discovery request
94                  */
95                 typedef unsigned int ID;
96
97                 /*
98                  * Typedef for callback of discoverResource API
99                  */
100                 typedef std::function<void(std::shared_ptr< PrimitiveResource >, ID)> FindCallback;
101
102             public:
103
104                 /*
105                  * @return Returns RCSDiscoveryManagerImpl instance.
106                  */
107                 static RCSDiscoveryManagerImpl* getInstance();
108
109                 DiscoverRequestInfo m_discoveryItem;
110                 std::unordered_map<ID,DiscoverRequestInfo> m_discoveryMap;
111                 PresenceCallback m_presenceCB;
112                 ExpiryTimer::Callback m_pollingCB;
113                 ExpiryTimer m_timer;
114                 ID m_timerHandle;
115
116             private:
117                 static RCSDiscoveryManagerImpl * s_instance;
118                 static std::mutex s_mutexForCreation;
119                 std::mutex m_mutex;
120
121             public:
122
123                 /**
124                  * Starting discovery of resource
125                  *
126                  * @return DiscoverTask pointer
127                  *
128                  * @param address        A RCSAddress object
129                  * @param relativeURI    The relative uri of resource to be searched
130                  * @param resourceType   Resource Type
131                  * @param cb             A callback to obtain discovered resource
132                  *
133                  * @throws InvalidParameterException If cb is empty
134                  *
135                  * @note If relativeURI is empty, will be discovered after be changed into "OC_RSRVD_WELL_KNOWN_URI"
136                  * @note If resourceType is empty, will be discovered all resources in network
137                  *
138                  * @see RCSAddress
139                  * @see RCSDiscoveryManager
140                  */
141                 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> startDiscovery(const RCSAddress& address,
142                         const std::string& relativeURI,const std::string& resourceType,
143                         RCSDiscoveryManager::ResourceDiscoveredCallback cb);
144
145             private:
146
147                 /**
148                  * Requesting presence by multicast
149                  */
150                 void requestMulticastPresence();
151
152                 /**
153                  * Initializing callback of presence and polling
154                  */
155                 void initializedDiscoveryEnvironment();
156
157                 /**
158                  * Checking duplicated callback and invoking callback when resource is discovered
159                  *
160                  * @param resource     A pointer of discovered resource
161                  * @param discoverID   The ID of discovery request
162                  *
163                  * @see PrimitiveResource
164                  */
165                 void findCallback(std::shared_ptr< PrimitiveResource > resource, ID discoverID);
166
167                 /**
168                  * Discovering resource on all requests and posting timer when timer is expired
169                  */
170                 void pollingCallback(unsigned int /*msg*/);
171
172                 /**
173                  * Discovering resource on all requests when supporting presence function resource enter into network
174                  *
175                  * @param ret          Not used in this class
176                  * @param seq          Not used in this class
177                  * @param address      A address of supporting presence function resource
178                  */
179                 void presenceCallback(OCStackResult, const unsigned int,const std::string&);
180
181                 /**
182                  * Checking duplicated callback
183                  *
184                  * @return The callback is duplicated or not
185                  *
186                  * @param resource A pointer of discovered resource
187                  * @param discoverID The ID of discovery request
188                  *
189                  * @see PrimitiveResource
190                  */
191                 bool isDuplicatedCallback(std::shared_ptr<PrimitiveResource> resource, ID discoverID);
192
193                 /**
194                  * Creating unique id
195                  *
196                  * @return Returns the id
197                  */
198                 ID createId();
199
200             private:
201                 RCSDiscoveryManagerImpl();
202                 ~RCSDiscoveryManagerImpl() = default;
203         };
204     }
205 }
206 #endif // RCSDISCOVERYMANAGER_IMPL_H