blob: f80a4bde070cb5b5edfc8900e4cd34c6f04c681f [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"
Alexander Afanasyev472acbe2014-02-26 19:30:44 -080013#include "mgmt/local-control-header-manager.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070014#include "mgmt/strategy-choice-manager.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;
Alexander Afanasyev472acbe2014-02-26 19:30:44 -080033static LocalControlHeaderManager* g_localControlHeaderManager;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070034static StrategyChoiceManager* g_strategyChoiceManager;
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 Shi09bf7c42014-01-31 11:10:25 -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 Shi09bf7c42014-01-31 11:10:25 -070058 while (1) {
59 int option_index = 0;
60 static ::option long_options[] = {
61 { "help" , no_argument , 0, 0 },
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070062 { "config" , required_argument, 0, 0 },
Junxiao Shi09bf7c42014-01-31 11:10:25 -070063 { 0 , 0 , 0, 0 }
64 };
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>();
91 g_forwarder->addFace(g_internalFace);
Steve DiBenedetto214563c2014-02-03 19:20:36 -070092
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070093 g_internalFace->getValidator().setConfigFile(config);
94
95
Steve DiBenedetto214563c2014-02-03 19:20:36 -070096 g_fibManager = new FibManager(g_forwarder->getFib(),
97 bind(&Forwarder::getFace, g_forwarder, _1),
98 g_internalFace);
99
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700100 g_faceManager = new FaceManager(g_forwarder->getFaceTable(), g_internalFace);
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600101 g_faceManager->setConfigFile(config);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700102
Alexander Afanasyev472acbe2014-02-26 19:30:44 -0800103 g_localControlHeaderManager =
104 new LocalControlHeaderManager(bind(&Forwarder::getFace, g_forwarder, _1),
105 g_internalFace);
106
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700107 g_strategyChoiceManager = new StrategyChoiceManager(g_forwarder->getStrategyChoice(),
108 g_internalFace);
109
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600110 config.parse(g_options.m_config, true);
111 config.parse(g_options.m_config, false);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700112
Alexander Afanasyev472acbe2014-02-26 19:30:44 -0800113 shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first;
Steve DiBenedetto214563c2014-02-03 19:20:36 -0700114 entry->addNextHop(g_internalFace, 0);
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700115}
116
117int
118main(int argc, char** argv)
119{
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700120 try {
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600121 bool isCommandLineValid = parseCommandLine(argc, argv);
122 if (!isCommandLineValid) {
123 usage(argv[0]);
124 return 1;
125 }
126 if (g_options.m_showUsage) {
127 usage(argv[0]);
128 return 0;
129 }
130
131 g_forwarder = new Forwarder();
132 initializeMgmt();
133
134 /// \todo Add signal processing to gracefully terminate the app
135
Junxiao Shic041ca32014-02-25 20:01:15 -0700136 getGlobalIoService().run();
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600137 // } catch(ConfigFile::Error& error) {
138 // NFD_LOG_ERROR("Error: " << error.what());
139 // NFD_LOG_ERROR("You should either specify --config option or copy sample configuration into "
140 // << DEFAULT_CONFIG_FILE);
141 } catch(boost::filesystem::filesystem_error& error) {
142 if (error.code() == boost::system::errc::permission_denied) {
143 NFD_LOG_ERROR("Error: Permissions denied for " << error.path1());
144 NFD_LOG_ERROR(argv[0] << " should be run as superuser");
145 } else {
146 NFD_LOG_ERROR("Error: " << error.what());
147 }
148 } catch(std::exception& exception) {
149 NFD_LOG_ERROR("Error: " << exception.what());
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700150 return 1;
151 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700152
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800153 return 0;
154}
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700155
156} // namespace nfd
157
158int
159main(int argc, char** argv)
160{
161 return nfd::main(argc, argv);
162}