34 #include "core/version.hpp" 38 #include <boost/config.hpp> 39 #include <boost/filesystem.hpp> 40 #include <boost/program_options/options_description.hpp> 41 #include <boost/program_options/parsers.hpp> 42 #include <boost/program_options/variables_map.hpp> 45 #include <boost/thread.hpp> 46 #include <boost/version.hpp> 49 #include <condition_variable> 52 #include <ndn-cxx/version.hpp> 54 #include <pcap/pcap.h> 57 #include <websocketpp/version.hpp> 60 namespace po = boost::program_options;
75 class NfdRunner : noncopyable
79 NfdRunner(
const std::string& configFile)
80 : m_nfd(configFile, m_nfdKeyChain)
81 , m_configFile(configFile)
85 m_terminationSignalSet.add(SIGINT);
86 m_terminationSignalSet.add(SIGTERM);
87 m_terminationSignalSet.async_wait(bind(&NfdRunner::terminate,
this, _1, _2));
89 m_reloadSignalSet.add(SIGHUP);
90 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
104 std::atomic_int retval(0);
107 boost::asio::io_service* ribIo =
nullptr;
114 std::condition_variable cv;
116 std::string configFile = this->m_configFile;
117 boost::thread ribThread([configFile, &retval, &ribIo, mainIo, &cv, &m] {
119 std::lock_guard<std::mutex> lock(m);
121 BOOST_ASSERT(ribIo != mainIo);
126 ndn::KeyChain ribKeyChain;
128 rib::Service ribService(configFile, ribKeyChain);
129 ribService.initialize();
132 catch (
const std::exception& e) {
139 std::lock_guard<std::mutex> lock(m);
147 std::unique_lock<std::mutex> lock(m);
148 cv.wait(lock, [&ribIo] {
return ribIo !=
nullptr; });
154 catch (
const std::exception& e) {
158 catch (
const PrivilegeHelper::Error& e) {
165 std::lock_guard<std::mutex> lock(m);
166 if (ribIo !=
nullptr) {
177 terminate(
const boost::system::error_code& error,
int signalNo)
182 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', exiting...");
187 reload(
const boost::system::error_code& error,
int signalNo)
192 NFD_LOG_INFO(
"Caught signal '" << ::strsignal(signalNo) <<
"', reloading...");
193 m_nfd.reloadConfigFile();
195 m_reloadSignalSet.async_wait(bind(&NfdRunner::reload,
this, _1, _2));
199 ndn::KeyChain m_nfdKeyChain;
201 std::string m_configFile;
203 boost::asio::signal_set m_terminationSignalSet;
204 boost::asio::signal_set m_reloadSignalSet;
209 const po::options_description& opts)
211 os <<
"Usage: " << programName <<
" [options]\n" 212 <<
"Run the NDN Forwarding Daemon (NFD)\n" 221 for (
const auto& module : factory.getModules()) {
222 os << module <<
"\n";
233 std::string configFile = DEFAULT_CONFIG_FILE;
235 po::options_description description(
"Options");
236 description.add_options()
237 (
"help,h",
"print this message and exit")
238 (
"version,V",
"show version information and exit")
239 (
"config,c", po::value<std::string>(&configFile),
240 "path to configuration file (default: " DEFAULT_CONFIG_FILE
")")
241 (
"modules,m",
"list available logging modules")
244 po::variables_map vm;
246 po::store(po::parse_command_line(argc, argv, description), vm);
249 catch (
const std::exception& e) {
252 std::cerr <<
"ERROR: " << e.what() <<
"\n\n";
257 if (vm.count(
"help") > 0) {
262 if (vm.count(
"version") > 0) {
263 std::cout << NFD_VERSION_BUILD_STRING << std::endl;
267 if (vm.count(
"modules") > 0) {
272 const std::string boostBuildInfo =
273 "with Boost version " + to_string(BOOST_VERSION / 100000) +
274 "." + to_string(BOOST_VERSION / 100 % 1000) +
275 "." + to_string(BOOST_VERSION % 100);
276 const std::string pcapBuildInfo =
278 "with " + std::string(pcap_lib_version());
282 const std::string wsBuildInfo =
283 #ifdef HAVE_WEBSOCKET 284 "with WebSocket++ version " + to_string(websocketpp::major_version) +
285 "." + to_string(websocketpp::minor_version) +
286 "." + to_string(websocketpp::patch_version);
288 "without WebSocket++";
291 NFD_LOG_INFO(
"NFD version " NFD_VERSION_BUILD_STRING
" starting");
293 ", with " BOOST_STDLIB
294 ", " << boostBuildInfo <<
295 ", " << pcapBuildInfo <<
296 ", " << wsBuildInfo <<
297 ", with ndn-cxx version " NDN_CXX_VERSION_BUILD_STRING);
299 NfdRunner runner(configFile);
303 catch (
const boost::filesystem::filesystem_error& e) {
304 if (e.code() == boost::system::errc::permission_denied) {
306 ". This program should be run as superuser");
314 catch (
const std::exception& e) {
318 catch (
const PrivilegeHelper::Error& e) {
std::string getExtendedErrorMessage(const E &exception)
static void printLogModules(std::ostream &os)
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)
static void printUsage(std::ostream &os, const char *programName, const po::options_description &opts)
#define NFD_LOG_FATAL(expression)
boost::asio::io_service & getGlobalIoService()