blob: 9713943f20fa481c92db4c3d854ddcddddc72b90 [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
Wentao Shanga8f3c402014-10-30 14:03:27 -070023static const std::string ndnRepoUsageMessage =
Shuo Chen478204c2014-03-18 18:27:04 -070024 /* argv[0] */ " - Next generation of NDN repository\n"
Shuo Chen9c2477f2014-03-13 15:01:06 -070025 "-h: show help message\n"
26 "-c: set config file path\n"
27 ;
28
Shuo Chen478204c2014-03-18 18:27:04 -070029void
30terminate(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 Shanga8f3c402014-10-30 14:03:27 -070047 signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
48 std::placeholders::_1, std::placeholders::_2,
49 std::ref(signalSet)));
Shuo Chen478204c2014-03-18 18:27:04 -070050 }
51}
52
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070053int
Shuo Chen478204c2014-03-18 18:27:04 -070054main(int argc, char** argv)
55{
Wentao Shanga8f3c402014-10-30 14:03:27 -070056 std::string configPath = DEFAULT_CONFIG_FILE;
Shuo Chen9c2477f2014-03-13 15:01:06 -070057 int opt;
Shuo Chen478204c2014-03-18 18:27:04 -070058 while ((opt = getopt(argc, argv, "hc:")) != -1) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070059 switch (opt) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070060 case 'h':
Shuo Chen478204c2014-03-18 18:27:04 -070061 std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
Shuo Chen9c2477f2014-03-13 15:01:06 -070062 return 1;
63 case 'c':
Wentao Shanga8f3c402014-10-30 14:03:27 -070064 configPath = std::string(optarg);
Shuo Chen9c2477f2014-03-13 15:01:06 -070065 break;
66 default:
67 break;
68 }
69 }
Shuo Chen9c2477f2014-03-13 15:01:06 -070070
Shuo Chen478204c2014-03-18 18:27:04 -070071 try {
72 boost::asio::io_service ioService;
Wentao Shanga8f3c402014-10-30 14:03:27 -070073 repo::Repo repoInstance(ioService, repo::parseConfig(configPath));
Shuo Chen478204c2014-03-18 18:27:04 -070074
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 Shanga8f3c402014-10-30 14:03:27 -070081 signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
82 std::placeholders::_1, std::placeholders::_2,
83 std::ref(signalSet)));
Shuo Chen478204c2014-03-18 18:27:04 -070084
Shuo Chena12f5282014-08-01 15:18:30 +080085 repoInstance.initializeStorage();
86
Shuo Chen028dcd32014-06-21 16:36:44 +080087 repoInstance.enableValidation();
88
Shuo Chen478204c2014-03-18 18:27:04 -070089 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 Chen9c2477f2014-03-13 15:01:06 -070096 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070097
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070098 return 0;
99}