Refactor initialization of NLSR instance
refs: #3091
Change-Id: I07108015dc0704e85f5c37b91b53838d7b0f41d0
diff --git a/src/main.cpp b/src/main.cpp
index a381737..93595c6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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 @@
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);
-}
\ No newline at end of file