-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathPDPServer.cpp
More file actions
2068 lines (1797 loc) · 81.7 KB
/
Copy pathPDPServer.cpp
File metadata and controls
2068 lines (1797 loc) · 81.7 KB
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 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 PDPServer.cpp
*
*/
#include <fstream>
#include <iostream>
#include <mutex>
#include <set>
#include <fastdds/dds/core/policy/QosPolicies.hpp>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/history/History.h>
#include <fastdds/rtps/history/ReaderHistory.h>
#include <fastdds/rtps/history/WriterHistory.h>
#include <fastdds/rtps/participant/RTPSParticipantListener.h>
#include <fastdds/rtps/writer/StatefulWriter.h>
#include <fastdds/utils/TimedMutex.hpp>
#include <fastdds/builtin/type_lookup_service/TypeLookupManager.hpp>
#include <rtps/builtin/BuiltinProtocols.h>
#include <rtps/builtin/discovery/database/backup/SharedBackupFunctions.hpp>
#include <rtps/builtin/discovery/endpoint/EDPServer.hpp>
#include <rtps/builtin/discovery/endpoint/EDPServerListeners.hpp>
#include <rtps/builtin/discovery/participant/DirectMessageSender.hpp>
#include <rtps/builtin/discovery/participant/DS/DiscoveryServerPDPEndpoints.hpp>
#include <rtps/builtin/discovery/participant/DS/DiscoveryServerPDPEndpointsSecure.hpp>
#include <rtps/builtin/discovery/participant/DS/FakeWriter.hpp>
#include <rtps/builtin/discovery/participant/DS/PDPSecurityInitiatorListener.hpp>
#include <rtps/builtin/discovery/participant/PDPServer.hpp>
#include <rtps/builtin/discovery/participant/PDPServerListener.hpp>
#include <rtps/builtin/discovery/participant/timedevent/DServerEvent.hpp>
#include <rtps/builtin/liveliness/WLP.h>
#include <rtps/participant/RTPSParticipantImpl.h>
#include <rtps/reader/StatefulReader.hpp>
#include <utils/TimeConversion.hpp>
namespace eprosima {
namespace fastdds {
namespace rtps {
using namespace eprosima::fastrtps::rtps;
PDPServer::PDPServer(
BuiltinProtocols* builtin,
const RTPSParticipantAllocationAttributes& allocation,
DurabilityKind_t durability_kind /* TRANSIENT_LOCAL */)
: PDP(builtin, allocation)
, routine_(nullptr)
, ping_(nullptr)
, discovery_db_(builtin->mp_participantImpl->getGuid().guidPrefix,
servers_prefixes())
, durability_ (durability_kind)
{
// Add remote servers from environment variable
RemoteServerList_t env_servers;
{
std::lock_guard<std::recursive_mutex> lock(*getMutex());
if (load_environment_server_info(env_servers))
{
for (auto server : env_servers)
{
{
std::unique_lock<eprosima::shared_mutex> disc_lock(mp_builtin->getDiscoveryMutex());
mp_builtin->m_DiscoveryServers.push_back(server);
}
m_discovery.discovery_config.m_DiscoveryServers.push_back(server);
discovery_db_.add_server(server.guidPrefix);
}
}
}
}
PDPServer::~PDPServer()
{
// Stop timed events
routine_->cancel_timer();
ping_->cancel_timer();
// Disable database
discovery_db_.disable();
// Delete timed events
delete(routine_);
delete(ping_);
// Clear ddb and release its changes
process_changes_release_(discovery_db_.clear());
}
bool PDPServer::init(
RTPSParticipantImpl* part)
{
if (!PDP::initPDP(part))
{
return false;
}
//INIT EDP
mp_EDP = new EDPServer(this, mp_RTPSParticipant, durability_);
if (!mp_EDP->initEDP(m_discovery))
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Endpoint discovery configuration failed");
return false;
}
std::vector<nlohmann::json> backup_queue;
if (durability_ == TRANSIENT)
{
nlohmann::json backup_json;
// If the DS is BACKUP, try to restore DDB from file
discovery_db().backup_in_progress(true);
if (read_backup(backup_json, backup_queue))
{
if (process_backup_discovery_database_restore(backup_json))
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "DiscoveryDataBase restored correctly");
}
}
else
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER,
"Error reading backup file. Corrupted or unmissing file, restarting from scratch");
}
discovery_db().backup_in_progress(false);
discovery_db_.persistence_enable(get_ddb_queue_persistence_file_name());
}
else
{
// Allows the ddb to process new messages from this point
discovery_db_.enable();
}
// Activate listeners
EDPServer* edp = static_cast<EDPServer*>(mp_EDP);
builtin_endpoints_->enable_pdp_readers(getRTPSParticipant());
getRTPSParticipant()->enableReader(edp->subscriptions_reader_.first);
getRTPSParticipant()->enableReader(edp->publications_reader_.first);
// Initialize server dedicated thread.
const RTPSParticipantAttributes& part_attr = getRTPSParticipant()->getRTPSParticipantAttributes();
uint32_t id_for_thread = static_cast<uint32_t>(part_attr.participantID);
const fastdds::rtps::ThreadSettings& thr_config = part_attr.discovery_server_thread;
resource_event_thread_.init_thread(thr_config, "dds.ds_ev.%u", id_for_thread);
/*
Given the fact that a participant is either a client or a server the
discoveryServer_client_syncperiod parameter has a context defined meaning.
*/
routine_ = new DServerRoutineEvent(this,
TimeConv::Duration_t2MilliSecondsDouble(
m_discovery.discovery_config.discoveryServer_client_syncperiod));
/*
Given the fact that a participant is either a client or a server the
discoveryServer_client_syncperiod parameter has a context defined meaning.
*/
ping_ = new DServerPingEvent(this,
TimeConv::Duration_t2MilliSecondsDouble(
m_discovery.discovery_config.discoveryServer_client_syncperiod));
ping_->restart_timer();
// Restoring the queue must be done after starting the routine
if (durability_ == TRANSIENT)
{
// This vector is empty till backup queue is implemented
process_backup_restore_queue(backup_queue);
}
return true;
}
ParticipantProxyData* PDPServer::createParticipantProxyData(
const ParticipantProxyData& participant_data,
const GUID_t& writer_guid)
{
std::lock_guard<std::recursive_mutex> lock(*getMutex());
// lease duration is controlled for owned clients or linked servers
// other clients liveliness is provided through server's PDP discovery data
// check if the DATA msg is relayed by another server
bool do_lease = participant_data.m_guid.guidPrefix == writer_guid.guidPrefix;
if (!do_lease)
{
// if not a client verify this participant is a server
{
eprosima::shared_lock<eprosima::shared_mutex> disc_lock(mp_builtin->getDiscoveryMutex());
for (auto& svr : mp_builtin->m_DiscoveryServers)
{
if (data_matches_with_prefix(svr.guidPrefix, participant_data))
{
do_lease = true;
}
}
}
}
ParticipantProxyData* pdata = add_participant_proxy_data(participant_data.m_guid, do_lease, &participant_data);
if (pdata != nullptr)
{
if (do_lease)
{
pdata->lease_duration_event->update_interval(pdata->m_leaseDuration);
pdata->lease_duration_event->restart_timer();
}
}
return pdata;
}
void PDPServer::update_builtin_locators()
{
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
mp_builtin->updateMetatrafficLocators(endpoints->reader.reader_->getAttributes().unicastLocatorList);
}
bool PDPServer::createPDPEndpoints()
{
#if HAVE_SECURITY
if (should_protect_discovery())
{
return create_secure_ds_pdp_endpoints();
}
#endif // HAVE_SECURITY
return create_ds_pdp_endpoints();
}
#if HAVE_SECURITY
bool PDPServer::should_protect_discovery()
{
return mp_RTPSParticipant->is_secure() && mp_RTPSParticipant->security_attributes().is_discovery_protected;
}
bool PDPServer::create_secure_ds_pdp_endpoints()
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "Beginning PDPServer Endpoints creation");
auto endpoints = new fastdds::rtps::DiscoveryServerPDPEndpointsSecure();
builtin_endpoints_.reset(endpoints);
bool ret_val = create_ds_pdp_reliable_endpoints(*endpoints, true) && create_ds_pdp_best_effort_reader(*endpoints);
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "PDPServer Endpoints creation finished");
return ret_val;
}
bool PDPServer::create_ds_pdp_best_effort_reader(
DiscoveryServerPDPEndpointsSecure& endpoints)
{
const RTPSParticipantAttributes& pattr = mp_RTPSParticipant->getRTPSParticipantAttributes();
HistoryAttributes hatt;
hatt.payloadMaxSize = mp_builtin->m_att.readerPayloadSize;
hatt.initialReservedCaches = pdp_initial_reserved_caches;
hatt.memoryPolicy = mp_builtin->m_att.readerHistoryMemoryPolicy;
endpoints.stateless_reader.history_.reset(new ReaderHistory(hatt));
ReaderAttributes ratt;
ratt.expectsInlineQos = false;
ratt.endpoint.endpointKind = READER;
ratt.endpoint.multicastLocatorList = mp_builtin->m_metatrafficMulticastLocatorList;
ratt.endpoint.unicastLocatorList = mp_builtin->m_metatrafficUnicastLocatorList;
ratt.endpoint.external_unicast_locators = mp_builtin->m_att.metatraffic_external_unicast_locators;
ratt.endpoint.ignore_non_matching_locators = pattr.ignore_non_matching_locators;
ratt.endpoint.topicKind = WITH_KEY;
// change depending of backup mode
ratt.endpoint.durabilityKind = VOLATILE;
ratt.endpoint.reliabilityKind = BEST_EFFORT;
endpoints.stateless_reader.listener_.reset(new PDPSecurityInitiatorListener(this,
[this](const ParticipantProxyData& participant_data)
{
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
std::lock_guard<fastrtps::RecursiveTimedMutex> wlock(endpoints->writer.writer_->getMutex());
CacheChange_t* change = discovery_db().cache_change_own_participant();
if (change != nullptr)
{
std::vector<GUID_t> remote_readers;
LocatorList locators;
remote_readers.emplace_back(participant_data.m_guid.guidPrefix, c_EntityId_SPDPReader);
for (auto& locator : participant_data.metatraffic_locators.unicast)
{
locators.push_back(locator);
}
send_announcement(change, remote_readers, locators, false);
}
}));
// Create PDP Reader
RTPSReader* reader = nullptr;
if (mp_RTPSParticipant->createReader(&reader, ratt, endpoints.stateless_reader.history_.get(),
endpoints.stateless_reader.listener_.get(), c_EntityId_SPDPReader, true, false))
{
endpoints.stateless_reader.reader_ = dynamic_cast<fastrtps::rtps::StatelessReader*>(reader);
mp_RTPSParticipant->set_endpoint_rtps_protection_supports(reader, false);
}
// Could not create PDP Reader, so return false
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "PDPServer security initiation Reader creation failed");
endpoints.stateless_reader.release();
return false;
}
return true;
}
#endif // HAVE_SECURITY
bool PDPServer::create_ds_pdp_endpoints()
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "Beginning PDPServer Endpoints creation");
auto endpoints = new fastdds::rtps::DiscoveryServerPDPEndpoints();
builtin_endpoints_.reset(endpoints);
bool ret_val = create_ds_pdp_reliable_endpoints(*endpoints, false);
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "PDPServer Endpoints creation finished");
return ret_val;
}
bool PDPServer::create_ds_pdp_reliable_endpoints(
DiscoveryServerPDPEndpoints& endpoints,
bool secure)
{
const RTPSParticipantAttributes& pattr = mp_RTPSParticipant->getRTPSParticipantAttributes();
/***********************************
* PDP READER
***********************************/
// PDP Reader History
HistoryAttributes hatt;
hatt.payloadMaxSize = mp_builtin->m_att.readerPayloadSize;
hatt.initialReservedCaches = pdp_initial_reserved_caches;
hatt.memoryPolicy = mp_builtin->m_att.readerHistoryMemoryPolicy;
endpoints.reader.history_.reset(new ReaderHistory(hatt));
// PDP Reader Attributes
ReaderAttributes ratt;
ratt.expectsInlineQos = false;
ratt.endpoint.endpointKind = READER;
ratt.endpoint.multicastLocatorList = mp_builtin->m_metatrafficMulticastLocatorList;
ratt.endpoint.unicastLocatorList = mp_builtin->m_metatrafficUnicastLocatorList;
ratt.endpoint.external_unicast_locators = mp_builtin->m_att.metatraffic_external_unicast_locators;
ratt.endpoint.ignore_non_matching_locators = pattr.ignore_non_matching_locators;
ratt.endpoint.topicKind = WITH_KEY;
// change depending of backup mode
ratt.endpoint.durabilityKind = durability_;
ratt.endpoint.reliabilityKind = RELIABLE;
ratt.times.heartbeatResponseDelay = pdp_heartbeat_response_delay;
#if HAVE_SECURITY
if (secure)
{
ratt.endpoint.security_attributes().is_submessage_protected = true;
ratt.endpoint.security_attributes().plugin_endpoint_attributes =
PLUGIN_ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_SUBMESSAGE_ENCRYPTED;
}
#endif // HAVE_SECURITY
#if HAVE_SQLITE3
ratt.endpoint.properties.properties().push_back(Property("dds.persistence.plugin", "builtin.SQLITE3"));
ratt.endpoint.properties.properties().push_back(Property("dds.persistence.sqlite3.filename",
get_reader_persistence_file_name()));
#endif // HAVE_SQLITE3
// PDP Listener
endpoints.reader.listener_.reset(new PDPServerListener(this));
// Create PDP Reader
RTPSReader* reader = nullptr;
#if HAVE_SECURITY
EntityId_t reader_entity = secure ? c_EntityId_spdp_reliable_participant_secure_reader : c_EntityId_SPDPReader;
#else
EntityId_t reader_entity = c_EntityId_SPDPReader;
#endif // if HAVE_SECURITY
if (mp_RTPSParticipant->createReader(&reader, ratt, endpoints.reader.history_.get(),
endpoints.reader.listener_.get(), reader_entity, true, false))
{
endpoints.reader.reader_ = dynamic_cast<fastrtps::rtps::StatefulReader*>(reader);
// Enable unknown clients to reach this reader
reader->enableMessagesFromUnkownWriters(true);
#if HAVE_SECURITY
mp_RTPSParticipant->set_endpoint_rtps_protection_supports(reader, false);
#endif // if HAVE_SECURITY
}
// Could not create PDP Reader, so return false
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "PDPServer Reader creation failed");
endpoints.reader.release();
return false;
}
/***********************************
* PDP WRITER
***********************************/
// PDP Writer History
hatt.payloadMaxSize = mp_builtin->m_att.writerPayloadSize;
hatt.initialReservedCaches = pdp_initial_reserved_caches;
hatt.memoryPolicy = mp_builtin->m_att.writerHistoryMemoryPolicy;
endpoints.writer.history_.reset(new WriterHistory(hatt));
// PDP Writer Attributes
WriterAttributes watt;
watt.endpoint.endpointKind = WRITER;
// VOLATILE durability to highlight that on steady state the history is empty (except for announcement DATAs)
// this setting is incompatible with CLIENTs TRANSIENT_LOCAL PDP readers but not validation is done on builitin
// endpoints
watt.endpoint.durabilityKind = durability_;
#if HAVE_SQLITE3
watt.endpoint.properties.properties().push_back(Property("dds.persistence.plugin", "builtin.SQLITE3"));
watt.endpoint.properties.properties().push_back(Property("dds.persistence.sqlite3.filename",
get_writer_persistence_file_name()));
#endif // HAVE_SQLITE3
watt.endpoint.reliabilityKind = RELIABLE;
watt.endpoint.topicKind = WITH_KEY;
watt.endpoint.multicastLocatorList = mp_builtin->m_metatrafficMulticastLocatorList;
watt.endpoint.unicastLocatorList = mp_builtin->m_metatrafficUnicastLocatorList;
watt.endpoint.external_unicast_locators = mp_builtin->m_att.metatraffic_external_unicast_locators;
watt.endpoint.ignore_non_matching_locators = pattr.ignore_non_matching_locators;
watt.times.heartbeatPeriod = pdp_heartbeat_period;
watt.times.nackResponseDelay = pdp_nack_response_delay;
watt.times.nackSupressionDuration = pdp_nack_supression_duration;
watt.mode = ASYNCHRONOUS_WRITER;
#if HAVE_SECURITY
if (secure)
{
watt.endpoint.security_attributes().is_submessage_protected = true;
watt.endpoint.security_attributes().plugin_endpoint_attributes =
PLUGIN_ENDPOINT_SECURITY_ATTRIBUTES_FLAG_IS_SUBMESSAGE_ENCRYPTED;
}
#endif // HAVE_SECURITY
// Create PDP Writer
RTPSWriter* wout = nullptr;
#if HAVE_SECURITY
EntityId_t writer_entity = secure ? c_EntityId_spdp_reliable_participant_secure_writer : c_EntityId_SPDPWriter;
#else
EntityId_t writer_entity = c_EntityId_SPDPWriter;
#endif // if HAVE_SECURITY
if (mp_RTPSParticipant->createWriter(&wout, watt, endpoints.writer.history_.get(), nullptr, writer_entity, true))
{
endpoints.writer.writer_ = dynamic_cast<fastrtps::rtps::StatefulWriter*>(wout);
#if HAVE_SECURITY
mp_RTPSParticipant->set_endpoint_rtps_protection_supports(wout, false);
#endif // if HAVE_SECURITY
// Set pdp filter to writer
IReaderDataFilter* pdp_filter = static_cast<ddb::PDPDataFilter<ddb::DiscoveryDataBase>*>(&discovery_db_);
wout->reader_data_filter(pdp_filter);
// Enable separate sending so the filter can be called for each change and reader proxy
wout->set_separate_sending(true);
if (!secure)
{
eprosima::shared_lock<eprosima::shared_mutex> disc_lock(mp_builtin->getDiscoveryMutex());
for (const eprosima::fastdds::rtps::RemoteServerAttributes& it : mp_builtin->m_DiscoveryServers)
{
match_pdp_reader_nts_(it);
}
}
}
// Could not create PDP Writer, so return false
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "PDPServer Writer creation failed");
endpoints.writer.release();
return false;
}
// TODO check if this should be done here or before this point in creation
endpoints.writer.history_->remove_all_changes();
// Perform matching with remote servers and ensure output channels are open in the transport for the corresponding
// locators
{
eprosima::shared_lock<eprosima::shared_mutex> disc_lock(mp_builtin->getDiscoveryMutex());
for (const eprosima::fastdds::rtps::RemoteServerAttributes& it : mp_builtin->m_DiscoveryServers)
{
auto entry = LocatorSelectorEntry::create_fully_selected_entry(
it.metatrafficUnicastLocatorList, it.metatrafficMulticastLocatorList);
mp_RTPSParticipant->createSenderResources(entry);
if (!secure)
{
match_pdp_writer_nts_(it);
match_pdp_reader_nts_(it);
}
}
}
return true;
}
void PDPServer::initializeParticipantProxyData(
ParticipantProxyData* participant_data)
{
PDP::initializeParticipantProxyData(participant_data);
if (getRTPSParticipant()->getAttributes().builtin.discovery_config.discoveryProtocol !=
DiscoveryProtocol_t::SERVER
&&
getRTPSParticipant()->getAttributes().builtin.discovery_config.discoveryProtocol !=
DiscoveryProtocol_t::BACKUP)
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Using a PDP Server object with another user's settings");
}
// A PDP server should always be provided with all EDP endpoints
// because it must relay all clients EDP info
participant_data->m_availableBuiltinEndpoints
|= DISC_BUILTIN_ENDPOINT_PUBLICATION_ANNOUNCER
| DISC_BUILTIN_ENDPOINT_SUBSCRIPTION_DETECTOR
| DISC_BUILTIN_ENDPOINT_PUBLICATION_DETECTOR
| DISC_BUILTIN_ENDPOINT_SUBSCRIPTION_ANNOUNCER;
#if HAVE_SECURITY
if (getRTPSParticipant()->is_secure())
{
participant_data->m_availableBuiltinEndpoints
|= DISC_BUILTIN_ENDPOINT_PUBLICATION_SECURE_ANNOUNCER
| DISC_BUILTIN_ENDPOINT_SUBSCRIPTION_SECURE_DETECTOR
| DISC_BUILTIN_ENDPOINT_SUBSCRIPTION_SECURE_ANNOUNCER
| DISC_BUILTIN_ENDPOINT_PUBLICATION_SECURE_DETECTOR;
}
#endif //HAVE_SECURITY
const SimpleEDPAttributes& se = getRTPSParticipant()->getAttributes().builtin.discovery_config.m_simpleEDP;
if (!(se.use_PublicationWriterANDSubscriptionReader && se.use_PublicationReaderANDSubscriptionWriter))
{
EPROSIMA_LOG_WARNING(RTPS_PDP_SERVER, "SERVER or BACKUP PDP requires always all EDP endpoints creation.");
}
// Set discovery server version property
participant_data->m_properties.push_back(
std::pair<std::string,
std::string>({dds::parameter_property_ds_version, dds::parameter_property_current_ds_version}));
}
void PDPServer::match_reliable_pdp_endpoints(
const ParticipantProxyData& pdata)
{
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
const NetworkFactory& network = mp_RTPSParticipant->network_factory();
uint32_t endp = pdata.m_availableBuiltinEndpoints;
bool use_multicast_locators = !mp_RTPSParticipant->getAttributes().builtin.avoid_builtin_multicast ||
pdata.metatraffic_locators.unicast.empty();
// only SERVER and CLIENT participants will be received. All builtin must be there
uint32_t auxendp = endp &
(DISC_BUILTIN_ENDPOINT_PARTICIPANT_ANNOUNCER |
DISC_BUILTIN_ENDPOINT_PARTICIPANT_SECURE_ANNOUNCER);
if (0 != auxendp)
{
auto temp_writer_data = get_temporary_writer_proxies_pool().get();
temp_writer_data->clear();
temp_writer_data->guid().guidPrefix = pdata.m_guid.guidPrefix;
temp_writer_data->guid().entityId = endpoints->writer.writer_->getGuid().entityId;
temp_writer_data->persistence_guid(pdata.get_persistence_guid());
temp_writer_data->set_persistence_entity_id(c_EntityId_SPDPWriter);
temp_writer_data->set_remote_locators(pdata.metatraffic_locators, network, use_multicast_locators);
temp_writer_data->m_qos.m_reliability.kind = dds::RELIABLE_RELIABILITY_QOS;
temp_writer_data->m_qos.m_durability.kind = dds::TRANSIENT_LOCAL_DURABILITY_QOS;
#if HAVE_SECURITY
if (should_protect_discovery())
{
mp_RTPSParticipant->security_manager().discovered_builtin_writer(
endpoints->reader.reader_->getGuid(), pdata.m_guid,
*temp_writer_data, endpoints->reader.reader_->getAttributes().security_attributes());
}
else
#endif // HAVE_SECURITY
{
endpoints->reader.reader_->matched_writer_add(*temp_writer_data);
}
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Participant " << pdata.m_guid.guidPrefix
<< " did not send information about builtin writers");
return;
}
// only SERVER and CLIENT participants will be received. All builtin must be there
auxendp = endp & (DISC_BUILTIN_ENDPOINT_PARTICIPANT_DETECTOR | DISC_BUILTIN_ENDPOINT_PARTICIPANT_SECURE_DETECTOR);
if (0 != auxendp)
{
auto temp_reader_data = get_temporary_reader_proxies_pool().get();
temp_reader_data->clear();
temp_reader_data->m_expectsInlineQos = false;
temp_reader_data->guid().guidPrefix = pdata.m_guid.guidPrefix;
temp_reader_data->guid().entityId = endpoints->reader.reader_->getGuid().entityId;
temp_reader_data->set_remote_locators(pdata.metatraffic_locators, network, use_multicast_locators);
temp_reader_data->m_qos.m_reliability.kind = dds::RELIABLE_RELIABILITY_QOS;
temp_reader_data->m_qos.m_durability.kind = dds::TRANSIENT_LOCAL_DURABILITY_QOS;
#if HAVE_SECURITY
if (should_protect_discovery())
{
mp_RTPSParticipant->security_manager().discovered_builtin_reader(
endpoints->writer.writer_->getGuid(), pdata.m_guid,
*temp_reader_data, endpoints->writer.writer_->getAttributes().security_attributes());
}
else
#endif // HAVE_SECURITY
{
endpoints->writer.writer_->matched_reader_add(*temp_reader_data);
}
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Participant " << pdata.m_guid.guidPrefix
<< " did not send information about builtin readers");
return;
}
}
void PDPServer::assignRemoteEndpoints(
ParticipantProxyData* pdata)
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "Assigning remote endpoint for RTPSParticipant: " << pdata->m_guid.guidPrefix);
match_reliable_pdp_endpoints(*pdata);
#if HAVE_SECURITY
if (mp_RTPSParticipant->security_manager().discovered_participant(*pdata))
#endif // HAVE_SECURITY
{
perform_builtin_endpoints_matching(*pdata);
}
}
void PDPServer::notifyAboveRemoteEndpoints(
const ParticipantProxyData& pdata,
bool /*notify_secure_endpoints*/)
{
static_cast<void>(pdata);
#if HAVE_SECURITY
match_reliable_pdp_endpoints(pdata);
#endif // HAVE_SECURITY
}
#if HAVE_SECURITY
bool PDPServer::pairing_remote_writer_with_local_reader_after_security(
const GUID_t& local_reader,
const WriterProxyData& remote_writer_data)
{
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
if (local_reader == endpoints->reader.reader_->getGuid())
{
endpoints->reader.reader_->matched_writer_add(remote_writer_data);
return true;
}
return PDP::pairing_remote_writer_with_local_reader_after_security(local_reader, remote_writer_data);
}
bool PDPServer::pairing_remote_reader_with_local_writer_after_security(
const GUID_t& local_writer,
const ReaderProxyData& remote_reader_data)
{
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
if (local_writer == endpoints->writer.writer_->getGuid())
{
endpoints->writer.writer_->matched_reader_add(remote_reader_data);
return true;
}
return PDP::pairing_remote_reader_with_local_writer_after_security(local_writer, remote_reader_data);
}
#endif // HAVE_SECURITY
void PDPServer::perform_builtin_endpoints_matching(
const ParticipantProxyData& pdata)
{
//Inform EDP of new RTPSParticipant data:
if (mp_EDP != nullptr)
{
mp_EDP->assignRemoteEndpoints(pdata, true);
}
if (mp_builtin->mp_WLP != nullptr)
{
mp_builtin->mp_WLP->assignRemoteEndpoints(pdata, true);
}
mp_builtin->typelookup_manager_->assign_remote_endpoints(pdata);
}
void PDPServer::removeRemoteEndpoints(
ParticipantProxyData* pdata)
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER, "For RTPSParticipant: " << pdata->m_guid);
uint32_t endp = pdata->m_availableBuiltinEndpoints;
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
if (endp & (DISC_BUILTIN_ENDPOINT_PARTICIPANT_ANNOUNCER | DISC_BUILTIN_ENDPOINT_PARTICIPANT_SECURE_ANNOUNCER))
{
GUID_t writer_guid(pdata->m_guid.guidPrefix, endpoints->writer.writer_->getGuid().entityId);
endpoints->reader.reader_->matched_writer_remove(writer_guid);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Participant " << pdata->m_guid.guidPrefix
<< " did not send information about builtin writers");
return;
}
if (endp & (DISC_BUILTIN_ENDPOINT_PARTICIPANT_DETECTOR | DISC_BUILTIN_ENDPOINT_PARTICIPANT_SECURE_DETECTOR))
{
GUID_t reader_guid(pdata->m_guid.guidPrefix, endpoints->reader.reader_->getGuid().entityId);
endpoints->writer.writer_->matched_reader_remove(reader_guid);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Participant " << pdata->m_guid.guidPrefix
<< " did not send information about builtin readers");
return;
}
}
std::ostringstream PDPServer::get_persistence_file_name_() const
{
assert(getRTPSParticipant());
std::ostringstream filename(std::ios_base::ate);
std::string prefix;
// . is not suitable separator for filenames
filename << "server-" << getRTPSParticipant()->getGuid().guidPrefix;
prefix = filename.str();
std::replace(prefix.begin(), prefix.end(), '.', '-');
filename.str(std::move(prefix));
//filename << ".json";
return filename;
}
std::string PDPServer::get_writer_persistence_file_name() const
{
std::ostringstream filename = get_persistence_file_name_();
filename << "_writer.db";
return filename.str();
}
std::string PDPServer::get_reader_persistence_file_name() const
{
std::ostringstream filename = get_persistence_file_name_();
filename << "_reader.db";
return filename.str();
}
std::string PDPServer::get_ddb_persistence_file_name() const
{
std::ostringstream filename = get_persistence_file_name_();
filename << ".json";
return filename.str();
}
std::string PDPServer::get_ddb_queue_persistence_file_name() const
{
std::ostringstream filename = get_persistence_file_name_();
filename << "_queue.json";
return filename.str();
}
void PDPServer::announceParticipantState(
bool new_change,
bool dispose /* = false */,
WriteParams& )
{
if (enabled_)
{
EPROSIMA_LOG_INFO(RTPS_PDP_SERVER,
"Announcing Server " << mp_RTPSParticipant->getGuid() << " (new change: " << new_change << ")");
CacheChange_t* change = nullptr;
/*
Protect writer sequence number. Make sure in order to prevent AB BA deadlock that the
PDP mutex is systematically locked before the writer one (if needed):
- transport callbacks on PDPListener
- initialization and removal on BuiltinProtocols::initBuiltinProtocols and ~BuiltinProtocols
- DSClientEvent (own thread)
- ResendParticipantProxyDataPeriod (participant event thread)
*/
getMutex()->lock();
auto endpoints = static_cast<fastdds::rtps::DiscoveryServerPDPEndpoints*>(builtin_endpoints_.get());
assert(endpoints->writer.writer_);
fastrtps::rtps::StatefulWriter& writer = *(endpoints->writer.writer_);
WriterHistory& history = *endpoints->writer.history_;
std::lock_guard<fastrtps::RecursiveTimedMutex> wlock(writer.getMutex());
if (!dispose)
{
// Create the CacheChange_t if necessary
if (m_hasChangedLocalPDP.exchange(false) || new_change)
{
// Copy the participant data
ParticipantProxyData proxy_data_copy(*getLocalParticipantProxyData());
// Prepare identity
WriteParams wp;
SequenceNumber_t sn = history.next_sequence_number();
{
SampleIdentity local;
local.writer_guid(writer.getGuid());
local.sequence_number(sn);
wp.sample_identity(local);
wp.related_sample_identity(local);
}
// Unlock PDP mutex since it's no longer needed.
getMutex()->unlock();
uint32_t cdr_size = proxy_data_copy.get_serialized_size(true);
change = writer.new_change(
[cdr_size]() -> uint32_t
{
return cdr_size;
},
ALIVE, proxy_data_copy.m_key);
if (change != nullptr)
{
CDRMessage_t aux_msg(change->serializedPayload);
#if __BIG_ENDIAN__
change->serializedPayload.encapsulation = (uint16_t)PL_CDR_BE;
aux_msg.msg_endian = BIGEND;
#else
change->serializedPayload.encapsulation = (uint16_t)PL_CDR_LE;
aux_msg.msg_endian = LITTLEEND;
#endif // if __BIG_ENDIAN__
if (proxy_data_copy.writeToCDRMessage(&aux_msg, true))
{
change->serializedPayload.length = (uint16_t)aux_msg.length;
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER, "Cannot serialize ParticipantProxyData.");
return;
}
// assign identity
change->sequenceNumber = sn;
// Create a RemoteLocatorList for metatraffic_locators
fastrtps::rtps::RemoteLocatorList metatraffic_locators(
mp_builtin->m_metatrafficUnicastLocatorList.size(),
mp_builtin->m_metatrafficMulticastLocatorList.size());
// Populate with server's unicast locators
for (auto locator : mp_builtin->m_metatrafficUnicastLocatorList)
{
metatraffic_locators.add_unicast_locator(locator);
}
// Populate with server's multicast locators
for (auto locator : mp_builtin->m_metatrafficMulticastLocatorList)
{
metatraffic_locators.add_multicast_locator(locator);
}
// Add our change to PDPWriterHistory
history.add_change(change, wp);
change->write_params = wp;
// Update the database with our own data
if (discovery_db().update(
change,
ddb::DiscoveryParticipantChangeData(metatraffic_locators, false, true)))
{
// Distribute
awake_routine_thread();
}
else
{
// Already there, dispose
EPROSIMA_LOG_ERROR(RTPS_PDP_SERVER,
"DiscoveryDatabase already initialized with local DATA(p) on creation");
writer.release_change(change);
}
}
// Doesn't make sense to send the DATA directly if it hasn't been introduced in the history yet (missing
// sequence number.
return;
}
else
{
// Unlock PDP mutex since it's no longer needed.
getMutex()->unlock();
// Retrieve the CacheChange_t from the database
change = discovery_db().cache_change_own_participant();
if (nullptr == change)
{
// This case is when the local Server DATA(P) has been included already in database by update method
// but the routine thread has not consumed it yet.
// This would happen when the routine thread is busy in initializing, i.e. it already has other
// DATA(P) to parse before the own one is inserted by update.
EPROSIMA_LOG_WARNING(RTPS_PDP_SERVER, "Local Server DATA(p) uninitialized before local on announcement. "
<< "It will be sent in next announce iteration.");
return;
}
}
}
else
{
// Copy the participant data
ParticipantProxyData* local_participant = getLocalParticipantProxyData();
InstanceHandle_t key = local_participant->m_key;
uint32_t cdr_size = local_participant->get_serialized_size(true);
local_participant = nullptr;
// Prepare identity
WriteParams wp;
SequenceNumber_t sn = history.next_sequence_number();
{
SampleIdentity local;
local.writer_guid(writer.getGuid());
local.sequence_number(sn);
wp.sample_identity(local);
wp.related_sample_identity(local);
}
// Unlock PDP mutex since it's no longer needed.
getMutex()->unlock();
change = writer.new_change(
[cdr_size]() -> uint32_t
{
return cdr_size;
},
NOT_ALIVE_DISPOSED_UNREGISTERED, key);
// Generate the Data(Up)
if (nullptr != change)
{
// Assign identity
change->sequenceNumber = sn;
change->write_params = std::move(wp);
// Update the database with our own data
if (discovery_db().update(change, ddb::DiscoveryParticipantChangeData()))
{
// Distribute
awake_routine_thread();
}
else
{
// Dispose if already there
// It may happen if the participant is not removed fast enough
writer.release_change(change);
return;
}
}