Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 7 | #include <getopt.h> |
| 8 | #include "core/logger.hpp" |
| 9 | #include "fw/forwarder.hpp" |
| 10 | #include "mgmt/internal-face.hpp" |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 11 | #include "mgmt/fib-manager.hpp" |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 12 | #include "mgmt/face-manager.hpp" |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 13 | #include "mgmt/local-control-header-manager.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 14 | #include "mgmt/strategy-choice-manager.hpp" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 15 | #include "mgmt/status-server.hpp" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 16 | #include "mgmt/config-file.hpp" |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 17 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 18 | #include <boost/filesystem.hpp> |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 19 | |
| 20 | namespace nfd { |
| 21 | |
| 22 | NFD_LOG_INIT("Main"); |
| 23 | |
| 24 | struct ProgramOptions |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 25 | { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 26 | bool m_showUsage; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 27 | std::string m_config; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 30 | static ProgramOptions g_options; |
| 31 | static Forwarder* g_forwarder; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 32 | static FibManager* g_fibManager; |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 33 | static FaceManager* g_faceManager; |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 34 | static LocalControlHeaderManager* g_localControlHeaderManager; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 35 | static StrategyChoiceManager* g_strategyChoiceManager; |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 36 | static StatusServer* g_statusServer; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 37 | static shared_ptr<InternalFace> g_internalFace; |
Alexander Afanasyev | c78b141 | 2014-02-19 14:08:26 -0800 | [diff] [blame] | 38 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 39 | |
| 40 | void |
| 41 | usage(char* programName) |
| 42 | { |
| 43 | printf( |
| 44 | "%s --help\n\tshow this help and exit\n" |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 45 | "%s " |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 46 | "[--config /path/to/nfd.conf]\n" |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 47 | "\trun forwarding daemon\n" |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 48 | "\t--config <configuration file>]: path to configuration file\n" |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 49 | "\n", |
| 50 | programName, programName |
| 51 | ); |
| 52 | } |
| 53 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 54 | bool |
| 55 | parseCommandLine(int argc, char** argv) |
| 56 | { |
| 57 | g_options.m_showUsage = false; |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 58 | g_options.m_config = DEFAULT_CONFIG_FILE; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 60 | while (true) { |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 61 | int option_index = 0; |
| 62 | static ::option long_options[] = { |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 63 | { "help" , no_argument , 0, 0 }, |
| 64 | { "config" , required_argument, 0, 0 }, |
| 65 | { 0 , 0 , 0, 0 } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 66 | }; |
| 67 | int c = getopt_long_only(argc, argv, "", long_options, &option_index); |
| 68 | if (c == -1) break; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 70 | switch (c) { |
| 71 | case 0: |
| 72 | switch (option_index) { |
| 73 | case 0://help |
| 74 | g_options.m_showUsage = true; |
| 75 | break; |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 76 | case 1://config |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 77 | g_options.m_config = ::optarg; |
| 78 | break; |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 79 | } |
| 80 | break; |
| 81 | } |
| 82 | } |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
| 87 | void |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 88 | initializeMgmt() |
| 89 | { |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 90 | ConfigFile config; |
| 91 | |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 92 | g_internalFace = make_shared<InternalFace>(); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 93 | g_internalFace->getValidator().setConfigFile(config); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 94 | g_forwarder->addFace(g_internalFace); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 95 | |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 96 | g_fibManager = new FibManager(g_forwarder->getFib(), |
| 97 | bind(&Forwarder::getFace, g_forwarder, _1), |
| 98 | g_internalFace); |
| 99 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 100 | g_faceManager = new FaceManager(g_forwarder->getFaceTable(), g_internalFace); |
Steve DiBenedetto | 4aca99c | 2014-03-11 11:27:54 -0600 | [diff] [blame] | 101 | g_faceManager->setConfigFile(config); |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 102 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 103 | g_localControlHeaderManager = |
| 104 | new LocalControlHeaderManager(bind(&Forwarder::getFace, g_forwarder, _1), |
| 105 | g_internalFace); |
| 106 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 107 | g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(), |
| 108 | g_internalFace); |
| 109 | |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 110 | g_statusServer = new StatusServer(g_internalFace, *g_forwarder); |
| 111 | |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 112 | config.parse(g_options.m_config, true); |
| 113 | config.parse(g_options.m_config, false); |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 472acbe | 2014-02-26 19:30:44 -0800 | [diff] [blame] | 115 | shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first; |
Steve DiBenedetto | 214563c | 2014-02-03 19:20:36 -0700 | [diff] [blame] | 116 | entry->addNextHop(g_internalFace, 0); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int |
| 120 | main(int argc, char** argv) |
| 121 | { |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 122 | bool isCommandLineValid = parseCommandLine(argc, argv); |
| 123 | if (!isCommandLineValid) { |
| 124 | usage(argv[0]); |
| 125 | return 1; |
| 126 | } |
| 127 | if (g_options.m_showUsage) { |
| 128 | usage(argv[0]); |
| 129 | return 0; |
| 130 | } |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 131 | |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 132 | try { |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 133 | g_forwarder = new Forwarder(); |
| 134 | initializeMgmt(); |
| 135 | |
| 136 | /// \todo Add signal processing to gracefully terminate the app |
| 137 | |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 138 | getGlobalIoService().run(); |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 139 | // } catch(ConfigFile::Error& error) { |
| 140 | // NFD_LOG_ERROR("Error: " << error.what()); |
| 141 | // NFD_LOG_ERROR("You should either specify --config option or copy sample configuration into " |
| 142 | // << DEFAULT_CONFIG_FILE); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 143 | } |
| 144 | catch (boost::filesystem::filesystem_error& e) { |
| 145 | if (e.code() == boost::system::errc::permission_denied) { |
| 146 | NFD_LOG_ERROR("Error: Permissions denied for " << e.path1()); |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 147 | NFD_LOG_ERROR(argv[0] << " should be run as superuser"); |
Steve DiBenedetto | 84da5bf | 2014-03-11 14:51:29 -0600 | [diff] [blame] | 148 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 149 | else { |
| 150 | NFD_LOG_ERROR("Error: " << e.what()); |
| 151 | } |
| 152 | } |
| 153 | catch (std::exception& e) { |
| 154 | NFD_LOG_ERROR("Error: " << e.what()); |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 155 | return 1; |
| 156 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 157 | |
Alexander Afanasyev | 2aa3962 | 2014-01-22 11:51:11 -0800 | [diff] [blame] | 158 | return 0; |
| 159 | } |
Junxiao Shi | 09bf7c4 | 2014-01-31 11:10:25 -0700 | [diff] [blame] | 160 | |
| 161 | } // namespace nfd |
| 162 | |
| 163 | int |
| 164 | main(int argc, char** argv) |
| 165 | { |
| 166 | return nfd::main(argc, argv); |
| 167 | } |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 168 | |