main.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  **/
21 
22 #include "conf-file-processor.hpp"
23 #include "nlsr-runner.hpp"
24 #include "version.hpp"
25 
26 #include <boost/exception/get_error_info.hpp>
27 #include <sstream>
28 
29 template<typename E>
30 static std::string
31 getExtendedErrorMessage(const E& exception)
32 {
33  std::ostringstream errorMessage;
34  errorMessage << exception.what();
35 
36  const char* const* file = boost::get_error_info<boost::throw_file>(exception);
37  const int* line = boost::get_error_info<boost::throw_line>(exception);
38  const char* const* func = boost::get_error_info<boost::throw_function>(exception);
39  if (file && line) {
40  errorMessage << " [from " << *file << ":" << *line;
41  if (func) {
42  errorMessage << " in " << *func;
43  }
44  errorMessage << "]";
45  }
46 
47  return errorMessage.str();
48 }
49 
50 static void
51 printUsage(std::ostream& os, const std::string& programName)
52 {
53  os << "Usage: " << programName << " [OPTIONS...]\n"
54  << "\n"
55  << "Options:\n"
56  << " -f <FILE> Path to configuration file\n"
57  << " -h Display this help message\n"
58  << " -V Display version information\n"
59  << std::endl;
60 }
61 
62 int
63 main(int argc, char** argv)
64 {
65  std::string programName(argv[0]);
66  std::string configFileName("nlsr.conf");
67 
68  int opt;
69  while ((opt = getopt(argc, argv, "hf:V")) != -1) {
70  switch (opt) {
71  case 'h':
72  printUsage(std::cout, programName);
73  return 0;
74  case 'f':
75  configFileName = optarg;
76  break;
77  case 'V':
78  std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
79  return 0;
80  default:
81  printUsage(std::cerr, programName);
82  return 2;
83  }
84  }
85 
86  boost::asio::io_service ioService;
87  ndn::Face face(ioService);
88 
89  nlsr::ConfParameter confParam(face, configFileName);
90  nlsr::ConfFileProcessor configProcessor(confParam);
91 
92  if (!configProcessor.processConfFile()) {
93  std::cerr << "Error in configuration file processing" << std::endl;
94  return 2;
95  }
96 
97  confParam.buildRouterPrefix();
98  confParam.writeLog();
99 
100  nlsr::NlsrRunner runner(face, confParam);
101 
102  try {
103  runner.run();
104  }
105  catch (const std::exception& e) {
106  std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl;
107  return 1;
108  }
109 
110  return 0;
111 }
A class to house all the configuration parameters for NLSR.
A wrapper class to instantiate and configure an NLSR object.
Definition: nlsr-runner.hpp:41
int main(int argc, char **argv)
Definition: main.cpp:63
static void printUsage(std::ostream &os, const std::string &programName)
Definition: main.cpp:51
static std::string getExtendedErrorMessage(const E &exception)
Definition: main.cpp:31
A class containing methods to parse an NLSR configuration file.
void run()
Instantiate, configure, and start the NLSR process.
Definition: nlsr-runner.cpp:34
bool processConfFile()
Load and parse the configuration file, then populate NLSR.
void writeLog()
Dump the current state of all attributes to the log.