blob: a8b2c45d7a55cbdbea166d2db006c7732d78da7b [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"
27#include "core/extended-error-message.hpp"
28#include "core/scheduler.hpp"
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/version.hpp"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080030
Junxiao Shicb766862017-07-07 22:21:04 +000031#include <signal.h>
32#include <string.h>
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>
Junxiao Shicb766862017-07-07 22:21:04 +000036#include <ndn-cxx/net/network-monitor.hpp>
37#include <ndn-cxx/util/scheduler.hpp>
Junxiao Shicb766862017-07-07 22:21:04 +000038#include <ndn-cxx/util/time.hpp>
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080039
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040040// suppress warning caused by boost::program_options::parse_config_file
41#ifdef __clang__
42#pragma clang diagnostic ignored "-Wundefined-func-template"
43#endif
44
Davide Pesavento59769b12017-11-12 23:52:06 -050045// ndn-autoconfig is an NDN tool not an NFD tool, so it uses ndn::tools::autoconfig namespace.
46// It lives in NFD repository because nfd-start can automatically start ndn-autoconfig in daemon mode.
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080047namespace ndn {
48namespace tools {
Junxiao Shi52fa45c2016-11-29 21:18:13 +000049namespace autoconfig {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080050
Junxiao Shicb766862017-07-07 22:21:04 +000051static const time::nanoseconds DAEMON_INITIAL_DELAY = time::milliseconds(100);
52static const time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = time::hours(1);
53static const time::nanoseconds NETMON_DAMPEN_PERIOD = time::seconds(5);
54
55namespace po = boost::program_options;
56
57static void
58usage(std::ostream& os,
Davide Pesavento59769b12017-11-12 23:52:06 -050059 const po::options_description& opts,
Junxiao Shicb766862017-07-07 22:21:04 +000060 const char* programName)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080061{
Davide Pesavento59769b12017-11-12 23:52:06 -050062 os << "Usage: " << programName << " [options]\n"
63 << "\n"
64 << opts;
Junxiao Shicb766862017-07-07 22:21:04 +000065}
66
67static void
68runDaemon(Procedure& proc)
69{
70 boost::asio::signal_set terminateSignals(proc.getIoService());
71 terminateSignals.add(SIGINT);
72 terminateSignals.add(SIGTERM);
73 terminateSignals.async_wait([&] (const boost::system::error_code& error, int signalNo) {
74 if (error) {
75 return;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080076 }
Junxiao Shicb766862017-07-07 22:21:04 +000077 const char* signalName = ::strsignal(signalNo);
Davide Pesavento59769b12017-11-12 23:52:06 -050078 std::cerr << "Exiting on signal ";
Junxiao Shicb766862017-07-07 22:21:04 +000079 if (signalName == nullptr) {
80 std::cerr << signalNo;
81 }
82 else {
83 std::cerr << signalName;
84 }
85 std::cerr << std::endl;
86 proc.getIoService().stop();
87 });
88
89 util::Scheduler sched(proc.getIoService());
Junxiao Shifeddc3c2019-01-17 19:06:00 +000090 util::scheduler::ScopedEventId runEvt;
Junxiao Shicb766862017-07-07 22:21:04 +000091 auto scheduleRerun = [&] (time::nanoseconds delay) {
92 runEvt = sched.scheduleEvent(delay, [&] { proc.runOnce(); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080093 };
94
Junxiao Shicb766862017-07-07 22:21:04 +000095 proc.onComplete.connect([&] (bool isSuccess) {
96 scheduleRerun(DAEMON_UNCONDITIONAL_INTERVAL);
97 });
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080098
Junxiao Shicb766862017-07-07 22:21:04 +000099 net::NetworkMonitor netmon(proc.getIoService());
100 netmon.onNetworkStateChanged.connect([&] { scheduleRerun(NETMON_DAMPEN_PERIOD); });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800101
Junxiao Shicb766862017-07-07 22:21:04 +0000102 scheduleRerun(DAEMON_INITIAL_DELAY);
103 proc.getIoService().run();
104}
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800105
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000106static int
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800107main(int argc, char** argv)
108{
Junxiao Shicb766862017-07-07 22:21:04 +0000109 Options options;
110 bool isDaemon = false;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800111 std::string configFile;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800112
Junxiao Shicb766862017-07-07 22:21:04 +0000113 po::options_description optionsDescription("Options");
114 optionsDescription.add_options()
115 ("help,h", "print this message and exit")
Davide Pesavento59769b12017-11-12 23:52:06 -0500116 ("version,V", "show version information and exit")
Junxiao Shicb766862017-07-07 22:21:04 +0000117 ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
Davide Pesavento59769b12017-11-12 23:52:06 -0500118 "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
Junxiao Shicb766862017-07-07 22:21:04 +0000119 "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
Davide Pesavento59769b12017-11-12 23:52:06 -0500120 "NOTE: if the connection to NFD fails, the daemon will exit.")
Junxiao Shicb766862017-07-07 22:21:04 +0000121 ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl),
Alexander Afanasyev2a001942016-12-14 18:18:41 -0800122 "URL for NDN-FCH (Find Closest Hub) service")
Junxiao Shicb766862017-07-07 22:21:04 +0000123 ("config,c", po::value<std::string>(&configFile),
Davide Pesavento59769b12017-11-12 23:52:06 -0500124 "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800125 ;
126
Junxiao Shicb766862017-07-07 22:21:04 +0000127 po::variables_map vm;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800128 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000129 po::store(po::parse_command_line(argc, argv, optionsDescription), vm);
130 po::notify(vm);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800131 }
132 catch (const std::exception& e) {
Davide Pesavento59769b12017-11-12 23:52:06 -0500133 std::cerr << "ERROR: " << e.what() << "\n\n";
Junxiao Shicb766862017-07-07 22:21:04 +0000134 usage(std::cerr, optionsDescription, argv[0]);
135 return 2;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800136 }
137
Junxiao Shicb766862017-07-07 22:21:04 +0000138 if (vm.count("help")) {
139 usage(std::cout, optionsDescription, argv[0]);
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800140 return 0;
141 }
142
Junxiao Shicb766862017-07-07 22:21:04 +0000143 if (vm.count("version")) {
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800144 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
145 return 0;
146 }
147
Junxiao Shicb766862017-07-07 22:21:04 +0000148 if (vm.count("config")) {
149 po::options_description configFileOptions;
150 configFileOptions.add_options()
151 ("enabled", po::value<bool>()->default_value(false))
152 ;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800153 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000154 po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm);
155 po::notify(vm);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800156 }
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800157 catch (const std::exception& e) {
Junxiao Shicb766862017-07-07 22:21:04 +0000158 std::cerr << "ERROR in config: " << e.what() << "\n\n";
159 return 2;
160 }
161 if (!vm["enabled"].as<bool>()) {
162 // not enabled in config
163 return 0;
Alexander Afanasyev5c475972015-12-20 16:16:56 -0800164 }
165 }
166
Junxiao Shicb766862017-07-07 22:21:04 +0000167 int exitCode = 0;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800168 try {
Junxiao Shicb766862017-07-07 22:21:04 +0000169 Face face;
170 KeyChain keyChain;
171 Procedure proc(face, keyChain);
172 proc.initialize(options);
173
174 if (isDaemon) {
175 runDaemon(proc);
176 }
177 else {
Davide Pesavento59769b12017-11-12 23:52:06 -0500178 proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
Junxiao Shicb766862017-07-07 22:21:04 +0000179 proc.runOnce();
180 face.processEvents();
181 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800182 }
Junxiao Shicb766862017-07-07 22:21:04 +0000183 catch (const std::exception& e) {
184 std::cerr << ::nfd::getExtendedErrorMessage(e) << std::endl;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800185 return 1;
186 }
Davide Pesavento59769b12017-11-12 23:52:06 -0500187
Junxiao Shicb766862017-07-07 22:21:04 +0000188 return exitCode;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800189}
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000190
191} // namespace autoconfig
192} // namespace tools
193} // namespace ndn
194
195int
196main(int argc, char** argv)
197{
198 return ndn::tools::autoconfig::main(argc, argv);
199}