Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | c52cd5e | 2022-03-05 20:40:54 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 4 | * 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 Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 26 | #include "procedure.hpp" |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 27 | #include "core/version.hpp" |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 28 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <string.h> |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 31 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/asio/signal_set.hpp> |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 33 | #include <boost/exception/diagnostic_information.hpp> |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 34 | #include <boost/program_options/options_description.hpp> |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 35 | #include <boost/program_options/parsers.hpp> |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 36 | #include <boost/program_options/variables_map.hpp> |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 37 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 38 | #include <ndn-cxx/net/network-monitor.hpp> |
| 39 | #include <ndn-cxx/util/scheduler.hpp> |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 40 | #include <ndn-cxx/util/time.hpp> |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 41 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 42 | namespace ndn::autoconfig { |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 43 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 44 | namespace po = boost::program_options; |
| 45 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 46 | constexpr time::nanoseconds DAEMON_INITIAL_DELAY = 100_ms; |
| 47 | constexpr time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = 1_h; |
| 48 | constexpr time::nanoseconds NETMON_DAMPEN_PERIOD = 5_s; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 49 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 50 | static void |
| 51 | usage(std::ostream& os, |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 52 | const po::options_description& opts, |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 53 | const char* programName) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 54 | { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 55 | os << "Usage: " << programName << " [options]\n" |
| 56 | << "\n" |
| 57 | << opts; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void |
| 61 | runDaemon(Procedure& proc) |
| 62 | { |
| 63 | boost::asio::signal_set terminateSignals(proc.getIoService()); |
| 64 | terminateSignals.add(SIGINT); |
| 65 | terminateSignals.add(SIGTERM); |
| 66 | terminateSignals.async_wait([&] (const boost::system::error_code& error, int signalNo) { |
| 67 | if (error) { |
| 68 | return; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 69 | } |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 70 | const char* signalName = ::strsignal(signalNo); |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 71 | std::cerr << "Exiting on signal "; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 72 | if (signalName == nullptr) { |
| 73 | std::cerr << signalNo; |
| 74 | } |
| 75 | else { |
| 76 | std::cerr << signalName; |
| 77 | } |
| 78 | std::cerr << std::endl; |
| 79 | proc.getIoService().stop(); |
| 80 | }); |
| 81 | |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 82 | Scheduler sched(proc.getIoService()); |
| 83 | scheduler::ScopedEventId runEvt; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 84 | auto scheduleRerun = [&] (time::nanoseconds delay) { |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 85 | runEvt = sched.schedule(delay, [&] { proc.runOnce(); }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 88 | proc.onComplete.connect([&] (bool isSuccess) { |
| 89 | scheduleRerun(DAEMON_UNCONDITIONAL_INTERVAL); |
| 90 | }); |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 91 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 92 | net::NetworkMonitor netmon(proc.getIoService()); |
| 93 | netmon.onNetworkStateChanged.connect([&] { scheduleRerun(NETMON_DAMPEN_PERIOD); }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 94 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 95 | scheduleRerun(DAEMON_INITIAL_DELAY); |
| 96 | proc.getIoService().run(); |
| 97 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 98 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 99 | static int |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 100 | main(int argc, char** argv) |
| 101 | { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 102 | Options options; |
| 103 | bool isDaemon = false; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 104 | std::string configFile; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 105 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 106 | po::options_description optionsDescription("Options"); |
| 107 | optionsDescription.add_options() |
| 108 | ("help,h", "print this message and exit") |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 109 | ("version,V", "show version information and exit") |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 110 | ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon), |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 111 | "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. " |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 112 | "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n" |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 113 | "NOTE: if the connection to NFD fails, the daemon will exit.") |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 114 | ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl), |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 115 | "URL for NDN-FCH (Find Closest Hub) service") |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 116 | ("config,c", po::value<std::string>(&configFile), |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 117 | "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.") |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 118 | ; |
| 119 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 120 | po::variables_map vm; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 121 | try { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 122 | po::store(po::parse_command_line(argc, argv, optionsDescription), vm); |
| 123 | po::notify(vm); |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 124 | } |
| 125 | catch (const std::exception& e) { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 126 | std::cerr << "ERROR: " << e.what() << "\n\n"; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 127 | usage(std::cerr, optionsDescription, argv[0]); |
| 128 | return 2; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 131 | if (vm.count("help")) { |
| 132 | usage(std::cout, optionsDescription, argv[0]); |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 136 | if (vm.count("version")) { |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 137 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 138 | return 0; |
| 139 | } |
| 140 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 141 | if (vm.count("config")) { |
| 142 | po::options_description configFileOptions; |
| 143 | configFileOptions.add_options() |
| 144 | ("enabled", po::value<bool>()->default_value(false)) |
| 145 | ; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 146 | try { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 147 | po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm); |
| 148 | po::notify(vm); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 149 | } |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 150 | catch (const std::exception& e) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 151 | std::cerr << "ERROR in config: " << e.what() << "\n\n"; |
| 152 | return 2; |
| 153 | } |
| 154 | if (!vm["enabled"].as<bool>()) { |
| 155 | // not enabled in config |
| 156 | return 0; |
Alexander Afanasyev | 5c47597 | 2015-12-20 16:16:56 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 160 | int exitCode = 0; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 161 | try { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 162 | Face face; |
| 163 | KeyChain keyChain; |
| 164 | Procedure proc(face, keyChain); |
| 165 | proc.initialize(options); |
| 166 | |
| 167 | if (isDaemon) { |
| 168 | runDaemon(proc); |
| 169 | } |
| 170 | else { |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 171 | proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 172 | proc.runOnce(); |
| 173 | face.processEvents(); |
| 174 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 175 | } |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 176 | catch (const std::exception& e) { |
Davide Pesavento | 97e3302 | 2019-02-14 16:00:50 -0500 | [diff] [blame] | 177 | std::cerr << "ERROR: " << boost::diagnostic_information(e); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 178 | return 1; |
| 179 | } |
Davide Pesavento | 59769b1 | 2017-11-12 23:52:06 -0500 | [diff] [blame] | 180 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 181 | return exitCode; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 182 | } |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 183 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 184 | } // namespace ndn::autoconfig |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 185 | |
| 186 | int |
| 187 | main(int argc, char** argv) |
| 188 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 189 | return ndn::autoconfig::main(argc, argv); |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 190 | } |