-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHelloWorld_main.cpp
More file actions
306 lines (270 loc) · 8.76 KB
/
Copy pathHelloWorld_main.cpp
File metadata and controls
306 lines (270 loc) · 8.76 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
// Copyright 2016 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 HelloWorld_main.cpp
*
*/
#include <regex>
#include <fastdds/dds/log/Log.hpp>
#include "HelloWorldPublisher.h"
#include "HelloWorldSubscriber.h"
#include "HelloWorldServer.h"
#include "optionparser.h"
struct Arg : public option::Arg
{
static void print_error(const char* msg1, const option::Option& opt, const char* msg2)
{
fprintf(stderr, "%s", msg1);
fwrite(opt.name, opt.namelen, 1, stderr);
fprintf(stderr, "%s", msg2);
}
static option::ArgStatus Unknown(const option::Option& option, bool msg)
{
if (msg) print_error("Unknown option '", option, "'\n");
return option::ARG_ILLEGAL;
}
static option::ArgStatus Required(const option::Option& option, bool msg)
{
if (option.arg != 0 && option.arg[0] != 0)
return option::ARG_OK;
if (msg) print_error("Option '", option, "' requires an argument\n");
return option::ARG_ILLEGAL;
}
static option::ArgStatus Numeric(const option::Option& option, bool msg)
{
char* endptr = 0;
if (option.arg != 0 && strtol(option.arg, &endptr, 10))
{
}
if (endptr != option.arg && *endptr == 0)
{
return option::ARG_OK;
}
if (msg)
{
print_error("Option '", option, "' requires a numeric argument\n");
}
return option::ARG_ILLEGAL;
}
static option::ArgStatus Locator(const option::Option& option, bool msg)
{
if (option.arg != 0)
{
// we must check if its a correct ip address plus port number
if(std::regex_match(option.arg, ipv4)
|| std::regex_match(option.arg, ipv6))
{
return option::ARG_OK;
}
}
if (msg)
{
print_error("Option '", option, "' requires an IPaddress[:portnumber] argument\n");
}
return option::ARG_ILLEGAL;
}
static const std::regex ipv4, ipv6;
};
enum optionIndex {
UNKNOWN_OPT,
HELP,
SAMPLES,
INTERVAL,
TCP,
LOCATOR
};
const option::Descriptor usage[] = {
{ UNKNOWN_OPT, 0,"", "", Arg::None,
"Usage: HelloWorldExampleDS <publisher|subscriber|server>\n\nGeneral options:" },
{ HELP, 0,"h", "help", Arg::None, " -h \t--help \tProduce help message." },
{ TCP,0,"t","tcp", Arg::None,
" -t \t--tcp \tUse tcp transport instead of the default UDP one." },
{ SAMPLES,0,"c","count", Arg::Numeric,
" -c <num>, \t--count=<num> \tNumber of datagrams to send (0 = infinite) defaults to 10." },
{ INTERVAL,0,"i","interval", Arg::Numeric,
" -i <num>, \t--interval=<num> \tTime between samples in milliseconds (Default: 100)." },
{ LOCATOR, 0, "l", "ip", Arg::Locator,
" -l <IPaddress[:port number]>, \t--ip=<IPaddress[:port number]> \tServer address." },
{ 0, 0, 0, 0, 0, 0 }
};
/*static*/ const std::regex Arg::ipv4(R"(^((?:[0-9]{1,3}\.){3}[0-9]{1,3})?:?(?:(\d+))?$)");
/*static*/ const std::regex Arg::ipv6(R"(^\[?((?:[0-9a-fA-F]{0,4}\:){7}[0-9a-fA-F]{0,4})?(?:\]:)?(?:(\d+))?$)");
using namespace eprosima::fastdds::rtps;
using namespace eprosima::fastdds::dds;
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
std::cout << "Starting "<< std::endl;
int type = 1;
int count = 20;
long sleep = 100;
eprosima::fastdds::rtps::Locator server_address;
server_address.port = 1811; // default physical port
if(argc > 1)
{
if (strcmp(argv[1], "publisher") == 0)
{
type = 1;
}
else if (strcmp(argv[1], "subscriber") == 0)
{
type = 2;
}
else if (strcmp(argv[1], "server") == 0)
{
type = 3;
}
else
{
option::printUsage(fwrite, stdout, usage, columns);
return 0;
}
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())
{
return 1;
}
for (int i = 0; i < parse.optionsCount(); ++i)
{
option::Option& opt = buffer[i];
switch (opt.index())
{
case HELP:
option::printUsage(fwrite, stdout, usage, columns);
return 0;
case SAMPLES:
count = strtol(opt.arg, nullptr, 10);
break;
case INTERVAL:
sleep = strtol(opt.arg, nullptr, 10);
break;
// remember that options can be parsed in any order
case TCP:
{
// locators default to LOCATOR_KIND_UDPv4
// promote from UDP to TCP
if(IsAddressDefined(server_address))
{
server_address.kind =
( server_address.kind == LOCATOR_KIND_UDPv4 ) ? LOCATOR_KIND_TCPv4 : LOCATOR_KIND_TCPv6;
}
else
{
server_address.kind = LOCATOR_KIND_TCPv4;
}
break;
}
case LOCATOR:
{
std::cmatch mr;
std::string ip_address;
uint16_t port = server_address.port;
bool v4 = true;
if((v4 = regex_match(opt.arg, mr, Arg::ipv4))
|| regex_match(opt.arg, mr, Arg::ipv6))
{
std::cmatch::iterator it = mr.cbegin();
ip_address = (++it)->str();
if((++it)->matched)
{
port = std::stoi(it->str());
}
}
// promote to v6 if needed
if(!v4)
{
server_address.kind =
(server_address.kind == LOCATOR_KIND_UDPv4) ? LOCATOR_KIND_UDPv6 : LOCATOR_KIND_TCPv6;
}
if(!ip_address.empty() && port > 1000)
{
IPLocator::setPhysicalPort(server_address, port);
if(v4)
{
IPLocator::setIPv4(server_address, ip_address);
}
else
{
IPLocator::setIPv6(server_address, ip_address);
}
}
break;
}
case UNKNOWN_OPT:
option::printUsage(fwrite, stdout, usage, columns);
return 0;
break;
}
}
}
else
{
std::cout << "publisher, subscriber OR server argument needed" << std::endl;
Log::Reset();
return 0;
}
switch(type)
{
case 1:
{
HelloWorldPublisher mypub;
if(mypub.init(server_address))
{
mypub.run(count, sleep);
}
break;
}
case 2:
{
HelloWorldSubscriber mysub;
if(mysub.init(server_address))
{
mysub.run();
}
break;
}
case 3:
{
HelloWorldServer myserver;
if (myserver.init(server_address))
{
myserver.run();
}
break;
}
}
Log::Flush();
return 0;
}