Refactor initialization of NLSR instance

refs: #3091

Change-Id: I07108015dc0704e85f5c37b91b53838d7b0f41d0
This commit is contained in:
Vince Lehman committed 2015-08-11 11:55:29 -05:00
1 parent 97473d47fd
commit c57c64b633
5 files changed
+169 -72

No files matched your search

+18 -57
View File
@@ -19,39 +19,27 @@
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include <ndn-cxx/util/scheduler.hpp>
#include "conf-file-processor.hpp"
#include "logger.hpp"
#include "nlsr.hpp"
#include "nlsr-runner.hpp"
#include "version.hpp"
// boost needs to be included after ndn-cxx, otherwise there will be conflict with _1, _2, ...
#include <boost/asio.hpp>
#include <boost/cstdint.hpp>
namespace nlsr {
int
main(int32_t argc, char** argv)
{
boost::asio::io_service ioService;
ndn::Scheduler scheduler(ioService);
ndn::Face face(ioService);
Nlsr nlsr(ioService, scheduler, face);
using namespace nlsr;
std::string programName(argv[0]);
nlsr.setConfFileName("nlsr.conf");
std::string configFileName = "nlsr.conf";
bool isDaemonProcess = false;
int32_t opt;
while ((opt = getopt(argc, argv, "df:hV")) != -1) {
switch (opt)
{
switch (opt) {
case 'f':
nlsr.setConfFileName(optarg);
configFileName = optarg;
break;
case 'd':
nlsr.setIsDaemonProcess(true);
isDaemonProcess = true;
break;
case 'V':
std::cout << NLSR_VERSION_BUILD_STRING << std::endl;
@@ -59,47 +47,20 @@ main(int32_t argc, char** argv)
break;
case 'h':
default:
nlsr.usage(programName);
NlsrRunner::printUsage(programName);
return EXIT_FAILURE;
}
}
}
ConfFileProcessor cfp(nlsr, nlsr.getConfFileName());
if (!cfp.processConfFile()) {
std::cerr << "Error in configuration file processing! Exiting from NLSR" << std::endl;
NlsrRunner runner(configFileName, isDaemonProcess);
try {
runner.run();
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
if (nlsr.getConfParameter().isLog4CxxConfAvailable()) {
INIT_LOG4CXX(nlsr.getConfParameter().getLog4CxxConfPath());
}
else {
INIT_LOGGERS(nlsr.getConfParameter().getLogDir(), nlsr.getConfParameter().getLogLevel());
}
INIT_LOGGER("Main");
nlsr.initialize();
if (nlsr.getIsSetDaemonProcess()) {
nlsr.daemonize();
}
try {
nlsr.startEventLoop();
}
catch (std::exception& e) {
_LOG_FATAL("ERROR: " << e.what());
std::cerr << "ERROR: " << e.what() << std::endl;
nlsr.getFib().clean();
nlsr.destroyFaces();
}
return EXIT_SUCCESS;
}
} // namespace nlsr
int
main(int32_t argc, char** argv)
{
return nlsr::main(argc, argv);
}
+85
View File
@@ -0,0 +1,85 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2015, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
* This file is part of NLSR (Named-data Link State Routing).
* See AUTHORS.md for complete list of NLSR authors and contributors.
*
* NLSR is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#include "nlsr-runner.hpp"
#include "conf-file-processor.hpp"
#include "logger.hpp"
namespace nlsr {
INIT_LOGGER("NlsrRunner");
NlsrRunner::NlsrRunner(std::string& configFileName, bool isDaemonProcess)
: m_scheduler(m_ioService)
, m_face(m_ioService)
, m_nlsr(m_ioService, m_scheduler, m_face)
{
m_nlsr.setConfFileName(configFileName);
m_nlsr.setIsDaemonProcess(isDaemonProcess);
}
void
NlsrRunner::run()
{
ConfFileProcessor configProcessor(m_nlsr, m_nlsr.getConfFileName());
if (!configProcessor.processConfFile()) {
throw Error("Error in configuration file processing! Exiting from NLSR");
}
if (m_nlsr.getConfParameter().isLog4CxxConfAvailable()) {
INIT_LOG4CXX(m_nlsr.getConfParameter().getLog4CxxConfPath());
}
else {
INIT_LOGGERS(m_nlsr.getConfParameter().getLogDir(), m_nlsr.getConfParameter().getLogLevel());
}
m_nlsr.initialize();
if (m_nlsr.getIsSetDaemonProcess()) {
m_nlsr.daemonize();
}
try {
m_nlsr.startEventLoop();
}
catch (std::exception& e) {
_LOG_FATAL("ERROR: " << e.what());
std::cerr << "ERROR: " << e.what() << std::endl;
m_nlsr.getFib().clean();
m_nlsr.destroyFaces();
}
}
void
NlsrRunner::printUsage(const std::string& programName)
{
std::cout << "Usage: " << programName << " [OPTIONS...]" << std::endl;
std::cout << " NDN routing...." << std::endl;
std::cout << " -d Run in daemon mode" << 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
+66
View File
@@ -0,0 +1,66 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014-2015, The University of Memphis,
* Regents of the University of California,
* Arizona Board of Regents.
*
* This file is part of NLSR (Named-data Link State Routing).
* See AUTHORS.md for complete list of NLSR authors and contributors.
*
* NLSR is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef NLSR_NLSR_RUNNER_HPP
#define NLSR_NLSR_RUNNER_HPP
#include "nlsr.hpp"
#include <ndn-cxx/face.hpp>
#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>
namespace nlsr {
class NlsrRunner
{
public:
class Error : public std::runtime_error
{
public:
explicit
Error(const std::string& what)
: std::runtime_error(what)
{
}
};
NlsrRunner(std::string& configFileName, bool isDaemonProcess);
void
run();
static void
printUsage(const std::string& programName);
private:
boost::asio::io_service m_ioService;
ndn::Scheduler m_scheduler;
ndn::Face m_face;
Nlsr m_nlsr;
};
} // namespace nlsr
#endif // NLSR_NLSR_RUNNER_HPP
-12
View File
@@ -387,16 +387,4 @@ Nlsr::startEventLoop()
m_nlsrFace.processEvents();
}
void
Nlsr::usage(const string& progname)
{
std::cout << "Usage: " << progname << " [OPTIONS...]" << std::endl;
std::cout << " NDN routing...." << std::endl;
std::cout << " -d Run in daemon mode" << 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
-3
View File
@@ -88,9 +88,6 @@ public:
void
startEventLoop();
void
usage(const std::string& progname);
std::string
getConfFileName() const
{