NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nrd.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "nrd.hpp"
27 
28 #include "rib-manager.hpp"
29 #include "core/config-file.hpp"
30 #include "core/logger-factory.hpp"
31 #include "core/global-io.hpp"
32 
33 #include <boost/property_tree/info_parser.hpp>
34 
35 #include <ndn-cxx/transport/unix-transport.hpp>
36 #include <ndn-cxx/transport/tcp-transport.hpp>
37 
38 namespace nfd {
39 namespace rib {
40 
41 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
42 
43 Nrd::Nrd(const std::string& configFile, ndn::KeyChain& keyChain)
44  : m_configFile(configFile)
45  , m_keyChain(keyChain)
46 {
47 }
48 
49 Nrd::Nrd(const ConfigSection& config, ndn::KeyChain& keyChain)
50  : m_configSection(config)
51  , m_keyChain(keyChain)
52 {
53 }
54 
56 {
57  // It is necessary to explicitly define the destructor, because some member variables
58  // (e.g., unique_ptr<RibManager>) are forward-declared, but implicitly declared destructor
59  // requires complete types for all members when instantiated.
60 }
61 
62 void
64 {
65  m_face.reset(new ndn::Face(getLocalNfdTransport(), getGlobalIoService(), m_keyChain));
66 
67  initializeLogging();
68 
69  m_ribManager.reset(new RibManager(*m_face, m_keyChain));
70 
71  ConfigFile config([] (const std::string& filename, const std::string& sectionName,
72  const ConfigSection& section, bool isDryRun) {
73  // Ignore "log" and sections belonging to NFD,
74  // but raise an error if we're missing a handler for a "rib" section.
75  if (sectionName != "rib" || sectionName == "log") {
76  // do nothing
77  }
78  else {
79  // missing NRD section
80  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
81  }
82  });
83  m_ribManager->setConfigFile(config);
84 
85  // parse config file
86  if (!m_configFile.empty()) {
87  config.parse(m_configFile, true);
88  config.parse(m_configFile, false);
89  }
90  else {
91  config.parse(m_configSection, true, INTERNAL_CONFIG);
92  config.parse(m_configSection, false, INTERNAL_CONFIG);
93  }
94 
95  m_ribManager->registerWithNfd();
96  m_ribManager->enableLocalControlHeader();
97 }
98 
99 void
100 Nrd::initializeLogging()
101 {
103  LoggerFactory::getInstance().setConfigFile(config);
104 
105  if (!m_configFile.empty()) {
106  config.parse(m_configFile, true);
107  config.parse(m_configFile, false);
108  }
109  else {
110  config.parse(m_configSection, true, INTERNAL_CONFIG);
111  config.parse(m_configSection, false, INTERNAL_CONFIG);
112  }
113 }
114 
115 shared_ptr<ndn::Transport>
116 Nrd::getLocalNfdTransport()
117 {
118  ConfigSection config;
119 
120  if (!m_configFile.empty()) {
121  // Any format errors should have been caught already
122  // If error is thrown at this point, it is development error
123  boost::property_tree::read_info(m_configFile, config);
124  }
125  else
126  config = m_configSection;
127 
128  if (config.get_child_optional("face_system.unix")) {
129  // unix socket enabled
130 
131  auto&& socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
132  // default socketPath should be the same as in FaceManager::processSectionUnix
133 
134  return make_shared<ndn::UnixTransport>(socketPath);
135  }
136  else if (config.get_child_optional("face_system.tcp") &&
137  config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
138  // tcp is enabled
139 
140  auto&& port = config.get<std::string>("face_system.tcp.port", "6363");
141  // default port should be the same as in FaceManager::processSectionTcp
142 
143  return make_shared<ndn::TcpTransport>("localhost", port);
144  }
145  else {
146  BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
147  }
148 }
149 
150 } // namespace rib
151 } // namespace nfd
static const std::string INTERNAL_CONFIG
Definition: nrd.cpp:41
Nrd(const std::string &configFile, ndn::KeyChain &keyChain)
Create NRD instance using absolute or relative path to configFile.
Definition: nrd.cpp:43
~Nrd()
Destructor.
Definition: nrd.cpp:55
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:34
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:38
Abstraction to communicate with local or remote NDN forwarder.
Definition: face.hpp:100
boost::property_tree::ptree ConfigSection
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:47
void initialize()
Perform initialization of NFD instance After initialization, NFD instance can be started by invoking ...
Definition: nrd.cpp:63
boost::asio::io_service & getGlobalIoService()
Definition: global-io.hpp:35
void parse(const std::string &filename, bool isDryRun)
Definition: config-file.cpp:68