blob: 767dc8fc5039d6dcb9fd16218a7ddfe430cfcf6a [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/*
Davide Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2018-2022, 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 Pesavento11904062022-04-14 22:33:28 -040024#include <iostream>
Davide Pesaventoe18d3682019-01-24 22:10:30 -050025#include <string.h> // for strsignal()
26
27#include <boost/asio/io_service.hpp>
28#include <boost/asio/signal_set.hpp>
29#include <boost/program_options.hpp>
weijia yuan3aa8d2b2018-03-06 15:35:57 -080030
31#include <ndn-cxx/util/logger.hpp>
32
33NDN_LOG_INIT(repo.main);
34
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070035int
Shuo Chen478204c2014-03-18 18:27:04 -070036main(int argc, char** argv)
37{
Davide Pesaventoe18d3682019-01-24 22:10:30 -050038 std::string configFile(DEFAULT_CONFIG_FILE);
39
40 namespace po = boost::program_options;
41 po::options_description optsDesc("Options");
42 optsDesc.add_options()
43 ("help,h", "print this help message and exit")
44 ("config-file,c", po::value<std::string>(&configFile)->default_value(configFile),
45 "path to configuration file")
46 ;
47
48 po::variables_map vm;
49 try {
50 po::store(po::parse_command_line(argc, argv, optsDesc), vm);
51 po::notify(vm);
52 }
53 catch (const po::error& e) {
54 std::cerr << "ERROR: " << e.what() << std::endl;
55 return 2;
56 }
57 catch (const boost::bad_any_cast& e) {
58 std::cerr << "ERROR: " << e.what() << std::endl;
59 return 2;
Shuo Chen9c2477f2014-03-13 15:01:06 -070060 }
Shuo Chen9c2477f2014-03-13 15:01:06 -070061
Davide Pesaventoe18d3682019-01-24 22:10:30 -050062 if (vm.count("help") != 0) {
63 std::cout << "Usage: " << argv[0] << " [options]\n"
64 << "\n"
65 << optsDesc;
66 return 0;
67 }
68
69 boost::asio::io_service ioService;
70
71 /// \todo reload config file on SIGHUP
72 boost::asio::signal_set signalSet(ioService, SIGINT, SIGTERM);
73 signalSet.async_wait([&ioService] (const boost::system::error_code& error, int signalNo) {
74 if (!error) {
75 NDN_LOG_FATAL("Exiting on signal " << signalNo << "/" << strsignal(signalNo));
76 ioService.stop();
77 }
78 });
79
Shuo Chen478204c2014-03-18 18:27:04 -070080 try {
Davide Pesaventoe18d3682019-01-24 22:10:30 -050081 repo::Repo repo(ioService, repo::parseConfig(configFile));
82 repo.initializeStorage();
83 repo.enableValidation();
84 repo.enableListening();
Shuo Chen478204c2014-03-18 18:27:04 -070085
86 ioService.run();
87 }
88 catch (const std::exception& e) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080089 NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
Davide Pesaventoe18d3682019-01-24 22:10:30 -050090 return 1;
Shuo Chen9c2477f2014-03-13 15:01:06 -070091 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070092
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070093 return 0;
94}