blob: 800b8c78740f4f5553ddab79ccfb364637baa7f5 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2018, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
*
* repo-ng is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.hpp"
#include "repo.hpp"
#include "extended-error-message.hpp"
#include <ndn-cxx/util/logger.hpp>
NDN_LOG_INIT(repo.main);
static const std::string ndnRepoUsageMessage =
/* argv[0] */ " - Next generation of NDN repository\n"
"-h: show help message\n"
"-c: set config file path\n"
;
void
terminate(boost::asio::io_service& ioService,
const boost::system::error_code& error,
int signalNo,
boost::asio::signal_set& signalSet)
{
if (error)
return;
if (signalNo == SIGINT || signalNo == SIGTERM) {
ioService.stop();
std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl;
}
else {
/// \todo try to reload config file
signalSet.async_wait(std::bind(&terminate, std::ref(ioService), _1, _2, std::ref(signalSet)));
}
}
int
main(int argc, char** argv)
{
std::string configPath = DEFAULT_CONFIG_FILE;
int opt;
while ((opt = getopt(argc, argv, "hc:")) != -1) {
switch (opt) {
case 'h':
std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
return 1;
case 'c':
configPath = std::string(optarg);
break;
default:
break;
}
}
try {
boost::asio::io_service ioService;
repo::Repo repoInstance(ioService, repo::parseConfig(configPath));
boost::asio::signal_set signalSet(ioService);
signalSet.add(SIGINT);
signalSet.add(SIGTERM);
signalSet.add(SIGHUP);
signalSet.add(SIGUSR1);
signalSet.add(SIGUSR2);
signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
std::placeholders::_1, std::placeholders::_2,
std::ref(signalSet)));
repoInstance.initializeStorage();
repoInstance.enableValidation();
repoInstance.enableListening();
ioService.run();
}
catch (const std::exception& e) {
NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
return 2;
}
return 0;
}