-
Notifications
You must be signed in to change notification settings - Fork 5
/
pubsub.c
3572 lines (3381 loc) · 117 KB
/
pubsub.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2017 PrismTech Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#ifdef __APPLE__
#define USE_EDITLINE 1
#endif
#define _ISOC99_SOURCE
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <signal.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#if USE_EDITLINE
#include <histedit.h>
#endif
#include "common.h"
#include "testtype.h"
#include "tglib.h"
#include "porting.h"
#include "ddsicontrol.h"
#if PRE_V6_5
#define DDS_DataReader_read DDS__FooDataReader_read
#define DDS_DataReader_take DDS__FooDataReader_take
#define DDS_DataReader_take_w_condition DDS__FooDataReader_take_w_condition
#define DDS_DataReader_read_w_condition DDS__FooDataReader_read_w_condition
#define DDS_DataReader_return_loan DDS__FooDataReader_return_loan
#define DDS_DataWriter_register_instance DDS__FooDataWriter_register_instance
#define DDS_DataWriter_register_instance_w_timestamp DDS__FooDataWriter_register_instance_w_timestamp
#define DDS_DataWriter_write DDS__FooDataWriter_write
#define DDS_DataWriter_write_w_timestamp DDS__FooDataWriter_write_w_timestamp
#define DDS_DataWriter_dispose_w_timestamp DDS__FooDataWriter_dispose_w_timestamp
#define DDS_DataWriter_writedispose_w_timestamp DDS__FooDataWriter_writedispose_w_timestamp
#define DDS_DataWriter_unregister_instance_w_timestamp DDS__FooDataWriter_unregister_instance_w_timestamp
#endif
#define NUMSTR "0123456789"
#define HOSTNAMESTR "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-." NUMSTR
typedef DDS_ReturnCode_t (*write_oper_t) (DDS_DataWriter wr, void *d, DDS_InstanceHandle_t h, const DDS_Time_t *ts);
static DDS_Topic find_topic(DDS_DomainParticipant dp, const char *name, const DDS_Duration_t *timeout);
enum topicsel { UNSPEC, KS, K32, K64, K128, K256, OU, ARB };
enum readermode { MODE_PRINT, MODE_CHECK, MODE_ZEROLOAD, MODE_DUMP, MODE_NONE };
#define PM_PID 1u
#define PM_TOPIC 2u
#define PM_TIME 4u
#define PM_IHANDLE 8u
#define PM_PHANDLE 16u
#define PM_STIME 32u
#define PM_RTIME 64u
#define PM_DGEN 128u
#define PM_NWGEN 256u
#define PM_RANKS 512u
#define PM_STATE 1024u
static int fdservsock = -1;
static volatile sig_atomic_t termflag = 0;
static int pid;
static DDS_GuardCondition termcond;
static unsigned nkeyvals = 1;
static int once_mode = 0;
static int extra_readers_at_end = 0;
static int wait_hist_data = 0;
static DDS_Duration_t wait_hist_data_timeout = { 0, 0 };
static double dur = 0.0;
static int sigpipe[2];
static int termpipe[2];
static int fdin = 0;
static int print_latency = 0;
static FILE *latlog_fp = NULL;
static enum tgprint_mode print_mode = TGPM_FIELDS;
static unsigned print_metadata = PM_STATE;
static unsigned print_chop = 0xffffffff;
static int printtype = 0;
static int print_final_take_notice = 1;
static int print_tcp = 0;
static DDS_Topic ddsi_control_topic = DDS_HANDLE_NIL;
static DDS_Publisher ddsi_control_pub;
static q_osplserModule_ddsi_controlDataWriter ddsi_control_wr;
#define T_SECOND ((int64_t) 1000000000)
struct tstamp_t {
int isabs;
int64_t t;
};
struct readerspec {
DDS_DataReader rd;
enum topicsel topicsel;
struct tgtopic *tgtp;
enum readermode mode;
int exit_on_out_of_seq;
int use_take;
unsigned sleep_us;
int polling;
int read_maxsamples;
int print_match_pre_read;
int do_final_take;
unsigned idx;
};
enum writermode {
WM_NONE,
WM_AUTO,
WM_INPUT
};
struct writerspec {
DDS_DataWriter wr;
DDS_DataWriter dupwr;
enum topicsel topicsel;
DDS_string tpname;
struct tgtopic *tgtp;
double writerate;
unsigned baggagesize;
int register_instances;
int duplicate_writer_flag;
unsigned burstsize;
enum writermode mode;
};
static const struct readerspec def_readerspec = {
.rd = DDS_HANDLE_NIL,
.topicsel = UNSPEC,
.tgtp = NULL,
.mode = MODE_PRINT,
.exit_on_out_of_seq = 0,
.use_take = 1,
.sleep_us = 0,
.polling = 0,
.read_maxsamples = DDS_LENGTH_UNLIMITED,
.print_match_pre_read = 0,
.do_final_take = 0
};
static const struct writerspec def_writerspec = {
.wr = DDS_HANDLE_NIL,
.dupwr = DDS_HANDLE_NIL,
.topicsel = UNSPEC,
.tpname = NULL,
.tgtp = NULL,
.writerate = 0.0,
.baggagesize = 0,
.register_instances = 0,
.duplicate_writer_flag = 0,
.burstsize = 1,
.mode = WM_INPUT
};
struct wrspeclist {
struct writerspec *spec;
struct wrspeclist *prev, *next; /* circular */
};
static void terminate (void)
{
const char c = 0;
termflag = 1;
write(termpipe[1], &c, 1);
DDS_GuardCondition_set_trigger_value(termcond, 1);
}
static void sigh (int sig __attribute__ ((unused)))
{
const char c = 1;
ssize_t r;
do {
r = write (sigpipe[1], &c, 1);
} while (r == -1 && errno == EINTR);
}
static void *sigthread(void *varg __attribute__ ((unused)))
{
while (1)
{
char c;
ssize_t r;
if ((r = read (sigpipe[0], &c, 1)) < 0)
{
if (errno == EINTR)
continue;
error ("sigthread: read failed, errno %d\n", (int) errno);
}
else if (r == 0)
error ("sigthread: unexpected eof\n");
else if (c == 0)
break;
else
terminate();
}
return NULL;
}
static int open_tcpserver_sock (int port)
{
struct sockaddr_in saddr;
int fd;
#if __APPLE__
saddr.sin_len = sizeof (saddr);
#endif
saddr.sin_family = AF_INET;
saddr.sin_port = htons (port);
saddr.sin_addr.s_addr = INADDR_ANY;
if ((fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
perror ("socket()");
exit (2); /* will kill any waiting threads */
}
if (bind (fd, (struct sockaddr *) &saddr, sizeof (saddr)) == -1)
{
perror ("bind()");
exit (2); /* will kill any waiting threads */
}
if (listen (fd, 1) == -1)
{
perror ("listen()");
exit (2); /* will kill any waiting threads */
}
if (print_tcp)
printf ("listening ... ");
fflush (stdout);
return fd;
}
static void usage (const char *argv0)
{
fprintf (stderr, "\
usage: %s [OPTIONS] PARTITION...\n\
\n\
OPTIONS:\n\
-T TOPIC[:TO][:EXPR] set topic name to TOPIC, TO is an optional timeout in\n\
seconds (default 10, use inf to wait indefinitely) for\n\
find_topic in ARB mode; EXPR can be used to create a content\n\
filtered topic \"cftN\" with filter expression EXPR based on\n\
the topic. Environment variables in EXPR are expanded in the\n\
usual manner and with:\n\
$SYSTEMID set to the current system id (in decimal)\n\
$NODE_BUILTIN_PARTITION set to the node-specific built-in\n\
partition\n\
specifying a topic name when one has already been given\n\
introduces a new reader/writer pair\n\
-K TYPE select type (ARB is default if topic specified, KS if not),\n\
(default topic name in parenthesis):\n\
KS - key, seq, octet sequence (PubSub)\n\
K32,K64,K128,K256 - key, seq, octet array (PubSub<N>)\n\
OU - one ulong, keyless (PubSubOU)\n\
ARB - use find_topic - no topic QoS override\n\
possible)\n\
<FILE> - read typename, keylist, metadata from\n\
FILE, then define topic named T with\n\
specified QoS\n\
specifying a type when one has already been given introduces\n\
a new reader/writer pair\n\
-q FS:QOS set QoS for entities indicated by FS, which must be one or\n\
more of: t (topic), p (publisher), s (subscriber),\n\
w (writer), r (reader), or a (all of them). For QoS syntax,\n\
see below. Inapplicable QoS's are ignored.\n\
-q provider=[PROFILE,]URI use URI, profile as QoS provider, in which case\n\
any QoS specification not of the LETTER=SETTING form gets\n\
passed as-is to the QoS provider (with the exception of the\n\
empty string, which is then translated to a null pointer to\n\
get the default value). Note that later specifications\n\
override earlier ones, so a QoS provider can be combined\n\
with the previous form as well. Also note that the default\n\
QoS's used by this program are slightly different from\n\
those of the DCPS API (topics default to by-source ordering\n\
and reliability, and readers and writers to the topic QoS),\n\
which may cause surprises :)\n\
-m [0|p[p]|{c|x}[p][:N]|z|d[p]] no reader, print values, check sequence\n\
numbers (x: exit 1 on receipt of out of sequence samples)\n\
(expecting N keys), \"zero-load\" mode or \"dump\" mode (which\n\
is differs from \"print\" primarily because it uses a data-\n\
available trigger and reads all samples in read-mode (default:\n\
p; pp, cp, dp are polling modes); set per-reader\n\
-D DUR run for DUR seconds\n\
-M TO:U wait for matching reader with user_data U and not owned\n\
by this instance of pubsub\n\
-n N limit take/read to N samples\n\
-O take/read once then exit 0 if samples present, or 1 if not\n\
-P MODES printing control (prefixing with \"no\" disables):\n\
meta enable printing of all metadata\n\
trad pid, time, phandle, stime, state\n\
pid process id of pubsub\n\
topic which topic (topic is def. for multi-topic)\n\
time read time relative to program start\n\
phandle publication handle\n\
ihandle instance handle\n\
stime source timestamp\n\
rtime reception timestamp\n\
dgen disposed generation count\n\
nwgen no-writers generation count\n\
ranks sample, generation, absolute generation ranks\n\
state instance/sample/view states\n\
latency[=F] show latency information for -mc[p] mode\n\
=F: write raw 64-bit current & source\n\
timestamps to file F\n\
additionally, for ARB types the following have effect:\n\
type print type definition at start up\n\
dense no additional white space, no field names\n\
fields field names, some white space\n\
multiline field names, one field per line\n\
chop:N chop to N characters (N = 0 is allowed)\n\
for non-once mode:\n\
finaltake print a \"final take\" notice before the\n\
results of the optional final take just\n\
before stopping\n\
for tcp-server setup:\n\
tcp print when listening for or accepting a new\n\
connection\n\
default is \"nometa,state,fields,finalttake\".\n\
-r register instances (-wN mode only)\n\
-R use 'read' instead of 'take'\n\
-$ perform one final take-all just before stopping\n\
-s MS sleep MS ms after each read/take (default: 0)\n\
-W TO wait_for_historical_data TO (TO in seconds or inf)\n\
-w F writer mode/input selection, F:\n\
- stdin (default)\n\
N cycle through N keys as fast as possible\n\
N:R*B cycle through N keys at R bursts/second, each burst\n\
consisting of B samples\n\
N:R as above, B=1\n\
:P listen on TCP port P\n\
H:P connect to TCP host H, port P\n\
no writer is created if -w0 and no writer listener\n\
automatic specifications can be given per writer; final\n\
interactive specification determines input used for non-\n\
automatic ones\n\
-S EVENTS monitor status events (comma separated; default: none)\n\
reader (abbreviated and full form):\n\
pr pre-read (virtual event)\n\
sl sample-lost\n\
sr sample-rejected\n\
lc liveliness-changed\n\
sm subscription-matched\n\
riq requested-incompatible-qos\n\
rdm requested-deadline-missed\n\
writer:\n\
ll liveliness-lost\n\
pm publication-matched\n\
oiq offered-incompatible-qos\n\
odm offered-deadline-missed\n\
if no -S option given, then readers are created with riq,\n\
writers with oiq, as these are nearly always actual issues\n\
-z N topic size (affects KeyedSeq only)\n\
-F set line-buffered mode\n\
-@ echo everything on duplicate writer (only for interactive)\n\
-* [M:]N sleep for M seconds just before deleteing participant and\n\
N seconds just before returning from main()\n\
-! disable signal handlers\n\
\n\
%s\n\
Note: defaults above are overridden as follows:\n\
r:k=all,R=10000/inf/inf\n\
w:k=all,R=100/inf/inf\n\
when group coherency is enabled, resource limits are forced to inf/inf/inf because of a restriction\n\
on the use of resource limits on the reader in combination with group coherency in some versions of\n\
OpenSplice.\n\
\n\
Input format is a white-space separated sequence (K* and OU, newline\n\
separated for ARB) of:\n\
N write next sample, key value N\n\
wN synonym for plain N; w@T N same with timestamp of T\n\
T is absolute if prefixed with \"=\", T currently in seconds\n\
dN dispose, key value N; d@T N as above\n\
DN write dispose, key value N; D@T N as above\n\
uN unregister, key value N; u@T N as above\n\
rN register, key value N; u@T N as above\n\
sN sleep for N seconds\n\
nN nap for N microseconds\n\
zN set topic size to N (affects KeyedSeq only)\n\
pX set publisher partition to comma-separated list X\n\
Y dispose_all\n\
B begin coherent changes\n\
E end coherent changes\n\
SP;T;U make persistent snapshot with given partition and topic\n\
expressions and URI\n\
CT;F;D write DDSI control topic (if feature enabled in config)\n\
T = {self|all|id} systemId of target\n\
F = [md]* flags, m = mute, d = deaf\n\
D = duration in seconds (floating or \"inf\")\n\
:M switch to writer M, where M is:\n\
+N, -N next, previous Nth writer of all non-automatic writers\n\
N defaults to 1\n\
N Nth writer of all non-automatic writers\n\
NAME unique writer of which topic name starts with NAME\n\
P print get_discovered_participants result\n\
Q quit - clean termination, same as EOF, SIGTERM or SIGINT\n\
) (a closing parenthesis) kill pubsub itself with signal SIGKILL\n\
Note: for K*, OU types, in the above N is always a decimal\n\
integer (possibly negative); because the OneULong type has no key\n\
the actual key value is irrelevant for OU mode. For ARB types, N must be\n\
a valid initializer. X must always be a list of names.\n\
\n\
When invoked as \"sub\", default is -w0 (no writer)\n\
When invoked as \"pub\", default is -m0 (no reader)\n",
argv0, qos_arg_usagestr);
exit (3);
}
static void expand_append (char **dst, size_t *sz, size_t *pos, char c)
{
if (*pos == *sz)
{
*sz += 1024;
*dst = realloc (*dst, *sz);
}
(*dst)[*pos] = c;
(*pos)++;
}
static char *expand_envvars (const char *src0);
static char *expand_env (const char *name, char op, const char *alt)
{
const char *env = getenv (name);
switch (op)
{
case 0:
return strdup (env ? env : "");
case '-':
return env ? strdup (env) : expand_envvars (alt);
case '?':
if (env)
return strdup (env);
else
{
char *altx = expand_envvars (alt);
error ("%s: %s\n", name, altx);
free (altx);
return NULL;
}
case '+':
return env ? expand_envvars (alt) : strdup ("");
default:
abort ();
}
}
static char *expand_envbrace (const char **src)
{
const char *start = *src + 1;
char *name, *x;
assert (**src == '{');
(*src)++;
while (**src && **src != ':' && **src != '}')
(*src)++;
if (**src == 0)
goto err;
name = malloc ((size_t) (*src - start) + 1);
memcpy (name, start, (size_t) (*src - start));
name[*src - start] = 0;
if (**src == '}')
{
(*src)++;
x = expand_env (name, 0, NULL);
free (name);
return x;
}
else
{
const char *altstart;
char *alt;
char op;
assert (**src == ':');
(*src)++;
switch (**src)
{
case '-': case '+': case '?':
op = **src;
(*src)++;
break;
default:
goto err;
}
altstart = *src;
while (**src && **src != '}')
{
if (**src == '\\')
{
(*src)++;
if (**src == 0)
goto err;
}
(*src)++;
}
if (**src == 0)
goto err;
assert (**src == '}');
alt = malloc ((size_t) (*src - altstart) + 1);
memcpy (alt, altstart, (size_t) (*src - altstart));
alt[*src - altstart] = 0;
(*src)++;
x = expand_env (name, op, alt);
free (alt);
free (name);
return x;
}
err:
error ("%*.*s: invalid expansion\n", (int) (*src - start), (int) (*src - start), start);
return NULL;
}
static char *expand_envsimple (const char **src)
{
const char *start = *src;
char *name, *x;
while (**src && (isalnum ((unsigned char)**src) || **src == '_'))
(*src)++;
assert (*src > start);
name = malloc ((size_t) (*src - start) + 1);
memcpy (name, start, (size_t) (*src - start));
name[*src - start] = 0;
x = expand_env (name, 0, NULL);
free (name);
return x;
}
static char *expand_envchar (const char **src)
{
char name[2];
assert (**src);
name[0] = **src;
name[1] = 0;
(*src)++;
return expand_env (name, 0, NULL);
}
static char *expand_envvars (const char *src0)
{
/* Expands $X, ${X}, ${X:-Y}, ${X:+Y}, ${X:?Y} forms */
const char *src = src0;
size_t sz = strlen (src) + 1, pos = 0;
char *dst = malloc (sz);
while (*src)
{
if (*src == '\\')
{
src++;
if (*src == 0)
error ("%s: incomplete escape at end of string\n", src0);
expand_append (&dst, &sz, &pos, *src++);
}
else if (*src == '$')
{
char *x, *xp;
src++;
if (*src == 0)
{
error ("%s: incomplete variable expansion at end of string\n", src0);
return NULL;
}
else if (*src == '{')
x = expand_envbrace (&src);
else if (isalnum ((unsigned char) *src) || *src == '_')
x = expand_envsimple (&src);
else
x = expand_envchar (&src);
xp = x;
while (*xp)
expand_append (&dst, &sz, &pos, *xp++);
free (x);
}
else
{
expand_append (&dst, &sz, &pos, *src++);
}
}
expand_append (&dst, &sz, &pos, 0);
return dst;
}
static unsigned split_partitions (const char ***p_ps, char **p_bufcopy, const char *buf)
{
const char *b;
const char **ps;
char *bufcopy, *bc;
unsigned i, nps;
nps = 1; for (b = buf; *b; b++) nps += (*b == ',');
ps = malloc (nps * sizeof (*ps));
bufcopy = expand_envvars (buf);
i = 0; bc = bufcopy;
while (1)
{
ps[i++] = bc;
while (*bc && *bc != ',') bc++;
if (*bc == 0) break;
*bc++ = 0;
}
assert (i == nps);
*p_ps = ps;
*p_bufcopy = bufcopy;
return nps;
}
static int set_pub_partition (DDS_Publisher pub, const char *buf)
{
const char **ps;
char *bufcopy;
unsigned nps = split_partitions(&ps, &bufcopy, buf);
DDS_ReturnCode_t rc;
if ((rc = change_publisher_partitions (pub, nps, ps)) != DDS_RETCODE_OK)
fprintf (stderr, "set_partition failed: %s (%d)\n", dds_strerror (rc), (int) rc);
free (bufcopy);
free (ps);
return 0;
}
#if 0
static int set_sub_partition (DDS_Subscriber sub, const char *buf)
{
const char **ps;
char *bufcopy;
unsigned nps = split_partitions(&ps, &bufcopy, buf);
DDS_ReturnCode_t rc;
if ((rc = change_subscriber_partitions (sub, nps, ps)) != DDS_RETCODE_OK)
fprintf (stderr, "set_partition failed: %s (%d)\n", dds_strerror (rc), (int) rc);
free (bufcopy);
free (ps);
return 0;
}
#endif
static void make_persistent_snapshot(const char *args)
{
DDS_DomainId_t id = DDS_DomainParticipant_get_domain_id(dp);
DDS_Domain dom;
DDS_ReturnCode_t ret;
char *p, *px = NULL, *tx = NULL, *uri = NULL;
px = strdup(args);
if ((p = strchr(px, ';')) == NULL) goto err;
*p++ = 0;
tx = p;
if ((p = strchr(tx, ';')) == NULL) goto err;
*p++ = 0;
uri = p;
if ((dom = DDS_DomainParticipantFactory_lookup_domain(dpf, id)) == NULL) {
printf ("failed to lookup domain\n");
} else {
if ((ret = DDS_Domain_create_persistent_snapshot(dom, px, tx, uri)) != DDS_RETCODE_OK)
printf ("failed to create persistent snapshot, error %d (%s)\n", (int) ret, dds_strerror(ret));
if ((ret = DDS_DomainParticipantFactory_delete_domain(dpf, dom)) != DDS_RETCODE_OK)
error ("failed to delete domain objet, error %d (%s)\n", (int) ret, dds_strerror(ret));
}
free(px);
return;
err:
printf ("%s: expected PART_EXPR;TOPIC_EXPR;URI\n", args);
free(px);
}
static void instancehandle_to_id (uint32_t *systemId, uint32_t *localId, DDS_InstanceHandle_t h)
{
/* Undocumented and unsupported trick */
union { struct { uint32_t systemId, localId; } s; DDS_InstanceHandle_t h; } u;
u.h = h;
*systemId = u.s.systemId & ~0x80000000;
*localId = u.s.localId;
}
static void do_ddsi_control(const char *args)
{
q_osplserModule_ddsi_control x;
const char *a = args;
struct qos *qos;
int pos;
memset(&x, 0, sizeof(x));
if (ddsi_control_topic == NULL) {
/* If enabled, topic exists, or will exist very soon; if disabled, we don't want to wait
for long. 100ms seems like a reasonable compromise. */
DDS_Duration_t to = { 0, 100000000 };
if ((ddsi_control_topic = find_topic(dp, "q_ddsiControl", &to)) == NULL) {
printf ("DDS_DomainParticipant_find_topic(\"q_ddsiControl\") failed\n");
return;
}
}
if (strncmp(a, "self", 4) == 0) {
uint32_t systemId, localId;
instancehandle_to_id (&systemId, &localId, DDS_Entity_get_instance_handle (dp));
x.systemId = systemId;
a += 4;
} else if (strncmp(a, "all", 3) == 0) {
x.systemId = 0;
a += 3;
} else if (sscanf(a, "%u%n", &x.systemId, &pos) == 1) {
a += pos;
} else {
printf ("ddsi control: invalid args: %s\n", args);
return;
}
if (*a++ != ';') {
printf ("ddsi control: invalid args: %s\n", args);
return;
}
while (*a && *a != ';') {
switch (*a++) {
case 'm': x.mute = 1; break;
case 'd': x.deaf = 1; break;
default: printf ("ddsi control: invalid flags: %s\n", args); return;
}
}
if (*a++ != ';') {
printf ("ddsi control: invalid args: %s\n", args);
return;
}
if (strcmp(a, "inf") == 0) {
x.duration = 0.0;
} else if (sscanf(a, "%lf%n", &x.duration, &pos) == 1 && a[pos] == 0) {
if (x.duration <= 0.0) {
printf ("ddsi control: invalid duration (<= 0): %s\n", args);
return;
}
} else {
printf ("ddsi control: invalid args: %s\n", args);
return;
}
if (ddsi_control_wr == NULL)
{
qos = new_pubqos();
qos_presentation(qos, "tnc");
ddsi_control_pub = new_publisher1(qos, "__BUILT-IN PARTITION__");
free_qos(qos);
qos = new_wrqos(ddsi_control_pub, ddsi_control_topic);
qos_autodispose_unregistered_instances(qos, "n");
ddsi_control_wr = new_datawriter(qos);
free_qos(qos);
}
q_osplserModule_ddsi_controlDataWriter_write(ddsi_control_wr, &x, DDS_HANDLE_NIL);
}
static int fd_getc (int fd)
{
/* like fgetc, but also returning EOF when need to terminate */
fd_set fds;
int maxfd;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
FD_SET(termpipe[0], &fds);
maxfd = (fd > termpipe[0]) ? fd : termpipe[0];
while (1)
{
r = select(maxfd + 1, &fds, NULL, NULL, NULL);
if ((r == -1 && errno == EINTR) || r == 0)
continue;
if (r == -1)
{
perror("fd_getc: select()");
exit(2);
}
if (FD_ISSET(termpipe[0], &fds))
{
return EOF;
}
if (FD_ISSET(fd, &fds))
{
unsigned char c;
ssize_t n = read (fd, &c, 1);
if (n == 1)
return c;
else if (n == 0)
return EOF;
else if (errno != EINTR)
{
perror("fd_getc: read()");
exit(2);
}
/* else try again */
}
}
}
static int read_int (int fd, char *buf, int bufsize, int pos, int accept_minus)
{
int c = EOF;
while (pos < bufsize-1 && (c = fd_getc (fd)) != EOF && (isdigit ((unsigned char) c) || (c == '-' && accept_minus)))
{
accept_minus = 0;
buf[pos++] = (char) c;
}
buf[pos] = 0;
if (c == EOF || isspace ((unsigned char) c))
return (pos > 0);
else if (!isdigit ((unsigned char) c))
{
fprintf (stderr, "%c: unexpected character\n", c);
return 0;
}
else if (pos == bufsize-1)
{
fprintf (stderr, "integer too long\n");
return 0;
}
return 1;
}
static int read_int_w_tstamp (struct tstamp_t *tstamp, int fd, char *buf, int bufsize, int pos)
{
int c;
assert (pos < bufsize - 2);
c = fd_getc (fd);
if (c == EOF)
return 0;
else if (c == '@')
{
int posoff = 0;
c = fd_getc (fd);
if (c == EOF)
return 0;
else if (c == '=')
tstamp->isabs = 1;
else
{
buf[pos] = (char) c;
posoff = 1;
}
if (read_int (fd, buf, bufsize, pos + posoff, 1))
tstamp->t = atoi (buf + pos) * T_SECOND;
else
return 0;
while ((c = fd_getc (fd)) != EOF && isspace ((unsigned char) c))
;
if (!isdigit ((unsigned char) c))
return 0;
}
buf[pos++] = (char) c;
while (pos < bufsize-1 && (c = fd_getc (fd)) != EOF && isdigit ((unsigned char) c))
buf[pos++] = (char) c;
buf[pos] = 0;
if (c == EOF || isspace ((unsigned char) c))
return (pos > 0);
else if (!isdigit ((unsigned char) c))
{
fprintf (stderr, "%c: unexpected character\n", c);
return 0;
}
else if (pos == bufsize-1)
{
fprintf (stderr, "integer too long\n");
return 0;
}
return 1;
}
static int read_value (int fd, char *command, int *key, struct tstamp_t *tstamp, char **arg)
{
char buf[1024];
int c;
if (*arg) { free(*arg); *arg = NULL; }
tstamp->isabs = 0;
tstamp->t = 0;
do {
while ((c = fd_getc (fd)) != EOF && isspace ((unsigned char) c))
;
if (c == EOF)
return 0;
switch (c)
{
case '-':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
buf[0] = (char) c;
if (read_int (fd, buf, sizeof (buf), 1, 0))
{
*command = 'w';
*key = atoi (buf);
return 1;
}
break;
case 'w': case 'd': case 'D': case 'u': case 'r':
*command = (char) c;
if (read_int_w_tstamp (tstamp, fd, buf, sizeof (buf), 0))
{
*key = atoi (buf);
return 1;
}
break;
case 'z': case 's': case 'n':
*command = (char) c;
if (read_int (fd, buf, sizeof (buf), 0, 0))
{
*key = atoi (buf);
return 1;
}
break;
case 'p': case 'S': case 'C': case ':': {
int i = 0;
*command = (char) c;
while ((c = fd_getc (fd)) != EOF && !isspace ((unsigned char) c))
{
assert (i < (int) sizeof (buf) - 1);
buf[i++] = (char) c;
}
buf[i] = 0;
*arg = strdup(buf);
ungetc (c, stdin);
return 1;
}
case 'Y': case 'B': case 'E': case 'W': case ')': case 'Q':
*command = (char) c;
return 1;
default:
fprintf (stderr, "'%c': unexpected character\n", c);
break;
}
while ((c = fd_getc (fd)) != EOF && !isspace ((unsigned char) c))
;
} while (c != EOF);
return 0;
}
static char *getl_simple (int fd, int *count)
{
size_t sz = 0, n = 0;
char *line;
int c;
if ((c = fd_getc(fd)) == EOF)
{
*count = 0;
return NULL;
}
line = NULL;
do {
if (n == sz) line = realloc(line, sz += 256);
line[n++] = (char) c;
} while ((c = fd_getc (fd)) != EOF && c != '\n');
if (n == sz) line = realloc(line, sz += 256);
line[n++] = 0;
*count = (int) (n-1);
return line;
}
struct getl_arg {
int use_editline;
union {
#if USE_EDITLINE
struct {
FILE *el_fp;
EditLine *el;
History *hist;
HistEvent ev;
} el;
#endif
struct {
int fd;
char *lastline;
} s;
} u;
};
static void getl_init_simple (struct getl_arg *arg, int fd)
{
arg->use_editline = 0;
arg->u.s.fd = fd;
arg->u.s.lastline = NULL;
}
#if USE_EDITLINE
static int el_getc_wrapper (EditLine *el, char *c)
{
void *fd;