-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathDiscoveryServer_main.cpp
More file actions
331 lines (301 loc) · 10.5 KB
/
Copy pathDiscoveryServer_main.cpp
File metadata and controls
331 lines (301 loc) · 10.5 KB
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
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.
/**
* @file DiscoveryServer_main.cpp
*
*/
#include <string>
#include "arg_configuration.h"
#include "DiscoveryServerPublisher.h"
#include "DiscoveryServerServer.h"
#include "DiscoveryServerSubscriber.h"
enum class EntityKind
{
PUBLISHER,
SUBSCRIBER,
SERVER,
};
int main(
int argc,
char** argv)
{
int columns;
#if defined(_WIN32)
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, "COLUMNS") == 0 && buf != nullptr)
{
columns = strtol(buf, nullptr, 10);
free(buf);
}
else
{
columns = 80;
}
#else
columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 80;
#endif // if defined(_WIN32)
EntityKind type = EntityKind::PUBLISHER;
std::string topic_name = "HelloWorldTopic";
int count = 0;
long sleep = 100;
// Transport
TransportKind transport = TransportKind::UDPv4;
// Discovery Server connection
std::string connection_address = "127.0.0.1"; // default ip address
uint16_t connection_port = 16166; // default physical port
bool id_ds_set = false;
// Discovery Server listening
std::string listening_address = "127.0.0.1"; // default ip address
uint16_t listening_port = 16166; // default physical port
uint32_t timeout = 0; // default DS id
if (argc > 1)
{
if (!strcmp(argv[1], "publisher"))
{
type = EntityKind::PUBLISHER;
}
else if (!strcmp(argv[1], "subscriber"))
{
type = EntityKind::SUBSCRIBER;
}
else if (!strcmp(argv[1], "server"))
{
type = EntityKind::SERVER;
}
// check if first argument is help, needed because we skip it when parsing
else if (!(strcmp(argv[1], "-h") && strcmp(argv[1], "--help")))
{
option::printUsage(fwrite, stdout, usage, columns);
return 0;
}
else
{
std::cerr << "ERROR: first argument can only be <publisher|subscriber|server>" << std::endl;
option::printUsage(fwrite, stdout, usage, columns);
return 1;
}
argc -= (argc > 0);
argv += (argc > 0); // skip program name argv[0] if present
--argc; ++argv; // skip pub/sub argument
option::Stats stats(usage, argc, argv);
std::vector<option::Option> options(stats.options_max);
std::vector<option::Option> buffer(stats.buffer_max);
option::Parser parse(usage, argc, argv, &options[0], &buffer[0]);
if (parse.error())
{
option::printUsage(fwrite, stdout, usage, columns);
return 1;
}
if (options[HELP])
{
option::printUsage(fwrite, stdout, usage, columns);
return 0;
}
for (int i = 0; i < parse.optionsCount(); ++i)
{
option::Option& opt = buffer[i];
switch (opt.index())
{
case optionIndex::HELP:
// not possible, because handled further above and exits the program
break;
case optionIndex::TOPIC:
if (type == EntityKind::SERVER)
{
print_warning("publisher|subscriber", opt.name);
}
else
{
topic_name = std::string(opt.arg);
}
break;
case optionIndex::SAMPLES:
if (type == EntityKind::SERVER)
{
print_warning("publisher|subscriber", opt.name);
}
else
{
count = strtol(opt.arg, nullptr, 10);
}
break;
case optionIndex::INTERVAL:
if (type == EntityKind::PUBLISHER)
{
sleep = strtol(opt.arg, nullptr, 10);
}
else
{
print_warning("publisher", opt.name);
}
break;
case optionIndex::TRANSPORT:
{
std::string transport_str(opt.arg);
if (transport_str == "udpv4")
{
transport = TransportKind::UDPv4;
}
else if (transport_str == "udpv6")
{
transport = TransportKind::UDPv6;
}
else if (transport_str == "tcpv4")
{
transport = TransportKind::TCPv4;
}
else if (transport_str == "tcpv6")
{
transport = TransportKind::TCPv6;
}
else
{
print_warning("udpv4|udpv6|tcpv4|tcpv6", opt.name);
}
break;
}
case optionIndex::CONNECTION_PORT:
id_ds_set = true;
connection_port = static_cast<uint16_t>(strtol(opt.arg, nullptr, 10));
break;
case optionIndex::CONNECTION_ADDRESS:
id_ds_set = true;
connection_address = opt.arg;
break;
case optionIndex::LISTENING_PORT:
if (type != EntityKind::SERVER)
{
print_warning("server", opt.name);
break;
}
listening_port = static_cast<uint16_t>(strtol(opt.arg, nullptr, 10));
break;
case optionIndex::LISTENING_ADDRESS:
if (type != EntityKind::SERVER)
{
print_warning("server", opt.name);
break;
}
listening_address = opt.arg;
break;
case optionIndex::TIMEOUT:
if (type != EntityKind::SERVER)
{
print_warning("server", opt.name);
break;
}
timeout = strtol(opt.arg, nullptr, 10);
break;
case optionIndex::UNKNOWN_OPT:
std::cerr << "ERROR: " << opt.name << " is not a valid argument." << std::endl;
option::printUsage(fwrite, stdout, usage, columns);
return 1;
break;
}
}
}
else
{
std::cerr << "ERROR: <publisher|subscriber|server> argument is required." << std::endl;
option::printUsage(fwrite, stdout, usage, columns);
return 1;
}
// Check that ip matches transport
// if (transport == TransportKind::UDPv4 && !eprosima::fastdds::rtps::IPLocator::isIPv4(listening_address))
// {
// std::cerr << "ERROR: IPv4 is needed to use UDPv4. Wrong IP address: " << listening_address << std::endl;
// option::printUsage(fwrite, stdout, usage, columns);
// return 1;
// }
// else if (transport == TransportKind::UDPv6 && !eprosima::fastdds::rtps::IPLocator::isIPv6(listening_address))
// {
// std::cerr << "ERROR: IPv6 is needed to use UDPv6. Wrong IP address: " << listening_address << std::endl;
// option::printUsage(fwrite, stdout, usage, columns);
// return 1;
// }
// Check that a DS has not same ip and port in listening and connection
if (id_ds_set &&
type == EntityKind::SERVER &&
listening_address == connection_address &&
listening_port == connection_port)
{
std::cerr << "ERROR: Discovery Servers ports must be different, "
<< " cannot connect to a server with same listening address "
<< listening_address << "(" << listening_port << ")" << std::endl;
option::printUsage(fwrite, stdout, usage, columns);
return 1;
}
switch (type)
{
case EntityKind::PUBLISHER:
{
HelloWorldPublisher mypub;
if (mypub.init(
topic_name,
connection_address,
connection_port,
transport))
{
mypub.run(static_cast<uint32_t>(count), static_cast<uint32_t>(sleep));
}
else
{
std::cerr << "ERROR: when initializing Publisher." << std::endl;
return 1;
}
break;
}
case EntityKind::SUBSCRIBER:
{
HelloWorldSubscriber mysub;
if (mysub.init(
topic_name,
static_cast<uint32_t>(count),
connection_address,
connection_port,
transport))
{
mysub.run(static_cast<uint32_t>(count));
}
else
{
std::cerr << "ERROR: when initializing Subscriber." << std::endl;
return 1;
}
break;
}
case EntityKind::SERVER:
{
DiscoveryServer myserver;
if (myserver.init(
listening_address,
listening_port,
transport,
id_ds_set,
connection_address,
connection_port))
{
myserver.run(timeout);
}
else
{
std::cerr << "ERROR: when initializing Server." << std::endl;
return 1;
}
break;
}
}
return 0;
}