blob: 44880b2571e77ab789ae351babacacfcbb298346 [file] [log] [blame]
Alexander Afanasyev2aa39622014-01-22 11:51:11 -08001/* -*- 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 Shi09bf7c42014-01-31 11:10:25 -07007#include <getopt.h>
8#include "core/logger.hpp"
9#include "fw/forwarder.hpp"
10#include "mgmt/internal-face.hpp"
Steve DiBenedetto214563c2014-02-03 19:20:36 -070011#include "mgmt/fib-manager.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070012#include "mgmt/face-manager.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070013#include "mgmt/strategy-choice-manager.hpp"
Junxiao Shiea48d8b2014-03-16 13:53:47 -070014#include "mgmt/status-server.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070015#include "mgmt/config-file.hpp"
Alexander Afanasyevc78b1412014-02-19 14:08:26 -080016
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060017#include <boost/filesystem.hpp>
Junxiao Shi09bf7c42014-01-31 11:10:25 -070018
19namespace nfd {
20
21NFD_LOG_INIT("Main");
22
23struct ProgramOptions
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080024{
Junxiao Shi09bf7c42014-01-31 11:10:25 -070025 bool m_showUsage;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070026 std::string m_config;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070027};
28
Junxiao Shi09bf7c42014-01-31 11:10:25 -070029static ProgramOptions g_options;
30static Forwarder* g_forwarder;
Steve DiBenedetto214563c2014-02-03 19:20:36 -070031static FibManager* g_fibManager;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070032static FaceManager* g_faceManager;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070033static StrategyChoiceManager* g_strategyChoiceManager;
Junxiao Shiea48d8b2014-03-16 13:53:47 -070034static StatusServer* g_statusServer;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070035static shared_ptr<InternalFace> g_internalFace;
Alexander Afanasyevc78b1412014-02-19 14:08:26 -080036
Junxiao Shi09bf7c42014-01-31 11:10:25 -070037
38void
39usage(char* programName)
40{
41 printf(
42 "%s --help\n\tshow this help and exit\n"
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060043 "%s "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070044 "[--config /path/to/nfd.conf]\n"
Junxiao Shiea48d8b2014-03-16 13:53:47 -070045 "\trun forwarding daemon\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070046 "\t--config <configuration file>]: path to configuration file\n"
Junxiao Shi09bf7c42014-01-31 11:10:25 -070047 "\n",
48 programName, programName
49 );
50}
51
Junxiao Shi09bf7c42014-01-31 11:10:25 -070052bool
53parseCommandLine(int argc, char** argv)
54{
55 g_options.m_showUsage = false;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070056 g_options.m_config = DEFAULT_CONFIG_FILE;
Junxiao Shic041ca32014-02-25 20:01:15 -070057
Junxiao Shiea48d8b2014-03-16 13:53:47 -070058 while (true) {
Junxiao Shi09bf7c42014-01-31 11:10:25 -070059 int option_index = 0;
60 static ::option long_options[] = {
Junxiao Shiea48d8b2014-03-16 13:53:47 -070061 { "help" , no_argument , 0, 0 },
62 { "config" , required_argument, 0, 0 },
63 { 0 , 0 , 0, 0 }
Junxiao Shi09bf7c42014-01-31 11:10:25 -070064 };
65 int c = getopt_long_only(argc, argv, "", long_options, &option_index);
66 if (c == -1) break;
Junxiao Shic041ca32014-02-25 20:01:15 -070067
Junxiao Shi09bf7c42014-01-31 11:10:25 -070068 switch (c) {
69 case 0:
70 switch (option_index) {
71 case 0://help
72 g_options.m_showUsage = true;
73 break;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060074 case 1://config
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070075 g_options.m_config = ::optarg;
76 break;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070077 }
78 break;
79 }
80 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070081
Junxiao Shi09bf7c42014-01-31 11:10:25 -070082 return true;
83}
84
85void
Junxiao Shi09bf7c42014-01-31 11:10:25 -070086initializeMgmt()
87{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070088 ConfigFile config;
89
Junxiao Shi09bf7c42014-01-31 11:10:25 -070090 g_internalFace = make_shared<InternalFace>();
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070091 g_internalFace->getValidator().setConfigFile(config);
Junxiao Shiea48d8b2014-03-16 13:53:47 -070092 g_forwarder->addFace(g_internalFace);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070093
Steve DiBenedetto214563c2014-02-03 19:20:36 -070094 g_fibManager = new FibManager(g_forwarder->getFib(),
95 bind(&Forwarder::getFace, g_forwarder, _1),
96 g_internalFace);
97
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070098 g_faceManager = new FaceManager(g_forwarder->getFaceTable(), g_internalFace);
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060099 g_faceManager->setConfigFile(config);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700100
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700101 g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(),
102 g_internalFace);
103
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700104 g_statusServer = new StatusServer(g_internalFace, *g_forwarder);
105
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600106 config.parse(g_options.m_config, true);
107 config.parse(g_options.m_config, false);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700108
Alexander Afanasyev472acbe2014-02-26 19:30:44 -0800109 shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first;
Steve DiBenedetto214563c2014-02-03 19:20:36 -0700110 entry->addNextHop(g_internalFace, 0);
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700111}
112
113int
114main(int argc, char** argv)
115{
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700116 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 DiBenedetto84da5bf2014-03-11 14:51:29 -0600125
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700126 try {
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600127 g_forwarder = new Forwarder();
128 initializeMgmt();
129
130 /// \todo Add signal processing to gracefully terminate the app
131
Junxiao Shic041ca32014-02-25 20:01:15 -0700132 getGlobalIoService().run();
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600133 // } 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 Shiea48d8b2014-03-16 13:53:47 -0700137 }
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 DiBenedetto84da5bf2014-03-11 14:51:29 -0600141 NFD_LOG_ERROR(argv[0] << " should be run as superuser");
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600142 }
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700143 else {
144 NFD_LOG_ERROR("Error: " << e.what());
145 }
146 }
147 catch (std::exception& e) {
148 NFD_LOG_ERROR("Error: " << e.what());
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700149 return 1;
150 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700151
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800152 return 0;
153}
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700154
155} // namespace nfd
156
157int
158main(int argc, char** argv)
159{
160 return nfd::main(argc, argv);
161}
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700162