blob: 6288ac04cfd923c36d866989f938c68a239c8e90 [file] [log] [blame]
Junxiao Shib8590312016-12-29 21:22:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shib8590312016-12-29 21:22:25 +00004 * 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"
Junxiao Shib47247d2017-01-24 15:09:16 +000027#include "protocol-factory.hpp"
Junxiao Shi2d491752017-07-14 21:32:05 +000028#include "core/global-io.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000029#include "fw/face-table.hpp"
30
Junxiao Shib8590312016-12-29 21:22:25 +000031namespace nfd {
32namespace face {
33
Junxiao Shib47247d2017-01-24 15:09:16 +000034NFD_LOG_INIT("FaceSystem");
Junxiao Shib8590312016-12-29 21:22:25 +000035
Junxiao Shi0ba6d642017-07-17 00:53:22 +000036FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon)
Junxiao Shib8590312016-12-29 21:22:25 +000037 : m_faceTable(faceTable)
Junxiao Shi0ba6d642017-07-17 00:53:22 +000038 , m_netmon(std::move(netmon))
Junxiao Shib8590312016-12-29 21:22:25 +000039{
Junxiao Shi0ba6d642017-07-17 00:53:22 +000040 auto pfCtorParams = this->makePFCtorParams();
Junxiao Shib47247d2017-01-24 15:09:16 +000041 for (const std::string& id : ProtocolFactory::listRegistered()) {
42 NFD_LOG_TRACE("creating factory " << id);
Junxiao Shi0ba6d642017-07-17 00:53:22 +000043 m_factories[id] = ProtocolFactory::create(id, pfCtorParams);
Junxiao Shib47247d2017-01-24 15:09:16 +000044 }
Junxiao Shib8590312016-12-29 21:22:25 +000045}
46
Junxiao Shi0ba6d642017-07-17 00:53:22 +000047ProtocolFactoryCtorParams
48FaceSystem::makePFCtorParams()
49{
50 auto addFace = bind(&FaceTable::add, &m_faceTable, _1);
51 return {addFace, m_netmon};
52}
53
Junxiao Shib47247d2017-01-24 15:09:16 +000054FaceSystem::~FaceSystem() = default;
55
Junxiao Shib8590312016-12-29 21:22:25 +000056std::set<const ProtocolFactory*>
57FaceSystem::listProtocolFactories() const
58{
59 std::set<const ProtocolFactory*> factories;
Junxiao Shib47247d2017-01-24 15:09:16 +000060 for (const auto& p : m_factories) {
Junxiao Shib8590312016-12-29 21:22:25 +000061 factories.insert(p.second.get());
62 }
63 return factories;
64}
65
66ProtocolFactory*
Junxiao Shi38b24c72017-01-05 02:59:31 +000067FaceSystem::getFactoryById(const std::string& id)
Junxiao Shib8590312016-12-29 21:22:25 +000068{
Junxiao Shi38b24c72017-01-05 02:59:31 +000069 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000070 return found == m_factories.end() ? nullptr : found->second.get();
71}
72
Junxiao Shi38b24c72017-01-05 02:59:31 +000073ProtocolFactory*
74FaceSystem::getFactoryByScheme(const std::string& scheme)
75{
76 auto found = m_factoryByScheme.find(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +000077 return found == m_factoryByScheme.end() ? nullptr : found->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +000078}
79
Junxiao Shib8590312016-12-29 21:22:25 +000080void
81FaceSystem::setConfigFile(ConfigFile& configFile)
82{
83 configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
84}
85
86void
87FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
88{
Junxiao Shi38b24c72017-01-05 02:59:31 +000089 ConfigContext context;
90 context.isDryRun = isDryRun;
Junxiao Shi38b24c72017-01-05 02:59:31 +000091
92 // process sections in protocol factories
93 for (const auto& pair : m_factories) {
94 const std::string& sectionName = pair.first;
Junxiao Shib47247d2017-01-24 15:09:16 +000095 ProtocolFactory* factory = pair.second.get();
Junxiao Shi38b24c72017-01-05 02:59:31 +000096
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;
Junxiao Shib47247d2017-01-24 15:09:16 +0000103 if (oldProvidedSchemes.erase(scheme) == 0) {
104 NFD_LOG_TRACE("factory " << sectionName <<
105 " provides " << scheme << " FaceUri scheme");
106 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000107 }
108 for (const std::string& scheme : oldProvidedSchemes) {
109 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000110 NFD_LOG_TRACE("factory " << sectionName <<
111 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000112 }
113 }
114 }
115
116 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000117 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000118 for (const auto& pair : configSection) {
119 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000120 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000121
Junxiao Shi38b24c72017-01-05 02:59:31 +0000122 if (!seenSections.insert(sectionName).second) {
123 BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000124 }
125
Junxiao Shi38b24c72017-01-05 02:59:31 +0000126 if (m_factories.count(sectionName) > 0) {
127 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000128 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000129
130 ///\todo #3521 nicfaces
131
Junxiao Shi64d99f22017-01-21 23:06:36 +0000132 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000133 }
134}
135
Junxiao Shib8590312016-12-29 21:22:25 +0000136} // namespace face
137} // namespace nfd