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-2021, 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 "common/global.hpp"
30 #include "fw/face-table.hpp"
31 
32 namespace nfd {
33 namespace face {
34 
36 
37 const std::string CFGSEC_FACESYSTEM = "face_system";
38 const std::string CFGSEC_GENERAL = "general";
39 const std::string CFGSEC_GENERAL_FQ = CFGSEC_FACESYSTEM + ".general";
40 const std::string CFGSEC_NETDEVBOUND = "netdev_bound";
41 
42 FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
43  : m_faceTable(faceTable)
44  , m_netmon(std::move(netmon))
45 {
46  auto pfCtorParams = this->makePFCtorParams();
47  for (const auto& id : ProtocolFactory::listRegistered()) {
48  NFD_LOG_TRACE("creating factory " << id);
49  m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
50  }
51 
52  m_netdevBound = make_unique<NetdevBound>(pfCtorParams, *this);
53 }
54 
56 FaceSystem::makePFCtorParams()
57 {
58  auto addFace = [this] (auto face) { m_faceTable.add(std::move(face)); };
59  return {addFace, m_netmon};
60 }
61 
62 FaceSystem::~FaceSystem() = default;
63 
64 std::set<const ProtocolFactory*>
66 {
67  std::set<const ProtocolFactory*> factories;
68  for (const auto& p : m_factories) {
69  factories.insert(p.second.get());
70  }
71  return factories;
72 }
73 
75 FaceSystem::getFactoryById(const std::string& id)
76 {
77  auto found = m_factories.find(id);
78  return found == m_factories.end() ? nullptr : found->second.get();
79 }
80 
82 FaceSystem::getFactoryByScheme(const std::string& scheme)
83 {
84  auto found = m_factoryByScheme.find(scheme);
85  return found == m_factoryByScheme.end() ? nullptr : found->second;
86 }
87 
88 bool
89 FaceSystem::hasFactoryForScheme(const std::string& scheme) const
90 {
91  return m_factoryByScheme.count(scheme) > 0;
92 }
93 
94 void
96 {
97  configFile.addSectionHandler(CFGSEC_FACESYSTEM, [this] (auto&&... args) {
98  processConfig(std::forward<decltype(args)>(args)...);
99  });
100 }
101 
102 void
103 FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
104 {
105  ConfigContext context;
106  context.isDryRun = isDryRun;
107 
108  // process general protocol factory config section
109  auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
110  if (generalSection) {
111  for (const auto& pair : *generalSection) {
112  const std::string& key = pair.first;
113  if (key == "enable_congestion_marking") {
114  context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
115  }
116  else {
117  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
118  }
119  }
120  }
121 
122  // process in protocol factories
123  for (const auto& pair : m_factories) {
124  const std::string& sectionName = pair.first;
125  ProtocolFactory* factory = pair.second.get();
126 
127  std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
128  factory->processConfig(configSection.get_child_optional(sectionName), context);
129 
130  if (!isDryRun) {
131  for (const std::string& scheme : factory->getProvidedSchemes()) {
132  m_factoryByScheme[scheme] = factory;
133  if (oldProvidedSchemes.erase(scheme) == 0) {
134  NFD_LOG_TRACE("factory " << sectionName <<
135  " provides " << scheme << " FaceUri scheme");
136  }
137  }
138  for (const std::string& scheme : oldProvidedSchemes) {
139  m_factoryByScheme.erase(scheme);
140  NFD_LOG_TRACE("factory " << sectionName <<
141  " no longer provides " << scheme << " FaceUri scheme");
142  }
143  }
144  }
145 
146  // process netdev_bound section, after factories start providing *+dev schemes
147  auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
148  m_netdevBound->processConfig(netdevBoundSection, context);
149 
150  // process other sections
151  std::set<std::string> seenSections;
152  for (const auto& pair : configSection) {
153  const std::string& sectionName = pair.first;
154  // const ConfigSection& subSection = pair.second;
155 
156  if (!seenSections.insert(sectionName).second) {
157  NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
158  }
159 
160  if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
161  m_factories.count(sectionName) > 0) {
162  continue;
163  }
164 
165  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
166  }
167 }
168 
169 } // namespace face
170 } // 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:60
const std::string CFGSEC_NETDEVBOUND
Definition: face-system.cpp:40
ProtocolFactory * getFactoryById(const std::string &id)
Definition: face-system.cpp:75
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
Process face_system subsection that corresponds to this protocol factory id.
configuration file parsing utility
Definition: config-file.hpp:57
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:58
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
Definition: face-system.cpp:82
STL namespace.
entry point of the face system
Definition: face-system.hpp:51
std::set< const ProtocolFactory * > listProtocolFactories() const
Definition: face-system.cpp:65
FaceSystem(FaceTable &faceTable, shared_ptr< ndn::net::NetworkMonitor > netmon)
Definition: face-system.cpp:42
#define NDN_THROW(e)
Definition: exception.hpp:61
Provides support for an underlying protocol.
const std::string CFGSEC_FACESYSTEM
Definition: face-system.cpp:37
container of all faces
Definition: face-table.hpp:38
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
void setConfigFile(ConfigFile &configFile)
register handler for face_system section of NFD configuration file
Definition: face-system.cpp:95
#define NFD_LOG_TRACE
Definition: logger.hpp:37
Parameters to ProtocolFactory constructor.
boost::property_tree::ptree ConfigSection
a config file section
bool hasFactoryForScheme(const std::string &scheme) const
Definition: face-system.cpp:89
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:96
const std::set< std::string > & getProvidedSchemes() const
Get FaceUri schemes accepted by this protocol factory.
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
const std::string CFGSEC_GENERAL
Definition: face-system.cpp:38
const std::string CFGSEC_GENERAL_FQ
Definition: face-system.cpp:39