NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
service.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "service.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 Service::Service(const std::string& configFile, ndn::KeyChain& keyChain)
44  : m_configFile(configFile)
45  , m_keyChain(keyChain)
46 {
47 }
48 
49 Service::Service(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  m_dispatcher.reset(new ndn::mgmt::Dispatcher(*m_face, m_keyChain));
67 
68  initializeLogging();
69 
70  m_ribManager.reset(new RibManager(*m_dispatcher, *m_face, m_keyChain));
71 
72  ConfigFile config([] (const std::string& filename, const std::string& sectionName,
73  const ConfigSection& section, bool isDryRun) {
74  // Ignore "log" and sections belonging to NFD,
75  // but raise an error if we're missing a handler for a "rib" section.
76  if (sectionName != "rib" || sectionName == "log") {
77  // do nothing
78  }
79  else {
80  // missing "rib" section handler
81  ConfigFile::throwErrorOnUnknownSection(filename, sectionName, section, isDryRun);
82  }
83  });
84  m_ribManager->setConfigFile(config);
85 
86  // parse config file
87  if (!m_configFile.empty()) {
88  config.parse(m_configFile, true);
89  config.parse(m_configFile, false);
90  }
91  else {
92  config.parse(m_configSection, true, INTERNAL_CONFIG);
93  config.parse(m_configSection, false, INTERNAL_CONFIG);
94  }
95 
96  m_ribManager->registerWithNfd();
97  m_ribManager->enableLocalFields();
98 }
99 
100 void
101 Service::initializeLogging()
102 {
104  LoggerFactory::getInstance().setConfigFile(config);
105 
106  if (!m_configFile.empty()) {
107  config.parse(m_configFile, true);
108  config.parse(m_configFile, false);
109  }
110  else {
111  config.parse(m_configSection, true, INTERNAL_CONFIG);
112  config.parse(m_configSection, false, INTERNAL_CONFIG);
113  }
114 }
115 
116 shared_ptr<ndn::Transport>
117 Service::getLocalNfdTransport()
118 {
119  ConfigSection config;
120 
121  if (!m_configFile.empty()) {
122  // Any format errors should have been caught already
123  // If error is thrown at this point, it is development error
124  boost::property_tree::read_info(m_configFile, config);
125  }
126  else
127  config = m_configSection;
128 
129  if (config.get_child_optional("face_system.unix")) {
130  // unix socket enabled
131 
132  auto&& socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
133  // default socketPath should be the same as in FaceManager::processSectionUnix
134 
135  return make_shared<ndn::UnixTransport>(socketPath);
136  }
137  else if (config.get_child_optional("face_system.tcp") &&
138  config.get<std::string>("face_system.tcp.listen", "yes") == "yes") {
139  // tcp is enabled
140 
141  auto&& port = config.get<std::string>("face_system.tcp.port", "6363");
142  // default port should be the same as in FaceManager::processSectionTcp
143 
144  return make_shared<ndn::TcpTransport>("localhost", port);
145  }
146  else {
147  BOOST_THROW_EXCEPTION(Error("No transport is available to communicate with NFD"));
148  }
149 }
150 
151 } // namespace rib
152 } // namespace nfd
The interface of signing key management.
Definition: key-chain.hpp:46
static const std::string INTERNAL_CONFIG
Definition: service.cpp:41
represents a dispatcher on server side of NFD Management protocol
Definition: dispatcher.hpp:130
configuration file parsing utility
Definition: config-file.hpp:57
detail::SimulatorIo & getGlobalIoService()
Definition: global-io.cpp:48
void initialize()
Perform initialization of NFD-RIB instance.
Definition: service.cpp:63
Service(const std::string &configFile, ndn::KeyChain &keyChain)
create NFD-RIB service
Definition: service.cpp:43
static void throwErrorOnUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:40
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:90
boost::property_tree::ptree ConfigSection
a config file section
static void ignoreUnknownSection(const std::string &filename, const std::string &sectionName, const ConfigSection &section, bool isDryRun)
Definition: config-file.cpp:50
~Service()
Destructor.
Definition: service.cpp:55