|
2 | 2 | #include <thread> |
3 | 3 |
|
4 | 4 | #include "Registry.h" |
5 | | -#include <boost/system/error_code.hpp> |
| 5 | + |
| 6 | +#include <boost/log/trivial.hpp> |
| 7 | +#include <boost/program_options.hpp> |
| 8 | +#include <boost/asio/io_context.hpp> |
| 9 | +#include <boost/asio/signal_set.hpp> |
6 | 10 |
|
7 | 11 | int main(int argc, const char* argv[]) |
8 | 12 | { |
| 13 | + namespace bpo = boost::program_options; |
| 14 | + bpo::options_description options("Allowed options"); |
| 15 | + options.add_options()("help", "Produce help message") // |
| 16 | + ("bind", bpo::value<std::string>()->default_value("0::0"), "Server port bind address [0::0]") // |
| 17 | + ("multicast", bpo::value<std::string>()->default_value("ff31::8000:1234"), "Server multicast broadcast address [ff31::8000:1234]"); |
| 18 | + |
| 19 | + bpo::variables_map vm; |
| 20 | + bpo::store(bpo::parse_command_line(argc, argv, options), vm); |
| 21 | + bpo::notify(vm); |
| 22 | + |
9 | 23 | std::unique_ptr<pfc::Registry> reg; |
10 | 24 | try { |
11 | | - reg = std::make_unique<pfc::Registry>("0::0", "ff31::8000:1234"); |
| 25 | + reg = std::make_unique<pfc::Registry>(vm["bind"].as<std::string>(), vm["multicast"].as<std::string>()); |
12 | 26 |
|
13 | | - } catch (boost::system::error_code& e) { |
14 | | - std::cout << e << std::endl; |
15 | | - } catch (...) { |
16 | | - std::cout << "Unknown Exception Caught\n"; |
| 27 | + } catch (std::exception& e) { |
| 28 | + std::cerr << e.what(); |
17 | 29 | } |
18 | 30 |
|
19 | | - if (reg) { |
20 | | - reg->start(); |
21 | | - } |
| 31 | + boost::asio::io_context context; |
| 32 | + boost::asio::signal_set signals(context); |
22 | 33 |
|
23 | | - std::cout << "Sleeping for 15 seconds\n"; |
24 | | - std::this_thread::sleep_for(std::chrono::seconds(15)); |
25 | | - std::cout << "Done Sleeping\n"; |
| 34 | + signals.add(SIGINT); |
| 35 | + signals.add(SIGTERM); |
| 36 | +#if defined(SIGQUIT) |
| 37 | + signals_.add(SIGQUIT); |
| 38 | +#endif |
| 39 | + signals.async_wait([&](const boost::system::error_code& /*ec*/, int /*no*/) { |
| 40 | + BOOST_LOG_TRIVIAL(info) << "Stopping PFC Registry\n"; |
| 41 | + reg->shutdown(); |
| 42 | + }); |
26 | 43 |
|
| 44 | + // Start an asynchronous wait for one of the signals to occur. |
27 | 45 | if (reg) { |
28 | | - reg->shutdown(); |
| 46 | + BOOST_LOG_TRIVIAL(info) << "Starting PFC Registry on " << vm["bind"].as<std::string>() << " broadcasting on " << vm["multicast"].as<std::string>() << "\n"; |
| 47 | + reg->start(); |
29 | 48 | } |
| 49 | + context.run(); |
30 | 50 | } |
0 commit comments