29 #include "core/version.hpp"
37 #include <boost/filesystem.hpp>
38 #include <boost/program_options/options_description.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 #include <boost/program_options/parsers.hpp>
44 #include <boost/thread.hpp>
47 #include <condition_variable>
62 class NfdRunner : noncopyable
66 NfdRunner(
const std::string& configFile)
67 : m_nfd(configFile, m_nfdKeyChain)
68 , m_configFile(configFile)
72 m_terminationSignalSet.add(SIGINT);
73 m_terminationSignalSet.add(SIGTERM);
74 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate,
this, _1, _2));
76 m_reloadSignalSet.add(SIGHUP);
77 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
81 printUsage(std::ostream& os,
const std::string& programName)
84 <<
" " << programName <<
" [options]\n"
86 <<
"Run NFD forwarding daemon\n"
89 <<
" [--help] - print this help message\n"
90 <<
" [--version] - print version and exit\n"
91 <<
" [--modules] - list available logging modules\n"
92 <<
" [--config /path/to/nfd.conf] - path to configuration file "
93 <<
"(default: " << DEFAULT_CONFIG_FILE <<
")\n"
98 printModules(std::ostream& os)
100 os <<
"Available logging modules: \n";
103 os << module <<
"\n";
120 std::atomic_int retval(0);
123 boost::asio::io_service* ribIo =
nullptr;
130 std::condition_variable cv;
132 std::string configFile = this->m_configFile;
133 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
135 std::lock_guard<std::mutex> lock(m);
137 BOOST_ASSERT(ribIo != mainIo);
142 ndn::KeyChain ribKeyChain;
144 rib::Service ribService(configFile, ribKeyChain);
145 ribService.initialize();
148 catch (
const std::exception& e) {
155 std::lock_guard<std::mutex> lock(m);
163 std::unique_lock<std::mutex> lock(m);
164 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
170 catch (
const std::exception& e) {
174 catch (
const PrivilegeHelper::Error& e) {
181 std::lock_guard<std::mutex> lock(m);
182 if (ribIo !=
nullptr) {
193 terminate(
const boost::system::error_code& error,
int signalNo)
198 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', exiting...");
203 reload(
const boost::system::error_code& error,
int signalNo)
208 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', reloading...");
209 m_nfd.reloadConfigFile();
211 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
215 ndn::KeyChain m_nfdKeyChain;
217 std::string m_configFile;
219 boost::asio::signal_set m_terminationSignalSet;
220 boost::asio::signal_set m_reloadSignalSet;
230 namespace po = boost::program_options;
232 po::options_description description;
234 std::string configFile = DEFAULT_CONFIG_FILE;
235 description.add_options()
236 (
"help,h",
"print this help message")
237 (
"version,V",
"print version and exit")
238 (
"modules,m",
"list available logging modules")
239 (
"config,c", po::value<std::string>(&configFile),
"path to configuration file")
242 po::variables_map vm;
244 po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
247 catch (
const std::exception& e) {
250 std::cerr <<
"ERROR: " << e.what() << std::endl;
251 NfdRunner::printUsage(std::cerr, argv[0]);
255 if (vm.count(
"help") > 0) {
256 NfdRunner::printUsage(std::cout, argv[0]);
260 if (vm.count(
"version") > 0) {
261 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
265 if (vm.count(
"modules") > 0) {
266 NfdRunner::printModules(std::cout);
270 NfdRunner runner(configFile);
275 catch (
const boost::filesystem::filesystem_error& e) {
276 if (e.code() == boost::system::errc::permission_denied) {
277 NFD_LOG_FATAL(
"Permissions denied for " << e.path1() <<
". " <<
278 argv[0] <<
" should be run as superuser");
286 catch (
const std::exception& e) {
290 catch (
const PrivilegeHelper::Error& e) {
std::string getExtendedErrorMessage(const E &exception)
int main(int argc, char **argv)
static LoggerFactory & getInstance()
#define NFD_LOG_INFO(expression)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
#define NFD_LOG_INIT(name)
#define NFD_LOG_FATAL(expression)
boost::asio::io_service & getGlobalIoService()