blob: d1095822265714cefe5b397900a7518c1fe038a5 [file] [log] [blame]
Yingdi Yu40cd1c32014-04-17 15:02:17 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe3c741d2019-01-29 20:28:15 -05002/*
3 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehman9dce0c92015-02-09 12:53:41 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Yingdi Yu40cd1c32014-04-17 15:02:17 -07006 *
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 **/
Alexander Afanasyevb669f9c2014-11-14 12:41:54 -080021
Ashlesh Gawande85998a12017-12-07 22:22:13 -060022#include "conf-file-processor.hpp"
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050023#include "nlsr.hpp"
Vince Lehmanb722b102014-08-24 16:33:49 -050024#include "version.hpp"
Yingdi Yu40cd1c32014-04-17 15:02:17 -070025
Alexander Afanasyev7afe22f2018-02-13 16:17:40 -050026#include <boost/exception/get_error_info.hpp>
27#include <sstream>
28
29template<typename E>
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050030static std::string
Alexander Afanasyev7afe22f2018-02-13 16:17:40 -050031getExtendedErrorMessage(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
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050050static void
51printUsage(std::ostream& os, const std::string& programName)
Yingdi Yu40cd1c32014-04-17 15:02:17 -070052{
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050053 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}
Vince Lehman7c603292014-09-11 17:48:16 -050061
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050062int
63main(int argc, char** argv)
64{
akmhoquefdbddb12014-05-02 18:35:19 -050065 std::string programName(argv[0]);
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050066 std::string configFileName("nlsr.conf");
Vince Lehmanc57c64b2015-08-10 12:21:31 -050067
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050068 int opt;
69 while ((opt = getopt(argc, argv, "hf:V")) != -1) {
Vince Lehmanc57c64b2015-08-10 12:21:31 -050070 switch (opt) {
Davide Pesaventoe3c741d2019-01-29 20:28:15 -050071 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;
Vince Lehmanc57c64b2015-08-10 12:21:31 -050083 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -070084 }
Muktadir R Chowdhurybfa27602014-10-31 10:57:41 -050085
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086 boost::asio::io_service ioService;
87 ndn::Face face(ioService);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050088 ndn::KeyChain keyChain;
Ashlesh Gawande85998a12017-12-07 22:22:13 -060089
90 nlsr::ConfParameter confParam(face, configFileName);
91 nlsr::ConfFileProcessor configProcessor(confParam);
92
93 if (!configProcessor.processConfFile()) {
94 std::cerr << "Error in configuration file processing" << std::endl;
95 return 2;
96 }
97
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050098 confParam.buildRouterAndSyncUserPrefix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -060099 confParam.writeLog();
100
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500101 nlsr::Nlsr nlsr(face, keyChain, confParam);
Vince Lehmanc57c64b2015-08-10 12:21:31 -0500102
103 try {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500104 face.processEvents();
Vince Lehmanc57c64b2015-08-10 12:21:31 -0500105 }
106 catch (const std::exception& e) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500107 nlsr.getFib().clean();
Davide Pesaventoe3c741d2019-01-29 20:28:15 -0500108 std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl;
109 return 1;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700110 }
Vince Lehmanf99b87f2014-08-26 15:54:27 -0500111
Davide Pesaventoe3c741d2019-01-29 20:28:15 -0500112 return 0;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700113}