-
Notifications
You must be signed in to change notification settings - Fork 9
/
splitter_main.cc
228 lines (190 loc) · 8.78 KB
/
splitter_main.cc
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
// Copyright (c) 2015-2016, FlightAware LLC.
// Copyright (c) 2015, Oliver Jowett <oliver@mutability.co.uk>
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "beast_input.h"
#include "beast_input_net.h"
#include "beast_input_serial.h"
#include "beast_output.h"
#include "modes_filter.h"
#include "status_writer.h"
#include <boost/asio/ip/address_v4.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/program_options.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <memory>
namespace po = boost::program_options;
using boost::asio::ip::tcp;
struct net_option {
std::string host;
std::string port;
};
struct output_option {
std::string host;
std::string port;
beast::Settings settings;
};
struct listen_option : output_option {};
struct connect_option : output_option {};
// Specializations of validate for --listen / --connect / --net
void validate(boost::any &v, const std::vector<std::string> &values, net_option *target_type, int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);
static const boost::regex r("([^:]+):(\\d+)");
boost::smatch match;
if (boost::regex_match(s, match, r)) {
net_option o;
o.host = match[1];
o.port = match[2];
v = boost::any(o);
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
}
void validate(boost::any &v, const std::vector<std::string> &values, connect_option *target_type, int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);
static const boost::regex r("([^:]+):(\\d+)(?::([a-zA-Z]+))?");
boost::smatch match;
if (boost::regex_match(s, match, r)) {
connect_option o;
o.host = match[1];
o.port = match[2];
o.settings = beast::Settings(match[3]);
v = boost::any(o);
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
}
void validate(boost::any &v, const std::vector<std::string> &values, listen_option *target_type, int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);
static const boost::regex r("(?:([^:]+):)?(\\d+)(?::([a-zA-Z]+))?");
boost::smatch match;
if (boost::regex_match(s, match, r)) {
listen_option o;
o.host = match[1];
o.port = match[2];
o.settings = beast::Settings(match[3]);
v = boost::any(o);
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
}
namespace beast {
void validate(boost::any &v, const std::vector<std::string> &values, beast::Settings *target_type, long int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);
static const boost::regex r("[cdefghijbrvCDEFGHIJBRV]*");
if (boost::regex_match(s, r)) {
v = boost::any(beast::Settings(s));
} else {
throw po::validation_error(po::validation_error::invalid_option_value);
}
}
} // namespace beast
#define EXIT_NO_RESTART (64)
static int realmain(int argc, char **argv) {
boost::asio::io_service io_service;
modes::FilterDistributor distributor;
po::options_description desc("Allowed options");
desc.add_options()("help", "produce help message")("serial", po::value<std::string>(), "read from given serial device")("net", po::value<net_option>(), "read from given network host:port")("status-file", po::value<std::string>(), "set path to status file")("fixed-baud", po::value<unsigned>()->default_value(0), "set a fixed baud rate, or 0 for autobauding")("listen", po::value<std::vector<listen_option>>(), "specify a [host:]port[:settings] to listen on")(
"connect", po::value<std::vector<connect_option>>(), "specify a host:port[:settings] to connect to")("force", po::value<beast::Settings>()->default_value(beast::Settings()), "specify settings to force on or off when configuring the Beast");
po::variables_map opts;
try {
po::store(po::parse_command_line(argc, argv, desc), opts);
po::notify(opts);
} catch (boost::program_options::error &err) {
std::cerr << err.what() << std::endl;
std::cerr << desc << std::endl;
return EXIT_NO_RESTART;
}
if (opts.count("help")) {
std::cerr << desc << std::endl;
return EXIT_NO_RESTART;
}
if (!opts.count("connect") && !opts.count("listen")) {
std::cerr << "At least one --connect or --listen argument is needed" << std::endl;
std::cerr << desc << std::endl;
return EXIT_NO_RESTART;
}
beast::BeastInput::pointer input;
if (opts.count("serial")) {
input = beast::SerialInput::create(io_service, opts["serial"].as<std::string>(), opts["fixed-baud"].as<unsigned>(), opts["force"].as<beast::Settings>());
} else if (opts.count("net")) {
auto net = opts["net"].as<net_option>();
input = beast::NetInput::create(io_service, net.host, net.port, opts["force"].as<beast::Settings>());
} else {
std::cerr << "A --serial or --net argument is needed" << std::endl;
std::cerr << desc << std::endl;
return EXIT_NO_RESTART;
}
distributor.set_filter_notifier(std::bind(&beast::BeastInput::set_filter, input, std::placeholders::_1));
tcp::resolver resolver(io_service);
if (opts.count("listen")) {
for (auto l : opts["listen"].as<std::vector<listen_option>>()) {
tcp::resolver::query query(l.host, l.port, tcp::resolver::query::passive);
boost::system::error_code ec;
bool success = false;
tcp::resolver::iterator end;
for (auto i = resolver.resolve(query, ec); i != end; ++i) {
const auto &endpoint = i->endpoint();
try {
auto listener = beast::SocketListener::create(io_service, endpoint, distributor, l.settings);
listener->start();
std::cerr << "Listening on " << endpoint << std::endl;
success = true;
} catch (boost::system::system_error &err) {
std::cerr << "Could not listen on " << endpoint << ": " << err.what() << std::endl;
ec = err.code();
}
}
if (!success) {
if (l.host.empty())
std::cerr << "Could not bind to port " << l.port << ": " << ec.message() << std::endl;
else
std::cerr << "Could not bind to " << l.host << ":" << l.port << ": " << ec.message() << std::endl;
return 1;
}
}
}
if (opts.count("connect")) {
for (auto l : opts["connect"].as<std::vector<connect_option>>()) {
auto connector = beast::SocketConnector::create(io_service, l.host, l.port, distributor, l.settings);
connector->start();
}
}
if (opts.count("status-file")) {
auto statuswriter = splitter::StatusWriter::create(io_service, distributor, input, opts["status-file"].as<std::string>());
statuswriter->start();
}
input->set_message_notifier(std::bind(&modes::FilterDistributor::broadcast, &distributor, std::placeholders::_1));
input->start();
io_service.run();
return 0;
}
int main(int argc, char **argv) {
try {
return realmain(argc, argv);
} catch (std::exception &e) {
std::cerr << "Uncaught exception: " << e.what() << std::endl;
return 2;
}
}