26 #include "core/version.hpp"
32 #include <ndn-cxx/util/network-monitor.hpp>
33 #include <ndn-cxx/util/scheduler.hpp>
34 #include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
36 #include <boost/noncopyable.hpp>
37 #include <boost/program_options/options_description.hpp>
38 #include <boost/program_options/variables_map.hpp>
39 #include <boost/program_options/parsers.hpp>
41 namespace po = boost::program_options;
46 class NdnAutoconfig : boost::noncopyable
49 class Error :
public std::runtime_error
53 Error(
const std::string& what)
54 :
std::runtime_error(what)
60 NdnAutoconfig(
bool isDaemonMode)
63 , m_startStagesEvent(m_scheduler)
64 , m_isDaemonMode(isDaemonMode)
65 , m_terminationSignalSet(m_io)
66 , m_stage1(m_face, m_keyChain,
67 [&] (const
std::string& errorMessage) {
68 std::cerr <<
"Stage 1 failed: " << errorMessage << std::endl;
71 , m_stage2(m_face, m_keyChain,
72 [&] (
const std::string& errorMessage) {
73 std::cerr <<
"Stage 2 failed: " << errorMessage << std::endl;
76 , m_stage3(m_face, m_keyChain,
77 [&] (
const std::string& errorMessage) {
78 std::cerr <<
"Stage 3 failed: " << errorMessage << std::endl;
80 BOOST_THROW_EXCEPTION(Error(
"No more stages, automatic discovery failed"));
82 std::cerr <<
"No more stages, automatic discovery failed" << std::endl;
86 m_networkMonitor.reset(
new util::NetworkMonitor(m_io));
87 m_networkMonitor->onNetworkStateChanged.connect([
this] {
90 m_startStagesEvent = m_scheduler.scheduleEvent(time::seconds(5),
91 bind(&NdnAutoconfig::startStages,
this));
96 m_startStagesEvent = m_scheduler.scheduleEvent(time::milliseconds(100),
97 bind(&NdnAutoconfig::startStages,
this));
103 if (m_isDaemonMode) {
104 m_terminationSignalSet.add(SIGINT);
105 m_terminationSignalSet.add(SIGTERM);
106 m_terminationSignalSet.async_wait(bind(&NdnAutoconfig::terminate,
this, _1, _2));
113 terminate(
const boost::system::error_code& error,
int signalNo)
123 usage(std::ostream& os,
124 const po::options_description& optionDescription,
125 const char* programName)
128 <<
" " << programName <<
" [options]\n"
130 os << optionDescription;
138 if (m_isDaemonMode) {
139 m_startStagesEvent = m_scheduler.scheduleEvent(time::hours(1),
140 bind(&NdnAutoconfig::startStages,
this));
145 boost::asio::io_service m_io;
148 unique_ptr<util::NetworkMonitor> m_networkMonitor;
149 util::Scheduler m_scheduler;
150 util::scheduler::ScopedEventId m_startStagesEvent;
152 boost::asio::signal_set m_terminationSignalSet;
154 autoconfig::MulticastDiscovery m_stage1;
155 autoconfig::GuessFromSearchDomains m_stage2;
156 autoconfig::GuessFromIdentityName m_stage3;
165 bool isDaemonMode =
false;
166 std::string configFile;
168 po::options_description optionDescription(
"Options");
169 optionDescription.add_options()
170 (
"help,h",
"produce help message")
171 (
"daemon,d", po::bool_switch(&isDaemonMode)->default_value(isDaemonMode),
172 "run in daemon mode, detecting network change events and re-running "
173 "auto-discovery procedure. In addition, the auto-discovery procedure "
174 "is unconditionally re-run every hour.\n"
175 "NOTE: if connection to NFD fails, the daemon will be terminated.")
176 (
"config,c", po::value<std::string>(&configFile),
"configuration file. If `enabled = true` "
177 "is not specified, no actions will be performed.")
178 (
"version,V",
"show version and exit")
181 po::variables_map options;
183 po::store(po::parse_command_line(argc, argv, optionDescription), options);
186 catch (
const std::exception& e) {
187 std::cerr <<
"ERROR: " << e.what() <<
"\n" << std::endl;
192 if (options.count(
"help")) {
197 if (options.count(
"version")) {
198 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
203 bool isEnabled =
true;
205 po::options_description configFileOptions;
206 configFileOptions.add_options()
207 (
"enabled", po::value<bool>(&isEnabled))
210 if (!configFile.empty()) {
213 po::store(po::parse_config_file<char>(configFile.c_str(), configFileOptions), options);
216 catch (
const std::exception& e) {
217 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
227 ndn::tools::NdnAutoconfig autoConfigInstance(isDaemonMode);
228 autoConfigInstance.run();
230 catch (
const std::exception& error) {
231 std::cerr <<
"ERROR: " << error.what() << std::endl;
Copyright (c) 2014-2016, Regents of the University of California, Arizona Board of Regents...
static void usage(const char *programName)
int main(int argc, char **argv)