blob: e69c32d8afc9f912e6738e3bac1c8d5dc445d8f1 [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve1e6f2a2014-04-25 11:28:12 -07003 * 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 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
23using namespace repo;
24
25static const string ndnRepoUsageMessage =
Shuo Chen478204c2014-03-18 18:27:04 -070026 /* argv[0] */ " - Next generation of NDN repository\n"
Shuo Chen9c2477f2014-03-13 15:01:06 -070027 "-h: show help message\n"
28 "-c: set config file path\n"
29 ;
30
Shuo Chen478204c2014-03-18 18:27:04 -070031void
32terminate(boost::asio::io_service& ioService,
33 const boost::system::error_code& error,
34 int signalNo,
35 boost::asio::signal_set& signalSet)
36{
37 if (error)
38 return;
39
40 if (signalNo == SIGINT ||
41 signalNo == SIGTERM)
42 {
43 ioService.stop();
44 std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl;
45 }
46 else
47 {
48 /// \todo May be try to reload config file
49 signalSet.async_wait(bind(&terminate, boost::ref(ioService), _1, _2,
50 boost::ref(signalSet)));
51 }
52}
53
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070054int
Shuo Chen478204c2014-03-18 18:27:04 -070055main(int argc, char** argv)
56{
57 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':
Shuo Chen478204c2014-03-18 18:27:04 -070065 configPath = 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;
74 Repo repoInstance(ioService, parseConfig(configPath));
75
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);
82 signalSet.async_wait(bind(&terminate, boost::ref(ioService), _1, _2,
83 boost::ref(signalSet)));
84
85 repoInstance.enableListening();
86
87 ioService.run();
88 }
89 catch (const std::exception& e) {
90 std::cerr << "ERROR: " << e.what() << std::endl;
91 return 2;
Shuo Chen9c2477f2014-03-13 15:01:06 -070092 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070093
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070094 return 0;
95}