ADC/tests/adc_asio_netserver_test.cpp
Timur A. Fatkhullin 4e3e3ec60e ...
2024-10-29 01:21:24 +03:00

68 lines
2.1 KiB
C++

#include <cxxopts.hpp>
#include <iostream>
#include "../net/adc_endpoint.h"
#include "../net/adc_netproto.h"
#include "../net/asio/adc_device_netserver_asio.h"
int main(int argc, char* argv[])
{
/* COMMANDLINE OPTS */
cxxopts::Options options(argv[0], "ADC-library test device network server (ASIO implementation)\n");
options.allow_unrecognised_options();
options.add_options()("h,help", "Print usage");
options.add_options()(
"endpoints", "endpoints server will be listening for",
cxxopts::value<std::vector<std::string>>()->default_value("local://stream/@ADC_ASIO_TEST_SERVER"));
options.parse_positional({"endpoints"});
try {
auto opt_result = options.parse(argc, argv);
if (opt_result["help"].count()) {
std::cout << options.help();
return 0;
}
asio::io_context io_ctx;
adc::impl::AdcDeviceNetServerASIO server("TEST SRV", io_ctx);
server.setupSignals();
auto epnt = opt_result["endpoints"].as<std::vector<std::string>>();
for (auto& ep : epnt) {
adc::AdcEndpointParser epn(ep);
if (epn.isValid()) {
if (epn.isLocalSeqpacket() || epn.isLocalStream()) {
if (epn.path()[0] == '@') { // replace '@' to '\0' (use of UNIX abstract namespace)
auto it = std::ranges::find(ep, '@');
*it = '\0';
epn = adc::AdcEndpointParser(ep);
}
}
std::cout << "try to start listenning at '" << ep << "' ...";
server.start<adc::AdcStopSeqSessionProto<>>(epn);
std::cout << "\tOK\n";
} else {
std::cerr << "Unrecognized endpoint: '" << ep << "'! Ignore!\n";
}
}
io_ctx.run();
} catch (const cxxopts::exceptions::exception& ex) {
std::cerr << "\nAn error occured while parsing input options: " << ex.what() << "\n";
return 127;
} catch (const std::system_error& ex) {
std::cerr << "\nAn error ocured: " << ex.what() << "\n";
return 128;
}
return 0;
}