blob: 2f8e56158c93e7eb2b28452c19c2d16d285a54a4 [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shicb766862017-07-07 22:21:04 +00002/*
Davide Pesaventoc52cd5e2022-03-05 20:40:54 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Junxiao Shicb766862017-07-07 22:21:04 +000026#include "procedure.hpp"
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000027#include "core/version.hpp"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080028
Junxiao Shicb766862017-07-07 22:21:04 +000029#include <signal.h>
30#include <string.h>
Davide Pesavento97e33022019-02-14 16:00:50 -050031
32#include <boost/exception/diagnostic_information.hpp>
Alexander Afanasyev5c475972015-12-20 16:16:56 -080033#include <boost/program_options/options_description.hpp>
Alexander Afanasyev5c475972015-12-20 16:16:56 -080034#include <boost/program_options/parsers.hpp>
Junxiao Shi52fa45c2016-11-29 21:18:13 +000035#include <boost/program_options/variables_map.hpp>
Davide Pesavento97e33022019-02-14 16:00:50 -050036
Junxiao Shicb766862017-07-07 22:21:04 +000037#include <ndn-cxx/net/network-monitor.hpp>
38#include <ndn-cxx/util/scheduler.hpp>
Junxiao Shicb766862017-07-07 22:21:04 +000039#include <ndn-cxx/util/time.hpp>
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080040
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041namespace ndn::autoconfig {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080042
Junxiao Shicb766862017-07-07 22:21:04 +000043namespace po = boost::program_options;
44
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040045constexpr time::nanoseconds DAEMON_INITIAL_DELAY = 100_ms;
46constexpr time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = 1_h;
47constexpr time::nanoseconds NETMON_DAMPEN_PERIOD = 5_s;
Davide Pesavento14e71f02019-03-28 17:35:25 -040048
Junxiao Shicb766862017-07-07 22:21:04 +000049static void
50usage(std::ostream& os,
Davide Pesavento59769b12017-11-12 23:52:06 -050051 const po::options_description& opts,
Junxiao Shicb766862017-07-07 22:21:04 +000052 const char* programName)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080053{
Davide Pesavento59769b12017-11-12 23:52:06 -050054 os << "Usage: " << programName << " [options]\n"
55 << "\n"
56 << opts;
Junxiao Shicb766862017-07-07 22:21:04 +000057}
58
59static void
60runDaemon(Procedure& proc)
61{
62 boost::asio::signal_set terminateSignals(proc.getIoService());
63 terminateSignals.add(SIGINT);
64 terminateSignals.add(SIGTERM);
65 terminateSignals.async_wait([&] (const boost::system::error_code& error, int signalNo) {
66 if (error) {
67 return;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080068 }
Junxiao Shicb766862017-07-07 22:21:04 +000069 const char* signalName = ::strsignal(signalNo);
Davide Pesavento59769b12017-11-12 23:52:06 -050070 std::cerr << "Exiting on signal ";
Junxiao Shicb766862017-07-07 22:21:04 +000071 if (signalName == nullptr) {
72 std::cerr << signalNo;
73 }
74 else {
75 std::cerr << signalName;
76 }
77 std::cerr << std::endl;
78 proc.getIoService().stop();
79 });
80
Davide Pesavento3dade002019-03-19 11:29:56 -060081 Scheduler sched(proc.getIoService());
82 scheduler::ScopedEventId runEvt;
Junxiao Shicb766862017-07-07 22:21:04 +000083 auto scheduleRerun = [&] (time::nanoseconds delay) {
Davide Pesavento3dade002019-03-19 11:29:56 -060084 runEvt = sched.schedule(delay, [&] { proc.runOnce(); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080085 };
86
Junxiao Shicb766862017-07-07 22:21:04 +000087 proc.onComplete.connect([&] (bool isSuccess) {
88 scheduleRerun(DAEMON_UNCONDITIONAL_INTERVAL);
89 });
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080090
Junxiao Shicb766862017-07-07 22:21:04 +000091 net::NetworkMonitor netmon(proc.getIoService());
92 netmon.onNetworkStateChanged.connect([&] { scheduleRerun(NETMON_DAMPEN_PERIOD); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080093
Junxiao Shicb766862017-07-07 22:21:04 +000094 scheduleRerun(DAEMON_INITIAL_DELAY);
95 proc.getIoService().run();
96}
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080097
Junxiao Shi52fa45c2016-11-29 21:18:13 +000098static int
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080099main(int argc, char** argv)
100{
Junxiao Shicb766862017-07-07 22:21:04 +0000101 Options options;
102 bool isDaemon = false;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800103 std::string configFile;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800104
Junxiao Shicb766862017-07-07 22:21:04 +0000105 po::options_description optionsDescription("Options");
106 optionsDescription.add_options()
107 ("help,h", "print this message and exit")
Davide Pesavento59769b12017-11-12 23:52:06 -0500108 ("version,V", "show version information and exit")
Junxiao Shicb766862017-07-07 22:21:04 +0000109 ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
Davide Pesavento59769b12017-11-12 23:52:06 -0500110 "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
Junxiao Shicb766862017-07-07 22:21:04 +0000111 "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500112 "NOTE: if the connection to NFD fails, the daemon will exit.")
Junxiao Shicb766862017-07-07 22:21:04 +0000113 ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl),
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800114 "URL for NDN-FCH (Find Closest Hub) service")
Junxiao Shicb766862017-07-07 22:21:04 +0000115 ("config,c", po::value<std::string>(&configFile),
Davide Pesavento59769b12017-11-12 23:52:06 -0500116 "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800117 ;
118
Junxiao Shicb766862017-07-07 22:21:04 +0000119 po::variables_map vm;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800120 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000121 po::store(po::parse_command_line(argc, argv, optionsDescription), vm);
122 po::notify(vm);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800123 }
124 catch (const std::exception& e) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500125 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shicb766862017-07-07 22:21:04 +0000126 usage(std::cerr, optionsDescription, argv[0]);
127 return 2;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800128 }
129
Junxiao Shicb766862017-07-07 22:21:04 +0000130 if (vm.count("help")) {
131 usage(std::cout, optionsDescription, argv[0]);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800132 return 0;
133 }
134
Junxiao Shicb766862017-07-07 22:21:04 +0000135 if (vm.count("version")) {
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800136 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
137 return 0;
138 }
139
Junxiao Shicb766862017-07-07 22:21:04 +0000140 if (vm.count("config")) {
141 po::options_description configFileOptions;
142 configFileOptions.add_options()
143 ("enabled", po::value<bool>()->default_value(false))
144 ;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800145 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000146 po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm);
147 po::notify(vm);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800148 }
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800149 catch (const std::exception& e) {
Junxiao Shicb766862017-07-07 22:21:04 +0000150 std::cerr << "ERROR in config: " << e.what() << "\n\n";
151 return 2;
152 }
153 if (!vm["enabled"].as<bool>()) {
154 // not enabled in config
155 return 0;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800156 }
157 }
158
Junxiao Shicb766862017-07-07 22:21:04 +0000159 int exitCode = 0;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800160 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000161 Face face;
162 KeyChain keyChain;
163 Procedure proc(face, keyChain);
164 proc.initialize(options);
165
166 if (isDaemon) {
167 runDaemon(proc);
168 }
169 else {
Davide Pesavento59769b12017-11-12 23:52:06 -0500170 proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
Junxiao Shicb766862017-07-07 22:21:04 +0000171 proc.runOnce();
172 face.processEvents();
173 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800174 }
Junxiao Shicb766862017-07-07 22:21:04 +0000175 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500176 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800177 return 1;
178 }
Davide Pesavento59769b12017-11-12 23:52:06 -0500179
Junxiao Shicb766862017-07-07 22:21:04 +0000180 return exitCode;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800181}
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000182
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400183} // namespace ndn::autoconfig
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000184
185int
186main(int argc, char** argv)
187{
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400188 return ndn::autoconfig::main(argc, argv);
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000189}