blob: 800b8c78740f4f5553ddab79ccfb364637baa7f5 [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
weijia yuan3aa8d2b2018-03-06 15:35:57 -08003 * Copyright (c) 2018, 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"
21#include "repo.hpp"
Shuo Chen9c2477f2014-03-13 15:01:06 -070022
weijia yuan3aa8d2b2018-03-06 15:35:57 -080023#include "extended-error-message.hpp"
24
25#include <ndn-cxx/util/logger.hpp>
26
27NDN_LOG_INIT(repo.main);
28
Wentao Shanga8f3c402014-10-30 14:03:27 -070029static const std::string ndnRepoUsageMessage =
Shuo Chen478204c2014-03-18 18:27:04 -070030 /* argv[0] */ " - Next generation of NDN repository\n"
Shuo Chen9c2477f2014-03-13 15:01:06 -070031 "-h: show help message\n"
32 "-c: set config file path\n"
33 ;
34
Shuo Chen478204c2014-03-18 18:27:04 -070035void
36terminate(boost::asio::io_service& ioService,
37 const boost::system::error_code& error,
38 int signalNo,
39 boost::asio::signal_set& signalSet)
40{
41 if (error)
42 return;
43
weijia yuan3aa8d2b2018-03-06 15:35:57 -080044 if (signalNo == SIGINT || signalNo == SIGTERM) {
45 ioService.stop();
46 std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl;
47 }
48 else {
49 /// \todo try to reload config file
50 signalSet.async_wait(std::bind(&terminate, std::ref(ioService), _1, _2, std::ref(signalSet)));
51 }
Shuo Chen478204c2014-03-18 18:27:04 -070052}
53
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070054int
Shuo Chen478204c2014-03-18 18:27:04 -070055main(int argc, char** argv)
56{
Wentao Shanga8f3c402014-10-30 14:03:27 -070057 std::string configPath = DEFAULT_CONFIG_FILE;
Shuo Chen9c2477f2014-03-13 15:01:06 -070058 int opt;
Shuo Chen478204c2014-03-18 18:27:04 -070059 while ((opt = getopt(argc, argv, "hc:")) != -1) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070060 switch (opt) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070061 case 'h':
Shuo Chen478204c2014-03-18 18:27:04 -070062 std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
Shuo Chen9c2477f2014-03-13 15:01:06 -070063 return 1;
64 case 'c':
Wentao Shanga8f3c402014-10-30 14:03:27 -070065 configPath = std::string(optarg);
Shuo Chen9c2477f2014-03-13 15:01:06 -070066 break;
67 default:
68 break;
69 }
70 }
Shuo Chen9c2477f2014-03-13 15:01:06 -070071
Shuo Chen478204c2014-03-18 18:27:04 -070072 try {
73 boost::asio::io_service ioService;
Wentao Shanga8f3c402014-10-30 14:03:27 -070074 repo::Repo repoInstance(ioService, repo::parseConfig(configPath));
Shuo Chen478204c2014-03-18 18:27:04 -070075
76 boost::asio::signal_set signalSet(ioService);
77 signalSet.add(SIGINT);
78 signalSet.add(SIGTERM);
79 signalSet.add(SIGHUP);
80 signalSet.add(SIGUSR1);
81 signalSet.add(SIGUSR2);
Wentao Shanga8f3c402014-10-30 14:03:27 -070082 signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
83 std::placeholders::_1, std::placeholders::_2,
84 std::ref(signalSet)));
Shuo Chen478204c2014-03-18 18:27:04 -070085
Shuo Chena12f5282014-08-01 15:18:30 +080086 repoInstance.initializeStorage();
87
Shuo Chen028dcd32014-06-21 16:36:44 +080088 repoInstance.enableValidation();
89
Shuo Chen478204c2014-03-18 18:27:04 -070090 repoInstance.enableListening();
91
92 ioService.run();
93 }
94 catch (const std::exception& e) {
weijia yuan3aa8d2b2018-03-06 15:35:57 -080095 NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
Shuo Chen478204c2014-03-18 18:27:04 -070096 return 2;
Shuo Chen9c2477f2014-03-13 15:01:06 -070097 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070098
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070099 return 0;
100}