blob: 859ed34fe37646c673c9bee77fe9223f5e764b6c [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/*
Junxiao Shifeddc3c2019-01-17 19:06:00 +00003 * Copyright (c) 2014-2019, 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 Pesaventoc0a5a392017-08-19 16:49:30 -040041// suppress warning caused by boost::program_options::parse_config_file
42#ifdef __clang__
43#pragma clang diagnostic ignored "-Wundefined-func-template"
44#endif
45
Davide Pesavento59769b12017-11-12 23:52:06 -050046// ndn-autoconfig is an NDN tool not an NFD tool, so it uses ndn::tools::autoconfig namespace.
47// It lives in NFD repository because nfd-start can automatically start ndn-autoconfig in daemon mode.
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080048namespace ndn {
49namespace tools {
Junxiao Shi52fa45c2016-11-29 21:18:13 +000050namespace autoconfig {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080051
Junxiao Shicb766862017-07-07 22:21:04 +000052namespace po = boost::program_options;
53
Davide Pesavento14e71f02019-03-28 17:35:25 -040054const time::nanoseconds DAEMON_INITIAL_DELAY = 100_ms;
55const time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = 1_h;
56const time::nanoseconds NETMON_DAMPEN_PERIOD = 5_s;
57
Junxiao Shicb766862017-07-07 22:21:04 +000058static void
59usage(std::ostream& os,
Davide Pesavento59769b12017-11-12 23:52:06 -050060 const po::options_description& opts,
Junxiao Shicb766862017-07-07 22:21:04 +000061 const char* programName)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080062{
Davide Pesavento59769b12017-11-12 23:52:06 -050063 os << "Usage: " << programName << " [options]\n"
64 << "\n"
65 << opts;
Junxiao Shicb766862017-07-07 22:21:04 +000066}
67
68static void
69runDaemon(Procedure& proc)
70{
71 boost::asio::signal_set terminateSignals(proc.getIoService());
72 terminateSignals.add(SIGINT);
73 terminateSignals.add(SIGTERM);
74 terminateSignals.async_wait([&] (const boost::system::error_code& error, int signalNo) {
75 if (error) {
76 return;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080077 }
Junxiao Shicb766862017-07-07 22:21:04 +000078 const char* signalName = ::strsignal(signalNo);
Davide Pesavento59769b12017-11-12 23:52:06 -050079 std::cerr << "Exiting on signal ";
Junxiao Shicb766862017-07-07 22:21:04 +000080 if (signalName == nullptr) {
81 std::cerr << signalNo;
82 }
83 else {
84 std::cerr << signalName;
85 }
86 std::cerr << std::endl;
87 proc.getIoService().stop();
88 });
89
Davide Pesavento3dade002019-03-19 11:29:56 -060090 Scheduler sched(proc.getIoService());
91 scheduler::ScopedEventId runEvt;
Junxiao Shicb766862017-07-07 22:21:04 +000092 auto scheduleRerun = [&] (time::nanoseconds delay) {
Davide Pesavento3dade002019-03-19 11:29:56 -060093 runEvt = sched.schedule(delay, [&] { proc.runOnce(); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080094 };
95
Junxiao Shicb766862017-07-07 22:21:04 +000096 proc.onComplete.connect([&] (bool isSuccess) {
97 scheduleRerun(DAEMON_UNCONDITIONAL_INTERVAL);
98 });
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080099
Junxiao Shicb766862017-07-07 22:21:04 +0000100 net::NetworkMonitor netmon(proc.getIoService());
101 netmon.onNetworkStateChanged.connect([&] { scheduleRerun(NETMON_DAMPEN_PERIOD); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800102
Junxiao Shicb766862017-07-07 22:21:04 +0000103 scheduleRerun(DAEMON_INITIAL_DELAY);
104 proc.getIoService().run();
105}
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800106
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000107static int
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800108main(int argc, char** argv)
109{
Junxiao Shicb766862017-07-07 22:21:04 +0000110 Options options;
111 bool isDaemon = false;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800112 std::string configFile;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800113
Junxiao Shicb766862017-07-07 22:21:04 +0000114 po::options_description optionsDescription("Options");
115 optionsDescription.add_options()
116 ("help,h", "print this message and exit")
Davide Pesavento59769b12017-11-12 23:52:06 -0500117 ("version,V", "show version information and exit")
Junxiao Shicb766862017-07-07 22:21:04 +0000118 ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
Davide Pesavento59769b12017-11-12 23:52:06 -0500119 "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
Junxiao Shicb766862017-07-07 22:21:04 +0000120 "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500121 "NOTE: if the connection to NFD fails, the daemon will exit.")
Junxiao Shicb766862017-07-07 22:21:04 +0000122 ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl),
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800123 "URL for NDN-FCH (Find Closest Hub) service")
Junxiao Shicb766862017-07-07 22:21:04 +0000124 ("config,c", po::value<std::string>(&configFile),
Davide Pesavento59769b12017-11-12 23:52:06 -0500125 "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800126 ;
127
Junxiao Shicb766862017-07-07 22:21:04 +0000128 po::variables_map vm;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800129 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000130 po::store(po::parse_command_line(argc, argv, optionsDescription), vm);
131 po::notify(vm);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800132 }
133 catch (const std::exception& e) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500134 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shicb766862017-07-07 22:21:04 +0000135 usage(std::cerr, optionsDescription, argv[0]);
136 return 2;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800137 }
138
Junxiao Shicb766862017-07-07 22:21:04 +0000139 if (vm.count("help")) {
140 usage(std::cout, optionsDescription, argv[0]);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800141 return 0;
142 }
143
Junxiao Shicb766862017-07-07 22:21:04 +0000144 if (vm.count("version")) {
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800145 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
146 return 0;
147 }
148
Junxiao Shicb766862017-07-07 22:21:04 +0000149 if (vm.count("config")) {
150 po::options_description configFileOptions;
151 configFileOptions.add_options()
152 ("enabled", po::value<bool>()->default_value(false))
153 ;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800154 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000155 po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm);
156 po::notify(vm);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800157 }
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800158 catch (const std::exception& e) {
Junxiao Shicb766862017-07-07 22:21:04 +0000159 std::cerr << "ERROR in config: " << e.what() << "\n\n";
160 return 2;
161 }
162 if (!vm["enabled"].as<bool>()) {
163 // not enabled in config
164 return 0;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800165 }
166 }
167
Junxiao Shicb766862017-07-07 22:21:04 +0000168 int exitCode = 0;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800169 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000170 Face face;
171 KeyChain keyChain;
172 Procedure proc(face, keyChain);
173 proc.initialize(options);
174
175 if (isDaemon) {
176 runDaemon(proc);
177 }
178 else {
Davide Pesavento59769b12017-11-12 23:52:06 -0500179 proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
Junxiao Shicb766862017-07-07 22:21:04 +0000180 proc.runOnce();
181 face.processEvents();
182 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800183 }
Junxiao Shicb766862017-07-07 22:21:04 +0000184 catch (const std::exception& e) {
Davide Pesavento97e33022019-02-14 16:00:50 -0500185 std::cerr << "ERROR: " << boost::diagnostic_information(e);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800186 return 1;
187 }
Davide Pesavento59769b12017-11-12 23:52:06 -0500188
Junxiao Shicb766862017-07-07 22:21:04 +0000189 return exitCode;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800190}
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000191
192} // namespace autoconfig
193} // namespace tools
194} // namespace ndn
195
196int
197main(int argc, char** argv)
198{
199 return ndn::tools::autoconfig::main(argc, argv);
200}