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