blob: 8abdba6012758293475bf2bdf5c39a16cd9597d2 [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"
Junxiao Shiea48d8b2014-03-16 13:53:47 -070015#include "mgmt/status-server.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070016#include "mgmt/config-file.hpp"
Alexander Afanasyevc78b1412014-02-19 14:08:26 -080017
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060018#include <boost/filesystem.hpp>
Junxiao Shi09bf7c42014-01-31 11:10:25 -070019
20namespace nfd {
21
22NFD_LOG_INIT("Main");
23
24struct ProgramOptions
Alexander Afanasyev2aa39622014-01-22 11:51:11 -080025{
Junxiao Shi09bf7c42014-01-31 11:10:25 -070026 bool m_showUsage;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027 std::string m_config;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070028};
29
Junxiao Shi09bf7c42014-01-31 11:10:25 -070030static ProgramOptions g_options;
31static Forwarder* g_forwarder;
Steve DiBenedetto214563c2014-02-03 19:20:36 -070032static FibManager* g_fibManager;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070033static FaceManager* g_faceManager;
Alexander Afanasyev472acbe2014-02-26 19:30:44 -080034static LocalControlHeaderManager* g_localControlHeaderManager;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070035static StrategyChoiceManager* g_strategyChoiceManager;
Junxiao Shiea48d8b2014-03-16 13:53:47 -070036static StatusServer* g_statusServer;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070037static shared_ptr<InternalFace> g_internalFace;
Alexander Afanasyevc78b1412014-02-19 14:08:26 -080038
Junxiao Shi09bf7c42014-01-31 11:10:25 -070039
40void
41usage(char* programName)
42{
43 printf(
44 "%s --help\n\tshow this help and exit\n"
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060045 "%s "
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070046 "[--config /path/to/nfd.conf]\n"
Junxiao Shiea48d8b2014-03-16 13:53:47 -070047 "\trun forwarding daemon\n"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070048 "\t--config <configuration file>]: path to configuration file\n"
Junxiao Shi09bf7c42014-01-31 11:10:25 -070049 "\n",
50 programName, programName
51 );
52}
53
Junxiao Shi09bf7c42014-01-31 11:10:25 -070054bool
55parseCommandLine(int argc, char** argv)
56{
57 g_options.m_showUsage = false;
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070058 g_options.m_config = DEFAULT_CONFIG_FILE;
Junxiao Shic041ca32014-02-25 20:01:15 -070059
Junxiao Shiea48d8b2014-03-16 13:53:47 -070060 while (true) {
Junxiao Shi09bf7c42014-01-31 11:10:25 -070061 int option_index = 0;
62 static ::option long_options[] = {
Junxiao Shiea48d8b2014-03-16 13:53:47 -070063 { "help" , no_argument , 0, 0 },
64 { "config" , required_argument, 0, 0 },
65 { 0 , 0 , 0, 0 }
Junxiao Shi09bf7c42014-01-31 11:10:25 -070066 };
67 int c = getopt_long_only(argc, argv, "", long_options, &option_index);
68 if (c == -1) break;
Junxiao Shic041ca32014-02-25 20:01:15 -070069
Junxiao Shi09bf7c42014-01-31 11:10:25 -070070 switch (c) {
71 case 0:
72 switch (option_index) {
73 case 0://help
74 g_options.m_showUsage = true;
75 break;
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -060076 case 1://config
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070077 g_options.m_config = ::optarg;
78 break;
Junxiao Shi09bf7c42014-01-31 11:10:25 -070079 }
80 break;
81 }
82 }
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070083
Junxiao Shi09bf7c42014-01-31 11:10:25 -070084 return true;
85}
86
87void
Junxiao Shi09bf7c42014-01-31 11:10:25 -070088initializeMgmt()
89{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070090 ConfigFile config;
91
Junxiao Shi09bf7c42014-01-31 11:10:25 -070092 g_internalFace = make_shared<InternalFace>();
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070093 g_internalFace->getValidator().setConfigFile(config);
Junxiao Shiea48d8b2014-03-16 13:53:47 -070094 g_forwarder->addFace(g_internalFace);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070095
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
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700110 g_statusServer = new StatusServer(g_internalFace, *g_forwarder);
111
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600112 config.parse(g_options.m_config, true);
113 config.parse(g_options.m_config, false);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700114
Alexander Afanasyev472acbe2014-02-26 19:30:44 -0800115 shared_ptr<fib::Entry> entry = g_forwarder->getFib().insert("/localhost/nfd").first;
Steve DiBenedetto214563c2014-02-03 19:20:36 -0700116 entry->addNextHop(g_internalFace, 0);
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700117}
118
119int
120main(int argc, char** argv)
121{
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700122 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 DiBenedetto84da5bf2014-03-11 14:51:29 -0600131
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700132 try {
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600133 g_forwarder = new Forwarder();
134 initializeMgmt();
135
136 /// \todo Add signal processing to gracefully terminate the app
137
Junxiao Shic041ca32014-02-25 20:01:15 -0700138 getGlobalIoService().run();
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600139 // } 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 Shiea48d8b2014-03-16 13:53:47 -0700143 }
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 DiBenedetto84da5bf2014-03-11 14:51:29 -0600147 NFD_LOG_ERROR(argv[0] << " should be run as superuser");
Steve DiBenedetto84da5bf2014-03-11 14:51:29 -0600148 }
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700149 else {
150 NFD_LOG_ERROR("Error: " << e.what());
151 }
152 }
153 catch (std::exception& e) {
154 NFD_LOG_ERROR("Error: " << e.what());
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700155 return 1;
156 }
Junxiao Shic041ca32014-02-25 20:01:15 -0700157
Alexander Afanasyev2aa39622014-01-22 11:51:11 -0800158 return 0;
159}
Junxiao Shi09bf7c42014-01-31 11:10:25 -0700160
161} // namespace nfd
162
163int
164main(int argc, char** argv)
165{
166 return nfd::main(argc, argv);
167}
Junxiao Shiea48d8b2014-03-16 13:53:47 -0700168