blob: 01ac6a7d8f9b0b2bd4790b07edd093be039bb386 [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)
75 throw Error("No more stages, automatic discovery failed");
76 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 Afanasyeve46279dc2015-01-29 15:39:17 -080098 m_terminationSignalSet.add(SIGINT);
99 m_terminationSignalSet.add(SIGTERM);
100 m_terminationSignalSet.async_wait(bind(&NdnAutoconfig::terminate, this, _1, _2));
101
102 m_io.run();
103 }
104
105 void
106 terminate(const boost::system::error_code& error, int signalNo)
107 {
108 if (error)
109 return;
110
111 m_io.stop();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800112 }
113
114 static void
115 usage(const char* programName)
116 {
117 std::cout << "Usage:\n"
118 << " " << programName << " [options]\n"
119 << "\n"
120 << "Options:\n"
121 << " [-h] - print usage and exit\n"
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800122 << " [-d] - run in daemon mode. In daemon mode, " << programName << " will try \n"
123 << " to detect network change events and re-run auto-discovery procedure.\n"
124 << " In addition, the auto-discovery procedure is unconditionally re-run\n"
125 << " every hour.\n"
126 << " NOTE: if connection to NFD fails, the daemon will be terminated.\n"
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800127 << " [-V] - print version number and exit\n"
128 << std::endl;
129 }
130
131private:
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800132 void
133 startStages()
134 {
135 m_stage1.start();
136 if (m_isDaemonMode) {
137 m_startStagesEvent = m_scheduler.scheduleEvent(time::hours(1),
138 bind(&NdnAutoconfig::startStages, this));
139 }
140 }
141
142private:
143 boost::asio::io_service m_io;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800144 Face m_face;
145 KeyChain m_keyChain;
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800146 unique_ptr<util::NetworkMonitor> m_networkMonitor;
147 util::Scheduler m_scheduler;
148 util::scheduler::ScopedEventId m_startStagesEvent;
149 bool m_isDaemonMode;
150 boost::asio::signal_set m_terminationSignalSet;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800151
152 autoconfig::MulticastDiscovery m_stage1;
153 autoconfig::GuessFromSearchDomains m_stage2;
154 autoconfig::GuessFromIdentityName m_stage3;
155};
156
157} // namespace tools
158} // namespace ndn
159
160int
161main(int argc, char** argv)
162{
163 int opt;
164 const char* programName = argv[0];
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800165 bool isDaemonMode = false;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800166
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800167 while ((opt = getopt(argc, argv, "dhV")) != -1) {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800168 switch (opt) {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800169 case 'd':
170 isDaemonMode = true;
171 break;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800172 case 'h':
173 ndn::tools::NdnAutoconfig::usage(programName);
174 return 0;
175 case 'V':
176 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
177 return 0;
178 }
179 }
180
181 try {
Alexander Afanasyeve46279dc2015-01-29 15:39:17 -0800182 ndn::tools::NdnAutoconfig autoConfigInstance(isDaemonMode);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800183 autoConfigInstance.run();
184 }
185 catch (const std::exception& error) {
186 std::cerr << "ERROR: " << error.what() << std::endl;
187 return 1;
188 }
189 return 0;
190}