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