/src/Fast-DDS/include/fastdds/rtps/RTPSDomain.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /** |
16 | | * @file RTPSDomain.h |
17 | | */ |
18 | | |
19 | | #ifndef _FASTDDS_RTPS_DOMAIN_H_ |
20 | | #define _FASTDDS_RTPS_DOMAIN_H_ |
21 | | |
22 | | #include <atomic> |
23 | | #include <mutex> |
24 | | #include <set> |
25 | | |
26 | | #include <fastdds/rtps/attributes/RTPSParticipantAttributes.h> |
27 | | #include <fastdds/rtps/common/Types.h> |
28 | | #include <fastdds/rtps/history/IPayloadPool.h> |
29 | | #include <fastdds/rtps/history/IChangePool.h> |
30 | | |
31 | | namespace eprosima { |
32 | | namespace fastrtps { |
33 | | namespace rtps { |
34 | | |
35 | | class RTPSParticipantImpl; |
36 | | class RTPSParticipant; |
37 | | class RTPSParticipantListener; |
38 | | class RTPSWriter; |
39 | | class WriterAttributes; |
40 | | class WriterHistory; |
41 | | class WriterListener; |
42 | | class RTPSReader; |
43 | | class ReaderAttributes; |
44 | | class ReaderHistory; |
45 | | class ReaderListener; |
46 | | class RTPSDomainImpl; |
47 | | |
48 | | /** |
49 | | * Class RTPSDomain,it manages the creation and destruction of RTPSParticipant RTPSWriter and RTPSReader. It stores |
50 | | * a list of all created RTPSParticipant. It has only static methods. |
51 | | * @ingroup RTPS_MODULE |
52 | | */ |
53 | | class RTPSDomain |
54 | | { |
55 | | |
56 | | friend class RTPSDomainImpl; |
57 | | |
58 | | public: |
59 | | |
60 | | /** |
61 | | * Method to shut down all RTPSParticipants, readers, writers, etc. |
62 | | * It must be called at the end of the process to avoid memory leaks. |
63 | | * It also shut downs the DomainRTPSParticipant. |
64 | | * |
65 | | * \post After this call, all the pointers to RTPS entities are invalidated and their use may |
66 | | * result in undefined behaviour. |
67 | | */ |
68 | | RTPS_DllAPI static void stopAll(); |
69 | | |
70 | | /** |
71 | | * @brief Create a RTPSParticipant. |
72 | | * @param domain_id DomainId to be used by the RTPSParticipant (80 by default). |
73 | | * @param attrs RTPSParticipant Attributes. |
74 | | * @param plisten Pointer to the ParticipantListener. |
75 | | * @return Pointer to the RTPSParticipant. |
76 | | * |
77 | | * \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(), |
78 | | * so its use may result in undefined behaviour. |
79 | | */ |
80 | | RTPS_DllAPI static RTPSParticipant* createParticipant( |
81 | | uint32_t domain_id, |
82 | | const RTPSParticipantAttributes& attrs, |
83 | | RTPSParticipantListener* plisten = nullptr); |
84 | | |
85 | | /** |
86 | | * @brief Create a RTPSParticipant. |
87 | | * @param domain_id DomainId to be used by the RTPSParticipant (80 by default). |
88 | | * @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable() |
89 | | * @param attrs RTPSParticipant Attributes. |
90 | | * @param plisten Pointer to the ParticipantListener. |
91 | | * @return Pointer to the RTPSParticipant. |
92 | | * |
93 | | * \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(), |
94 | | * so its use may result in undefined behaviour. |
95 | | */ |
96 | | RTPS_DllAPI static RTPSParticipant* createParticipant( |
97 | | uint32_t domain_id, |
98 | | bool enabled, |
99 | | const RTPSParticipantAttributes& attrs, |
100 | | RTPSParticipantListener* plisten = nullptr); |
101 | | |
102 | | /** |
103 | | * Create a RTPSWriter in a participant. |
104 | | * @param p Pointer to the RTPSParticipant. |
105 | | * @param watt Writer Attributes. |
106 | | * @param hist Pointer to the WriterHistory. |
107 | | * @param listen Pointer to the WriterListener. |
108 | | * @return Pointer to the created RTPSWriter. |
109 | | * |
110 | | * \warning The returned pointer is invalidated after a call to removeRTPSWriter() or stopAll(), |
111 | | * so its use may result in undefined behaviour. |
112 | | */ |
113 | | RTPS_DllAPI static RTPSWriter* createRTPSWriter( |
114 | | RTPSParticipant* p, |
115 | | WriterAttributes& watt, |
116 | | WriterHistory* hist, |
117 | | WriterListener* listen = nullptr); |
118 | | |
119 | | /** |
120 | | * Create a RTPSWriter in a participant using a custom payload pool. |
121 | | * @param p Pointer to the RTPSParticipant. |
122 | | * @param watt Writer Attributes. |
123 | | * @param payload_pool Shared pointer to the IPayloadPool |
124 | | * @param hist Pointer to the WriterHistory. |
125 | | * @param listen Pointer to the WriterListener. |
126 | | * @return Pointer to the created RTPSWriter. |
127 | | * |
128 | | * \warning The returned pointer is invalidated after a call to removeRTPSWriter() or stopAll(), |
129 | | * so its use may result in undefined behaviour. |
130 | | */ |
131 | | RTPS_DllAPI static RTPSWriter* createRTPSWriter( |
132 | | RTPSParticipant* p, |
133 | | WriterAttributes& watt, |
134 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
135 | | WriterHistory* hist, |
136 | | WriterListener* listen = nullptr); |
137 | | |
138 | | /** |
139 | | * Create a RTPSWriter in a participant using a custom payload pool. |
140 | | * @param p Pointer to the RTPSParticipant. |
141 | | * @param watt Writer Attributes. |
142 | | * @param payload_pool Shared pointer to the IPayloadPool |
143 | | * @param change_pool Shared pointer to the IChangePool |
144 | | * @param hist Pointer to the WriterHistory. |
145 | | * @param listen Pointer to the WriterListener. |
146 | | * @return Pointer to the created RTPSWriter. |
147 | | * |
148 | | * \warning The returned pointer is invalidated after a call to removeRTPSWriter() or stopAll(), |
149 | | * so its use may result in undefined behaviour. |
150 | | */ |
151 | | RTPS_DllAPI static RTPSWriter* createRTPSWriter( |
152 | | RTPSParticipant* p, |
153 | | WriterAttributes& watt, |
154 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
155 | | const std::shared_ptr<IChangePool>& change_pool, |
156 | | WriterHistory* hist, |
157 | | WriterListener* listen = nullptr); |
158 | | |
159 | | /** |
160 | | * Create a RTPSWriter in a participant using a custom payload pool. |
161 | | * @param p Pointer to the RTPSParticipant. |
162 | | * @param entity_id Specific entity id to use for the created writer. |
163 | | * @param watt Writer Attributes. |
164 | | * @param payload_pool Shared pointer to the IPayloadPool |
165 | | * @param change_pool Shared pointer to the IChangePool |
166 | | * @param hist Pointer to the WriterHistory. |
167 | | * @param listen Pointer to the WriterListener. |
168 | | * @return Pointer to the created RTPSWriter. |
169 | | * |
170 | | * \warning The returned pointer is invalidated after a call to removeRTPSWriter() or stopAll(), |
171 | | * so its use may result in undefined behaviour. |
172 | | */ |
173 | | RTPS_DllAPI static RTPSWriter* createRTPSWriter( |
174 | | RTPSParticipant* p, |
175 | | const EntityId_t& entity_id, |
176 | | WriterAttributes& watt, |
177 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
178 | | const std::shared_ptr<IChangePool>& change_pool, |
179 | | WriterHistory* hist, |
180 | | WriterListener* listen = nullptr); |
181 | | |
182 | | /** |
183 | | * Create a RTPSWriter in a participant. |
184 | | * @param p Pointer to the RTPSParticipant. |
185 | | * @param entity_id Specific entity id to use for the created writer. |
186 | | * @param watt Writer Attributes. |
187 | | * @param payload_pool Shared pointer to the IPayloadPool |
188 | | * @param hist Pointer to the WriterHistory. |
189 | | * @param listen Pointer to the WriterListener. |
190 | | * @return Pointer to the created RTPSWriter. |
191 | | * |
192 | | * \warning The returned pointer is invalidated after a call to removeRTPSWriter() or stopAll(), |
193 | | * so its use may result in undefined behaviour. |
194 | | */ |
195 | | RTPS_DllAPI static RTPSWriter* createRTPSWriter( |
196 | | RTPSParticipant* p, |
197 | | const EntityId_t& entity_id, |
198 | | WriterAttributes& watt, |
199 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
200 | | WriterHistory* hist, |
201 | | WriterListener* listen = nullptr); |
202 | | |
203 | | /** |
204 | | * Remove a RTPSWriter. |
205 | | * @param writer Pointer to the writer you want to remove. |
206 | | * @return True if correctly removed. |
207 | | */ |
208 | | RTPS_DllAPI static bool removeRTPSWriter( |
209 | | RTPSWriter* writer); |
210 | | |
211 | | /** |
212 | | * Create a RTPSReader in a participant. |
213 | | * @param p Pointer to the RTPSParticipant. |
214 | | * @param ratt Reader Attributes. |
215 | | * @param hist Pointer to the ReaderHistory. |
216 | | * @param listen Pointer to the ReaderListener. |
217 | | * @return Pointer to the created RTPSReader. |
218 | | * |
219 | | * \warning The returned pointer is invalidated after a call to removeRTPSReader() or stopAll(), |
220 | | * so its use may result in undefined behaviour. |
221 | | */ |
222 | | RTPS_DllAPI static RTPSReader* createRTPSReader( |
223 | | RTPSParticipant* p, |
224 | | ReaderAttributes& ratt, |
225 | | ReaderHistory* hist, |
226 | | ReaderListener* listen = nullptr); |
227 | | |
228 | | /** |
229 | | * Create a RTPReader in a participant using a custom payload pool. |
230 | | * @param p Pointer to the RTPSParticipant. |
231 | | * @param ratt Reader Attributes. |
232 | | * @param payload_pool Shared pointer to the IPayloadPool |
233 | | * @param hist Pointer to the ReaderHistory. |
234 | | * @param listen Pointer to the ReaderListener. |
235 | | * @return Pointer to the created RTPSReader. |
236 | | * |
237 | | * \warning The returned pointer is invalidated after a call to removeRTPSReader() or stopAll(), |
238 | | * so its use may result in undefined behaviour. |
239 | | */ |
240 | | RTPS_DllAPI static RTPSReader* createRTPSReader( |
241 | | RTPSParticipant* p, |
242 | | ReaderAttributes& ratt, |
243 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
244 | | ReaderHistory* hist, |
245 | | ReaderListener* listen = nullptr); |
246 | | |
247 | | /** |
248 | | * Create a RTPSReader in a participant using a custom payload pool. |
249 | | * @param p Pointer to the RTPSParticipant. |
250 | | * @param entity_id Specific entity id to use for the created reader. |
251 | | * @param ratt Reader Attributes. |
252 | | * @param payload_pool Shared pointer to the IPayloadPool |
253 | | * @param hist Pointer to the ReaderHistory. |
254 | | * @param listen Pointer to the ReaderListener. |
255 | | * @return Pointer to the created RTPSReader. |
256 | | * |
257 | | * \warning The returned pointer is invalidated after a call to removeRTPSReader() or stopAll(), |
258 | | * so its use may result in undefined behaviour. |
259 | | */ |
260 | | RTPS_DllAPI static RTPSReader* createRTPSReader( |
261 | | RTPSParticipant* p, |
262 | | const EntityId_t& entity_id, |
263 | | ReaderAttributes& ratt, |
264 | | const std::shared_ptr<IPayloadPool>& payload_pool, |
265 | | ReaderHistory* hist, |
266 | | ReaderListener* listen = nullptr); |
267 | | |
268 | | /** |
269 | | * Remove a RTPSReader. |
270 | | * @param reader Pointer to the reader you want to remove. |
271 | | * @return True if correctly removed. |
272 | | */ |
273 | | RTPS_DllAPI static bool removeRTPSReader( |
274 | | RTPSReader* reader); |
275 | | |
276 | | /** |
277 | | * Remove a RTPSParticipant and delete all its associated Writers, Readers, resources, etc. |
278 | | * @param[in] p Pointer to the RTPSParticipant; |
279 | | * @return True if correct. |
280 | | */ |
281 | | RTPS_DllAPI static bool removeRTPSParticipant( |
282 | | RTPSParticipant* p); |
283 | | |
284 | | /** |
285 | | * Set the maximum RTPSParticipantID. |
286 | | * @param maxRTPSParticipantId ID. |
287 | | */ |
288 | | static inline void setMaxRTPSParticipantId( |
289 | | uint32_t maxRTPSParticipantId) |
290 | 0 | { |
291 | 0 | m_maxRTPSParticipantID = maxRTPSParticipantId; |
292 | 0 | } |
293 | | |
294 | | /** |
295 | | * Creates a RTPSParticipant as default server or client if ROS_MASTER_URI environment variable is set. |
296 | | * @param domain_id DDS domain associated |
297 | | * @param enabled True if the RTPSParticipant should be enabled on creation. False if it will be enabled later with RTPSParticipant::enable() |
298 | | * @param attrs RTPSParticipant Attributes. |
299 | | * @param listen Pointer to the ParticipantListener. |
300 | | * @return Pointer to the RTPSParticipant. |
301 | | * |
302 | | * \warning The returned pointer is invalidated after a call to removeRTPSParticipant() or stopAll(), |
303 | | * so its use may result in undefined behaviour. |
304 | | */ |
305 | | static RTPSParticipant* clientServerEnvironmentCreationOverride( |
306 | | uint32_t domain_id, |
307 | | bool enabled, |
308 | | const RTPSParticipantAttributes& attrs, |
309 | | RTPSParticipantListener* listen /*= nullptr*/); |
310 | | |
311 | | private: |
312 | | |
313 | | typedef std::pair<RTPSParticipant*, RTPSParticipantImpl*> t_p_RTPSParticipant; |
314 | | |
315 | | RTPSDomain() = delete; |
316 | | |
317 | | /** |
318 | | * DomainRTPSParticipant destructor |
319 | | */ |
320 | | ~RTPSDomain() = delete; |
321 | | |
322 | | /** |
323 | | * @brief Get Id to create a RTPSParticipant. |
324 | | * @return Different ID for each call. |
325 | | */ |
326 | | static inline uint32_t getNewId() |
327 | 0 | { |
328 | 0 | return m_maxRTPSParticipantID++; |
329 | 0 | } |
330 | | |
331 | | static void removeRTPSParticipant_nts( |
332 | | t_p_RTPSParticipant&); |
333 | | |
334 | | static std::mutex m_mutex; |
335 | | |
336 | | static std::atomic<uint32_t> m_maxRTPSParticipantID; |
337 | | |
338 | | static std::vector<t_p_RTPSParticipant> m_RTPSParticipants; |
339 | | |
340 | | static std::set<uint32_t> m_RTPSParticipantIDs; |
341 | | }; |
342 | | |
343 | | } // namespace rtps |
344 | | } /* namespace fastrtps */ |
345 | | } /* namespace eprosima */ |
346 | | |
347 | | #endif /* _FASTDDS_RTPS_DOMAIN_H_*/ |