main: fix error handling and normalize exit codes
Change-Id: I3b6d6dd1f5a8fac3ec4998bd81af80a232350337
diff --git a/src/main.cpp b/src/main.cpp
index abe2b05..a2b9924 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2019, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
@@ -26,7 +26,7 @@
#include <sstream>
template<typename E>
-std::string
+static std::string
getExtendedErrorMessage(const E& exception)
{
std::ostringstream errorMessage;
@@ -46,41 +46,55 @@
return errorMessage.str();
}
-int
-main(int32_t argc, char** argv)
+static void
+printUsage(std::ostream& os, const std::string& programName)
{
- using namespace nlsr;
+ os << "Usage: " << programName << " [OPTIONS...]\n"
+ << "\n"
+ << "Options:\n"
+ << " -f <FILE> Path to configuration file\n"
+ << " -h Display this help message\n"
+ << " -V Display version information\n"
+ << std::endl;
+}
+int
+main(int argc, char** argv)
+{
std::string programName(argv[0]);
+ std::string configFileName("nlsr.conf");
- std::string configFileName = "nlsr.conf";
-
- int32_t opt;
- while ((opt = getopt(argc, argv, "df:hV")) != -1) {
+ int opt;
+ while ((opt = getopt(argc, argv, "hf:V")) != -1) {
switch (opt) {
- case 'f':
- configFileName = optarg;
- break;
- case 'V':
- std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
- return EXIT_SUCCESS;
- break;
- case 'h':
- default:
- NlsrRunner::printUsage(programName);
- return EXIT_FAILURE;
+ case 'h':
+ printUsage(std::cout, programName);
+ return 0;
+ case 'f':
+ configFileName = optarg;
+ break;
+ case 'V':
+ std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
+ return 0;
+ default:
+ printUsage(std::cerr, programName);
+ return 2;
}
}
- NlsrRunner runner(configFileName);
+ nlsr::NlsrRunner runner(configFileName);
try {
runner.run();
}
+ catch (const nlsr::NlsrRunner::ConfFileError& e) {
+ std::cerr << e.what() << std::endl;
+ return 2;
+ }
catch (const std::exception& e) {
- std::cerr << getExtendedErrorMessage(e) << std::endl;
- return EXIT_FAILURE;
+ std::cerr << "FATAL: " << getExtendedErrorMessage(e) << std::endl;
+ return 1;
}
- return EXIT_SUCCESS;
+ return 0;
}
diff --git a/src/nlsr-runner.cpp b/src/nlsr-runner.cpp
index 7f27f45..57066e9 100644
--- a/src/nlsr-runner.cpp
+++ b/src/nlsr-runner.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2019, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
@@ -21,13 +21,10 @@
#include "nlsr-runner.hpp"
#include "conf-file-processor.hpp"
-#include "logger.hpp"
namespace nlsr {
-INIT_LOGGER(NlsrRunner);
-
-NlsrRunner::NlsrRunner(std::string& configFileName)
+NlsrRunner::NlsrRunner(const std::string& configFileName)
: m_scheduler(m_ioService)
, m_face(m_ioService)
, m_nlsr(m_ioService, m_scheduler, m_face, m_keyChain)
@@ -41,41 +38,26 @@
ConfFileProcessor configProcessor(m_nlsr, m_nlsr.getConfFileName());
if (!configProcessor.processConfFile()) {
- BOOST_THROW_EXCEPTION(Error("Error in configuration file processing! Exiting from NLSR"));
+ BOOST_THROW_EXCEPTION(ConfFileError("Error in configuration file processing"));
}
- /** Because URI canonization needs to occur before initialization,
- we have to pass initialize as the finally() in neighbor
- canonization.
- */
+ // Because URI canonization needs to occur before initialization,
+ // we have to pass initialize as the finally() in neighbor canonization.
m_nlsr.canonizeNeighborUris(m_nlsr.getAdjacencyList().getAdjList().begin(),
[this] (std::list<Adjacent>::iterator iterator) {
- m_nlsr.canonizeContinuation(iterator, [this] {
- m_nlsr.initialize();
- });
+ m_nlsr.canonizeContinuation(iterator, [this] { m_nlsr.initialize(); });
},
[this] {
m_nlsr.initialize();
});
+
try {
m_nlsr.startEventLoop();
}
- catch (const std::exception& e) {
- NLSR_LOG_FATAL("ERROR: " << e.what());
- std::cerr << "ERROR: " << e.what() << std::endl;
-
+ catch (...) {
m_nlsr.getFib().clean();
+ throw;
}
}
-void
-NlsrRunner::printUsage(const std::string& programName)
-{
- std::cout << "Usage: " << programName << " [OPTIONS...]" << std::endl;
- std::cout << " NDN routing...." << std::endl;
- std::cout << " -f <FILE> Specify configuration file name" << std::endl;
- std::cout << " -V Display version information" << std::endl;
- std::cout << " -h Display this help message" << std::endl;
-}
-
} // namespace nlsr
diff --git a/src/nlsr-runner.hpp b/src/nlsr-runner.hpp
index 76d0456..af32e1e 100644
--- a/src/nlsr-runner.hpp
+++ b/src/nlsr-runner.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018, The University of Memphis,
+/*
+ * Copyright (c) 2014-2019, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
@@ -28,7 +28,7 @@
#include <ndn-cxx/util/scheduler.hpp>
// boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ...
-#include <boost/asio.hpp>
+#include <boost/asio/io_service.hpp>
namespace nlsr {
@@ -40,22 +40,17 @@
* This class only exists to provide this functionality, and there is
* no special reliance of NLSR on this class.
*/
-
class NlsrRunner
{
public:
- class Error : public std::runtime_error
+ class ConfFileError : public std::invalid_argument
{
public:
- explicit
- Error(const std::string& what)
- : std::runtime_error(what)
- {
- }
+ using std::invalid_argument::invalid_argument;
};
explicit
- NlsrRunner(std::string& configFileName);
+ NlsrRunner(const std::string& configFileName);
/*! \brief Instantiate, configure, and start the NLSR process.
*
@@ -64,17 +59,13 @@
* relationship. If one wants to create multiple NLSR classes,
* multiple NLSR runners need to be created, too.
*
- * \throws Error If the configuration file cannot be processed. NLSR
- * is not started.
- *
+ * \throws ConfFileError The configuration file cannot be processed.
+ * NLSR is not started.
* \sa Nlsr::canonizeNeighborUris
*/
void
run();
- static void
- printUsage(const std::string& programName);
-
private:
boost::asio::io_service m_ioService;
ndn::Scheduler m_scheduler;