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> 63 class NfdRunner : noncopyable
67 NfdRunner(
const std::string& configFile)
68 : m_nfd(configFile, m_nfdKeyChain)
69 , m_configFile(configFile)
73 m_terminationSignalSet.add(SIGINT);
74 m_terminationSignalSet.add(SIGTERM);
75 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate,
this, _1, _2));
77 m_reloadSignalSet.add(SIGHUP);
78 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
82 printUsage(std::ostream& os,
const std::string& programName)
85 <<
" " << programName <<
" [options]\n" 87 <<
"Run NFD forwarding daemon\n" 90 <<
" [--help] - print this help message\n" 91 <<
" [--version] - print version and exit\n" 92 <<
" [--modules] - list available logging modules\n" 93 <<
" [--config /path/to/nfd.conf] - path to configuration file " 94 <<
"(default: " << DEFAULT_CONFIG_FILE <<
")\n" 99 printModules(std::ostream& os)
101 os <<
"Available logging modules: \n";
104 os << module <<
"\n";
121 std::atomic_int retval(0);
124 boost::asio::io_service* ribIo =
nullptr;
131 std::condition_variable cv;
133 std::string configFile = this->m_configFile;
134 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
136 std::lock_guard<std::mutex> lock(m);
138 BOOST_ASSERT(ribIo != mainIo);
143 ndn::KeyChain ribKeyChain;
145 rib::Service ribService(configFile, ribKeyChain);
146 ribService.initialize();
149 catch (
const std::exception& e) {
156 std::lock_guard<std::mutex> lock(m);
164 std::unique_lock<std::mutex> lock(m);
165 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
171 catch (
const std::exception& e) {
175 catch (
const PrivilegeHelper::Error& e) {
182 std::lock_guard<std::mutex> lock(m);
183 if (ribIo !=
nullptr) {
194 terminate(
const boost::system::error_code& error,
int signalNo)
199 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', exiting...");
204 reload(
const boost::system::error_code& error,
int signalNo)
209 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', reloading...");
210 m_nfd.reloadConfigFile();
212 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
216 ndn::KeyChain m_nfdKeyChain;
218 std::string m_configFile;
220 boost::asio::signal_set m_terminationSignalSet;
221 boost::asio::signal_set m_reloadSignalSet;
231 namespace po = boost::program_options;
233 po::options_description description;
235 std::string configFile = DEFAULT_CONFIG_FILE;
236 description.add_options()
237 (
"help,h",
"print this help message")
238 (
"version,V",
"print version and exit")
239 (
"modules,m",
"list available logging modules")
240 (
"config,c", po::value<std::string>(&configFile),
"path to configuration file")
243 po::variables_map vm;
245 po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
248 catch (
const std::exception& e) {
251 std::cerr <<
"ERROR: " << e.what() << std::endl;
252 NfdRunner::printUsage(std::cerr, argv[0]);
256 if (vm.count(
"help") > 0) {
257 NfdRunner::printUsage(std::cout, argv[0]);
261 if (vm.count(
"version") > 0) {
262 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
266 if (vm.count(
"modules") > 0) {
267 NfdRunner::printModules(std::cout);
271 NfdRunner runner(configFile);
276 catch (
const boost::filesystem::filesystem_error& e) {
277 if (e.code() == boost::system::errc::permission_denied) {
278 NFD_LOG_FATAL(
"Permissions denied for " << e.path1() <<
". " <<
279 argv[0] <<
" should be run as superuser");
287 catch (
const std::exception& e) {
291 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()