blob: 622975948c50614e2e9cbd3ec92d6a6b9b4233fc [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe18d3682019-01-24 22:10:30 -05002/*
3 * Copyright (c) 2018-2019, Regents of the University of California.
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070018 */
19
Shuo Chen478204c2014-03-18 18:27:04 -070020#include "config.hpp"
Davide Pesaventoe18d3682019-01-24 22:10:30 -050021#include "extended-error-message.hpp"
Shuo Chen478204c2014-03-18 18:27:04 -070022#include "repo.hpp"
Shuo Chen9c2477f2014-03-13 15:01:06 -070023
Davide Pesaventoe18d3682019-01-24 22:10:30 -050024#include <string.h> // for strsignal()
25
26#include <boost/asio/io_service.hpp>
27#include <boost/asio/signal_set.hpp>
28#include <boost/program_options.hpp>
weijia yuan3aa8d2b2018-03-06 15:35:57 -080029
30#include <ndn-cxx/util/logger.hpp>
31
32NDN_LOG_INIT(repo.main);
33
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070034int
Shuo Chen478204c2014-03-18 18:27:04 -070035main(int argc, char** argv)
36{
Davide Pesaventoe18d3682019-01-24 22:10:30 -050037 std::string configFile(DEFAULT_CONFIG_FILE);
38
39 namespace po = boost::program_options;
40 po::options_description optsDesc("Options");
41 optsDesc.add_options()
42 ("help,h", "print this help message and exit")
43 ("config-file,c", po::value<std::string>(&configFile)->default_value(configFile),
44 "path to configuration file")
45 ;
46
47 po::variables_map vm;
48 try {
49 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
50 po::notify(vm);
51 }
52 catch (const po::error& e) {
53 std::cerr << "ERROR: " << e.what() << std::endl;
54 return 2;
55 }
56 catch (const boost::bad_any_cast& e) {
57 std::cerr << "ERROR: " << e.what() << std::endl;
58 return 2;
Shuo Chen9c2477f2014-03-13 15:01:06 -070059 }
Shuo Chen9c2477f2014-03-13 15:01:06 -070060
Davide Pesaventoe18d3682019-01-24 22:10:30 -050061 if (vm.count("help") != 0) {
62 std::cout << "Usage: " << argv[0] << " [options]\n"
63 << "\n"
64 << optsDesc;
65 return 0;
66 }
67
68 boost::asio::io_service ioService;
69
70 /// \todo reload config file on SIGHUP
71 boost::asio::signal_set signalSet(ioService, SIGINT, SIGTERM);
72 signalSet.async_wait([&ioService] (const boost::system::error_code& error, int signalNo) {
73 if (!error) {
74 NDN_LOG_FATAL("Exiting on signal " << signalNo << "/" << strsignal(signalNo));
75 ioService.stop();
76 }
77 });
78
Shuo Chen478204c2014-03-18 18:27:04 -070079 try {
Davide Pesaventoe18d3682019-01-24 22:10:30 -050080 repo::Repo repo(ioService, repo::parseConfig(configFile));
81 repo.initializeStorage();
82 repo.enableValidation();
83 repo.enableListening();
Shuo Chen478204c2014-03-18 18:27:04 -070084
85 ioService.run();
86 }
87 catch (const std::exception& e) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080088 NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
Davide Pesaventoe18d3682019-01-24 22:10:30 -050089 return 1;
Shuo Chen9c2477f2014-03-13 15:01:06 -070090 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070091
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070092 return 0;
93}