Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | e1e6f2a | 2014-04-25 11:28:12 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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" |
| 21 | #include "repo.hpp" |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 22 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 23 | static const std::string ndnRepoUsageMessage = |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 24 | /* argv[0] */ " - Next generation of NDN repository\n" |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 25 | "-h: show help message\n" |
| 26 | "-c: set config file path\n" |
| 27 | ; |
| 28 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 29 | void |
| 30 | terminate(boost::asio::io_service& ioService, |
| 31 | const boost::system::error_code& error, |
| 32 | int signalNo, |
| 33 | boost::asio::signal_set& signalSet) |
| 34 | { |
| 35 | if (error) |
| 36 | return; |
| 37 | |
| 38 | if (signalNo == SIGINT || |
| 39 | signalNo == SIGTERM) |
| 40 | { |
| 41 | ioService.stop(); |
| 42 | std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | /// \todo May be try to reload config file |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 47 | signalSet.async_wait(std::bind(&terminate, std::ref(ioService), |
| 48 | std::placeholders::_1, std::placeholders::_2, |
| 49 | std::ref(signalSet))); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 53 | int |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 54 | main(int argc, char** argv) |
| 55 | { |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 56 | std::string configPath = DEFAULT_CONFIG_FILE; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 57 | int opt; |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 58 | while ((opt = getopt(argc, argv, "hc:")) != -1) { |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 59 | switch (opt) { |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 60 | case 'h': |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 61 | std::cout << argv[0] << ndnRepoUsageMessage << std::endl; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 62 | return 1; |
| 63 | case 'c': |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 64 | configPath = std::string(optarg); |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 65 | break; |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | } |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 70 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 71 | try { |
| 72 | boost::asio::io_service ioService; |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 73 | repo::Repo repoInstance(ioService, repo::parseConfig(configPath)); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 74 | |
| 75 | boost::asio::signal_set signalSet(ioService); |
| 76 | signalSet.add(SIGINT); |
| 77 | signalSet.add(SIGTERM); |
| 78 | signalSet.add(SIGHUP); |
| 79 | signalSet.add(SIGUSR1); |
| 80 | signalSet.add(SIGUSR2); |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 81 | signalSet.async_wait(std::bind(&terminate, std::ref(ioService), |
| 82 | std::placeholders::_1, std::placeholders::_2, |
| 83 | std::ref(signalSet))); |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 84 | |
Shuo Chen | a12f528 | 2014-08-01 15:18:30 +0800 | [diff] [blame] | 85 | repoInstance.initializeStorage(); |
| 86 | |
Shuo Chen | 028dcd3 | 2014-06-21 16:36:44 +0800 | [diff] [blame] | 87 | repoInstance.enableValidation(); |
| 88 | |
Shuo Chen | 478204c | 2014-03-18 18:27:04 -0700 | [diff] [blame] | 89 | repoInstance.enableListening(); |
| 90 | |
| 91 | ioService.run(); |
| 92 | } |
| 93 | catch (const std::exception& e) { |
| 94 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 95 | return 2; |
Shuo Chen | 9c2477f | 2014-03-13 15:01:06 -0700 | [diff] [blame] | 96 | } |
Shuo Chen | 29c77fe | 2014-03-18 11:29:41 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 3fd14f0 | 2014-03-26 14:34:39 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |