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