Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2018-2019, Regents of the University of California. |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 20 | #include "config.hpp" |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 21 | #include "extended-error-message.hpp" |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 22 | #include "repo.hpp" |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 24 | #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 yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 29 | |
| 30 | #include <ndn-cxx/util/logger.hpp> |
| 31 | |
| 32 | NDN_LOG_INIT(repo.main); |
| 33 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 34 | int |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 35 | main(int argc, char** argv) |
| 36 | { |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 37 | 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 Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 59 | } |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 60 | |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 61 | 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 Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 79 | try { |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 80 | repo::Repo repo(ioService, repo::parseConfig(configFile)); |
| 81 | repo.initializeStorage(); |
| 82 | repo.enableValidation(); |
| 83 | repo.enableListening(); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 84 | |
| 85 | ioService.run(); |
| 86 | } |
| 87 | catch (const std::exception& e) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 88 | NDN_LOG_FATAL(repo::getExtendedErrorMessage(e)); |
Davide Pesavento | e18d368 | 2019-01-24 22:10:30 -0500 | [diff] [blame] | 89 | return 1; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 90 | } |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 92 | return 0; |
| 93 | } |