Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 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. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 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/>. |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 26 | #include "nrd.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 28 | #include "version.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 29 | #include "core/logger.hpp" |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 30 | #include "core/global-io.hpp" |
| 31 | |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include <boost/filesystem.hpp> |
| 35 | #include <boost/program_options/options_description.hpp> |
| 36 | #include <boost/program_options/variables_map.hpp> |
| 37 | #include <boost/program_options/parsers.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 38 | |
| 39 | namespace nfd { |
| 40 | namespace rib { |
| 41 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 42 | NFD_LOG_INIT("NRD"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 44 | class NrdRunner : noncopyable |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 45 | { |
| 46 | public: |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 47 | explicit |
| 48 | NrdRunner(const std::string& configFile) |
| 49 | : m_nrd(configFile, m_keyChain) |
| 50 | , m_terminationSignalSet(getGlobalIoService()) |
Steve DiBenedetto | 34c95f7 | 2014-04-17 20:56:00 -0600 | [diff] [blame] | 51 | { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 52 | m_terminationSignalSet.add(SIGINT); |
| 53 | m_terminationSignalSet.add(SIGTERM); |
| 54 | m_terminationSignalSet.async_wait(bind(&NrdRunner::terminate, this, _1, _2)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 57 | static void |
| 58 | printUsage(std::ostream& os, const std::string& programName) |
| 59 | { |
| 60 | os << "Usage: \n" |
| 61 | << " " << programName << " [options]\n" |
| 62 | << "\n" |
| 63 | << "Run NRD daemon\n" |
| 64 | << "\n" |
| 65 | << "Options:\n" |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 66 | << " [--help] - print this help message\n" |
| 67 | << " [--version] - print version and exit\n" |
| 68 | << " [--modules] - list available logging modules\n" |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 69 | << " [--config /path/to/nfd.conf] - path to configuration file " |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 70 | << "(default: " << DEFAULT_CONFIG_FILE << ")\n" |
| 71 | ; |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | printModules(std::ostream& os) |
| 76 | { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 77 | os << "Available logging modules: \n"; |
| 78 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 79 | for (const auto& module : LoggerFactory::getInstance().getModules()) { |
| 80 | os << module << "\n"; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 81 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 84 | void |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 85 | run() |
| 86 | { |
| 87 | getGlobalIoService().run(); |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | initialize() |
| 92 | { |
| 93 | m_nrd.initialize(); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | terminate(const boost::system::error_code& error, int signalNo) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 98 | { |
| 99 | if (error) |
| 100 | return; |
| 101 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 102 | NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting..."); |
| 103 | getGlobalIoService().stop(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 107 | ndn::KeyChain m_keyChain; |
| 108 | Nrd m_nrd; // must be after m_io and m_keyChain |
| 109 | boost::asio::signal_set m_terminationSignalSet; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 112 | } // namespace nfd |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 113 | } // namespace rib |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 114 | |
| 115 | int |
| 116 | main(int argc, char** argv) |
| 117 | { |
| 118 | using namespace nfd::rib; |
| 119 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 120 | namespace po = boost::program_options; |
| 121 | |
| 122 | po::options_description description; |
| 123 | |
| 124 | std::string configFile = DEFAULT_CONFIG_FILE; |
| 125 | description.add_options() |
| 126 | ("help,h", "print this help message") |
| 127 | ("version,V", "print version and exit") |
| 128 | ("modules,m", "list available logging modules") |
| 129 | ("config,c", po::value<std::string>(&configFile), "path to configuration file") |
| 130 | ; |
| 131 | |
| 132 | po::variables_map vm; |
| 133 | try { |
| 134 | po::store(po::command_line_parser(argc, argv).options(description).run(), vm); |
| 135 | po::notify(vm); |
| 136 | } |
| 137 | catch (const std::exception& e) { |
| 138 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 139 | NrdRunner::printUsage(std::cerr, argv[0]); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 140 | return 1; |
| 141 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 142 | |
| 143 | if (vm.count("help") > 0) { |
| 144 | NrdRunner::printUsage(std::cout, argv[0]); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 148 | if (vm.count("version") > 0) { |
Alexander Afanasyev | b47d538 | 2014-05-05 14:35:03 -0700 | [diff] [blame] | 149 | std::cout << NFD_VERSION_BUILD_STRING << std::endl; |
| 150 | return 0; |
| 151 | } |
| 152 | |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 153 | if (vm.count("modules") > 0) { |
| 154 | NrdRunner::printModules(std::cout); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | NrdRunner runner(configFile); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 159 | |
| 160 | try { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 161 | runner.initialize(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 162 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 163 | catch (const boost::filesystem::filesystem_error& e) { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 164 | if (e.code() == boost::system::errc::permission_denied) { |
| 165 | NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " << |
| 166 | argv[0] << " should be run as superuser"); |
| 167 | } |
| 168 | else { |
| 169 | NFD_LOG_FATAL(e.what()); |
| 170 | } |
| 171 | return 1; |
| 172 | } |
| 173 | catch (const std::exception& e) { |
| 174 | NFD_LOG_FATAL(e.what()); |
| 175 | return 2; |
| 176 | } |
| 177 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 178 | try { |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 179 | runner.run(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 180 | } |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 181 | catch (const std::exception& e) { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 182 | NFD_LOG_FATAL(e.what()); |
Alexander Afanasyev | 3136792 | 2015-02-09 20:51:10 -0800 | [diff] [blame^] | 183 | return 4; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |