-
Notifications
You must be signed in to change notification settings - Fork 17
/
shape_main.cxx
1165 lines (1050 loc) · 44.3 KB
/
shape_main.cxx
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
/****************************************************************
* Use and redistribution is source and binary forms is permitted
* subject to the OMG-DDS INTEROPERABILITY TESTING LICENSE found
* at the following URL:
*
* https://github.com/omg-dds/dds-rtps/blob/master/LICENSE.md
*/
/****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <stdarg.h>
#include <iostream>
#if defined(RTI_CONNEXT_DDS)
#include "shape_configurator_rti_connext_dds.h"
#elif defined(TWINOAKS_COREDX)
#include "shape_configurator_toc_coredx_dds.h"
#elif defined(OPENDDS)
#include "shape_configurator_opendds.h"
#elif defined(EPROSIMA_FAST_DDS)
#include "shape_configurator_eprosima_fast_dds.h"
#else
#error "Must define the DDS vendor"
#endif
#ifndef STRING_IN
#define STRING_IN
#endif
#ifndef STRING_INOUT
#define STRING_INOUT
#endif
#ifndef NAME_ACCESSOR
#define NAME_ACCESSOR
#endif
#ifndef LISTENER_STATUS_MASK_NONE
#define LISTENER_STATUS_MASK_NONE 0
#endif
#ifndef SECONDS_FIELD_NAME
#define SECONDS_FIELD_NAME sec
#endif
#ifndef FIELD_ACCESSOR
#define FIELD_ACCESSOR
#endif
#ifndef GET_TOPIC_DESCRIPTION
#define GET_TOPIC_DESCRIPTION(dr) dr->get_topicdescription()
#endif
#ifndef ADD_PARTITION
#define ADD_PARTITION(field, value) StringSeq_push(field.name, value)
#endif
using namespace DDS;
/*************************************************************/
int all_done = 0;
/*************************************************************/
void
handle_sig(int sig)
{
if (sig == SIGINT) {
all_done = 1;
}
}
/*************************************************************/
int
install_sig_handlers()
{
struct sigaction int_action;
int_action.sa_handler = handle_sig;
sigemptyset(&int_action.sa_mask);
sigaddset(&int_action.sa_mask, SIGINT);
int_action.sa_flags = 0;
sigaction(SIGINT, &int_action, NULL);
return 0;
}
enum Verbosity
{
ERROR=1,
DEBUG=2,
};
class QosUtils {
public:
static std::string to_string(ReliabilityQosPolicyKind reliability_value)
{
if (reliability_value == BEST_EFFORT_RELIABILITY_QOS){
return "BEST_EFFORT";
} else if (reliability_value == RELIABLE_RELIABILITY_QOS){
return "RELIABLE";
}
return "Error stringifying Reliability kind.";
}
static std::string to_string(DurabilityQosPolicyKind durability_value)
{
if ( durability_value == VOLATILE_DURABILITY_QOS){
return "VOLATILE";
} else if (durability_value == TRANSIENT_LOCAL_DURABILITY_QOS){
return "TRANSIENT_LOCAL";
} else if (durability_value == TRANSIENT_DURABILITY_QOS){
return "TRANSIENT";
} else if (durability_value == PERSISTENT_DURABILITY_QOS){
return "PERSISTENT";
}
return "Error stringifying Durability kind.";
}
static std::string to_string(DataRepresentationId_t data_representation_value)
{
if (data_representation_value == XCDR_DATA_REPRESENTATION){
return "XCDR";
} else if (data_representation_value == XCDR2_DATA_REPRESENTATION){
return "XCDR2";
}
return "Error stringifying DataRepresentation.";
}
static std::string to_string(Verbosity verbosity_value)
{
switch (verbosity_value)
{
case ERROR:
return "ERROR";
break;
case DEBUG:
return "DEBUG";
break;
default:
break;
}
return "Error stringifying verbosity.";
}
static std::string to_string(OwnershipQosPolicyKind ownership_kind_value)
{
if (ownership_kind_value == SHARED_OWNERSHIP_QOS){
return "SHARED";
} else if (ownership_kind_value == EXCLUSIVE_OWNERSHIP_QOS){
return "EXCLUSIVE";
}
return "Error stringifying Ownership kind.";
}
static std::string to_string(HistoryQosPolicyKind history_kind_value)
{
if (history_kind_value == KEEP_ALL_HISTORY_QOS){
return "KEEP_ALL";
} else if (history_kind_value == KEEP_LAST_HISTORY_QOS){
return "KEEP_LAST";
}
return "Error stringifying History kind.";
}
};
class Logger{
public:
Logger(enum Verbosity v)
{
verbosity_ = v;
}
void verbosity(enum Verbosity v)
{
verbosity_ = v;
}
enum Verbosity verbosity()
{
return verbosity_;
}
void log_message(std::string message, enum Verbosity level_verbosity)
{
if (level_verbosity <= verbosity_) {
std::cout << message << std::endl;
}
}
private:
enum Verbosity verbosity_;
};
/*************************************************************/
Logger logger(ERROR);
/*************************************************************/
class ShapeOptions {
public:
DomainId_t domain_id;
ReliabilityQosPolicyKind reliability_kind;
DurabilityQosPolicyKind durability_kind;
DataRepresentationId_t data_representation;
int history_depth;
int ownership_strength;
char *topic_name;
char *color;
char *partition;
bool publish;
bool subscribe;
int timebasedfilter_interval;
int deadline_interval;
int da_width;
int da_height;
int xvel;
int yvel;
int shapesize;
bool print_writer_samples;
public:
//-------------------------------------------------------------
ShapeOptions()
{
domain_id = 0;
reliability_kind = RELIABLE_RELIABILITY_QOS;
durability_kind = VOLATILE_DURABILITY_QOS;
data_representation = XCDR_DATA_REPRESENTATION;
history_depth = -1; /* means default */
ownership_strength = -1; /* means shared */
topic_name = NULL;
color = NULL;
partition = NULL;
publish = false;
subscribe = false;
timebasedfilter_interval = 0; /* off */
deadline_interval = 0; /* off */
da_width = 240;
da_height = 270;
xvel = 3;
yvel = 3;
shapesize = 20;
print_writer_samples = false;
}
//-------------------------------------------------------------
~ShapeOptions()
{
if (topic_name) free(topic_name);
if (color) free(color);
if (partition) free(partition);
}
//-------------------------------------------------------------
void print_usage( const char *prog )
{
printf("%s: \n", prog);
printf(" -d <int> : domain id (default: 0)\n");
printf(" -b : BEST_EFFORT reliability\n");
printf(" -r : RELIABLE reliability\n");
printf(" -k <depth> : keep history depth [0: KEEP_ALL]\n");
printf(" -f <interval> : set a 'deadline' with interval (seconds) [0: OFF]\n");
printf(" -i <interval> : apply 'time based filter' with interval (seconds) [0: OFF]\n");
printf(" -s <int> : set ownership strength [-1: SHARED]\n");
printf(" -t <topic_name> : set the topic name\n");
printf(" -c <color> : set color to publish (filter if subscriber)\n");
printf(" -p <partition> : set a 'partition' string\n");
printf(" -D [v|l|t|p] : set durability [v: VOLATILE, l: TRANSIENT_LOCAL]\n");
printf(" t: TRANSIENT, p: PERSISTENT]\n");
printf(" -P : publish samples\n");
printf(" -S : subscribe samples\n");
printf(" -x [1|2] : set data representation [1: XCDR, 2: XCDR2]\n");
printf(" -w : print Publisher's samples\n");
printf(" -z <int> : set shapesize (between 10-99)\n");
printf(" -v [e|d] : set log message verbosity [e: ERROR, d: DEBUG]\n");
}
//-------------------------------------------------------------
bool validate() {
if (topic_name == NULL) {
logger.log_message("please specify topic name [-t]", Verbosity::ERROR);
return false;
}
if ( (!publish) && (!subscribe) ) {
logger.log_message("please specify publish [-P] or subscribe [-S]", Verbosity::ERROR);
return false;
}
if ( publish && subscribe ) {
logger.log_message("please specify only one of: publish [-P] or subscribe [-S]", Verbosity::ERROR);
return false;
}
if (publish && (color == NULL) ) {
color = strdup("BLUE");
logger.log_message("warning: color was not specified, defaulting to \"BLUE\"", Verbosity::ERROR);
}
return true;
}
//-------------------------------------------------------------
bool parse(int argc, char *argv[])
{
int opt;
bool parse_ok = true;
// double d;
while ((opt = getopt(argc, argv, "hbrc:d:D:f:i:k:p:s:x:t:v:z:wPS")) != -1)
{
switch (opt)
{
case 'v':
{
if (optarg[0] != '\0')
{
switch (optarg[0])
{
case 'd':
{
logger.verbosity(DEBUG);
break;
}
case 'e':
{
logger.verbosity(ERROR);
break;
}
default:
{
logger.log_message("unrecognized value for verbosity "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
}
}
break;
}
case 'w':
{
print_writer_samples = true;
break;
}
case 'b':
{
reliability_kind = BEST_EFFORT_RELIABILITY_QOS;
break;
}
case 'c':
{
color = strdup(optarg);
break;
}
case 'd':
{
int converted_param = sscanf(optarg, "%d", &domain_id);
if (converted_param == 0) {
logger.log_message("unrecognized value for domain_id "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
else if (domain_id < 0) {
logger.log_message("incorrect value for domain_id "
+ std::to_string(domain_id),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case 'D':
{
if (optarg[0] != '\0')
{
switch (optarg[0])
{
case 'v':
{
durability_kind = VOLATILE_DURABILITY_QOS;
break;
}
case 'l':
{
durability_kind = TRANSIENT_LOCAL_DURABILITY_QOS;
break;
}
case 't':
{
durability_kind = TRANSIENT_DURABILITY_QOS;
break;
}
case 'p':
{
durability_kind = PERSISTENT_DURABILITY_QOS;
break;
}
default:
{
logger.log_message("unrecognized value for durability "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
}
}
break;
}
case 'i':
{
int converted_param = sscanf(optarg, "%d", &timebasedfilter_interval);
if (converted_param == 0) {
logger.log_message("unrecognized value for timebasedfilter_interval "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
else if (timebasedfilter_interval < 0) {
logger.log_message("incorrect value for timebasedfilter_interval "
+ std::to_string(timebasedfilter_interval),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case 'f':
{
int converted_param = sscanf(optarg, "%d", &deadline_interval);
if (converted_param == 0) {
logger.log_message("unrecognized value for deadline_interval "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
} else if (deadline_interval < 0) {
logger.log_message("incorrect value for deadline_interval "
+ std::to_string(deadline_interval),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case 'k':
{
int converted_param = sscanf(optarg, "%d", &history_depth);
if (converted_param == 0){
logger.log_message("unrecognized value for history_depth "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
} else if (history_depth < 0) {
logger.log_message("incorrect value for history_depth "
+ std::to_string(history_depth),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case 'p':
{
partition = strdup(optarg);
break;
}
case 'r':
{
reliability_kind = RELIABLE_RELIABILITY_QOS;
break;
}
case 's':
{
int converted_param = sscanf(optarg, "%d", &ownership_strength);
if (converted_param == 0){
logger.log_message("unrecognized value for ownership_strength "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
} else if (ownership_strength < -1) {
logger.log_message("incorrect value for ownership_strength "
+ std::to_string(ownership_strength),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case 't':
{
topic_name = strdup(optarg);
break;
}
case 'P':
{
publish = true;
break;
}
case 'S':
{
subscribe = true;
break;
}
case 'h':
{
print_usage(argv[0]);
exit(0);
break;
}
case 'x':
{
if (optarg[0] != '\0')
{
switch (optarg[0])
{
case '1':
{
data_representation = XCDR_DATA_REPRESENTATION;
break;
}
case '2':
{
data_representation = XCDR2_DATA_REPRESENTATION;
break;
}
default:
{
logger.log_message("unrecognized value for data representation "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
}
}
break;
}
case 'z':
{
int converted_param = sscanf(optarg, "%d", &shapesize);
if (converted_param == 0){
logger.log_message("unrecognized value for shapesize "
+ std::string(1, optarg[0]),
Verbosity::ERROR);
parse_ok = false;
}
else if (shapesize < 10 || shapesize > 99) {
logger.log_message("incorrect value for shapesize "
+ std::to_string(shapesize),
Verbosity::ERROR);
parse_ok = false;
}
break;
}
case '?':
{
parse_ok = false;
break;
}
}
}
if ( parse_ok ) {
parse_ok = validate();
}
if ( !parse_ok ) {
print_usage(argv[0]);
} else {
std::string app_kind = publish ? "publisher" : "subscriber";
logger.log_message("Shape Options: "
"\n This application is a " + app_kind +
"\n DomainId = " + std::to_string(domain_id) +
"\n ReliabilityKind = " + QosUtils::to_string(reliability_kind) +
"\n DurabilityKind = " + QosUtils::to_string(durability_kind) +
"\n DataRepresentation = " + QosUtils::to_string(data_representation) +
"\n HistoryDepth = " + std::to_string(history_depth) +
"\n OwnershipStrength = " + std::to_string(ownership_strength) +
"\n TimeBasedFilterInterval = " + std::to_string(timebasedfilter_interval) +
"\n DeadlineInterval = " + std::to_string(deadline_interval) +
"\n Shapesize = " + std::to_string(shapesize) +
"\n Verbosity = " + QosUtils::to_string(logger.verbosity()),
Verbosity::DEBUG);
if (topic_name != NULL){
logger.log_message(" Topic = " + std::string(topic_name),
Verbosity::DEBUG);
}
if (color != NULL) {
logger.log_message(" Color = " + std::string(color),
Verbosity::DEBUG);
}
if (partition != NULL) {
logger.log_message(" Partition = " + std::string(partition), Verbosity::DEBUG);
}
}
return parse_ok;
}
};
/*************************************************************/
class DPListener : public DomainParticipantListener
{
public:
void on_inconsistent_topic (Topic *topic, const InconsistentTopicStatus &) {
const char *topic_name = topic->get_name() NAME_ACCESSOR;
const char *type_name = topic->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s'\n", __FUNCTION__, topic_name, type_name);
}
void on_offered_incompatible_qos(DataWriter *dw, const OfferedIncompatibleQosStatus & status) {
Topic *topic = dw->get_topic( );
const char *topic_name = topic->get_name() NAME_ACCESSOR;
const char *type_name = topic->get_type_name() NAME_ACCESSOR;
const char *policy_name = NULL;
policy_name = get_qos_policy_name(status.last_policy_id);
printf("%s() topic: '%s' type: '%s' : %d (%s)\n", __FUNCTION__,
topic_name, type_name,
status.last_policy_id,
policy_name );
}
void on_publication_matched (DataWriter *dw, const PublicationMatchedStatus & status) {
Topic *topic = dw->get_topic( );
const char *topic_name = topic->get_name() NAME_ACCESSOR;
const char *type_name = topic->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : matched readers %d (change = %d)\n", __FUNCTION__,
topic_name, type_name, status.current_count, status.current_count_change);
}
void on_offered_deadline_missed (DataWriter *dw, const OfferedDeadlineMissedStatus & status) {
Topic *topic = dw->get_topic( );
const char *topic_name = topic->get_name() NAME_ACCESSOR;
const char *type_name = topic->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : (total = %d, change = %d)\n", __FUNCTION__,
topic_name, type_name, status.total_count, status.total_count_change);
}
void on_liveliness_lost (DataWriter *dw, const LivelinessLostStatus & status) {
Topic *topic = dw->get_topic( );
const char *topic_name = topic->get_name() NAME_ACCESSOR;
const char *type_name = topic->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : (total = %d, change = %d)\n", __FUNCTION__,
topic_name, type_name, status.total_count, status.total_count_change);
}
void on_requested_incompatible_qos (DataReader *dr, const RequestedIncompatibleQosStatus & status) {
TopicDescription *td = GET_TOPIC_DESCRIPTION(dr);
const char *topic_name = td->get_name() NAME_ACCESSOR;
const char *type_name = td->get_type_name() NAME_ACCESSOR;
const char *policy_name = NULL;
policy_name = get_qos_policy_name(status.last_policy_id);
printf("%s() topic: '%s' type: '%s' : %d (%s)\n", __FUNCTION__,
topic_name, type_name, status.last_policy_id,
policy_name);
}
void on_subscription_matched (DataReader *dr, const SubscriptionMatchedStatus & status) {
TopicDescription *td = GET_TOPIC_DESCRIPTION(dr);
const char *topic_name = td->get_name() NAME_ACCESSOR;
const char *type_name = td->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : matched writers %d (change = %d)\n", __FUNCTION__,
topic_name, type_name, status.current_count, status.current_count_change);
}
void on_requested_deadline_missed (DataReader *dr, const RequestedDeadlineMissedStatus & status) {
TopicDescription *td = GET_TOPIC_DESCRIPTION(dr);
const char *topic_name = td->get_name() NAME_ACCESSOR;
const char *type_name = td->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : (total = %d, change = %d)\n", __FUNCTION__,
topic_name, type_name, status.total_count, status.total_count_change);
}
void on_liveliness_changed (DataReader *dr, const LivelinessChangedStatus & status) {
TopicDescription *td = GET_TOPIC_DESCRIPTION(dr);
const char *topic_name = td->get_name() NAME_ACCESSOR;
const char *type_name = td->get_type_name() NAME_ACCESSOR;
printf("%s() topic: '%s' type: '%s' : (alive = %d, not_alive = %d)\n", __FUNCTION__,
topic_name, type_name, status.alive_count, status.not_alive_count);
}
void on_sample_rejected (DataReader *, const SampleRejectedStatus &) {}
void on_data_available (DataReader *) {}
void on_sample_lost (DataReader *, const SampleLostStatus &) {}
void on_data_on_readers (Subscriber *) {}
};
/*************************************************************/
class ShapeApplication {
private:
DPListener dp_listener;
DomainParticipantFactory *dpf;
DomainParticipant *dp;
Publisher *pub;
Subscriber *sub;
Topic *topic;
ShapeTypeDataReader *dr;
ShapeTypeDataWriter *dw;
char *color;
int xvel;
int yvel;
int da_width;
int da_height;
public:
//-------------------------------------------------------------
ShapeApplication()
{
dpf = NULL;
dp = NULL;
pub = NULL;
sub = NULL;
color = NULL;
}
//-------------------------------------------------------------
~ShapeApplication()
{
if (dp) dp->delete_contained_entities( );
if (dpf) dpf->delete_participant( dp );
if (color) free(color);
}
//-------------------------------------------------------------
bool initialize(ShapeOptions *options)
{
#ifndef OBTAIN_DOMAIN_PARTICIPANT_FACTORY
#define OBTAIN_DOMAIN_PARTICIPANT_FACTORY DomainParticipantFactory::get_instance()
#endif
DomainParticipantFactory *dpf = OBTAIN_DOMAIN_PARTICIPANT_FACTORY;
if (dpf == NULL) {
logger.log_message("failed to create participant factory (missing license?).", Verbosity::ERROR);
return false;
}
logger.log_message("Participant Factory created", Verbosity::DEBUG);
#ifdef CONFIGURE_PARTICIPANT_FACTORY
CONFIGURE_PARTICIPANT_FACTORY
#endif
dp = dpf->create_participant( options->domain_id, PARTICIPANT_QOS_DEFAULT, &dp_listener, LISTENER_STATUS_MASK_ALL );
if (dp == NULL) {
logger.log_message("failed to create participant (missing license?).", Verbosity::ERROR);
return false;
}
logger.log_message("Participant created", Verbosity::DEBUG);
#ifndef REGISTER_TYPE
#define REGISTER_TYPE ShapeTypeTypeSupport::register_type
#endif
REGISTER_TYPE(dp, "ShapeType");
printf("Create topic: %s\n", options->topic_name );
topic = dp->create_topic( options->topic_name, "ShapeType", TOPIC_QOS_DEFAULT, NULL, LISTENER_STATUS_MASK_NONE);
if (topic == NULL) {
logger.log_message("failed to create topic", Verbosity::ERROR);
return false;
}
if ( options->publish ) {
return init_publisher(options);
}
else {
return init_subscriber(options);
}
}
//-------------------------------------------------------------
bool run(ShapeOptions *options)
{
if ( pub != NULL ) {
return run_publisher(options);
}
else if ( sub != NULL ) {
return run_subscriber();
}
return false;
}
//-------------------------------------------------------------
bool init_publisher(ShapeOptions *options)
{
logger.log_message("Initializing Publisher", Verbosity::DEBUG);
PublisherQos pub_qos;
DataWriterQos dw_qos;
ShapeType shape;
dp->get_default_publisher_qos( pub_qos );
if ( options->partition != NULL ) {
ADD_PARTITION(pub_qos.partition, options->partition);
}
pub = dp->create_publisher(pub_qos, NULL, LISTENER_STATUS_MASK_NONE);
if (pub == NULL) {
logger.log_message("failed to create publisher", Verbosity::ERROR);
return false;
}
logger.log_message("Publisher created", Verbosity::DEBUG);
logger.log_message("Data Writer QoS:", Verbosity::DEBUG);
pub->get_default_datawriter_qos( dw_qos );
dw_qos.reliability FIELD_ACCESSOR.kind = options->reliability_kind;
logger.log_message(" Reliability = " + QosUtils::to_string(dw_qos.reliability FIELD_ACCESSOR.kind), Verbosity::DEBUG);
dw_qos.durability FIELD_ACCESSOR.kind = options->durability_kind;
logger.log_message(" Durability = " + QosUtils::to_string(dw_qos.durability FIELD_ACCESSOR.kind), Verbosity::DEBUG);
#if defined(RTI_CONNEXT_DDS)
DataRepresentationIdSeq data_representation_seq;
data_representation_seq.ensure_length(1,1);
data_representation_seq[0] = options->data_representation;
dw_qos.representation.value = data_representation_seq;
#elif defined(TWINOAKS_COREDX)
dw_qos.representation.value.clear( );
dw_qos.representation.value.push_back( options->data_representation );
#elif defined(OPENDDS)
dw_qos.representation.value.length(1);
dw_qos.representation.value[0] = options->data_representation;
#endif
#if !defined(EPROSIMA_FAST_DDS)
logger.log_message(" Data_Representation = " + QosUtils::to_string(dw_qos.representation FIELD_ACCESSOR.value[0]), Verbosity::DEBUG);
#endif
if ( options->ownership_strength != -1 ) {
dw_qos.ownership FIELD_ACCESSOR.kind = EXCLUSIVE_OWNERSHIP_QOS;
dw_qos.ownership_strength FIELD_ACCESSOR.value = options->ownership_strength;
}
if ( options->ownership_strength == -1 ) {
dw_qos.ownership FIELD_ACCESSOR.kind = SHARED_OWNERSHIP_QOS;
}
logger.log_message(" Ownership = " + QosUtils::to_string(dw_qos.ownership FIELD_ACCESSOR.kind), Verbosity::DEBUG);
if (dw_qos.ownership FIELD_ACCESSOR.kind == EXCLUSIVE_OWNERSHIP_QOS){
logger.log_message(" OwnershipStrength = " + std::to_string(dw_qos.ownership_strength FIELD_ACCESSOR.value), Verbosity::DEBUG);
}
if ( options->deadline_interval > 0 ) {
dw_qos.deadline FIELD_ACCESSOR.period.SECONDS_FIELD_NAME = options->deadline_interval;
dw_qos.deadline FIELD_ACCESSOR.period.nanosec = 0;
}
logger.log_message(" DeadlinePeriod = " + std::to_string(dw_qos.deadline FIELD_ACCESSOR.period.SECONDS_FIELD_NAME), Verbosity::DEBUG);
// options->history_depth < 0 means leave default value
if ( options->history_depth > 0 ) {
dw_qos.history FIELD_ACCESSOR.kind = KEEP_LAST_HISTORY_QOS;
dw_qos.history FIELD_ACCESSOR.depth = options->history_depth;
}
else if ( options->history_depth == 0 ) {
dw_qos.history FIELD_ACCESSOR.kind = KEEP_ALL_HISTORY_QOS;
}
logger.log_message(" History = " + QosUtils::to_string(dw_qos.history FIELD_ACCESSOR.kind), Verbosity::DEBUG);
if (dw_qos.history FIELD_ACCESSOR.kind == KEEP_LAST_HISTORY_QOS){
logger.log_message(" HistoryDepth = " + std::to_string(dw_qos.history FIELD_ACCESSOR.depth), Verbosity::DEBUG);
}
printf("Create writer for topic: %s color: %s\n", options->topic_name, options->color );
dw = dynamic_cast<ShapeTypeDataWriter *>(pub->create_datawriter( topic, dw_qos, NULL, LISTENER_STATUS_MASK_NONE));
if (dw == NULL) {
logger.log_message("failed to create datawriter", Verbosity::ERROR);
return false;
}
color = strdup(options->color);
xvel = options->xvel;
yvel = options->yvel;
da_width = options->da_width;
da_height = options->da_height;
logger.log_message("Data Writer created", Verbosity::DEBUG);
logger.log_message("Color " + std::string(color), Verbosity::DEBUG);
logger.log_message("xvel " + std::to_string(xvel), Verbosity::DEBUG);
logger.log_message("yvel " + std::to_string(yvel), Verbosity::DEBUG);
logger.log_message("da_width " + std::to_string(da_width), Verbosity::DEBUG);
logger.log_message("da_height " + std::to_string(da_height), Verbosity::DEBUG);
return true;
}
//-------------------------------------------------------------
bool init_subscriber(ShapeOptions *options)
{
SubscriberQos sub_qos;
DataReaderQos dr_qos;
dp->get_default_subscriber_qos( sub_qos );
if ( options->partition != NULL ) {
ADD_PARTITION(sub_qos.partition, options->partition);
}
sub = dp->create_subscriber( sub_qos, NULL, LISTENER_STATUS_MASK_NONE );
if (sub == NULL) {
logger.log_message("failed to create subscriber", Verbosity::ERROR);
return false;
}
logger.log_message("Subscriber created", Verbosity::DEBUG);
logger.log_message("Data Reader QoS:", Verbosity::DEBUG);
sub->get_default_datareader_qos( dr_qos );
dr_qos.reliability FIELD_ACCESSOR.kind = options->reliability_kind;
logger.log_message(" Reliability = " + QosUtils::to_string(dr_qos.reliability FIELD_ACCESSOR.kind), Verbosity::DEBUG);
dr_qos.durability FIELD_ACCESSOR.kind = options->durability_kind;
logger.log_message(" Durability = " + QosUtils::to_string(dr_qos.durability FIELD_ACCESSOR.kind), Verbosity::DEBUG);
#if defined(RTI_CONNEXT_DDS)
DataRepresentationIdSeq data_representation_seq;
data_representation_seq.ensure_length(1,1);
data_representation_seq[0] = options->data_representation;
dr_qos.representation.value = data_representation_seq;
#elif defined(TWINOAKS_COREDX)
dr_qos.representation.value.clear( );
dr_qos.representation.value.push_back( options->data_representation );
#elif defined(OPENDDS)
dr_qos.representation.value.length(1);
dr_qos.representation.value[0] = options->data_representation;
#endif
#if !defined(EPROSIMA_FAST_DDS)
logger.log_message(" DataRepresentation = " + QosUtils::to_string(dr_qos.representation FIELD_ACCESSOR.value[0]), Verbosity::DEBUG);
#endif
if ( options->ownership_strength != -1 ) {
dr_qos.ownership FIELD_ACCESSOR.kind = EXCLUSIVE_OWNERSHIP_QOS;
}
logger.log_message(" Ownership = " + QosUtils::to_string(dr_qos.ownership FIELD_ACCESSOR.kind), Verbosity::DEBUG);
if ( options->timebasedfilter_interval > 0) {
dr_qos.time_based_filter FIELD_ACCESSOR.minimum_separation.SECONDS_FIELD_NAME = options->timebasedfilter_interval;
dr_qos.time_based_filter FIELD_ACCESSOR.minimum_separation.nanosec = 0;
}
logger.log_message(" TimeBasedFilter = " + std::to_string(dr_qos.time_based_filter FIELD_ACCESSOR.minimum_separation.SECONDS_FIELD_NAME), Verbosity::DEBUG);
if ( options->deadline_interval > 0 ) {
dr_qos.deadline FIELD_ACCESSOR.period.SECONDS_FIELD_NAME = options->deadline_interval;
dr_qos.deadline FIELD_ACCESSOR.period.nanosec = 0;
}
logger.log_message(" DeadlinePeriod = " + std::to_string(dr_qos.deadline FIELD_ACCESSOR.period.SECONDS_FIELD_NAME), Verbosity::DEBUG);
// options->history_depth < 0 means leave default value
if ( options->history_depth > 0 ) {
dr_qos.history FIELD_ACCESSOR.kind = KEEP_LAST_HISTORY_QOS;
dr_qos.history FIELD_ACCESSOR.depth = options->history_depth;
}
else if ( options->history_depth == 0 ) {
dr_qos.history FIELD_ACCESSOR.kind = KEEP_ALL_HISTORY_QOS;
}
logger.log_message(" History = " + QosUtils::to_string(dr_qos.history FIELD_ACCESSOR.kind), Verbosity::DEBUG);
if (dr_qos.history FIELD_ACCESSOR.kind == KEEP_LAST_HISTORY_QOS){
logger.log_message(" HistoryDepth = " + std::to_string(dr_qos.history FIELD_ACCESSOR.depth), Verbosity::DEBUG);
}
if ( options->color != NULL ) {
/* filter on specified color */
ContentFilteredTopic *cft = NULL;
StringSeq cf_params;
#if defined(RTI_CONNEXT_DDS)
char parameter[64];
sprintf(parameter, "'%s'", options->color);
StringSeq_push(cf_params, parameter);
cft = dp->create_contentfilteredtopic(options->topic_name, topic, "color MATCH %0", cf_params);
logger.log_message(" ContentFilterTopic = color MATCH " + std::string(parameter), Verbosity::DEBUG);
#elif defined(TWINOAKS_COREDX) || defined(OPENDDS)
StringSeq_push(cf_params, options->color);
cft = dp->create_contentfilteredtopic(options->topic_name, topic, "color = %0", cf_params);
logger.log_message(" ContentFilterTopic = color = " + std::string(options->color), Verbosity::DEBUG);
#elif defined(EPROSIMA_FAST_DDS)
cf_params.push_back(std::string("'") + options->color + std::string("'"));
cft = dp->create_contentfilteredtopic(std::string(options->topic_name) + "_filtered", topic, "color = %0", cf_params);
logger.log_message(" ContentFilterTopic = color = " + std::string(options->color), Verbosity::DEBUG);
#endif
if (cft == NULL) {
logger.log_message("failed to create content filtered topic", Verbosity::ERROR);
return false;
}
printf("Create reader for topic: %s color: %s\n", options->topic_name, options->color );
dr = dynamic_cast<ShapeTypeDataReader *>(sub->create_datareader(cft, dr_qos, NULL, LISTENER_STATUS_MASK_NONE));
}
else {
printf("Create reader for topic: %s\n", options->topic_name );
dr = dynamic_cast<ShapeTypeDataReader *>(sub->create_datareader(topic, dr_qos, NULL, LISTENER_STATUS_MASK_NONE));
}
if (dr == NULL) {
logger.log_message("failed to create datareader", Verbosity::ERROR);
return false;
}
logger.log_message("Data Reader created", Verbosity::DEBUG);
return true;
}
//-------------------------------------------------------------
bool run_subscriber()
{
while ( ! all_done ) {
ReturnCode_t retval;
SampleInfoSeq sample_infos;