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