-
Notifications
You must be signed in to change notification settings - Fork 10
/
rmc_test.c
224 lines (187 loc) · 7.17 KB
/
rmc_test.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
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "rmc_internal.h"
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "rmc_log.h"
extern void test_packet_interval();
extern void run_list_tests();
extern void test_pub(void);
extern void test_sub(void);
extern void test_rmc_proto_pub(char* mcast_group_addr,
char* mcast_if_addr,
char* listen_if_addr,
int mcast_port,
int listen_port,
rmc_node_id_t ctx_id,
uint64_t count,
int expected_subscribers,
int seed,
usec_timestamp_t send_interval, //usec
int jitter, // usec
float drop_rate);
extern void test_rmc_proto_sub(char* mcast_addr,
char* mcast_if_addr,
int mcast_port,
rmc_node_id_t ctx_id,
uint8_t* ctx_id_map,
int ctx_id_map_size);
extern void test_circular_buffer(void);
#define LISTEN_IF_ADDR_DEFAULT "0.0.0.0"
#define MULTICAST_IF_ADDR_DEFAULT "0.0.0.0"
#define MULTICAST_ADDR_DEFAULT "239.0.0.1"
#define MULTICAST_PORT_DEFAULT 4723
#define TCP_PORT_DEFAULT 0
void usage(char* prog)
{
fprintf(stderr,
"Usage: %s -L <log-level> [-P] [-M <ip-addr>] [-m <ip-addr>] [-l <ip-addr>] [-P <port>] [-p <port>]\n",
prog);
fprintf(stderr, " -L <log-level> Set log-level. 0=None. 1=Fatal. 2=Error 3=Warning. 4=Info. 5=Comment. 6=Debug. Default is 0\n");
fprintf(stderr, " -S Run as subscriber instead of default publisher\n");
fprintf(stderr, " -M <ip-addr> Multicast IP address (default: %s)\n", MULTICAST_ADDR_DEFAULT);
fprintf(stderr, " -m <ip-addr> Multicast interface IP (default: %s)\n", MULTICAST_IF_ADDR_DEFAULT);
fprintf(stderr, " -l <ip-addr> Listen interface IP (default: %s)\n", LISTEN_IF_ADDR_DEFAULT);
fprintf(stderr, " -P <port> Multicast port (default: %d)\n", MULTICAST_PORT_DEFAULT);
fprintf(stderr, " -p <port> Listen port (defaultL %d - random port)\n\n", TCP_PORT_DEFAULT);
fprintf(stderr, " -c <count> Number of packets to transmit (publisher only). Default 1\n");
fprintf(stderr, " -i <id> Unique node ID among all publishers and subscribers. Legal value 1-1000. Default = 1\n");
fprintf(stderr, " -r <seed> Random seed number. Default 1\n");
fprintf(stderr, " -d <drop-rate> Chance of sender dropping packet. 0.0 - never. 1.0 - always. Default 0.0\n");
fprintf(stderr, " -s <interval> Time, in usec, to wait between each packet send. \n");
fprintf(stderr, " -j <jitter> Max jitter, in usec, for send interval. Actual interval will be send-interval +- (0.0-1.0)*jitter. Default 0. \n");
fprintf(stderr, " -E <subscriber-count> Expected number of subscribers to connect before we start sending. Publisher only. Default 1 \n");
fprintf(stderr, " -e <node-id> Expect packets from node_id. Legal value 1-1000. Susbscriber only. Can be repeated. Default 1 \n");
}
int main(int argc, char* argv[])
{
int opt = 0;
int publisher = 1;
char mcast_group_addr[80] = { 0 };
char mcast_if_addr[80] = { 0 };
char listen_if_addr[80];
int listen_port = TCP_PORT_DEFAULT;
int mcast_port = MULTICAST_PORT_DEFAULT;
int rand_seed = 1;
uint64_t packet_count = 1;
rmc_node_id_t node_id = 1;
float drop_rate = 0.0;
int send_interval = 0;
int jitter = 0;
uint8_t expected_node_id[1024];
int expect_node_id = 0;
int expected_subscriber_count = 1;
int e_arg_set = 0;
int log_level = RMC_LOG_LEVEL_NONE;
strcpy(mcast_if_addr, MULTICAST_IF_ADDR_DEFAULT);
strcpy(listen_if_addr, LISTEN_IF_ADDR_DEFAULT);
strcpy(mcast_group_addr, MULTICAST_ADDR_DEFAULT);
memset(expected_node_id, 0, sizeof(expected_node_id));
while ((opt = getopt(argc, argv, "SP:M:m:l:p:c:n:r:s:j:d:e:E:L:i:")) != -1) {
switch (opt) {
case 'L':
log_level = atoi(optarg);
break;
case 'S':
publisher = 0;
break;
case 'M':
strcpy(mcast_group_addr, optarg);
break;
case 'm':
strcpy(mcast_if_addr, optarg);
break;
case 'l':
strcpy(listen_if_addr, optarg);
break;
case 'P':
mcast_port = atoi(optarg);
break;
case 'p':
listen_port = atoi(optarg);
break;
case 'i':
node_id = atoi(optarg);
break;
case 'c':
packet_count = atoll(optarg);
break;
case 'r':
rand_seed = atoi(optarg);
break;
case 's':
send_interval = atoi(optarg);
break;
case 'j':
jitter = atoi(optarg);
break;
case 'd':
drop_rate = atof(optarg);
break;
case 'E':
expected_subscriber_count = atoi(optarg);
case 'e':
expect_node_id = atoi(optarg);
if (expect_node_id < 1 || expect_node_id >= 1024) {
usage(argv[0]);
exit(1);
}
expected_node_id[expect_node_id] = 1;
e_arg_set = 1;
break;
default: /* '?' */
usage(argv[0]);
exit(1);
}
}
// Default
if (!e_arg_set)
expected_node_id[1] = 1;
if (rmc_set_log_level(log_level)) {
usage(argv[0]);
exit(1);
}
run_list_tests();
test_packet_interval();
test_circular_buffer();
test_pub();
test_sub();
setlinebuf(stdout);
setlinebuf(stderr);
if (!publisher) {
RMC_LOG_INFO("SUBSCRIBER");
test_rmc_proto_sub(mcast_group_addr,
mcast_if_addr,
mcast_port,
node_id,
expected_node_id,
sizeof(expected_node_id) / sizeof(expected_node_id[0]));
exit(0);
}
setlinebuf(stdout);
setlinebuf(stderr);
RMC_LOG_INFO("PUBLISHER");
test_rmc_proto_pub(mcast_group_addr,
mcast_if_addr,
listen_if_addr,
mcast_port,
listen_port,
node_id,
packet_count,
expected_subscriber_count,
rand_seed,
send_interval,
jitter,
drop_rate);
exit(0);
}