blob: 1adc68fa944dcc6addfa7580bc33ce2055127eee [file] [log] [blame]
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -07001/* -*- 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 Chen478204c2014-03-18 18:27:04 -07007#include "config.hpp"
8#include "repo.hpp"
Shuo Chen9c2477f2014-03-13 15:01:06 -07009
10using namespace repo;
11
12static const string ndnRepoUsageMessage =
Shuo Chen478204c2014-03-18 18:27:04 -070013 /* argv[0] */ " - Next generation of NDN repository\n"
Shuo Chen9c2477f2014-03-13 15:01:06 -070014 "-h: show help message\n"
15 "-c: set config file path\n"
16 ;
17
Shuo Chen478204c2014-03-18 18:27:04 -070018void
19terminate(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 Afanasyev3fd14f02014-03-26 14:34:39 -070041int
Shuo Chen478204c2014-03-18 18:27:04 -070042main(int argc, char** argv)
43{
44 string configPath = DEFAULT_CONFIG_FILE;
Shuo Chen9c2477f2014-03-13 15:01:06 -070045 int opt;
Shuo Chen478204c2014-03-18 18:27:04 -070046 while ((opt = getopt(argc, argv, "hc:")) != -1) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070047 switch (opt) {
Shuo Chen9c2477f2014-03-13 15:01:06 -070048 case 'h':
Shuo Chen478204c2014-03-18 18:27:04 -070049 std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
Shuo Chen9c2477f2014-03-13 15:01:06 -070050 return 1;
51 case 'c':
Shuo Chen478204c2014-03-18 18:27:04 -070052 configPath = string(optarg);
Shuo Chen9c2477f2014-03-13 15:01:06 -070053 break;
54 default:
55 break;
56 }
57 }
Shuo Chen9c2477f2014-03-13 15:01:06 -070058
Shuo Chen478204c2014-03-18 18:27:04 -070059 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 Chen9c2477f2014-03-13 15:01:06 -070079 }
Shuo Chen29c77fe2014-03-18 11:29:41 -070080
Alexander Afanasyev3fd14f02014-03-26 14:34:39 -070081 return 0;
82}