main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, Regents of the University of California,
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 
26 #include "procedure.hpp"
28 #include "core/scheduler.hpp"
29 #include "core/version.hpp"
30 
31 #include <signal.h>
32 #include <string.h>
33 #include <boost/program_options/options_description.hpp>
34 #include <boost/program_options/parsers.hpp>
35 #include <boost/program_options/variables_map.hpp>
36 #include <ndn-cxx/net/network-monitor.hpp>
37 #include <ndn-cxx/util/scheduler.hpp>
38 #include <ndn-cxx/util/time.hpp>
39 
40 // suppress warning caused by boost::program_options::parse_config_file
41 #ifdef __clang__
42 #pragma clang diagnostic ignored "-Wundefined-func-template"
43 #endif
44 
45 // 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.
47 namespace ndn {
48 namespace tools {
49 namespace autoconfig {
50 
51 static const time::nanoseconds DAEMON_INITIAL_DELAY = time::milliseconds(100);
52 static const time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL = time::hours(1);
53 static const time::nanoseconds NETMON_DAMPEN_PERIOD = time::seconds(5);
54 
55 namespace po = boost::program_options;
56 
57 static void
58 usage(std::ostream& os,
59  const po::options_description& opts,
60  const char* programName)
61 {
62  os << "Usage: " << programName << " [options]\n"
63  << "\n"
64  << opts;
65 }
66 
67 static void
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;
76  }
77  const char* signalName = ::strsignal(signalNo);
78  std::cerr << "Exiting on signal ";
79  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());
90  util::scheduler::ScopedEventId runEvt;
91  auto scheduleRerun = [&] (time::nanoseconds delay) {
92  runEvt = sched.scheduleEvent(delay, [&] { proc.runOnce(); });
93  };
94 
95  proc.onComplete.connect([&] (bool isSuccess) {
96  scheduleRerun(DAEMON_UNCONDITIONAL_INTERVAL);
97  });
98 
99  net::NetworkMonitor netmon(proc.getIoService());
100  netmon.onNetworkStateChanged.connect([&] { scheduleRerun(NETMON_DAMPEN_PERIOD); });
101 
102  scheduleRerun(DAEMON_INITIAL_DELAY);
103  proc.getIoService().run();
104 }
105 
106 static int
107 main(int argc, char** argv)
108 {
109  Options options;
110  bool isDaemon = false;
111  std::string configFile;
112 
113  po::options_description optionsDescription("Options");
114  optionsDescription.add_options()
115  ("help,h", "print this message and exit")
116  ("version,V", "show version information and exit")
117  ("daemon,d", po::bool_switch(&isDaemon)->default_value(isDaemon),
118  "Run in daemon mode, detecting network change events and re-running the auto-discovery procedure. "
119  "In addition, the auto-discovery procedure is unconditionally re-run every hour.\n"
120  "NOTE: if the connection to NFD fails, the daemon will exit.")
121  ("ndn-fch-url", po::value<std::string>(&options.ndnFchUrl)->default_value(options.ndnFchUrl),
122  "URL for NDN-FCH (Find Closest Hub) service")
123  ("config,c", po::value<std::string>(&configFile),
124  "Configuration file. Exit immediately unless 'enabled = true' is specified in the config file.")
125  ;
126 
127  po::variables_map vm;
128  try {
129  po::store(po::parse_command_line(argc, argv, optionsDescription), vm);
130  po::notify(vm);
131  }
132  catch (const std::exception& e) {
133  std::cerr << "ERROR: " << e.what() << "\n\n";
134  usage(std::cerr, optionsDescription, argv[0]);
135  return 2;
136  }
137 
138  if (vm.count("help")) {
139  usage(std::cout, optionsDescription, argv[0]);
140  return 0;
141  }
142 
143  if (vm.count("version")) {
144  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
145  return 0;
146  }
147 
148  if (vm.count("config")) {
149  po::options_description configFileOptions;
150  configFileOptions.add_options()
151  ("enabled", po::value<bool>()->default_value(false))
152  ;
153  try {
154  po::store(po::parse_config_file<char>(configFile.data(), configFileOptions), vm);
155  po::notify(vm);
156  }
157  catch (const std::exception& e) {
158  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;
164  }
165  }
166 
167  int exitCode = 0;
168  try {
169  Face face;
170  KeyChain keyChain;
171  Procedure proc(face, keyChain);
172  proc.initialize(options);
173 
174  if (isDaemon) {
175  runDaemon(proc);
176  }
177  else {
178  proc.onComplete.connect([&exitCode] (bool isSuccess) { exitCode = isSuccess ? 0 : 1; });
179  proc.runOnce();
180  face.processEvents();
181  }
182  }
183  catch (const std::exception& e) {
184  std::cerr << ::nfd::getExtendedErrorMessage(e) << std::endl;
185  return 1;
186  }
187 
188  return exitCode;
189 }
190 
191 } // namespace autoconfig
192 } // namespace tools
193 } // namespace ndn
194 
195 int
196 main(int argc, char** argv)
197 {
198  return ndn::tools::autoconfig::main(argc, argv);
199 }
static void usage(std::ostream &os, const po::options_description &opts, const char *programName)
Definition: main.cpp:58
static const time::nanoseconds DAEMON_UNCONDITIONAL_INTERVAL
Definition: main.cpp:52
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
static int main(int argc, char **argv)
Definition: main.cpp:107
std::string getExtendedErrorMessage(const E &exception)
std::string ndnFchUrl
HTTP base URL of NDN-FCH service.
Definition: procedure.hpp:40
void initialize(const Options &options)
Definition: procedure.cpp:52
util::Signal< Procedure, bool > onComplete
signal when procedure completes
Definition: procedure.hpp:80
static const time::nanoseconds DAEMON_INITIAL_DELAY
Definition: main.cpp:51
static void runDaemon(Procedure &proc)
Definition: main.cpp:68
static const time::nanoseconds NETMON_DAMPEN_PERIOD
Definition: main.cpp:53
void runOnce()
run HUB discovery procedure once
Definition: procedure.cpp:79
boost::asio::io_service & getIoService()
Definition: procedure.hpp:60