33 #include "core/version.hpp" 37 #include <boost/config.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> 44 #include <boost/thread.hpp> 45 #include <boost/version.hpp> 48 #include <condition_variable> 51 #include <ndn-cxx/util/logging.hpp> 52 #include <ndn-cxx/version.hpp> 55 #include <pcap/pcap.h> 58 #include <websocketpp/version.hpp> 61 namespace po = boost::program_options;
76 class NfdRunner : noncopyable
80 NfdRunner(
const std::string& configFile)
81 : m_nfd(configFile, m_nfdKeyChain)
82 , m_configFile(configFile)
86 m_terminationSignalSet.add(SIGINT);
87 m_terminationSignalSet.add(SIGTERM);
88 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate,
this, _1, _2));
90 m_reloadSignalSet.add(SIGHUP);
91 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
105 std::atomic_int retval(0);
109 boost::asio::io_service* ribIo =
nullptr;
116 std::condition_variable cv;
118 std::string configFile = this->m_configFile;
119 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
121 std::lock_guard<std::mutex> lock(m);
123 BOOST_ASSERT(ribIo != mainIo);
129 ndn::KeyChain ribKeyChain;
131 rib::Service ribService(configFile, ribKeyChain);
134 catch (
const std::exception& e) {
141 std::lock_guard<std::mutex> lock(m);
149 std::unique_lock<std::mutex> lock(m);
150 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
156 catch (
const std::exception& e) {
160 catch (
const PrivilegeHelper::Error& e) {
167 std::lock_guard<std::mutex> lock(m);
168 if (ribIo !=
nullptr) {
179 terminate(
const boost::system::error_code& error,
int signalNo)
184 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', exiting...");
189 reload(
const boost::system::error_code& error,
int signalNo)
194 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', reloading...");
195 m_nfd.reloadConfigFile();
197 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
201 ndn::KeyChain m_nfdKeyChain;
203 std::string m_configFile;
205 boost::asio::signal_set m_terminationSignalSet;
206 boost::asio::signal_set m_reloadSignalSet;
210 printUsage(std::ostream& os,
const char* programName,
const po::options_description& opts)
212 os <<
"Usage: " << programName <<
" [options]\n" 214 <<
"Run the NDN Forwarding Daemon (NFD)\n" 222 const auto& modules = ndn::util::Logging::getLoggerNames();
223 std::copy(modules.begin(), modules.end(), ndn::make_ostream_joiner(os,
"\n"));
234 std::string configFile = DEFAULT_CONFIG_FILE;
236 po::options_description description(
"Options");
237 description.add_options()
238 (
"help,h",
"print this message and exit")
239 (
"version,V",
"show version information and exit")
240 (
"config,c", po::value<std::string>(&configFile),
241 "path to configuration file (default: " DEFAULT_CONFIG_FILE
")")
242 (
"modules,m",
"list available logging modules")
245 po::variables_map vm;
247 po::store(po::parse_command_line(argc, argv, description), vm);
250 catch (
const std::exception& e) {
254 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
259 if (vm.count(
"help") > 0) {
264 if (vm.count(
"version") > 0) {
265 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
269 if (vm.count(
"modules") > 0) {
274 const std::string boostBuildInfo =
275 "with Boost version " + to_string(BOOST_VERSION / 100000) +
276 "." + to_string(BOOST_VERSION / 100 % 1000) +
277 "." + to_string(BOOST_VERSION % 100);
278 const std::string pcapBuildInfo =
280 "with " + std::string(pcap_lib_version());
284 const std::string wsBuildInfo =
285 #ifdef HAVE_WEBSOCKET 286 "with WebSocket++ version " + to_string(websocketpp::major_version) +
287 "." + to_string(websocketpp::minor_version) +
288 "." + to_string(websocketpp::patch_version);
290 "without WebSocket++";
293 std::clog <<
"NFD version " NFD_VERSION_BUILD_STRING
" starting\n" 294 <<
"Built with " BOOST_COMPILER
", with " BOOST_STDLIB
295 ", " << boostBuildInfo <<
296 ", " << pcapBuildInfo <<
297 ", " << wsBuildInfo <<
298 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING
301 NfdRunner runner(configFile);
305 catch (
const boost::filesystem::filesystem_error& e) {
306 if (e.code() == boost::system::errc::permission_denied) {
308 ". This program should be run as superuser");
316 catch (
const std::exception& e) {
320 catch (
const PrivilegeHelper::Error& e) {
std::string getExtendedErrorMessage(const E &exception)
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...
#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()