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-2019, 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 
35 NFD_LOG_INIT(FaceSystem);
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, bind(&FaceSystem::processConfig, this, _1, _2, _3));
98 }
99 
100 void
101 FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string&)
102 {
103  ConfigContext context;
104  context.isDryRun = isDryRun;
105 
106  // process general protocol factory config section
107  auto generalSection = configSection.get_child_optional(CFGSEC_GENERAL);
108  if (generalSection) {
109  for (const auto& pair : *generalSection) {
110  const std::string& key = pair.first;
111  if (key == "enable_congestion_marking") {
112  context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, CFGSEC_GENERAL_FQ);
113  }
114  else {
115  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_GENERAL_FQ + "." + key));
116  }
117  }
118  }
119 
120  // process in protocol factories
121  for (const auto& pair : m_factories) {
122  const std::string& sectionName = pair.first;
123  ProtocolFactory* factory = pair.second.get();
124 
125  std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
126  factory->processConfig(configSection.get_child_optional(sectionName), context);
127 
128  if (!isDryRun) {
129  for (const std::string& scheme : factory->getProvidedSchemes()) {
130  m_factoryByScheme[scheme] = factory;
131  if (oldProvidedSchemes.erase(scheme) == 0) {
132  NFD_LOG_TRACE("factory " << sectionName <<
133  " provides " << scheme << " FaceUri scheme");
134  }
135  }
136  for (const std::string& scheme : oldProvidedSchemes) {
137  m_factoryByScheme.erase(scheme);
138  NFD_LOG_TRACE("factory " << sectionName <<
139  " no longer provides " << scheme << " FaceUri scheme");
140  }
141  }
142  }
143 
144  // process netdev_bound section, after factories start providing *+dev schemes
145  auto netdevBoundSection = configSection.get_child_optional(CFGSEC_NETDEVBOUND);
146  m_netdevBound->processConfig(netdevBoundSection, context);
147 
148  // process other sections
149  std::set<std::string> seenSections;
150  for (const auto& pair : configSection) {
151  const std::string& sectionName = pair.first;
152  // const ConfigSection& subSection = pair.second;
153 
154  if (!seenSections.insert(sectionName).second) {
155  NDN_THROW(ConfigFile::Error("Duplicate section " + CFGSEC_FACESYSTEM + "." + sectionName));
156  }
157 
158  if (sectionName == CFGSEC_GENERAL || sectionName == CFGSEC_NETDEVBOUND ||
159  m_factories.count(sectionName) > 0) {
160  continue;
161  }
162 
163  NDN_THROW(ConfigFile::Error("Unrecognized option " + CFGSEC_FACESYSTEM + "." + sectionName));
164  }
165 }
166 
167 } // namespace face
168 } // namespace nfd
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:77
static std::set< std::string > listRegistered()
Get all registered protocol factory ids.
const std::string CFGSEC_NETDEVBOUND
Definition: face-system.cpp:40
FaceSystem(FaceTable &faceTable, shared_ptr< ndn::net::NetworkMonitor > netmon)
Definition: face-system.cpp:42
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
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
Definition: face-system.cpp:82
#define NFD_LOG_TRACE
Definition: logger.hpp:37
const std::set< std::string > & getProvidedSchemes() const
Get FaceUri schemes accepted by this protocol factory.
STL namespace.
container of all faces
Definition: face-table.hpp:38
void setConfigFile(ConfigFile &configFile)
register handler for face_system section of NFD configuration file
Definition: face-system.cpp:95
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
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
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
const std::string CFGSEC_FACESYSTEM
Definition: face-system.cpp:37
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:58
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:31
ProtocolFactory * getFactoryById(const std::string &id)
Definition: face-system.cpp:75
const std::string CFGSEC_GENERAL
Definition: face-system.cpp:38
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
std::set< const ProtocolFactory * > listProtocolFactories() const
Definition: face-system.cpp:65
const std::string CFGSEC_GENERAL_FQ
Definition: face-system.cpp:39