NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
face-system.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #include "face-system.hpp"
27 #include "protocol-factory.hpp"
28 #include "netdev-bound.hpp"
29 #include "core/global-io.hpp"
30 #include "fw/face-table.hpp"
31 
32 namespace nfd {
33 namespace face {
34 
36 
37 static const std::string SECTION_GENERAL = "general";
38 static const std::string SECTION_NETDEVBOUND = "netdev_bound";
39 
40 FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
41  : m_faceTable(faceTable)
42  , m_netmon(std::move(netmon))
43 {
44  auto pfCtorParams = this->makePFCtorParams();
45  for (const std::string& id : ProtocolFactory::listRegistered()) {
46  NFD_LOG_TRACE("creating factory " << id);
47  m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
48  }
49 
50  m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
51 }
52 
54 FaceSystem::makePFCtorParams()
55 {
56  auto addFace = [&ft = m_faceTable] (auto face) { ft.add(std::move(face)); };
57  return {addFace, m_netmon};
58 }
59 
60 FaceSystem::~FaceSystem() = default;
61 
62 std::set<const ProtocolFactory*>
64 {
65  std::set<const ProtocolFactory*> factories;
66  for (const auto& p : m_factories) {
67  factories.insert(p.second.get());
68  }
69  return factories;
70 }
71 
73 FaceSystem::getFactoryById(const std::string& id)
74 {
75  auto found = m_factories.find(id);
76  return found == m_factories.end() ? nullptr : found->second.get();
77 }
78 
80 FaceSystem::getFactoryByScheme(const std::string& scheme)
81 {
82  auto found = m_factoryByScheme.find(scheme);
83  return found == m_factoryByScheme.end() ? nullptr : found->second;
84 }
85 
86 bool
87 FaceSystem::hasFactoryForScheme(const std::string& scheme) const
88 {
89  return m_factoryByScheme.count(scheme) > 0;
90 }
91 
92 void
94 {
95  configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
96 }
97 
98 void
99 FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
100 {
101  ConfigContext context;
102  context.isDryRun = isDryRun;
103 
104  // process general protocol factory config section
105  auto generalSection = configSection.get_child_optional(SECTION_GENERAL);
106  if (generalSection) {
107  for (const auto& pair : *generalSection) {
108  const std::string& key = pair.first;
109  if (key == "enable_congestion_marking") {
110  context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, "face_system.general");
111  }
112  else {
113  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.general." + key));
114  }
115  }
116  }
117 
118  // process in protocol factories
119  for (const auto& pair : m_factories) {
120  const std::string& sectionName = pair.first;
121  ProtocolFactory* factory = pair.second.get();
122 
123  std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
124  factory->processConfig(configSection.get_child_optional(sectionName), context);
125 
126  if (!isDryRun) {
127  for (const std::string& scheme : factory->getProvidedSchemes()) {
128  m_factoryByScheme[scheme] = factory;
129  if (oldProvidedSchemes.erase(scheme) == 0) {
130  NFD_LOG_TRACE("factory " << sectionName <<
131  " provides " << scheme << " FaceUri scheme");
132  }
133  }
134  for (const std::string& scheme : oldProvidedSchemes) {
135  m_factoryByScheme.erase(scheme);
136  NFD_LOG_TRACE("factory " << sectionName <<
137  " no longer provides " << scheme << " FaceUri scheme");
138  }
139  }
140  }
141 
142  // process netdev_bound section, after factories start providing *+dev schemes
143  auto netdevBoundSection = configSection.get_child_optional(SECTION_NETDEVBOUND);
144  m_netdevBound->processConfig(netdevBoundSection, context);
145 
146  // process other sections
147  std::set<std::string> seenSections;
148  for (const auto& pair : configSection) {
149  const std::string& sectionName = pair.first;
150  // const ConfigSection& subSection = pair.second;
151 
152  if (!seenSections.insert(sectionName).second) {
153  BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName));
154  }
155 
156  if (sectionName == SECTION_GENERAL || sectionName == SECTION_NETDEVBOUND ||
157  m_factories.count(sectionName) > 0) {
158  continue;
159  }
160 
161  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName));
162  }
163 }
164 
165 } // namespace face
166 } // namespace nfd
static std::set< std::string > listRegistered()
Get all registered protocol factory ids.
static bool parseYesNo(const ConfigSection &node, const std::string &key, const std::string &sectionName)
parse a config option that can be either "yes" or "no"
Definition: config-file.cpp:59
ProtocolFactory * getFactoryById(const std::string &id)
Definition: face-system.cpp:73
configuration file parsing utility
Definition: config-file.hpp:57
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
Definition: face-system.cpp:80
entry point of the face system
Definition: face-system.hpp:51
std::set< const ProtocolFactory * > listProtocolFactories() const
Definition: face-system.cpp:63
FaceSystem(FaceTable &faceTable, shared_ptr< ndn::net::NetworkMonitor > netmon)
Definition: face-system.cpp:40
Provides support for an underlying protocol.
container of all faces
Definition: face-table.hpp:37
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
#define NFD_LOG_TRACE
Definition: logger.hpp:37
void setConfigFile(ConfigFile &configFile)
register handler for face_system section of NFD configuration file
Definition: face-system.cpp:93
Parameters to ProtocolFactory constructor.
boost::property_tree::ptree ConfigSection
a config file section
bool hasFactoryForScheme(const std::string &scheme) const
Definition: face-system.cpp:87
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
static const std::string SECTION_GENERAL
Definition: face-system.cpp:37
static const std::string SECTION_NETDEVBOUND
Definition: face-system.cpp:38
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31