forked from eProsima/Fast-DDS-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeTester.cpp
2075 lines (1724 loc) · 81.9 KB
/
CodeTester.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 <fastrtps/Domain.h>
#include <fastrtps/rtps/RTPSDomain.h>
#include <fastrtps/publisher/Publisher.h>
#include <fastrtps/subscriber/Subscriber.h>
#include <fastrtps/participant/ParticipantListener.h>
#include <fastrtps/publisher/PublisherListener.h>
#include <fastrtps/subscriber/SubscriberListener.h>
#include <fastrtps/rtps/writer/RTPSWriter.h>
#include <fastrtps/rtps/reader/RTPSReader.h>
#include <fastrtps/rtps/reader/ReaderListener.h>
#include <fastrtps/rtps/history/WriterHistory.h>
#include <fastrtps/rtps/history/ReaderHistory.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastrtps/transport/UDPv4TransportDescriptor.h>
#include <fastrtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastrtps/types/DynamicDataFactory.h>
#include <fastrtps/utils/IPLocator.h>
#include <fastrtps/log/Log.h>
#include <fastrtps/log/FileConsumer.h>
#include <fastrtps/subscriber/SampleInfo.h>
#include <fastrtps/TopicDataType.h>
#include <fstream>
using namespace eprosima::fastrtps;
using namespace ::rtps;
using namespace ::security;
using namespace ::types;
using SharedMemTransportDescriptor = eprosima::fastdds::rtps::SharedMemTransportDescriptor;
class HelloWorld
{
public:
void msg(
const std::string&)
{
}
std::string msg()
{
return "";
}
};
class HelloWorldPubSubType : public TopicDataType
{
bool serialize(
void* data,
rtps::SerializedPayload_t* payload) override
{
return false;
}
bool deserialize(
rtps::SerializedPayload_t* payload,
void* data) override
{
return false;
}
std::function<uint32_t()> getSerializedSizeProvider(
void* data) override
{
return []
{
return 0;
};
}
void* createData() override
{
return nullptr;
}
void deleteData(
void* data) override
{
}
bool getKey(
void* data,
rtps::InstanceHandle_t* ihandle,
bool force_md5 = false) override
{
return false;
}
};
//PUBSUB_API_PUBLISHER_LISTENER
class PubListener : public PublisherListener
{
public:
PubListener()
{
}
~PubListener()
{
}
void onPublicationmatched(
Publisher* pub,
MatchingInfo& info)
{
//Callback implementation. This is called each time the Publisher finds a Subscriber on the network that listens to the same topic.
}
};
//!--
//PUBSUB_API_SUBSCRIBER_LISTENER
class SubListener : public SubscriberListener
{
public:
SubListener()
{
}
~SubListener()
{
}
void onNewDataMessage(
Subscriber* sub)
{
if (sub->takeNextData((void*)&sample, &sample_info))
{
if (sample_info.sampleKind == ALIVE)
{
std::cout << "New message: " << sample.msg() << std::endl;
}
}
}
HelloWorld sample; //Storage for incoming messages
SampleInfo_t sample_info; //Auxiliary structure with meta-data on the message
};
//!--
void configuration_compilation_check()
{
ParticipantAttributes participant_attr;
PublisherAttributes publisher_attr;
SubscriberAttributes subscriber_attr;
{
//CONF-QOS-PARTITIONS
PublisherAttributes pub_11_attr;
pub_11_attr.qos.m_partition.push_back("Partition_1");
pub_11_attr.qos.m_partition.push_back("Partition_2");
PublisherAttributes pub_12_attr;
pub_12_attr.qos.m_partition.push_back("*");
PublisherAttributes pub_21_attr;
//No partitions defined for pub_21
PublisherAttributes pub_22_attr;
pub_22_attr.qos.m_partition.push_back("Partition*");
SubscriberAttributes subs_31_attr;
subs_31_attr.qos.m_partition.push_back("Partition_1");
SubscriberAttributes subs_32_attr;
subs_32_attr.qos.m_partition.push_back("Partition_2");
SubscriberAttributes subs_33_attr;
subs_33_attr.qos.m_partition.push_back("Partition_3");
SubscriberAttributes subs_34_attr;
//No partitions defined for subs_34
//!--
}
//CONF-COMMON-TRANSPORT-SETTING
//Create a descriptor for the new transport.
auto custom_transport = std::make_shared<UDPv4TransportDescriptor>();
custom_transport->sendBufferSize = 9216;
custom_transport->receiveBufferSize = 9216;
//Disable the built-in Transport Layer.
participant_attr.rtps.useBuiltinTransports = false;
//Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(custom_transport);
//!--
//CONF-UDP-TRANSPORT-SETTING
//Create a descriptor for the new transport.
auto udp_transport = std::make_shared<UDPv4TransportDescriptor>();
udp_transport->sendBufferSize = 9216;
udp_transport->receiveBufferSize = 9216;
udp_transport->non_blocking_send = true;
//Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(udp_transport);
//!--
//CONF-TCP-TRANSPORT-SETTING
//Create a descriptor for the new transport.
auto tcp_transport = std::make_shared<TCPv4TransportDescriptor>();
tcp_transport->sendBufferSize = 9216;
tcp_transport->receiveBufferSize = 9216;
tcp_transport->add_listener_port(5100);
tcp_transport->set_WAN_address("80.80.99.45");
//Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(tcp_transport);
//!--
//CONF-SHM-TRANSPORT-SETTING
// Create a descriptor for the new transport.
std::shared_ptr<SharedMemTransportDescriptor> shm_transport = std::make_shared<SharedMemTransportDescriptor>();
// Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(shm_transport);
//!--
//CONF-TCP2-TRANSPORT-SETTING
auto tcp2_transport = std::make_shared<TCPv4TransportDescriptor>();
//Disable the built-in Transport Layer.
participant_attr.rtps.useBuiltinTransports = false;
//Set initial peers.
Locator_t initial_peer_locator;
initial_peer_locator.kind = LOCATOR_KIND_TCPv4;
IPLocator::setIPv4(initial_peer_locator, "80.80.99.45");
initial_peer_locator.port = 5100;
participant_attr.rtps.builtin.initialPeersList.push_back(initial_peer_locator);
//Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(tcp2_transport);
//!--
//CONF-ALLOCATION-QOS-EXAMPLE
// Before creating a participant:
// We know we have 3 participants on the domain
participant_attr.rtps.allocation.participants =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(3u);
// We know we have at most 2 readers on each participant
participant_attr.rtps.allocation.readers =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(2u);
// We know we have at most 1 writer on each participant
participant_attr.rtps.allocation.writers =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(1u);
// We know the maximum size of partition data
participant_attr.rtps.allocation.data_limits.max_partitions = 256u;
// We know the maximum size of user data
participant_attr.rtps.allocation.data_limits.max_user_data = 256u;
// We know the maximum size of properties data
participant_attr.rtps.allocation.data_limits.max_properties = 512u;
// Before creating the publisher for topic 1:
// we know we will only have three matching subscribers
publisher_attr.matched_subscriber_allocation =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(3u);
// Before creating the publisher for topic 2:
// we know we will only have two matching subscribers
publisher_attr.matched_subscriber_allocation =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(2u);
// Before creating a subscriber:
// we know we will only have one matching publisher
subscriber_attr.matched_publisher_allocation =
eprosima::fastrtps::ResourceLimitedContainerConfig::fixed_size_configuration(1u);
//!--
//TYPELOOKUP_SERVICE_ENABLING
participant_attr.rtps.builtin.typelookup_config.use_client = true;
participant_attr.rtps.builtin.typelookup_config.use_server = true;
//!--
{
//CONF-TCP-TLS-SERVER
auto tls_transport = std::make_shared<TCPv4TransportDescriptor>();
using TLSOptions = TCPTransportDescriptor::TLSConfig::TLSOptions;
tls_transport->apply_security = true;
tls_transport->tls_config.password = "test";
tls_transport->tls_config.cert_chain_file = "server.pem";
tls_transport->tls_config.private_key_file = "serverkey.pem";
tls_transport->tls_config.tmp_dh_file = "dh2048.pem";
tls_transport->tls_config.add_option(TLSOptions::DEFAULT_WORKAROUNDS);
tls_transport->tls_config.add_option(TLSOptions::SINGLE_DH_USE);
tls_transport->tls_config.add_option(TLSOptions::NO_SSLV2);
//!--
}
{
//CONF-TCP-TLS-CLIENT
auto tls_transport = std::make_shared<TCPv4TransportDescriptor>();
using TLSOptions = TCPTransportDescriptor::TLSConfig::TLSOptions;
using TLSVerifyMode = TCPTransportDescriptor::TLSConfig::TLSVerifyMode;
tls_transport->apply_security = true;
tls_transport->tls_config.verify_file = "ca.pem";
tls_transport->tls_config.verify_mode = TLSVerifyMode::VERIFY_PEER;
tls_transport->tls_config.add_option(TLSOptions::DEFAULT_WORKAROUNDS);
tls_transport->tls_config.add_option(TLSOptions::SINGLE_DH_USE);
tls_transport->tls_config.add_option(TLSOptions::NO_SSLV2);
//!--
}
{
//CONF-IPLOCATOR-USAGE
Locator_t locator;
// Get & Set Physical Port
uint16_t physical_port = IPLocator::getPhysicalPort(locator);
IPLocator::setPhysicalPort(locator, 5555);
// Get & Set Logical Port
uint16_t logical_port = IPLocator::getLogicalPort(locator);
IPLocator::setLogicalPort(locator, 7400);
// Set WAN Address
IPLocator::setWan(locator, "80.88.75.55");
//!--
}
{
//CONF-METAMULTICASTLOCATOR
// This locator will open a socket to listen network messages on UDPv4 port 22222 over multicast address 239.255.0.1
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 239, 255, 0, 1);
locator.port = 22222;
participant_attr.rtps.builtin.metatrafficMulticastLocatorList.push_back(locator);
//!--
}
{
//CONF-METAUNICASTLOCATOR
// This locator will open a socket to listen network messages on UDPv4 port 22223 over network interface 192.168.0.1
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 192, 168, 0, 1);
locator.port = 22223;
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(locator);
//!--
}
{
//CONF-USERMULTICASTLOCATOR
// This locator will open a socket to listen network messages on UDPv4 port 22224 over multicast address 239.255.0.1
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 239, 255, 0, 1);
locator.port = 22224;
participant_attr.rtps.defaultMulticastLocatorList.push_back(locator);
//!--
}
{
//CONF-USERUNICASTLOCATOR
// This locator will open a socket to listen network messages on UDPv4 port 22225 over network interface 192.168.0.1
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 192, 168, 0, 1);
locator.port = 22225;
participant_attr.rtps.defaultUnicastLocatorList.push_back(locator);
//!--
}
{
//CONF-INITIALPEERS
// This locator configures as initial peer the UDPv4 address 192.168.0.2:7600.
// Initial discovery network messages will send to this UDPv4 address.
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, "192.168.0.2");
locator.port = 7600;
participant_attr.rtps.builtin.initialPeersList.push_back(locator);
//!--
}
//CONF-DISABLE-MULTICAST
// Metatraffic Multicast Locator List will be empty.
// Metatraffic Unicast Locator List will contain one locator, with null address and null port.
// Then eProsima Fast RTPS will use all network interfaces to receive network messages using a well-known port.
Locator_t default_unicast_locator;
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(default_unicast_locator);
// Initial peer will be UDPv4 addresss 192.168.0.1. The port will be a well-known port.
// Initial discovery network messages will be sent to this UDPv4 address.
Locator_t initial_peer;
IPLocator::setIPv4(initial_peer, 192, 168, 0, 1);
participant_attr.rtps.builtin.initialPeersList.push_back(initial_peer);
//!--
//CONF-NON-BLOCKING-WRITE
//Create a descriptor for the new transport.
auto non_blocking_UDP_transport = std::make_shared<UDPv4TransportDescriptor>();
non_blocking_UDP_transport->non_blocking_send = false;
//Disable the built-in Transport Layer.
participant_attr.rtps.useBuiltinTransports = false;
//Link the Transport Layer to the Participant.
participant_attr.rtps.userTransports.push_back(non_blocking_UDP_transport);
//!--
//CONF-QOS-FLOWCONTROLLER
// Limit to 300kb per second.
ThroughputControllerDescriptor slowPublisherThroughputController{300000, 1000};
publisher_attr.throughputController = slowPublisherThroughputController;
//!--
//CONF_QOS_RTPS_FLOWCONTROLLER
WriterAttributes writer_attr;
writer_attr.throughputController.bytesPerPeriod = 300000; //300kb
writer_attr.throughputController.periodMillisecs = 1000; //1000ms
//CONF-QOS-PUBLISHMODE
// Allows fragmentation.
publisher_attr.qos.m_publishMode.kind = ASYNCHRONOUS_PUBLISH_MODE;
//!--
{
//CONF_QOS_RTPS_PUBLISHMODE
WriterAttributes write_attr;
write_attr.mode = ASYNCHRONOUS_WRITER; // Allows fragmentation
//!--
}
//CONF-QOS-DISABLE-DISCOVERY
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::NONE;
//!--
//CONF-QOS-DISCOVERY-EDP-ATTRIBUTES
participant_attr.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = true;
participant_attr.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader = true;
participant_attr.rtps.builtin.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter = false;
//!--
//CONF-QOS-DISCOVERY-SERVERLIST
Locator_t server_address(LOCATOR_KIND_UDPv4, 5574);
IPLocator::setIPv4(server_address, 192, 168, 2, 65);
RemoteServerAttributes ratt;
ratt.ReadguidPrefix("44.53.00.5f.45.50.52.4f.53.49.4d.41");
ratt.metatrafficUnicastLocatorList.push_back(server_address);
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::CLIENT;
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(ratt);
//!--
//CONF-QOS-DISCOVERY-LEASEDURATION
participant_attr.rtps.builtin.discovery_config.leaseDuration = Duration_t(5, 0);
participant_attr.rtps.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(2, 0);
//!--
//CONF-QOS-INCREASE-SOCKETBUFFERS
// Increase the sending buffer size
participant_attr.rtps.sendSocketBufferSize = 1048576;
// Increase the receiving buffer size
participant_attr.rtps.listenSocketBufferSize = 4194304;
//!--
//CONF-TRANSPORT-DESCRIPTORS
UDPv4TransportDescriptor descriptor;
descriptor.interfaceWhiteList.emplace_back("127.0.0.1");
//!--
//CONF_QOS_STATIC_DISCOVERY_CODE
participant_attr.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = false;
participant_attr.rtps.builtin.discovery_config.use_STATIC_EndpointDiscoveryProtocol = true;
//!--
//CONF_QOS_STATIC_DISCOVERY_XML
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://RemotePublisher.xml");
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://RemoteSubscriber.xml");
//!--
{
//CONF_QOS_STATIC_DISCOVERY_USERID
SubscriberAttributes sub_attr;
sub_attr.setUserDefinedID(3);
PublisherAttributes pub_attr;
pub_attr.setUserDefinedID(5);
//!--
}
//CONF_QOS_TUNING_RELIABLE_PUBLISHER
publisher_attr.times.heartbeatPeriod.seconds = 0;
publisher_attr.times.heartbeatPeriod.nanosec = 500000000; //500 ms
//!--
//CONF_QOS_TUNING_RELIABLE_WRITER
writer_attr.times.heartbeatPeriod.seconds = 0;
writer_attr.times.heartbeatPeriod.nanosec = 500000000; //500 ms
//!--
//CONF_INITIAL_PEERS_BASIC
Locator_t initial_peers_locator;
IPLocator::setIPv4(initial_peers_locator, "192.168.10.13");
initial_peers_locator.port = 7412;
participant_attr.rtps.builtin.initialPeersList.push_back(initial_peers_locator);
//!--
//CONF_INITIAL_PEERS_METAUNICAST
Locator_t meta_unicast_locator;
IPLocator::setIPv4(meta_unicast_locator, "192.168.10.13");
meta_unicast_locator.port = 7412;
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(meta_unicast_locator);
//!--
//CONF_DS_MAIN_SCENARIO_SERVER
Locator_t server_locator;
IPLocator::setIPv4(server_locator, "192.168.10.57");
server_locator.port = 56542;
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SERVER;
participant_attr.rtps.ReadguidPrefix("72.61.73.70.66.61.72.6d.74.65.73.74");
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(server_locator);
participant_attr.rtps.builtin.discovery_config.discoveryServer_client_syncperiod = Duration_t(0, 250000000);
//!--
//CONF_DS_MAIN_SCENARIO_CLIENT
Locator_t remote_server_locator;
IPLocator::setIPv4(remote_server_locator, "192.168.10.57");
remote_server_locator.port = 56542;
RemoteServerAttributes remote_server_attr;
remote_server_attr.ReadguidPrefix("72.61.73.70.66.61.72.6d.74.65.73.74");
remote_server_attr.metatrafficUnicastLocatorList.push_back(remote_server_locator);
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::CLIENT;
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr);
//!--
{
//CONF_DS_REDUNDANCY_SCENARIO_SERVER
Locator_t server_locator_1, server_locator_2;
IPLocator::setIPv4(server_locator_1, "192.168.10.57");
server_locator_1.port = 56542;
IPLocator::setIPv4(server_locator_2, "192.168.10.60");
server_locator_2.port = 56543;
ParticipantAttributes participant_attr_1, participant_attr_2;
participant_attr_1.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SERVER;
participant_attr_1.rtps.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.31");
participant_attr_1.rtps.builtin.metatrafficUnicastLocatorList.push_back(server_locator_1);
participant_attr_2.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SERVER;
participant_attr_2.rtps.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.32");
participant_attr_2.rtps.builtin.metatrafficUnicastLocatorList.push_back(server_locator_2);
//!--
}
{
//CONF_DS_REDUNDANCY_SCENARIO_CLIENT
Locator_t remote_server_locator_1, remote_server_locator_2;
IPLocator::setIPv4(remote_server_locator_1, "192.168.10.57");
remote_server_locator.port = 56542;
IPLocator::setIPv4(remote_server_locator_2, "192.168.10.60");
server_locator.port = 56543;
RemoteServerAttributes remote_server_attr_1, remote_server_attr_2;
remote_server_attr_1.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.31");
remote_server_attr_1.metatrafficUnicastLocatorList.push_back(remote_server_locator_1);
remote_server_attr_2.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.32");
remote_server_attr_2.metatrafficUnicastLocatorList.push_back(remote_server_locator_2);
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::CLIENT;
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr_1);
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr_2);
//!--
}
{
//CONF_DS_PARTITION_2
Locator_t server_locator, remote_server_locator;
IPLocator::setIPv4(server_locator, "192.168.10.60");
server_locator.port = 56543;
IPLocator::setIPv4(remote_server_locator, "192.168.10.57");
remote_server_locator.port = 56542;
RemoteServerAttributes remote_server_attr;
remote_server_attr.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.32");
remote_server_attr.metatrafficUnicastLocatorList.push_back(remote_server_locator);
participant_attr.rtps.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.31");
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(server_locator);
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SERVER;
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr);
//!--
}
{
//CONF_DS_PARTITION_3
Locator_t server_locator, remote_server_locator_A, remote_server_locator_B;
IPLocator::setIPv4(server_locator, "192.168.10.54");
server_locator.port = 56541;
IPLocator::setIPv4(remote_server_locator_A, "192.168.10.60");
remote_server_locator_A.port = 56543;
IPLocator::setIPv4(remote_server_locator_B, "192.168.10.57");
remote_server_locator_B.port = 56542;
RemoteServerAttributes remote_server_attr_A, remote_server_attr_B;
remote_server_attr_A.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.31");
remote_server_attr_A.metatrafficUnicastLocatorList.push_back(remote_server_locator_A);
remote_server_attr_B.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.32");
remote_server_attr_B.metatrafficUnicastLocatorList.push_back(remote_server_locator_B);
participant_attr.rtps.ReadguidPrefix("75.63.2D.73.76.72.63.6C.6E.74.2D.33");
participant_attr.rtps.builtin.metatrafficUnicastLocatorList.push_back(server_locator);
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SERVER;
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr_A);
participant_attr.rtps.builtin.discovery_config.m_DiscoveryServers.push_back(remote_server_attr_B);
//!--
}
{
//STATIC_DISCOVERY_USE_CASE_PUB
// Participant attributes
participant_attr.rtps.setName("HelloWorldPublisher");
participant_attr.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = false;
participant_attr.rtps.builtin.discovery_config.use_STATIC_EndpointDiscoveryProtocol = true;
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://HelloWorldSubscriber.xml");
// Publisher attributes
publisher_attr.topic.topicName = "HelloWorldTopic";
publisher_attr.topic.topicDataType = "HelloWorld";
publisher_attr.setUserDefinedID(1);
publisher_attr.setEntityID(2);
//!--
}
{
//STATIC_DISCOVERY_USE_CASE_SUB
// Participant attributes
participant_attr.rtps.setName("HelloWorldSubscriber");
participant_attr.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = false;
participant_attr.rtps.builtin.discovery_config.use_STATIC_EndpointDiscoveryProtocol = true;
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://HelloWorldPublisher.xml");
// Subscriber attributes
subscriber_attr.topic.topicName = "HelloWorldTopic";
subscriber_attr.topic.topicDataType = "HelloWorld";
subscriber_attr.setUserDefinedID(3);
subscriber_attr.setEntityID(4);
//!--
}
//CONF-DISCOVERY-PROTOCOL
participant_attr.rtps.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol_t::SIMPLE;
//!--
//CONF-DISCOVERY-IGNORE-FLAGS
participant_attr.rtps.builtin.discovery_config.ignoreParticipantFlags =
static_cast<ParticipantFilteringFlags_t>(
ParticipantFilteringFlags_t::FILTER_DIFFERENT_PROCESS |
ParticipantFilteringFlags_t::FILTER_SAME_PROCESS);
//!--
//CONF-DISCOVERY-LEASE-DURATION
participant_attr.rtps.builtin.discovery_config.leaseDuration = Duration_t(10, 20);
//!--
//CONF-DISCOVERY-LEASE-ANNOUNCEMENT
participant_attr.rtps.builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(1, 2);
//!--
//DISCOVERY-CONFIG-INITIAL-ANNOUNCEMENT
participant_attr.rtps.builtin.discovery_config.initial_announcements.count = 5;
participant_attr.rtps.builtin.discovery_config.initial_announcements.period = Duration_t(0, 100000000u);
//!--
//CONF_STATIC_DISCOVERY_CODE
participant_attr.rtps.builtin.discovery_config.use_SIMPLE_EndpointDiscoveryProtocol = false;
participant_attr.rtps.builtin.discovery_config.use_STATIC_EndpointDiscoveryProtocol = true;
//!--
//CONF_STATIC_DISCOVERY_XML_FILE
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://RemotePublisher.xml");
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("file://RemoteSubscriber.xml");
//!--
//CONF_STATIC_DISCOVERY_XML_DATA
participant_attr.rtps.builtin.discovery_config.static_edp_xml_config("data://<?xml version=\"1.0\" encoding=\"utf-8\"?>" \
"<staticdiscovery><participant><name>RTPSParticipant</name></participant></staticdiscovery>");
//!--
{
//CONF_QOS_STATIC_DISCOVERY_USERID
SubscriberAttributes sub_attr;
sub_attr.setUserDefinedID(3);
PublisherAttributes pub_attr;
pub_attr.setUserDefinedID(5);
//!--
}
//CONF_SERVER_PREFIX_EXAMPLE
participant_attr.rtps.ReadguidPrefix("44.53.00.5f.45.50.52.4f.53.49.4d.41");
//!--
{
//CONF_SERVER_DISCOVERY_PROTOCOL
DiscoverySettings& ds = participant_attr.rtps.builtin.discovery_config;
ds.discoveryProtocol = DiscoveryProtocol_t::CLIENT;
ds.discoveryProtocol = DiscoveryProtocol_t::SUPER_CLIENT;
ds.discoveryProtocol = DiscoveryProtocol_t::SERVER;
ds.discoveryProtocol = DiscoveryProtocol_t::BACKUP;
//!--
}
{
//CONF_SERVER_CLIENT_GUIDPREFIX
RemoteServerAttributes server;
server.ReadguidPrefix("44.53.00.5f.45.50.52.4f.53.49.4d.41");
DiscoverySettings& ds = participant_attr.rtps.builtin.discovery_config;
ds.m_DiscoveryServers.push_back(server);
//!--
}
{
//CONF_SERVER_SERVER_GUIDPREFIX
participant_attr.rtps.ReadguidPrefix("44.53.00.5f.45.50.52.4f.53.49.4d.41");
//!--
}
{
//CONF_SERVER_CLIENT_LOCATORS
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 192, 168, 1, 133);
locator.port = 64863;
RemoteServerAttributes server;
server.metatrafficUnicastLocatorList.push_back(locator);
DiscoverySettings& ds = participant_attr.rtps.builtin.discovery_config;
ds.m_DiscoveryServers.push_back(server);
//!--
}
{
//CONF_SERVER_SERVER_LOCATORS
eprosima::fastrtps::rtps::Locator_t locator;
IPLocator::setIPv4(locator, 192, 168, 1, 133);
locator.port = 64863;
LocatorList_t& ull = participant_attr.rtps.builtin.metatrafficUnicastLocatorList;
ull.push_back(locator);
//!--
}
{
//CONF_SERVER_CLIENT_PING
DiscoverySettings& ds = participant_attr.rtps.builtin.discovery_config;
ds.discoveryServer_client_syncperiod = Duration_t(0, 250000000);
//!--
}
}
//API-DISCOVERY-TOPICS-LISTENER
class CustomParticipantListener : public eprosima::fastrtps::ParticipantListener
{
/* Custom Listener onSubscriberDiscovery */
void onSubscriberDiscovery(
eprosima::fastrtps::Participant* participant,
eprosima::fastrtps::rtps::ReaderDiscoveryInfo&& info) override
{
(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 subscriber for 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 << "Subscriber for topic '" << info.info.topicName() << "' of type '" <<
info.info.typeName() <<
"' left the domain.";
break;
}
}
/* Custom Listener onPublisherDiscovery */
void onPublisherDiscovery(
eprosima::fastrtps::Participant* participant,
eprosima::fastrtps::rtps::WriterDiscoveryInfo&& info) override
{
(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 publisher for 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 << "publisher for topic '" << info.info.topicName() << "' of type '" <<
info.info.typeName() <<
"' left the domain.";
break;
}
}
};
//!--
//RTPS_API_READER_LISTENER
class MyReaderListener : public ReaderListener
{
public:
MyReaderListener()
{
}
~MyReaderListener()
{
}
void onNewCacheChangeAdded(
RTPSReader* reader,
const CacheChange_t* const change)
{
// The incoming message is enclosed within the `change` in the function parameters
printf("%s\n", change->serializedPayload.data);
// Once done, remove the change
reader->getHistory()->remove_change((CacheChange_t*)change);
}
};
//!--
void rtps_api_example_create_entities()
{
//RTPS_API_CREATE_PARTICIPANT
RTPSParticipantAttributes participant_attr;
participant_attr.setName("participant");
RTPSParticipant* participant = RTPSDomain::createParticipant(0, participant_attr);
//!--
{
//RTPS_API_CONF_PARTICIPANT
RTPSParticipantAttributes participant_attr;
participant_attr.setName("my_participant");
//etc.
//!--
}
//RTPS_API_WRITER_CONF_HISTORY
HistoryAttributes history_attr;
WriterHistory* history = new WriterHistory(history_attr);
WriterAttributes writer_attr;
RTPSWriter* writer = RTPSDomain::createRTPSWriter(participant, writer_attr, history);
//!--
{
//RTPS_API_READER_CONF_HISTORY
class MyReaderListener : public ReaderListener
{
// Callbacks override
};
MyReaderListener listener;
HistoryAttributes history_attr;
ReaderHistory* history = new ReaderHistory(history_attr);
ReaderAttributes reader_attr;
RTPSReader* reader = RTPSDomain::createRTPSReader(participant, reader_attr, history, &listener);
//!--
}
//RTPS_API_WRITE_SAMPLE
//Request a change from the writer
CacheChange_t* change = writer->new_change([]() -> uint32_t
{
return 255;
}, ALIVE);
//Write serialized data into the change
change->serializedPayload.length = sprintf((char*) change->serializedPayload.data, "My example string %d", 2) + 1;
//Insert change into the history. The Writer takes care of the rest.
history->add_change(change);
//!--
}
void rtps_api_example_create_entities_with_custom_pool()
{
RTPSParticipantAttributes participant_attr;
participant_attr.setName("participant");
RTPSParticipant* participant = RTPSDomain::createParticipant(0, participant_attr);
//RTPS_API_ENTITY_CREATE_WITH_PAYLOAD_POOL
// A simple payload pool that reserves and frees memory each time
class CustomPayloadPool : public IPayloadPool
{
bool get_payload(
uint32_t size,
CacheChange_t& cache_change) override
{
// Reserve new memory for the payload buffer
octet* payload = new octet[size];
// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = size;
cache_change.serializedPayload.max_size = size;
// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);
return true;
}
bool get_payload(
SerializedPayload_t& data,
IPayloadPool*& /* data_owner */,
CacheChange_t& cache_change) override
{
// Reserve new memory for the payload buffer
octet* payload = new octet[data.length];
// Copy the data
memcpy(payload, data.data, data.length);
// Assign the payload buffer to the CacheChange and update sizes
cache_change.serializedPayload.data = payload;
cache_change.serializedPayload.length = data.length;
cache_change.serializedPayload.max_size = data.length;
// Tell the CacheChange who needs to release its payload
cache_change.payload_owner(this);
return true;
}
bool release_payload(
CacheChange_t& cache_change) override
{
// Ensure precondition
assert(this == cache_change.payload_owner());
// Dealloc the buffer of the payload
delete[] cache_change.serializedPayload.data;
// Reset sizes and pointers
cache_change.serializedPayload.data = nullptr;
cache_change.serializedPayload.length = 0;
cache_change.serializedPayload.max_size = 0;
// Reset the owner of the payload
cache_change.payload_owner(nullptr);
return true;
}
};
std::shared_ptr<CustomPayloadPool> payload_pool = std::make_shared<CustomPayloadPool>();
// A writer using the custom payload pool
HistoryAttributes writer_history_attr;
WriterHistory* writer_history = new WriterHistory(writer_history_attr);
WriterAttributes writer_attr;
RTPSWriter* writer = RTPSDomain::createRTPSWriter(participant, writer_attr, payload_pool, writer_history);
// A reader using the same instance of the custom payload pool
HistoryAttributes reader_history_attr;
ReaderHistory* reader_history = new ReaderHistory(reader_history_attr);
ReaderAttributes reader_attr;
RTPSReader* reader = RTPSDomain::createRTPSReader(participant, reader_attr, payload_pool, reader_history);
// Write and Read operations work as usual, but take the Payloads from the pool.
// Requesting a change to the Writer will provide one with an empty Payload taken from the pool
CacheChange_t* change = writer->new_change([]() -> uint32_t
{
return 255;
}, ALIVE);
// Write serialized data into the change and add it to the history