blob: 2216535213fcb0d1a50680c8d0ac87155ecd418a [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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 "version.hpp"
27
28#include "multicast-discovery.hpp"
29#include "guess-from-search-domains.hpp"
30#include "guess-from-identity-name.hpp"
31
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080032#include <ndn-cxx/util/network-monitor.hpp>
33#include <ndn-cxx/util/scheduler.hpp>
34#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
35
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080036#include <boost/noncopyable.hpp>
37
38namespace ndn {
39namespace tools {
40
41class NdnAutoconfig : boost::noncopyable
42{
43public:
44 class Error : public std::runtime_error
45 {
46 public:
47 explicit
48 Error(const std::string& what)
49 : std::runtime_error(what)
50 {
51 }
52 };
53
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080054 explicit
55 NdnAutoconfig(bool isDaemonMode)
56 : m_face(m_io)
57 , m_scheduler(m_io)
58 , m_startStagesEvent(m_scheduler)
59 , m_isDaemonMode(isDaemonMode)
60 , m_terminationSignalSet(m_io)
61 , m_stage1(m_face, m_keyChain,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080062 [&] (const std::string& errorMessage) {
63 std::cerr << "Stage 1 failed: " << errorMessage << std::endl;
64 m_stage2.start();
65 })
66 , m_stage2(m_face, m_keyChain,
67 [&] (const std::string& errorMessage) {
68 std::cerr << "Stage 2 failed: " << errorMessage << std::endl;
69 m_stage3.start();
70 })
71 , m_stage3(m_face, m_keyChain,
72 [&] (const std::string& errorMessage) {
73 std::cerr << "Stage 3 failed: " << errorMessage << std::endl;
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080074 if (!m_isDaemonMode)
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070075 BOOST_THROW_EXCEPTION(Error("No more stages, automatic discovery failed"));
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080076 else
77 std::cerr << "No more stages, automatic discovery failed" << std::endl;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080078 })
79 {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -080080 if (m_isDaemonMode) {
81 m_networkMonitor.reset(new util::NetworkMonitor(m_io));
82 m_networkMonitor->onNetworkStateChanged.connect([this] {
83 // delay stages, so if multiple events are triggered in short sequence,
84 // only one auto-detection procedure is triggered
85 m_startStagesEvent = m_scheduler.scheduleEvent(time::seconds(5),
86 bind(&NdnAutoconfig::startStages, this));
87 });
88 }
89
90 // Delay a little bit
91 m_startStagesEvent = m_scheduler.scheduleEvent(time::milliseconds(100),
92 bind(&NdnAutoconfig::startStages, this));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080093 }
94
95 void
96 run()
97 {
Alexander Afanasyevba3f79e2015-02-02 13:56:13 -080098 if (m_isDaemonMode) {
99 m_terminationSignalSet.add(SIGINT);
100 m_terminationSignalSet.add(SIGTERM);
101 m_terminationSignalSet.async_wait(bind(&NdnAutoconfig::terminate, this, _1, _2));
102 }
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800103
104 m_io.run();
105 }
106
107 void
108 terminate(const boost::system::error_code& error, int signalNo)
109 {
110 if (error)
111 return;
112
113 m_io.stop();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800114 }
115
116 static void
117 usage(const char* programName)
118 {
119 std::cout << "Usage:\n"
120 << " " << programName << " [options]\n"
121 << "\n"
122 << "Options:\n"
123 << " [-h] - print usage and exit\n"
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800124 << " [-d] - run in daemon mode. In daemon mode, " << programName << " will try \n"
125 << " to detect network change events and re-run auto-discovery procedure.\n"
126 << " In addition, the auto-discovery procedure is unconditionally re-run\n"
127 << " every hour.\n"
128 << " NOTE: if connection to NFD fails, the daemon will be terminated.\n"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800129 << " [-V] - print version number and exit\n"
130 << std::endl;
131 }
132
133private:
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800134 void
135 startStages()
136 {
137 m_stage1.start();
138 if (m_isDaemonMode) {
139 m_startStagesEvent = m_scheduler.scheduleEvent(time::hours(1),
140 bind(&NdnAutoconfig::startStages, this));
141 }
142 }
143
144private:
145 boost::asio::io_service m_io;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800146 Face m_face;
147 KeyChain m_keyChain;
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800148 unique_ptr<util::NetworkMonitor> m_networkMonitor;
149 util::Scheduler m_scheduler;
150 util::scheduler::ScopedEventId m_startStagesEvent;
151 bool m_isDaemonMode;
152 boost::asio::signal_set m_terminationSignalSet;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800153
154 autoconfig::MulticastDiscovery m_stage1;
155 autoconfig::GuessFromSearchDomains m_stage2;
156 autoconfig::GuessFromIdentityName m_stage3;
157};
158
159} // namespace tools
160} // namespace ndn
161
162int
163main(int argc, char** argv)
164{
165 int opt;
166 const char* programName = argv[0];
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800167 bool isDaemonMode = false;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800168
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800169 while ((opt = getopt(argc, argv, "dhV")) != -1) {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800170 switch (opt) {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800171 case 'd':
172 isDaemonMode = true;
173 break;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800174 case 'h':
175 ndn::tools::NdnAutoconfig::usage(programName);
176 return 0;
177 case 'V':
178 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
179 return 0;
180 }
181 }
182
183 try {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800184 ndn::tools::NdnAutoconfig autoConfigInstance(isDaemonMode);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800185 autoConfigInstance.run();
186 }
187 catch (const std::exception& error) {
188 std::cerr << "ERROR: " << error.what() << std::endl;
189 return 1;
190 }
191 return 0;
192}