NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "nfd.hpp"
27 #include "rib/nrd.hpp"
28 
29 #include "version.hpp"
30 #include "core/global-io.hpp"
31 #include "core/logger.hpp"
34 
35 #include <string.h>
36 
37 #include <boost/filesystem.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 #include <boost/program_options/parsers.hpp>
41 
42 // boost::thread is used instead of std::thread to guarantee proper cleanup of thread local storage,
43 // see http://www.boost.org/doc/libs/1_48_0/doc/html/thread/thread_local_storage.html
44 #include <boost/thread.hpp>
45 
46 #include <atomic>
47 #include <condition_variable>
48 
49 namespace nfd {
50 
51 NFD_LOG_INIT("NFD");
52 
62 class NfdRunner : noncopyable
63 {
64 public:
65  explicit
66  NfdRunner(const std::string& configFile)
67  : m_nfd(configFile, m_nfdKeyChain)
68  , m_configFile(configFile)
69  , m_terminationSignalSet(getGlobalIoService())
70  , m_reloadSignalSet(getGlobalIoService())
71  {
72  m_terminationSignalSet.add(SIGINT);
73  m_terminationSignalSet.add(SIGTERM);
74  m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate, this, _1, _2));
75 
76  m_reloadSignalSet.add(SIGHUP);
77  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
78  }
79 
80  static void
81  printUsage(std::ostream& os, const std::string& programName)
82  {
83  os << "Usage: \n"
84  << " " << programName << " [options]\n"
85  << "\n"
86  << "Run NFD forwarding daemon\n"
87  << "\n"
88  << "Options:\n"
89  << " [--help] - print this help message\n"
90  << " [--version] - print version and exit\n"
91  << " [--modules] - list available logging modules\n"
92  << " [--config /path/to/nfd.conf] - path to configuration file "
93  << "(default: " << DEFAULT_CONFIG_FILE << ")\n"
94  ;
95  }
96 
97  static void
98  printModules(std::ostream& os)
99  {
100  os << "Available logging modules: \n";
101 
102  for (const auto& module : LoggerFactory::getInstance().getModules()) {
103  os << module << "\n";
104  }
105  }
106 
107  void
109  {
110  m_nfd.initialize();
111  }
112 
113  int
114  run()
115  {
120  std::atomic_int retval(0);
121 
122  boost::asio::io_service* const mainIo = &getGlobalIoService();
123  boost::asio::io_service* nrdIo = nullptr;
124 
125  // Mutex and conditional variable to implement synchronization between main and RIB manager
126  // threads:
127  // - to block main thread until RIB manager thread starts and initializes nrdIo (to allow
128  // stopping it later)
129  std::mutex m;
130  std::condition_variable cv;
131 
132  std::string configFile = this->m_configFile; // c++11 lambda cannot capture member variables
133  boost::thread nrdThread([configFile, &retval, &nrdIo, mainIo, &cv, &m] {
134  {
135  std::lock_guard<std::mutex> lock(m);
136  nrdIo = &getGlobalIoService();
137  BOOST_ASSERT(nrdIo != mainIo);
138  }
139  cv.notify_all(); // notify that nrdIo has been assigned
140 
141  try {
142  ndn::KeyChain nrdKeyChain;
143  // must be created inside a separate thread
144  rib::Nrd nrd(configFile, nrdKeyChain);
145  nrd.initialize();
146  getGlobalIoService().run(); // nrdIo is not thread-safe to use here
147  }
148  catch (const std::exception& e) {
149  NFD_LOG_FATAL(e.what());
150  retval = 6;
151  mainIo->stop();
152  }
153 
154  {
155  std::lock_guard<std::mutex> lock(m);
156  nrdIo = nullptr;
157  }
158  });
159 
160  {
161  // Wait to guarantee that nrdIo is properly initialized, so it can be used to terminate
162  // RIB manager thread.
163  std::unique_lock<std::mutex> lock(m);
164  cv.wait(lock, [&nrdIo] { return nrdIo != nullptr; });
165  }
166 
167  try {
168  mainIo->run();
169  }
170  catch (const std::exception& e) {
171  NFD_LOG_FATAL(e.what());
172  retval = 4;
173  }
174  catch (const PrivilegeHelper::Error& e) {
175  NFD_LOG_FATAL(e.what());
176  retval = 5;
177  }
178 
179  {
180  // nrdIo is guaranteed to be alive at this point
181  std::lock_guard<std::mutex> lock(m);
182  if (nrdIo != nullptr) {
183  nrdIo->stop();
184  nrdIo = nullptr;
185  }
186  }
187  nrdThread.join();
188 
189  return retval;
190  }
191 
192  void
193  terminate(const boost::system::error_code& error, int signalNo)
194  {
195  if (error)
196  return;
197 
198  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', exiting...");
199  getGlobalIoService().stop();
200  }
201 
202  void
203  reload(const boost::system::error_code& error, int signalNo)
204  {
205  if (error)
206  return;
207 
208  NFD_LOG_INFO("Caught signal '" << ::strsignal(signalNo) << "', reloading...");
209  m_nfd.reloadConfigFile();
210 
211  m_reloadSignalSet.async_wait(bind(&NfdRunner::reload, this, _1, _2));
212  }
213 
214 private:
215  ndn::KeyChain m_nfdKeyChain;
216  Nfd m_nfd;
217  std::string m_configFile;
218 
219  boost::asio::signal_set m_terminationSignalSet;
220  boost::asio::signal_set m_reloadSignalSet;
221 };
222 
223 } // namespace nfd
224 
225 int
226 main(int argc, char** argv)
227 {
228  using namespace nfd;
229 
230  namespace po = boost::program_options;
231 
232  po::options_description description;
233 
234  std::string configFile = DEFAULT_CONFIG_FILE;
235  description.add_options()
236  ("help,h", "print this help message")
237  ("version,V", "print version and exit")
238  ("modules,m", "list available logging modules")
239  ("config,c", po::value<std::string>(&configFile), "path to configuration file")
240  ;
241 
242  po::variables_map vm;
243  try {
244  po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
245  po::notify(vm);
246  }
247  catch (const std::exception& e) {
248  // avoid NFD_LOG_FATAL to ensure that errors related to command-line parsing always appear on the
249  // terminal and are not littered with timestamps and other things added by the logging subsystem
250  std::cerr << "ERROR: " << e.what() << std::endl;
251  NfdRunner::printUsage(std::cerr, argv[0]);
252  return 1;
253  }
254 
255  if (vm.count("help") > 0) {
256  NfdRunner::printUsage(std::cout, argv[0]);
257  return 0;
258  }
259 
260  if (vm.count("version") > 0) {
261  std::cout << NFD_VERSION_BUILD_STRING << std::endl;
262  return 0;
263  }
264 
265  if (vm.count("modules") > 0) {
266  NfdRunner::printModules(std::cout);
267  return 0;
268  }
269 
270  NfdRunner runner(configFile);
271 
272  try {
273  runner.initialize();
274  }
275  catch (const boost::filesystem::filesystem_error& e) {
276  if (e.code() == boost::system::errc::permission_denied) {
277  NFD_LOG_FATAL("Permissions denied for " << e.path1() << ". " <<
278  argv[0] << " should be run as superuser");
279  return 3;
280  }
281  else {
283  return 2;
284  }
285  }
286  catch (const std::exception& e) {
288  return 2;
289  }
290  catch (const PrivilegeHelper::Error& e) {
291  // PrivilegeHelper::Errors do not inherit from std::exception
292  // and represent seteuid/gid failures
293 
294  NFD_LOG_FATAL(e.what());
295  return 3;
296  }
297 
298  return runner.run();
299 }
static void printUsage(std::ostream &os, const std::string &programName)
Definition: main.cpp:81
void initialize()
Definition: main.cpp:108
std::string getExtendedErrorMessage(const E &exception)
represents a serious seteuid/gid failure
int main(int argc, char **argv)
Definition: main.cpp:226
static void printModules(std::ostream &os)
Definition: main.cpp:98
The packet signing interface.
Definition: key-chain.hpp:48
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
NfdRunner(const std::string &configFile)
Definition: main.cpp:66
Class representing NRD (NFD RIB Manager) instance This class can be used to initialize all components...
Definition: nrd.hpp:45
void reload(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:203
int run()
Definition: main.cpp:114
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
void reloadConfigFile()
Reload configuration file and apply update (if any)
Definition: nfd.cpp:203
Class representing NFD instance This class can be used to initialize all components of NFD...
Definition: nfd.hpp:60
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nfd.cpp:85
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
Executes NFD with RIB manager.
Definition: main.cpp:62
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nrd.cpp:63
#define NFD_LOG_FATAL(expression)
Definition: logger.hpp:59
void terminate(const boost::system::error_code &error, int signalNo)
Definition: main.cpp:193