blob: 62b2ffc4fccc1faece4c24fbc4ffc1c5fc35a991 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev31367922015-02-09 20:51:10 -08003 * 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 Afanasyev3ecec502014-04-16 13:42:44 -070010 *
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 Lehman5144f822014-07-23 15:12:56 -070024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
Alexander Afanasyev31367922015-02-09 20:51:10 -080026#include "nrd.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070027
Alexander Afanasyevb47d5382014-05-05 14:35:03 -070028#include "version.hpp"
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070029#include "core/logger.hpp"
Alexander Afanasyev31367922015-02-09 20:51:10 -080030#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 Afanasyev3ecec502014-04-16 13:42:44 -070038
39namespace nfd {
40namespace rib {
41
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070042NFD_LOG_INIT("NRD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070043
Alexander Afanasyev31367922015-02-09 20:51:10 -080044class NrdRunner : noncopyable
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070045{
46public:
Alexander Afanasyev31367922015-02-09 20:51:10 -080047 explicit
48 NrdRunner(const std::string& configFile)
49 : m_nrd(configFile, m_keyChain)
50 , m_terminationSignalSet(getGlobalIoService())
Steve DiBenedetto34c95f72014-04-17 20:56:00 -060051 {
Alexander Afanasyev31367922015-02-09 20:51:10 -080052 m_terminationSignalSet.add(SIGINT);
53 m_terminationSignalSet.add(SIGTERM);
54 m_terminationSignalSet.async_wait(bind(&NrdRunner::terminate, this, _1, _2));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070055 }
56
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070057 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 Afanasyevb47d5382014-05-05 14:35:03 -070066 << " [--help] - print this help message\n"
67 << " [--version] - print version and exit\n"
68 << " [--modules] - list available logging modules\n"
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070069 << " [--config /path/to/nfd.conf] - path to configuration file "
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070070 << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
71 ;
72 }
73
74 static void
75 printModules(std::ostream& os)
76 {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070077 os << "Available logging modules: \n";
78
Alexander Afanasyev31367922015-02-09 20:51:10 -080079 for (const auto& module : LoggerFactory::getInstance().getModules()) {
80 os << module << "\n";
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070081 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070082 }
83
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070084 void
Alexander Afanasyev31367922015-02-09 20:51:10 -080085 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 Afanasyev3ecec502014-04-16 13:42:44 -070098 {
99 if (error)
100 return;
101
Alexander Afanasyev31367922015-02-09 20:51:10 -0800102 NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
103 getGlobalIoService().stop();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700104 }
105
106private:
Alexander Afanasyev31367922015-02-09 20:51:10 -0800107 ndn::KeyChain m_keyChain;
108 Nrd m_nrd; // must be after m_io and m_keyChain
109 boost::asio::signal_set m_terminationSignalSet;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700110};
111
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700112} // namespace nfd
Alexander Afanasyev31367922015-02-09 20:51:10 -0800113} // namespace rib
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700114
115int
116main(int argc, char** argv)
117{
118 using namespace nfd::rib;
119
Alexander Afanasyev31367922015-02-09 20:51:10 -0800120 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 Afanasyev3ecec502014-04-16 13:42:44 -0700140 return 1;
141 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800142
143 if (vm.count("help") > 0) {
144 NrdRunner::printUsage(std::cout, argv[0]);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700145 return 0;
146 }
147
Alexander Afanasyev31367922015-02-09 20:51:10 -0800148 if (vm.count("version") > 0) {
Alexander Afanasyevb47d5382014-05-05 14:35:03 -0700149 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
150 return 0;
151 }
152
Alexander Afanasyev31367922015-02-09 20:51:10 -0800153 if (vm.count("modules") > 0) {
154 NrdRunner::printModules(std::cout);
155 return 0;
156 }
157
158 NrdRunner runner(configFile);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700159
160 try {
Alexander Afanasyev31367922015-02-09 20:51:10 -0800161 runner.initialize();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700162 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800163 catch (const boost::filesystem::filesystem_error& e) {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700164 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 Afanasyev3ecec502014-04-16 13:42:44 -0700178 try {
Alexander Afanasyev31367922015-02-09 20:51:10 -0800179 runner.run();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700180 }
Alexander Afanasyev31367922015-02-09 20:51:10 -0800181 catch (const std::exception& e) {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700182 NFD_LOG_FATAL(e.what());
Alexander Afanasyev31367922015-02-09 20:51:10 -0800183 return 4;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700184 }
185
186 return 0;
187}