-
Notifications
You must be signed in to change notification settings - Fork 793
/
Copy pathRTPSDomain.cpp
566 lines (498 loc) · 17.8 KB
/
RTPSDomain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* @file RTPSDomain.cpp
*/
#include <fastdds/rtps/RTPSDomain.h>
#include <chrono>
#include <cstdlib>
#include <regex>
#include <string>
#include <thread>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/history/WriterHistory.h>
#include <fastdds/rtps/participant/RTPSParticipant.h>
#include <fastdds/rtps/reader/RTPSReader.h>
#include <fastdds/rtps/writer/RTPSWriter.h>
#include <rtps/transport/UDPv4Transport.h>
#include <rtps/transport/UDPv6Transport.h>
#include <rtps/transport/test_UDPv4Transport.h>
#include <fastrtps/utils/IPFinder.h>
#include <fastrtps/utils/IPLocator.h>
#include <fastrtps/utils/System.h>
#include <fastrtps/utils/md5.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <rtps/RTPSDomainImpl.hpp>
#include <rtps/participant/RTPSParticipantImpl.h>
#include <rtps/common/GuidUtils.hpp>
#include <utils/Host.hpp>
#include <utils/SystemInfo.hpp>
namespace eprosima {
namespace fastrtps {
namespace rtps {
static void guid_prefix_create(
uint32_t ID,
GuidPrefix_t& guidP)
{
eprosima::fastdds::rtps::GuidUtils::instance().guid_prefix_create(ID, guidP);
}
std::mutex RTPSDomain::m_mutex;
std::atomic<uint32_t> RTPSDomain::m_maxRTPSParticipantID(1);
std::vector<RTPSDomain::t_p_RTPSParticipant> RTPSDomain::m_RTPSParticipants;
std::set<uint32_t> RTPSDomain::m_RTPSParticipantIDs;
FileWatchHandle RTPSDomainImpl::file_watch_handle_;
void RTPSDomain::stopAll()
{
std::unique_lock<std::mutex> lock(m_mutex);
logInfo(RTPS_PARTICIPANT, "DELETING ALL ENDPOINTS IN THIS DOMAIN");
// Stop monitoring environment file
SystemInfo::stop_watching_file(RTPSDomainImpl::file_watch_handle_);
while (m_RTPSParticipants.size() > 0)
{
RTPSDomain::t_p_RTPSParticipant participant = m_RTPSParticipants.back();
m_RTPSParticipantIDs.erase(m_RTPSParticipantIDs.find(participant.second->getRTPSParticipantID()));
m_RTPSParticipants.pop_back();
lock.unlock();
RTPSDomain::removeRTPSParticipant_nts(participant);
lock.lock();
}
logInfo(RTPS_PARTICIPANT, "RTPSParticipants deleted correctly ");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
RTPSParticipant* RTPSDomain::createParticipant(
uint32_t domain_id,
const RTPSParticipantAttributes& attrs,
RTPSParticipantListener* listen)
{
return createParticipant(domain_id, true, attrs, listen);
}
RTPSParticipant* RTPSDomain::createParticipant(
uint32_t domain_id,
bool enabled,
const RTPSParticipantAttributes& attrs,
RTPSParticipantListener* listen)
{
logInfo(RTPS_PARTICIPANT, "");
RTPSParticipantAttributes PParam = attrs;
if (PParam.builtin.discovery_config.leaseDuration < c_TimeInfinite &&
PParam.builtin.discovery_config.leaseDuration <=
PParam.builtin.discovery_config.leaseDuration_announcementperiod)
{
logError(RTPS_PARTICIPANT,
"RTPSParticipant Attributes: LeaseDuration should be >= leaseDuration announcement period");
return nullptr;
}
// Only the first time, initialize environment file watch if the corresponding environment variable is set
if (!RTPSDomainImpl::file_watch_handle_)
{
std::string filename = SystemInfo::get_environment_file();
if (!filename.empty() && SystemInfo::file_exists(filename))
{
// Create filewatch
RTPSDomainImpl::file_watch_handle_ = SystemInfo::watch_file(filename, RTPSDomainImpl::file_watch_callback);
}
else
{
logWarning(RTPS_PARTICIPANT, filename + " does not exist. File watching not initialized.");
}
}
uint32_t ID;
{
std::lock_guard<std::mutex> guard(m_mutex);
if (PParam.participantID < 0)
{
ID = getNewId();
while (m_RTPSParticipantIDs.insert(ID).second == false)
{
ID = getNewId();
}
}
else
{
ID = PParam.participantID;
if (m_RTPSParticipantIDs.insert(ID).second == false)
{
logError(RTPS_PARTICIPANT, "RTPSParticipant with the same ID already exists");
return nullptr;
}
}
}
if (!PParam.defaultUnicastLocatorList.isValid())
{
logError(RTPS_PARTICIPANT, "Default Unicast Locator List contains invalid Locator");
return nullptr;
}
if (!PParam.defaultMulticastLocatorList.isValid())
{
logError(RTPS_PARTICIPANT, "Default Multicast Locator List contains invalid Locator");
return nullptr;
}
PParam.participantID = ID;
// Generate a new GuidPrefix_t
GuidPrefix_t guidP;
guid_prefix_create(ID, guidP);
RTPSParticipant* p = new RTPSParticipant(nullptr);
RTPSParticipantImpl* pimpl = nullptr;
// If we force the participant to have a specific prefix we must define a different persistence GuidPrefix_t that
// would ensure builtin endpoints are able to differentiate between a communication loss and a participant recovery
if (PParam.prefix != c_GuidPrefix_Unknown)
{
pimpl = new RTPSParticipantImpl(domain_id, PParam, PParam.prefix, guidP, p, listen);
}
else
{
pimpl = new RTPSParticipantImpl(domain_id, PParam, guidP, p, listen);
}
// Above constructors create the sender resources. If a given listening port cannot be allocated an iterative
// mechanism will allocate another by default. Change the default listening port is unacceptable for server
// discovery.
if ((PParam.builtin.discovery_config.discoveryProtocol == DiscoveryProtocol_t::SERVER
|| PParam.builtin.discovery_config.discoveryProtocol == DiscoveryProtocol_t::BACKUP)
&& pimpl->did_mutation_took_place_on_meta(
PParam.builtin.metatrafficMulticastLocatorList,
PParam.builtin.metatrafficUnicastLocatorList))
{
// we do not log an error because the library may use participant creation as a trial for server existence
logInfo(RTPS_PARTICIPANT, "Server wasn't able to allocate the specified listening port");
delete pimpl;
return nullptr;
}
// Check there is at least one transport registered.
if (!pimpl->networkFactoryHasRegisteredTransports())
{
logError(RTPS_PARTICIPANT, "Cannot create participant, because there is any transport");
delete pimpl;
return nullptr;
}
#if HAVE_SECURITY
// Check security was correctly initialized
if (!pimpl->is_security_initialized())
{
logError(RTPS_PARTICIPANT, "Cannot create participant due to security initialization error");
delete pimpl;
return nullptr;
}
#endif // if HAVE_SECURITY
{
std::lock_guard<std::mutex> guard(m_mutex);
m_RTPSParticipants.push_back(t_p_RTPSParticipant(p, pimpl));
}
if (enabled)
{
// Start protocols
pimpl->enable();
}
return p;
}
bool RTPSDomain::removeRTPSParticipant(
RTPSParticipant* p)
{
if (p != nullptr)
{
assert((p->mp_impl != nullptr) && "This participant has been previously invalidated");
p->mp_impl->disable();
std::unique_lock<std::mutex> lock(m_mutex);
for (auto it = m_RTPSParticipants.begin(); it != m_RTPSParticipants.end(); ++it)
{
if (it->second->getGuid().guidPrefix == p->getGuid().guidPrefix)
{
RTPSDomain::t_p_RTPSParticipant participant = *it;
m_RTPSParticipants.erase(it);
m_RTPSParticipantIDs.erase(m_RTPSParticipantIDs.find(participant.second->getRTPSParticipantID()));
lock.unlock();
removeRTPSParticipant_nts(participant);
return true;
}
}
}
logError(RTPS_PARTICIPANT, "RTPSParticipant not valid or not recognized");
return false;
}
void RTPSDomain::removeRTPSParticipant_nts(
RTPSDomain::t_p_RTPSParticipant& participant)
{
// The destructor of RTPSParticipantImpl already deletes the associated RTPSParticipant and sets
// its pointer to the RTPSParticipant to nullptr, so there is no need to do it here manually.
delete(participant.second);
}
RTPSWriter* RTPSDomain::createRTPSWriter(
RTPSParticipant* p,
WriterAttributes& watt,
WriterHistory* hist,
WriterListener* listen)
{
RTPSParticipantImpl* impl = RTPSDomainImpl::find_local_participant(p->getGuid());
if (impl)
{
RTPSWriter* ret_val = nullptr;
if (impl->createWriter(&ret_val, watt, hist, listen))
{
return ret_val;
}
}
return nullptr;
}
RTPSWriter* RTPSDomain::createRTPSWriter(
RTPSParticipant* p,
WriterAttributes& watt,
const std::shared_ptr<IPayloadPool>& payload_pool,
WriterHistory* hist,
WriterListener* listen)
{
RTPSParticipantImpl* impl = RTPSDomainImpl::find_local_participant(p->getGuid());
if (impl)
{
RTPSWriter* ret_val = nullptr;
if (impl->createWriter(&ret_val, watt, payload_pool, hist, listen))
{
return ret_val;
}
}
return nullptr;
}
RTPSWriter* RTPSDomain::createRTPSWriter(
RTPSParticipant* p,
const EntityId_t& entity_id,
WriterAttributes& watt,
const std::shared_ptr<IPayloadPool>& payload_pool,
WriterHistory* hist,
WriterListener* listen)
{
RTPSParticipantImpl* impl = RTPSDomainImpl::find_local_participant(p->getGuid());
if (impl)
{
RTPSWriter* ret_val = nullptr;
if (impl->createWriter(&ret_val, watt, payload_pool, hist, listen, entity_id))
{
return ret_val;
}
}
return nullptr;
}
bool RTPSDomain::removeRTPSWriter(
RTPSWriter* writer)
{
if (writer != nullptr)
{
std::unique_lock<std::mutex> lock(m_mutex);
for (auto it = m_RTPSParticipants.begin(); it != m_RTPSParticipants.end(); ++it)
{
if (it->first->getGuid().guidPrefix == writer->getGuid().guidPrefix)
{
t_p_RTPSParticipant participant = *it;
lock.unlock();
return participant.second->deleteUserEndpoint((Endpoint*)writer);
}
}
}
return false;
}
RTPSReader* RTPSDomain::createRTPSReader(
RTPSParticipant* p,
ReaderAttributes& ratt,
ReaderHistory* rhist,
ReaderListener* rlisten)
{
RTPSParticipantImpl* impl = RTPSDomainImpl::find_local_participant(p->getGuid());
if (impl)
{
RTPSReader* reader;
if (impl->createReader(&reader, ratt, rhist, rlisten))
{
return reader;
}
}
return nullptr;
}
RTPSReader* RTPSDomain::createRTPSReader(
RTPSParticipant* p,
ReaderAttributes& ratt,
const std::shared_ptr<IPayloadPool>& payload_pool,
ReaderHistory* rhist,
ReaderListener* rlisten)
{
RTPSParticipantImpl* impl = RTPSDomainImpl::find_local_participant(p->getGuid());
if (impl)
{
RTPSReader* reader;
if (impl->createReader(&reader, ratt, payload_pool, rhist, rlisten))
{
return reader;
}
}
return nullptr;
}
bool RTPSDomain::removeRTPSReader(
RTPSReader* reader)
{
if (reader != nullptr)
{
std::unique_lock<std::mutex> lock(m_mutex);
for (auto it = m_RTPSParticipants.begin(); it != m_RTPSParticipants.end(); ++it)
{
if (it->first->getGuid().guidPrefix == reader->getGuid().guidPrefix)
{
t_p_RTPSParticipant participant = *it;
lock.unlock();
return participant.second->deleteUserEndpoint((Endpoint*)reader);
}
}
}
return false;
}
RTPSParticipant* RTPSDomain::clientServerEnvironmentCreationOverride(
uint32_t domain_id,
bool enabled,
const RTPSParticipantAttributes& att,
RTPSParticipantListener* listen /*= nullptr*/)
{
// Check the specified discovery protocol: if other than simple it has priority over ros environment variable
if (att.builtin.discovery_config.discoveryProtocol != DiscoveryProtocol_t::SIMPLE)
{
logInfo(DOMAIN, "Detected non simple discovery protocol attributes."
<< " Ignoring auto default client-server setup.");
return nullptr;
}
// we only make the attributes copy when we are sure is worth
RTPSParticipantAttributes client_att(att);
// Retrieve the info from the environment variable
// TODO(jlbueno) This should be protected with the PDP mutex.
if (load_environment_server_info(client_att.builtin.discovery_config.m_DiscoveryServers) &&
client_att.builtin.discovery_config.m_DiscoveryServers.empty())
{
// it's not an error, the environment variable may not be set. Any issue with environment
// variable syntax is logError already
return nullptr;
}
logInfo(DOMAIN, "Detected auto client-server environment variable."
"Trying to create client with the default server setup.");
client_att.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::CLIENT;
// RemoteServerAttributes already fill in above
RTPSParticipant* part = RTPSDomain::createParticipant(domain_id, enabled, client_att, listen);
if (nullptr != part)
{
// client successfully created
logInfo(DOMAIN, "Auto default server-client setup. Default client created.");
part->mp_impl->client_override(true);
return part;
}
// unable to create auto server-client default participants
logError(DOMAIN, "Auto default server-client setup. Unable to create the client.");
return nullptr;
}
void RTPSDomainImpl::create_participant_guid(
int32_t& participant_id,
GUID_t& guid)
{
if (participant_id < 0)
{
std::lock_guard<std::mutex> guard(RTPSDomain::m_mutex);
do
{
participant_id = RTPSDomain::getNewId();
} while (RTPSDomain::m_RTPSParticipantIDs.find(participant_id) != RTPSDomain::m_RTPSParticipantIDs.end());
}
guid_prefix_create(participant_id, guid.guidPrefix);
guid.entityId = c_EntityId_RTPSParticipant;
}
RTPSParticipantImpl* RTPSDomainImpl::find_local_participant(
const GUID_t& guid)
{
std::lock_guard<std::mutex> guard(RTPSDomain::m_mutex);
for (const RTPSDomain::t_p_RTPSParticipant& participant : RTPSDomain::m_RTPSParticipants)
{
if (participant.second->getGuid().guidPrefix == guid.guidPrefix)
{
// Participant found, forward the query
return participant.second;
}
}
return nullptr;
}
RTPSReader* RTPSDomainImpl::find_local_reader(
const GUID_t& reader_guid)
{
std::lock_guard<std::mutex> guard(RTPSDomain::m_mutex);
for (const RTPSDomain::t_p_RTPSParticipant& participant : RTPSDomain::m_RTPSParticipants)
{
if (participant.second->getGuid().guidPrefix == reader_guid.guidPrefix)
{
// Participant found, forward the query
return participant.second->find_local_reader(reader_guid);
}
}
return nullptr;
}
RTPSWriter* RTPSDomainImpl::find_local_writer(
const GUID_t& writer_guid)
{
std::lock_guard<std::mutex> guard(RTPSDomain::m_mutex);
for (const RTPSDomain::t_p_RTPSParticipant& participant : RTPSDomain::m_RTPSParticipants)
{
if (participant.second->getGuid().guidPrefix == writer_guid.guidPrefix)
{
// Participant found, forward the query
return participant.second->find_local_writer(writer_guid);
}
}
return nullptr;
}
/**
* Check whether intraprocess delivery should be used between two GUIDs.
*
* @param local_guid GUID of the local endpoint performing the query.
* @param matched_guid GUID being queried about.
*
* @returns true when intraprocess delivery is enabled, false otherwise.
*/
bool RTPSDomainImpl::should_intraprocess_between(
const GUID_t& local_guid,
const GUID_t& matched_guid)
{
if (!local_guid.is_on_same_process_as(matched_guid))
{
// Not on the same process, should not use intraprocess mechanism.
return false;
}
if (local_guid.entityId == c_EntityId_SPDPWriter || local_guid.entityId == c_EntityId_SPDPReader)
{
// Always disabled for PDP, to avoid inter-domain communications.
return false;
}
switch (xmlparser::XMLProfileManager::library_settings().intraprocess_delivery)
{
case IntraprocessDeliveryType::INTRAPROCESS_FULL:
return true;
case IntraprocessDeliveryType::INTRAPROCESS_USER_DATA_ONLY:
return !matched_guid.is_builtin();
case IntraprocessDeliveryType::INTRAPROCESS_OFF:
default:
break;
}
return false;
}
void RTPSDomainImpl::file_watch_callback()
{
// Ensure that all changes have been saved by the OS
std::this_thread::sleep_for(std::chrono::seconds(1));
// For all RTPSParticipantImpl registered in the RTPSDomain, call RTPSParticipantImpl::environment_file_has_changed
std::lock_guard<std::mutex> guard(RTPSDomain::m_mutex);
for (auto participant : RTPSDomain::m_RTPSParticipants)
{
participant.second->environment_file_has_changed();
}
}
} // namespace rtps
} // namespace fastrtps
} // namespace eprosima