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-2017, 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 "core/global-io.hpp"
29 #include "fw/face-table.hpp"
30 
31 namespace nfd {
32 namespace face {
33 
34 NFD_LOG_INIT("FaceSystem");
35 
36 FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
37  : m_faceTable(faceTable)
38  , m_netmon(std::move(netmon))
39 {
40  auto pfCtorParams = this->makePFCtorParams();
41  for (const std::string& id : ProtocolFactory::listRegistered()) {
42  NFD_LOG_TRACE("creating factory " << id);
43  m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
44  }
45 }
46 
48 FaceSystem::makePFCtorParams()
49 {
50  auto addFace = bind(&FaceTable::add, &m_faceTable, _1);
51  return {addFace, m_netmon};
52 }
53 
54 FaceSystem::~FaceSystem() = default;
55 
56 std::set<const ProtocolFactory*>
58 {
59  std::set<const ProtocolFactory*> factories;
60  for (const auto& p : m_factories) {
61  factories.insert(p.second.get());
62  }
63  return factories;
64 }
65 
67 FaceSystem::getFactoryById(const std::string& id)
68 {
69  auto found = m_factories.find(id);
70  return found == m_factories.end() ? nullptr : found->second.get();
71 }
72 
74 FaceSystem::getFactoryByScheme(const std::string& scheme)
75 {
76  auto found = m_factoryByScheme.find(scheme);
77  return found == m_factoryByScheme.end() ? nullptr : found->second;
78 }
79 
80 void
82 {
83  configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
84 }
85 
86 void
87 FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
88 {
89  ConfigContext context;
90  context.isDryRun = isDryRun;
91 
92  // process sections in protocol factories
93  for (const auto& pair : m_factories) {
94  const std::string& sectionName = pair.first;
95  ProtocolFactory* factory = pair.second.get();
96 
97  std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
98  factory->processConfig(configSection.get_child_optional(sectionName), context);
99 
100  if (!isDryRun) {
101  for (const std::string& scheme : factory->getProvidedSchemes()) {
102  m_factoryByScheme[scheme] = factory;
103  if (oldProvidedSchemes.erase(scheme) == 0) {
104  NFD_LOG_TRACE("factory " << sectionName <<
105  " provides " << scheme << " FaceUri scheme");
106  }
107  }
108  for (const std::string& scheme : oldProvidedSchemes) {
109  m_factoryByScheme.erase(scheme);
110  NFD_LOG_TRACE("factory " << sectionName <<
111  " no longer provides " << scheme << " FaceUri scheme");
112  }
113  }
114  }
115 
116  // process other sections
117  std::set<std::string> seenSections;
118  for (const auto& pair : configSection) {
119  const std::string& sectionName = pair.first;
120  // const ConfigSection& subSection = pair.second;
121 
122  if (!seenSections.insert(sectionName).second) {
123  BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName));
124  }
125 
126  if (m_factories.count(sectionName) > 0) {
127  continue;
128  }
129 
131 
132  BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName));
133  }
134 }
135 
136 } // namespace face
137 } // namespace nfd
void addSectionHandler(const std::string &sectionName, ConfigSectionHandler subscriber)
setup notification of configuration file sections
Definition: config-file.cpp:76
static std::set< std::string > listRegistered()
Get registered protocol factory ids.
FaceSystem(FaceTable &faceTable, shared_ptr< ndn::net::NetworkMonitor > netmon)
Definition: face-system.cpp:36
configuration file parsing utility
Definition: config-file.hpp:58
ProtocolFactory * getFactoryByScheme(const std::string &scheme)
Definition: face-system.cpp:74
STL namespace.
container of all faces
Definition: face-table.hpp:37
void setConfigFile(ConfigFile &configFile)
register handler for face_system section of NFD configuration file
Definition: face-system.cpp:81
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:85
void add(shared_ptr< Face > face)
add a face
Definition: face-table.cpp:57
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
virtual void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)=0
Process face_system subsection that corresponds to this ProtocolFactory type.
boost::property_tree::ptree ConfigSection
a config file section
Definition: config-file.hpp:37
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
#define NFD_LOG_INIT(name)
Definition: logger.hpp:122
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:160
const std::set< std::string > & getProvidedSchemes()
Get FaceUri schemes accepted by this ProtocolFactory.
ProtocolFactory * getFactoryById(const std::string &id)
Definition: face-system.cpp:67
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:57