32 #include <boost/asio/signal_set.hpp>
33 #include <boost/exception/diagnostic_information.hpp>
34 #include <boost/program_options/options_description.hpp>
35 #include <boost/program_options/parsers.hpp>
36 #include <boost/program_options/variables_map.hpp>
38 #include <ndn-cxx/net/network-monitor.hpp>
39 #include <ndn-cxx/util/scheduler.hpp>
40 #include <ndn-cxx/util/time.hpp>
44 namespace po = boost::program_options;
52 const po::options_description& opts,
53 const char* programName)
55 os <<
"Usage: " << programName <<
" [options]\n"
63 boost::asio::signal_set terminateSignals(proc.
getIoContext());
64 terminateSignals.add(SIGINT);
65 terminateSignals.add(SIGTERM);
66 terminateSignals.async_wait([&] (
const auto& error,
int signalNo) {
70 const char* signalName = ::strsignal(signalNo);
71 std::cerr <<
"Exiting on signal ";
72 if (signalName ==
nullptr) {
73 std::cerr << signalNo;
76 std::cerr << signalName;
78 std::cerr << std::endl;
83 scheduler::ScopedEventId runEvt;
84 auto scheduleRerun = [&] (time::nanoseconds delay) {
85 runEvt = sched.schedule(delay, [&] { proc.
runOnce(); });
103 bool isDaemon =
false;
104 std::string configFile;
106 po::options_description optionsDescription(
"Options");
107 optionsDescription.add_options()
108 (
"help,h",
"print this message and exit")
109 (
"version,V",
"show version information and exit")
110 (
"daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
111 "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
112 "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
113 "NOTE: if the connection to NFD fails, the daemon will exit.")
114 (
"ndn-fch-url", po::value<std::string>(&options.
ndnFchUrl)->default_value(options.
ndnFchUrl),
115 "URL for NDN-FCH (Find Closest Hub) service")
116 (
"config,c", po::value<std::string>(&configFile),
117 "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
120 po::variables_map vm;
122 po::store(po::parse_command_line(argc, argv, optionsDescription), vm);
125 catch (
const std::exception& e) {
126 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
127 usage(std::cerr, optionsDescription, argv[0]);
131 if (vm.count(
"help")) {
132 usage(std::cout, optionsDescription, argv[0]);
136 if (vm.count(
"version")) {
141 if (vm.count(
"config")) {
142 po::options_description configFileOptions;
143 configFileOptions.add_options()
144 (
"enabled", po::value<bool>()->default_value(
false))
147 po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm);
150 catch (
const std::exception& e) {
151 std::cerr <<
"ERROR in config: " << e.what() <<
"\n\n";
154 if (!vm[
"enabled"].as<bool>()) {
171 proc.
onComplete.connect([&exitCode] (
bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
173 face.processEvents();
176 catch (
const std::exception& e) {
177 std::cerr <<
"ERROR: " << boost::diagnostic_information(e);
signal::Signal< Procedure, bool > onComplete
Signal emitted when the procedure completes.
void initialize(const Options &options)
boost::asio::io_context & getIoContext() const noexcept
void runOnce()
Run the HUB discovery procedure once.
int main(int argc, char **argv)
constexpr time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL
static void usage(std::ostream &os, const po::options_description &opts, const char *programName)
static void runDaemon(Procedure &proc)
static int main(int argc, char **argv)
constexpr time::nanoseconds DAEMON_INITIAL_DELAY
constexpr time::nanoseconds NETMON_DAMPEN_PERIOD
std::string ndnFchUrl
HTTP base URL of NDN-FCH service.
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information if NFD is build from a specific git commit.