Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 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> |
| 35 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 36 | #include <boost/noncopyable.hpp> |
| 37 | |
| 38 | namespace ndn { |
| 39 | namespace tools { |
| 40 | |
| 41 | class NdnAutoconfig : boost::noncopyable |
| 42 | { |
| 43 | public: |
| 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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 54 | 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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 62 | [&] (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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 74 | 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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 78 | }) |
| 79 | { |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 80 | 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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | run() |
| 97 | { |
Alexander Afanasyev | ba3f79e | 2015-02-02 13:56:13 -0800 | [diff] [blame^] | 98 | 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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 103 | |
| 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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 114 | } |
| 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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 124 | << " [-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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 129 | << " [-V] - print version number and exit\n" |
| 130 | << std::endl; |
| 131 | } |
| 132 | |
| 133 | private: |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 134 | 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 | |
| 144 | private: |
| 145 | boost::asio::io_service m_io; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 146 | Face m_face; |
| 147 | KeyChain m_keyChain; |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 148 | 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 Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 153 | |
| 154 | autoconfig::MulticastDiscovery m_stage1; |
| 155 | autoconfig::GuessFromSearchDomains m_stage2; |
| 156 | autoconfig::GuessFromIdentityName m_stage3; |
| 157 | }; |
| 158 | |
| 159 | } // namespace tools |
| 160 | } // namespace ndn |
| 161 | |
| 162 | int |
| 163 | main(int argc, char** argv) |
| 164 | { |
| 165 | int opt; |
| 166 | const char* programName = argv[0]; |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 167 | bool isDaemonMode = false; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 169 | while ((opt = getopt(argc, argv, "dhV")) != -1) { |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 170 | switch (opt) { |
Alexander Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 171 | case 'd': |
| 172 | isDaemonMode = true; |
| 173 | break; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 174 | 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 Afanasyev | e46279dc | 2015-01-29 15:39:17 -0800 | [diff] [blame] | 184 | ndn::tools::NdnAutoconfig autoConfigInstance(isDaemonMode); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 185 | 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 | } |