forked from eProsima/Fast-DDS-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDSCodeTester.cpp
5982 lines (5119 loc) · 209 KB
/
DDSCodeTester.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
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
#include <fastdds/dds/core/condition/GuardCondition.hpp>
#include <fastdds/dds/core/condition/WaitSet.hpp>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/DomainParticipant.hpp>
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
#include <fastdds/dds/publisher/PublisherListener.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/subscriber/SubscriberListener.hpp>
#include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
#include <fastdds/dds/subscriber/SubscriberListener.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/topic/Topic.hpp>
#include <fastdds/dds/topic/qos/TopicQos.hpp>
#include <fastdds/dds/topic/TopicListener.hpp>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastrtps/types/DynamicTypePtr.h>
#include <fastrtps/types/DynamicDataFactory.h>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/dds/log/OStreamConsumer.hpp>
#include <fastdds/dds/log/StdoutConsumer.hpp>
#include <fastdds/dds/log/StdoutErrConsumer.hpp>
#include <fastdds/dds/log/FileConsumer.hpp>
#include <fastdds/rtps/transport/TCPTransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastdds/rtps/transport/ChainingTransportDescriptor.h>
#include <fastdds/rtps/transport/ChainingTransport.h>
#include <fastdds/statistics/dds/domain/DomainParticipant.hpp>
#include <fastdds/statistics/dds/publisher/qos/DataWriterQos.hpp>
#include <fastdds/statistics/topic_names.hpp>
#include <fastrtps/utils/IPLocator.h>
#include <fastcdr/Cdr.h>
#include <sstream>
using namespace eprosima::fastdds::dds;
class CustomChainingTransportDescriptor : public eprosima::fastdds::rtps::ChainingTransportDescriptor
{
public:
CustomChainingTransportDescriptor(
std::shared_ptr<eprosima::fastdds::rtps::TransportDescriptorInterface> low_level)
: ChainingTransportDescriptor(low_level)
{
}
eprosima::fastdds::rtps::TransportInterface* create_transport() const override;
};
//CHAINING_TRANSPORT_OVERRIDE
class CustomChainingTransport : public eprosima::fastdds::rtps::ChainingTransport
{
public:
CustomChainingTransport(
const CustomChainingTransportDescriptor& descriptor)
: ChainingTransport(descriptor)
, descriptor_(descriptor)
{
}
eprosima::fastdds::rtps::TransportDescriptorInterface* get_configuration()
{
return &descriptor_;
}
bool send(
eprosima::fastrtps::rtps::SenderResource* low_sender_resource,
const eprosima::fastrtps::rtps::octet* send_buffer,
uint32_t send_buffer_size,
eprosima::fastrtps::rtps::LocatorsIterator* destination_locators_begin,
eprosima::fastrtps::rtps::LocatorsIterator* destination_locators_end,
const std::chrono::steady_clock::time_point& timeout) override
{
//
// Preprocess outcoming buffer.
//
// Call low level transport
return low_sender_resource->send(send_buffer, send_buffer_size, destination_locators_begin,
destination_locators_end, timeout);
}
void receive(
eprosima::fastdds::rtps::TransportReceiverInterface* next_receiver,
const eprosima::fastrtps::rtps::octet* receive_buffer,
uint32_t receive_buffer_size,
const eprosima::fastrtps::rtps::Locator_t& local_locator,
const eprosima::fastrtps::rtps::Locator_t& remote_locator) override
{
//
// Preprocess incoming buffer.
//
// Call upper level
next_receiver->OnDataReceived(receive_buffer, receive_buffer_size, local_locator, remote_locator);
}
private:
CustomChainingTransportDescriptor descriptor_;
};
//!--
eprosima::fastdds::rtps::TransportInterface* CustomChainingTransportDescriptor::create_transport() const
{
return new CustomChainingTransport(*this);
}
//DDS_DOMAINPARTICIPANT_LISTENER_SPECIALIZATION
class CustomDomainParticipantListener : public DomainParticipantListener
{
public:
CustomDomainParticipantListener()
: DomainParticipantListener()
{
}
virtual ~CustomDomainParticipantListener()
{
}
void on_participant_discovery(
DomainParticipant* /*participant*/,
eprosima::fastrtps::rtps::ParticipantDiscoveryInfo&& info) override
{
if (info.status == eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::DISCOVERED_PARTICIPANT)
{
std::cout << "New participant discovered" << std::endl;
}
else if (info.status == eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::REMOVED_PARTICIPANT ||
info.status == eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::DROPPED_PARTICIPANT)
{
std::cout << "New participant lost" << std::endl;
}
}
#if HAVE_SECURITY
void onParticipantAuthentication(
DomainParticipant* /*participant*/,
eprosima::fastrtps::rtps::ParticipantAuthenticationInfo&& info) override
{
if (info.status == eprosima::fastrtps::rtps::ParticipantAuthenticationInfo::AUTHORIZED_PARTICIPANT)
{
std::cout << "A participant was authorized" << std::endl;
}
else if (info.status == eprosima::fastrtps::rtps::ParticipantAuthenticationInfo::UNAUTHORIZED_PARTICIPANT)
{
std::cout << "A participant failed authorization" << std::endl;
}
}
#endif // if HAVE_SECURITY
void on_subscriber_discovery(
DomainParticipant* /*participant*/,
eprosima::fastrtps::rtps::ReaderDiscoveryInfo&& info) override
{
if (info.status == eprosima::fastrtps::rtps::ReaderDiscoveryInfo::DISCOVERED_READER)
{
std::cout << "New subscriber discovered" << std::endl;
}
else if (info.status == eprosima::fastrtps::rtps::ReaderDiscoveryInfo::REMOVED_READER)
{
std::cout << "New subscriber lost" << std::endl;
}
}
void on_publisher_discovery(
DomainParticipant* /*participant*/,
eprosima::fastrtps::rtps::WriterDiscoveryInfo&& info) override
{
if (info.status == eprosima::fastrtps::rtps::WriterDiscoveryInfo::DISCOVERED_WRITER)
{
std::cout << "New publisher discovered" << std::endl;
}
else if (info.status == eprosima::fastrtps::rtps::WriterDiscoveryInfo::REMOVED_WRITER)
{
std::cout << "New publisher lost" << std::endl;
}
}
void on_type_discovery(
DomainParticipant* participant,
const eprosima::fastrtps::rtps::SampleIdentity& request_sample_id,
const eprosima::fastrtps::string_255& topic,
const eprosima::fastrtps::types::TypeIdentifier* identifier,
const eprosima::fastrtps::types::TypeObject* object,
eprosima::fastrtps::types::DynamicType_ptr dyn_type) override
{
static_cast<void>(participant);
static_cast<void>(request_sample_id);
static_cast<void>(topic);
static_cast<void>(identifier);
static_cast<void>(object);
static_cast<void>(dyn_type);
std::cout << "New data type discovered" << std::endl;
}
void on_type_dependencies_reply(
DomainParticipant* participant,
const eprosima::fastrtps::rtps::SampleIdentity& request_sample_id,
const eprosima::fastrtps::types::TypeIdentifierWithSizeSeq& dependencies) override
{
static_cast<void>(participant);
static_cast<void>(request_sample_id);
static_cast<void>(dependencies);
std::cout << "Answer to a request for type dependencies was received" << std::endl;
}
void on_type_information_received(
DomainParticipant* participant,
const eprosima::fastrtps::string_255 topic_name,
const eprosima::fastrtps::string_255 type_name,
const eprosima::fastrtps::types::TypeInformation& type_information) override
{
static_cast<void>(participant);
static_cast<void>(topic_name);
static_cast<void>(type_name);
static_cast<void>(type_information);
std::cout << "New data type information received" << std::endl;
}
};
//!--
void dds_domain_examples()
{
{
//DDS_LOAD_XML_PROFILE
// Load the XML with the profiles
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
// Profiles can now be used to create Entities
DomainParticipant* participant_with_profile =
DomainParticipantFactory::get_instance()->create_participant_with_profile(0, "participant_profile");
if (nullptr == participant_with_profile)
{
// Error
return;
}
//!--
}
{
//DDS_CREATE_DOMAINPARTICIPANT
// Create a DomainParticipant with default DomainParticipantQos and no Listener
// The value PARTICIPANT_QOS_DEFAULT is used to denote the default QoS.
DomainParticipant* participant_with_default_attributes =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant_with_default_attributes)
{
// Error
return;
}
// A custom DomainParticipantQos can be provided to the creation method
DomainParticipantQos custom_qos;
// Modify QoS attributes
// (...)
DomainParticipant* participant_with_custom_qos =
DomainParticipantFactory::get_instance()->create_participant(0, custom_qos);
if (nullptr == participant_with_custom_qos)
{
// Error
return;
}
// Create a DomainParticipant with default QoS and a custom Listener.
// CustomDomainParticipantListener inherits from DomainParticipantListener.
// The value PARTICIPANT_QOS_DEFAULT is used to denote the default QoS.
CustomDomainParticipantListener custom_listener;
DomainParticipant* participant_with_default_qos_and_custom_listener =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT,
&custom_listener);
if (nullptr == participant_with_default_qos_and_custom_listener)
{
// Error
return;
}
//!--
}
{
//DDS_CREATE_PROFILE_DOMAINPARTICIPANT
// First load the XML with the profiles
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
// Create a DomainParticipant using a profile and no Listener
DomainParticipant* participant_with_profile =
DomainParticipantFactory::get_instance()->create_participant_with_profile(0, "participant_profile");
if (nullptr == participant_with_profile)
{
// Error
return;
}
// Create a DomainParticipant using a profile and a custom Listener.
// CustomDomainParticipantListener inherits from DomainParticipantListener.
CustomDomainParticipantListener custom_listener;
DomainParticipant* participant_with_profile_and_custom_listener =
DomainParticipantFactory::get_instance()->create_participant_with_profile(0, "participant_profile",
&custom_listener);
if (nullptr == participant_with_profile_and_custom_listener)
{
// Error
return;
}
//!--
}
{
//DDS_CHANGE_DOMAINPARTICIPANTQOS
// Create a DomainParticipant with default DomainParticipantQos
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant)
{
// Error
return;
}
// Get the current QoS or create a new one from scratch
DomainParticipantQos qos = participant->get_qos();
// Modify QoS attributes
qos.entity_factory().autoenable_created_entities = false;
// Assign the new Qos to the object
participant->set_qos(qos);
//!--
}
{
//DDS_CHANGE_DOMAINPARTICIPANTQOS_TO_DEFAULT
// Create a custom DomainParticipantQos
DomainParticipantQos custom_qos;
// Modify QoS attributes
// (...)
// Create a DomainParticipant with a custom DomainParticipantQos
DomainParticipant* participant = DomainParticipantFactory::get_instance()->create_participant(0, custom_qos);
if (nullptr == participant)
{
// Error
return;
}
// Set the QoS on the participant to the default
if (participant->set_qos(PARTICIPANT_QOS_DEFAULT) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// The previous instruction is equivalent to the following:
if (participant->set_qos(DomainParticipantFactory::get_instance()->get_default_participant_qos())
!= ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
//!--
}
{
//DDS_DELETE_DOMAINPARTICIPANT
// Create a DomainParticipant
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant)
{
// Error
return;
}
// Use the DomainParticipant to communicate
// (...)
// Delete entities created by the DomainParticipant
if (participant->delete_contained_entities() != ReturnCode_t::RETCODE_OK)
{
// DomainParticipant failed to delete the entities it created.
return;
}
// Delete the DomainParticipant
if (DomainParticipantFactory::get_instance()->delete_participant(participant) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
//!--
}
{
//DDS_CHANGE_DEFAULT_DOMAINPARTICIPANTQOS
// Get the current QoS or create a new one from scratch
DomainParticipantQos qos_type1 = DomainParticipantFactory::get_instance()->get_default_participant_qos();
// Modify QoS attributes
// (...)
// Set as the new default TopicQos
if (DomainParticipantFactory::get_instance()->set_default_participant_qos(qos_type1) !=
ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Create a DomainParticipant with the new default DomainParticipantQos.
DomainParticipant* participant_with_qos_type1 =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant_with_qos_type1)
{
// Error
return;
}
// Get the current QoS or create a new one from scratch
DomainParticipantQos qos_type2;
// Modify QoS attributes
// (...)
// Set as the new default TopicQos
if (DomainParticipantFactory::get_instance()->set_default_participant_qos(qos_type2) !=
ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Create a Topic with the new default TopicQos.
DomainParticipant* participant_with_qos_type2 =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant_with_qos_type2)
{
// Error
return;
}
// Resetting the default DomainParticipantQos to the original default constructed values
if (DomainParticipantFactory::get_instance()->set_default_participant_qos(PARTICIPANT_QOS_DEFAULT)
!= ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// The previous instruction is equivalent to the following
if (DomainParticipantFactory::get_instance()->set_default_participant_qos(DomainParticipantQos())
!= ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
//!--
}
{
//DDS_CHANGE_DOMAINPARTICIPANTFACTORYQOS
DomainParticipantFactoryQos qos;
// Setting autoenable_created_entities to true makes the created DomainParticipants
// to be enabled upon creation
qos.entity_factory().autoenable_created_entities = true;
if (DomainParticipantFactory::get_instance()->set_qos(qos) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Create a DomainParticipant with the new DomainParticipantFactoryQos.
// The returned DomainParticipant is already enabled
DomainParticipant* enabled_participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == enabled_participant)
{
// Error
return;
}
// Setting autoenable_created_entities to false makes the created DomainParticipants
// to be disabled upon creation
qos.entity_factory().autoenable_created_entities = false;
if (DomainParticipantFactory::get_instance()->set_qos(qos) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Create a DomainParticipant with the new DomainParticipantFactoryQos.
// The returned DomainParticipant is disabled and will need to be enabled explicitly
DomainParticipant* disabled_participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == disabled_participant)
{
// Error
return;
}
//!--
}
{
// DDS_SECURITY_AUTH_PLUGIN
DomainParticipantQos pqos;
// Activate DDS:Auth:PKI-DH plugin
pqos.properties().properties().emplace_back("dds.sec.auth.plugin",
"builtin.PKI-DH");
// Configure DDS:Auth:PKI-DH plugin
pqos.properties().properties().emplace_back(
"dds.sec.auth.builtin.PKI-DH.identity_ca",
"file://maincacert.pem");
pqos.properties().properties().emplace_back(
"dds.sec.auth.builtin.PKI-DH.identity_certificate",
"file://partcert.pem");
pqos.properties().properties().emplace_back(
"dds.sec.auth.builtin.PKI-DH.identity_crl",
"file://crl.pem");
pqos.properties().properties().emplace_back(
"dds.sec.auth.builtin.PKI-DH.private_key",
"file://partkey.pem");
pqos.properties().properties().emplace_back(
"dds.sec.auth.builtin.PKI-DH.password",
"domainParticipantPassword");
//!--
}
{
// DDS_SECURITY_ACCESS_CONTROL_PLUGIN
DomainParticipantQos pqos;
// Activate DDS:Access:Permissions plugin
pqos.properties().properties().emplace_back("dds.sec.access.plugin",
"builtin.Access-Permissions");
// Configure DDS:Access:Permissions plugin
pqos.properties().properties().emplace_back(
"dds.sec.access.builtin.Access-Permissions.permissions_ca",
"file://certs/maincacert.pem");
pqos.properties().properties().emplace_back(
"dds.sec.access.builtin.Access-Permissions.governance",
"file://certs/governance.smime");
pqos.properties().properties().emplace_back(
"dds.sec.access.builtin.Access-Permissions.permissions",
"file://certs/permissions.smime");
//!--
}
{
// DDS_SECURITY_CRYPTO_PLUGIN_DOMAINPARTICIPANT
DomainParticipantQos pqos;
// Activate DDS:Crypto:AES-GCM-GMAC plugin
pqos.properties().properties().emplace_back("dds.sec.crypto.plugin",
"builtin.AES-GCM-GMAC");
// Only if DDS:Access:Permissions plugin is not enabled
// Configure DDS:Crypto:AES-GCM-GMAC plugin
pqos.properties().properties().emplace_back(
"rtps.participant.rtps_protection_kind",
"ENCRYPT");
//!--
// DDS_SECURITY_CRYPTO_PLUGIN_DATAWRITER
DataWriterQos wqos;
// Only if DDS:Access:Permissions plugin is not enabled
// Configure DDS:Crypto:AES-GCM-GMAC plugin
wqos.properties().properties().emplace_back(
"rtps.endpoint.submessage_protection_kind",
"ENCRYPT");
wqos.properties().properties().emplace_back(
"rtps.endpoint.payload_protection_kind",
"ENCRYPT");
//!--
// DDS_SECURITY_CRYPTO_PLUGIN_DATAREADER
DataWriterQos rqos;
// Only if DDS:Access:Permissions plugin is not enabled
// Configure DDS:Crypto:AES-GCM-GMAC plugin
rqos.properties().properties().emplace_back(
"rtps.endpoint.submessage_protection_kind",
"ENCRYPT");
//!--
}
{
// DDS_SECURITY_LOGGING_PLUGIN
DomainParticipantQos pqos;
// Activate DDS:Logging:DDS_LogTopic plugin
pqos.properties().properties().emplace_back("dds.sec.log.plugin",
"builtin.DDS_LogTopic");
// Configure DDS:Logging:DDS_LogTopic plugin
pqos.properties().properties().emplace_back(
"dds.sec.log.builtin.DDS_LogTopic.logging_level",
"EMERGENCY_LEVEL");
pqos.properties().properties().emplace_back(
"dds.sec.log.builtin.DDS_LogTopic.log_file",
"myLogFile.log");
//!--
}
{
// FASTDDS_STATISTICS_MODULE
DomainParticipantQos pqos;
// Activate Fast DDS Statistics module
pqos.properties().properties().emplace_back("fastdds.statistics",
"HISTORY_LATENCY_TOPIC;ACKNACK_COUNT_TOPIC;DISCOVERY_TOPIC;PHYSICAL_DATA_TOPIC");
//!--
}
{
// FASTDDS_PHYSICAL_PROPERTIES
/* Create participant which announces default physical properties */
DomainParticipantQos pqos_default_physical;
// NOTE: If FASTDDS_STATISTICS is defined, then setting the properties to "" is not necessary
pqos_default_physical.properties().properties().emplace_back("fastdds.physical_data.host", "");
pqos_default_physical.properties().properties().emplace_back("fastdds.physical_data.user", "");
pqos_default_physical.properties().properties().emplace_back("fastdds.physical_data.process", "");
DomainParticipant* participant_with_physical = DomainParticipantFactory::get_instance()->create_participant(0,
pqos_default_physical);
/* Create participant which announces custom physical properties */
DomainParticipantQos pqos_custom_physical;
// NOTE: If FASTDDS_STATISTICS is defined, then clear the properties before setting them
// pqos_custom_physical.properties().properties().clear()
pqos_custom_physical.properties().properties().emplace_back("fastdds.physical_data.host", "custom_hostname");
pqos_custom_physical.properties().properties().emplace_back("fastdds.physical_data.user", "custom_username");
pqos_custom_physical.properties().properties().emplace_back("fastdds.physical_data.process", "custom_process");
DomainParticipant* participant_custom_physical = DomainParticipantFactory::get_instance()->create_participant(0,
pqos_custom_physical);
/* Create participant which does not announce physical properties */
DomainParticipantQos pqos_no_physical;
pqos_no_physical.properties().properties().clear();
DomainParticipant* participant_without_physical = DomainParticipantFactory::get_instance()->create_participant(
0, pqos_no_physical);
/* Load physical properties from default XML file */
DomainParticipantFactory::get_instance()->load_profiles();
DomainParticipantQos pqos_default_xml_physical =
DomainParticipantFactory::get_instance()->get_default_participant_qos();
DomainParticipant* participant_default_xml_physical =
DomainParticipantFactory::get_instance()->create_participant(0, pqos_default_xml_physical);
/* Load physical properties from specific XML file */
DomainParticipantFactory::get_instance()->load_XML_profiles_file("somefile.xml");
DomainParticipantFactory::get_instance()->load_profiles();
DomainParticipantQos pqos_custom_xml_physical =
DomainParticipantFactory::get_instance()->get_default_participant_qos();
DomainParticipant* participant_custom_xml_physical =
DomainParticipantFactory::get_instance()->create_participant(0, pqos_custom_xml_physical);
//!--
DomainParticipantFactory::get_instance()->delete_participant(participant_with_physical);
DomainParticipantFactory::get_instance()->delete_participant(participant_custom_physical);
DomainParticipantFactory::get_instance()->delete_participant(participant_without_physical);
DomainParticipantFactory::get_instance()->delete_participant(participant_default_xml_physical);
DomainParticipantFactory::get_instance()->delete_participant(participant_custom_xml_physical);
}
{
// ENABLE_DISABLE_STATISTICS_DATAWRITER
// Create a DomainParticipant
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant)
{
// Error
return;
}
// Obtain pointer to child class
eprosima::fastdds::statistics::dds::DomainParticipant* statistics_participant =
eprosima::fastdds::statistics::dds::DomainParticipant::narrow(participant);
// Enable statistics DataWriter
if (statistics_participant->enable_statistics_datawriter(eprosima::fastdds::statistics::GAP_COUNT_TOPIC,
eprosima::fastdds::statistics::dds::STATISTICS_DATAWRITER_QOS) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Use the DomainParticipant to communicate
// (...)
// Disable statistics DataWriter
if (statistics_participant->disable_statistics_datawriter(eprosima::fastdds::statistics::GAP_COUNT_TOPIC) !=
ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
// Delete DomainParticipant
if (DomainParticipantFactory::get_instance()->delete_participant(participant) != ReturnCode_t::RETCODE_OK)
{
// Error
return;
}
//!--
}
{
// PULL_MODE_DATAWRITER
DataWriterQos wqos;
// Enable pull mode
wqos.properties().properties().emplace_back(
"fastdds.push_mode",
"false");
//!--
}
{
//CONF-QOS-PARTITIONS
PublisherQos pub_11_qos;
pub_11_qos.partition().push_back("Partition_1");
pub_11_qos.partition().push_back("Partition_2");
PublisherQos pub_12_qos;
pub_12_qos.partition().push_back("*");
PublisherQos pub_21_qos;
//No partitions defined for pub_21
PublisherQos pub_22_qos;
pub_22_qos.partition().push_back("Partition*");
SubscriberQos subs_31_qos;
subs_31_qos.partition().push_back("Partition_1");
SubscriberQos subs_32_qos;
subs_32_qos.partition().push_back("Partition_2");
SubscriberQos subs_33_qos;
subs_33_qos.partition().push_back("Partition_3");
SubscriberQos subs_34_qos;
//No partitions defined for subs_34
//!--
}
{
// PARTITION-ON-ENDPOINT
DataWriterQos wqos;
// Add partitions
wqos.properties().properties().emplace_back(
"partitions",
"part1;part2");
DataReaderQos rqos;
// Add partitions
rqos.properties().properties().emplace_back(
"partitions",
"part1;part2");
//!--
}
{
//DDS-STATIC-DISCOVERY-FORMAT
DomainParticipantQos participant_qos;
participant_qos.properties().properties().emplace_back(
"dds.discovery.static_edp.exchange_format",
"v1_Reduced"
);
//!--
}
}
//DOMAINPARTICIPANTLISTENER-DISCOVERY-CALLBACKS
class DiscoveryDomainParticipantListener : public DomainParticipantListener
{
/* Custom Callback on_participant_discovery */
void on_participant_discovery(
DomainParticipant* participant,
eprosima::fastrtps::rtps::ParticipantDiscoveryInfo&& info) override
{
static_cast<void>(participant);
switch (info.status){
case eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::DISCOVERED_PARTICIPANT:
/* Process the case when a new DomainParticipant was found in the domain */
std::cout << "New DomainParticipant '" << info.info.m_participantName <<
"' with ID '" << info.info.m_guid.entityId << "' and GuidPrefix '" <<
info.info.m_guid.guidPrefix << "' discovered." << std::endl;
break;
case eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::CHANGED_QOS_PARTICIPANT:
/* Process the case when a DomainParticipant changed its QOS */
break;
case eprosima::fastrtps::rtps::ParticipantDiscoveryInfo::REMOVED_PARTICIPANT:
/* Process the case when a DomainParticipant was removed from the domain */
std::cout << "New DomainParticipant '" << info.info.m_participantName <<
"' with ID '" << info.info.m_guid.entityId << "' and GuidPrefix '" <<
info.info.m_guid.guidPrefix << "' left the domain." << std::endl;
break;
}
}
/* Custom Callback on_subscriber_discovery */
void on_subscriber_discovery(
DomainParticipant* participant,
eprosima::fastrtps::rtps::ReaderDiscoveryInfo&& info) override
{
static_cast<void>(participant);
switch (info.status){
case eprosima::fastrtps::rtps::ReaderDiscoveryInfo::DISCOVERED_READER:
/* Process the case when a new subscriber was found in the domain */
std::cout << "New DataReader subscribed to topic '" << info.info.topicName() <<
"' of type '" << info.info.typeName() << "' discovered";
break;
case eprosima::fastrtps::rtps::ReaderDiscoveryInfo::CHANGED_QOS_READER:
/* Process the case when a subscriber changed its QOS */
break;
case eprosima::fastrtps::rtps::ReaderDiscoveryInfo::REMOVED_READER:
/* Process the case when a subscriber was removed from the domain */
std::cout << "New DataReader subscribed to topic '" << info.info.topicName() <<
"' of type '" << info.info.typeName() << "' left the domain.";
break;
}
}
/* Custom Callback on_publisher_discovery */
void on_publisher_discovery(
DomainParticipant* participant,
eprosima::fastrtps::rtps::WriterDiscoveryInfo&& info) override
{
static_cast<void>(participant);
switch (info.status){
case eprosima::fastrtps::rtps::WriterDiscoveryInfo::DISCOVERED_WRITER:
/* Process the case when a new publisher was found in the domain */
std::cout << "New DataWriter publishing under topic '" << info.info.topicName() <<
"' of type '" << info.info.typeName() << "' discovered";
break;
case eprosima::fastrtps::rtps::WriterDiscoveryInfo::CHANGED_QOS_WRITER:
/* Process the case when a publisher changed its QOS */
break;
case eprosima::fastrtps::rtps::WriterDiscoveryInfo::REMOVED_WRITER:
/* Process the case when a publisher was removed from the domain */
std::cout << "New DataWriter publishing under topic '" << info.info.topicName() <<
"' of type '" << info.info.typeName() << "' left the domain.";
break;
}
}
/* Custom Callback on_type_discovery */
void on_type_discovery(
DomainParticipant* participant,
const eprosima::fastrtps::rtps::SampleIdentity& request_sample_id,
const eprosima::fastrtps::string_255& topic,
const eprosima::fastrtps::types::TypeIdentifier* identifier,
const eprosima::fastrtps::types::TypeObject* object,
eprosima::fastrtps::types::DynamicType_ptr dyn_type) override
{
static_cast<void>(participant);
static_cast<void>(request_sample_id);
static_cast<void>(topic);
static_cast<void>(identifier);
static_cast<void>(object);
static_cast<void>(dyn_type);
std::cout << "New data type of topic '" << topic << "' discovered." << std::endl;
}
};
//!--
void dds_discovery_examples()
{
using Locator_t = eprosima::fastrtps::rtps::Locator_t;
using RemoteServerAttributes = eprosima::fastrtps::rtps::RemoteServerAttributes;
using IPLocator = eprosima::fastrtps::rtps::IPLocator;
using DiscoveryProtocol_t = eprosima::fastrtps::rtps::DiscoveryProtocol_t;
using ParticipantFilteringFlags_t = eprosima::fastrtps::rtps::ParticipantFilteringFlags_t;
{
//SET-DISCOVERY-CALLBACKS
// Create the participant QoS and configure values
DomainParticipantQos pqos;
// Create a custom user DomainParticipantListener
DiscoveryDomainParticipantListener* plistener = new DiscoveryDomainParticipantListener();
// Pass the listener on DomainParticipant creation.
DomainParticipant* participant =
DomainParticipantFactory::get_instance()->create_participant(
0, pqos, plistener);
//!--
}
{
//CONF-DISCOVERY-PROTOCOL
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.discoveryProtocol =
DiscoveryProtocol_t::SIMPLE;
//!--
}
{
//CONF-DISCOVERY-IGNORE-FLAGS
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.ignoreParticipantFlags =
static_cast<eprosima::fastrtps::rtps::ParticipantFilteringFlags_t>(
ParticipantFilteringFlags_t::FILTER_DIFFERENT_PROCESS |
ParticipantFilteringFlags_t::FILTER_SAME_PROCESS);
//!--
}
{
//CONF-DISCOVERY-LEASE-DURATION
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.leaseDuration = Duration_t(10, 20);
//!--
}
{
//CONF-DISCOVERY-LEASE-ANNOUNCEMENT
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(1, 2);
//!--
}
{
//DISCOVERY-CONFIG-INITIAL-ANNOUNCEMENT
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.initial_announcements.count = 5;
pqos.wire_protocol().builtin.discovery_config.initial_announcements.period = Duration_t(0, 100000000u);
//!--
}
{
//CONF-QOS-DISCOVERY-EDP-ATTRIBUTES
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
pqos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
pqos.wire_protocol().builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = false;
//!--
}
{
//CONF_STATIC_DISCOVERY_CODE
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = false;
pqos.wire_protocol().builtin.discovery_config.use_STATIC_EndpointDiscoveryProtocol = true;
//!--
}
{
//CONF_QOS_STATIC_DISCOVERY_USERID
// Configure the DataWriter
DataWriterQos wqos;
wqos.endpoint().user_defined_id = 1;
// Configure the DataReader
DataReaderQos rqos;
rqos.endpoint().user_defined_id = 3;
//!--
}
{
//CONF_STATIC_DISCOVERY_XML_FILE
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.static_edp_xml_config("file://RemotePublisher.xml");
pqos.wire_protocol().builtin.discovery_config.static_edp_xml_config("file://RemoteSubscriber.xml");
//!--
}
{
//CONF_STATIC_DISCOVERY_XML_DATA
DomainParticipantQos pqos;
pqos.wire_protocol().builtin.discovery_config.static_edp_xml_config(
"data://<?xml version=\"1.0\" encoding=\"utf-8\"?>" \
"<staticdiscovery><participant><name>RTPSParticipant</name></participant></staticdiscovery>");
//!--
}