26 #include "core/version.hpp"
33 #include <ndn-cxx/util/network-monitor.hpp>
34 #include <ndn-cxx/util/scheduler.hpp>
35 #include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
37 #include <boost/noncopyable.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/parsers.hpp>
40 #include <boost/program_options/variables_map.hpp>
42 namespace po = boost::program_options;
46 namespace autoconfig {
50 class NdnAutoconfig : boost::noncopyable
53 class Error :
public std::runtime_error
57 Error(
const std::string& what)
58 :
std::runtime_error(what)
64 NdnAutoconfig(
const std::string& ndnFchUrl,
bool isDaemonMode)
67 , m_startStagesEvent(m_scheduler)
68 , m_isDaemonMode(isDaemonMode)
69 , m_terminationSignalSet(m_io)
70 , m_stage1(m_face, m_keyChain,
71 [&] (const
std::string& errorMessage) {
72 std::cerr <<
"Stage 1 failed: " << errorMessage << std::endl;
75 , m_stage2(m_face, m_keyChain,
76 [&] (
const std::string& errorMessage) {
77 std::cerr <<
"Stage 2 failed: " << errorMessage << std::endl;
80 , m_stage3(m_face, m_keyChain,
82 [&] (
const std::string& errorMessage) {
83 std::cerr <<
"Stage 3 failed: " << errorMessage << std::endl;
86 , m_stage4(m_face, m_keyChain,
87 [&] (
const std::string& errorMessage) {
88 std::cerr <<
"Stage 4 failed: " << errorMessage << std::endl;
90 BOOST_THROW_EXCEPTION(Error(
"No more stages, automatic discovery failed"));
92 std::cerr <<
"No more stages, automatic discovery failed" << std::endl;
96 m_networkMonitor.reset(
new ndn::util::NetworkMonitor(m_io));
97 m_networkMonitor->onNetworkStateChanged.connect([
this] {
100 m_startStagesEvent = m_scheduler.scheduleEvent(time::seconds(5),
101 bind(&NdnAutoconfig::startStages,
this));
106 m_startStagesEvent = m_scheduler.scheduleEvent(time::milliseconds(100),
107 bind(&NdnAutoconfig::startStages,
this));
113 if (m_isDaemonMode) {
114 m_terminationSignalSet.add(SIGINT);
115 m_terminationSignalSet.add(SIGTERM);
116 m_terminationSignalSet.async_wait(bind(&NdnAutoconfig::terminate,
this, _1, _2));
123 terminate(
const boost::system::error_code& error,
int signalNo)
132 usage(std::ostream& os,
133 const po::options_description& optionDescription,
134 const char* programName)
137 <<
" " << programName <<
" [options]\n"
139 os << optionDescription;
147 if (m_isDaemonMode) {
148 m_startStagesEvent = m_scheduler.scheduleEvent(time::hours(1),
149 bind(&NdnAutoconfig::startStages,
this));
154 boost::asio::io_service m_io;
157 unique_ptr<util::NetworkMonitor> m_networkMonitor;
158 util::Scheduler m_scheduler;
159 util::scheduler::ScopedEventId m_startStagesEvent;
161 boost::asio::signal_set m_terminationSignalSet;
163 MulticastDiscovery m_stage1;
164 GuessFromSearchDomains m_stage2;
165 NdnFchDiscovery m_stage3;
166 GuessFromIdentityName m_stage4;
172 bool isDaemonMode =
false;
173 std::string configFile;
174 std::string ndnFchUrl;
176 po::options_description optionDescription(
"Options");
177 optionDescription.add_options()
178 (
"help,h",
"produce help message")
179 (
"daemon,d", po::bool_switch(&isDaemonMode)->default_value(isDaemonMode),
180 "run in daemon mode, detecting network change events and re-running "
181 "auto-discovery procedure. In addition, the auto-discovery procedure "
182 "is unconditionally re-run every hour.\n"
183 "NOTE: if connection to NFD fails, the daemon will be terminated.")
184 (
"ndn-fch-url", po::value<std::string>(&ndnFchUrl)->default_value(
"http://ndn-fch.named-data.net"),
185 "URL for NDN-FCH (Find Closest Hub) service")
186 (
"config,c", po::value<std::string>(&configFile),
"configuration file. If `enabled = true` "
187 "is not specified, no actions will be performed.")
188 (
"version,V",
"show version and exit")
191 po::variables_map options;
193 po::store(po::parse_command_line(argc, argv, optionDescription), options);
196 catch (
const std::exception& e) {
197 std::cerr <<
"ERROR: " << e.what() <<
"\n" << std::endl;
202 if (options.count(
"help")) {
207 if (options.count(
"version")) {
208 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
213 bool isEnabled =
true;
215 po::options_description configFileOptions;
216 configFileOptions.add_options()
217 (
"enabled", po::value<bool>(&isEnabled))
220 if (!configFile.empty()) {
223 po::store(po::parse_config_file<char>(configFile.c_str(), configFileOptions), options);
226 catch (
const std::exception& e) {
227 std::cerr <<
"ERROR: " << e.what() << std::endl << std::endl;
237 NdnAutoconfig autoConfigInstance(ndnFchUrl, isDaemonMode);
238 autoConfigInstance.run();
240 catch (
const std::exception& error) {
241 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)