36 #include <boost/config.hpp> 37 #include <boost/exception/diagnostic_information.hpp> 38 #include <boost/filesystem.hpp> 39 #include <boost/program_options/options_description.hpp> 40 #include <boost/program_options/parsers.hpp> 41 #include <boost/program_options/variables_map.hpp> 42 #include <boost/version.hpp> 45 #include <condition_variable> 49 #include <ndn-cxx/util/logging.hpp> 50 #include <ndn-cxx/util/ostream-joiner.hpp> 51 #include <ndn-cxx/version.hpp> 54 #include <pcap/pcap.h> 57 #include <systemd/sd-daemon.h> 60 #include <websocketpp/version.hpp> 63 namespace po = boost::program_options;
78 class NfdRunner : noncopyable
82 NfdRunner(
const std::string& configFile)
83 : m_nfd(configFile, m_nfdKeyChain)
84 , m_configFile(configFile)
88 m_terminationSignalSet.add(SIGINT);
89 m_terminationSignalSet.add(SIGTERM);
90 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate,
this, _1, _2));
92 m_reloadSignalSet.add(SIGHUP);
93 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
107 std::atomic_int retval(0);
111 boost::asio::io_service* ribIo =
nullptr;
118 std::condition_variable cv;
120 std::thread ribThread([configFile = m_configFile, &retval, &ribIo, mainIo, &cv, &m] {
122 std::lock_guard<std::mutex> lock(m);
124 BOOST_ASSERT(ribIo != mainIo);
130 ndn::KeyChain ribKeyChain;
132 rib::Service ribService(configFile, ribKeyChain);
135 catch (
const std::exception& e) {
142 std::lock_guard<std::mutex> lock(m);
150 std::unique_lock<std::mutex> lock(m);
151 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
155 systemdNotify(
"READY=1");
158 catch (
const std::exception& e) {
162 catch (
const PrivilegeHelper::Error& e) {
169 std::lock_guard<std::mutex> lock(m);
170 if (ribIo !=
nullptr) {
181 systemdNotify(
const char* state)
190 terminate(
const boost::system::error_code& error,
int signalNo)
195 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), exiting...");
197 systemdNotify(
"STOPPING=1");
202 reload(
const boost::system::error_code& error,
int signalNo)
207 NFD_LOG_INFO(
"Caught signal " << signalNo <<
" (" << ::strsignal(signalNo) <<
"), reloading...");
209 systemdNotify(
"RELOADING=1");
210 m_nfd.reloadConfigFile();
211 systemdNotify(
"READY=1");
213 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
217 ndn::KeyChain m_nfdKeyChain;
219 std::string m_configFile;
221 boost::asio::signal_set m_terminationSignalSet;
222 boost::asio::signal_set m_reloadSignalSet;
226 printUsage(std::ostream& os,
const char* programName,
const po::options_description& opts)
228 os <<
"Usage: " << programName <<
" [options]\n" 230 <<
"Run the NDN Forwarding Daemon (NFD)\n" 238 const auto& modules = ndn::util::Logging::getLoggerNames();
239 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os,
"\n"));
250 std::string configFile = DEFAULT_CONFIG_FILE;
252 po::options_description description(
"Options");
253 description.add_options()
254 (
"help,h",
"print this message and exit")
255 (
"version,V",
"show version information and exit")
256 (
"config,c", po::value<std::string>(&configFile),
257 "path to configuration file (default: " DEFAULT_CONFIG_FILE
")")
258 (
"modules,m",
"list available logging modules")
261 po::variables_map vm;
263 po::store(po::parse_command_line(argc, argv, description), vm);
266 catch (
const std::exception& e) {
270 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
275 if (vm.count(
"help") > 0) {
280 if (vm.count(
"version") > 0) {
285 if (vm.count(
"modules") > 0) {
290 const std::string boostBuildInfo =
291 "with Boost version " + to_string(BOOST_VERSION / 100000) +
292 "." + to_string(BOOST_VERSION / 100 % 1000) +
293 "." + to_string(BOOST_VERSION % 100);
294 const std::string pcapBuildInfo =
296 "with " + std::string(pcap_lib_version());
300 const std::string wsBuildInfo =
301 #ifdef HAVE_WEBSOCKET 302 "with WebSocket++ version " + to_string(websocketpp::major_version) +
303 "." + to_string(websocketpp::minor_version) +
304 "." + to_string(websocketpp::patch_version);
306 "without WebSocket++";
310 <<
"Built with " BOOST_COMPILER
", with " BOOST_STDLIB
311 ", " << boostBuildInfo <<
312 ", " << pcapBuildInfo <<
313 ", " << wsBuildInfo <<
314 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
317 NfdRunner runner(configFile);
321 catch (
const boost::filesystem::filesystem_error& e) {
323 return e.code() == boost::system::errc::permission_denied ? 4 : 1;
325 catch (
const std::exception& e) {
329 catch (
const PrivilegeHelper::Error& e) {
static void printLogModules(std::ostream &os)
int main(int argc, char **argv)
void setMainIoService(boost::asio::io_service *mainIo)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const char NFD_VERSION_BUILD_STRING[]
NFD version string, including git commit information, if NFD is build from specific git commit...
#define NFD_LOG_INIT(name)
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
void setRibIoService(boost::asio::io_service *ribIo)
boost::asio::io_service & getGlobalIoService()
Returns the global io_service instance for the calling thread.