blob: a955d976530b503238566cba2e008e28d514d796 [file] [log] [blame]
Junxiao Shib8590312016-12-29 21:22:25 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
28#include "core/logger.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
36FaceSystem::FaceSystem(FaceTable& faceTable)
37 : m_faceTable(faceTable)
38{
Junxiao Shib47247d2017-01-24 15:09:16 +000039 for (const std::string& id : ProtocolFactory::listRegistered()) {
40 NFD_LOG_TRACE("creating factory " << id);
41 m_factories[id] = ProtocolFactory::create(id);
42 }
Junxiao Shib8590312016-12-29 21:22:25 +000043}
44
Junxiao Shib47247d2017-01-24 15:09:16 +000045FaceSystem::~FaceSystem() = default;
46
Junxiao Shib8590312016-12-29 21:22:25 +000047std::set<const ProtocolFactory*>
48FaceSystem::listProtocolFactories() const
49{
50 std::set<const ProtocolFactory*> factories;
Junxiao Shib47247d2017-01-24 15:09:16 +000051 for (const auto& p : m_factories) {
Junxiao Shib8590312016-12-29 21:22:25 +000052 factories.insert(p.second.get());
53 }
54 return factories;
55}
56
57ProtocolFactory*
Junxiao Shi38b24c72017-01-05 02:59:31 +000058FaceSystem::getFactoryById(const std::string& id)
Junxiao Shib8590312016-12-29 21:22:25 +000059{
Junxiao Shi38b24c72017-01-05 02:59:31 +000060 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000061 return found == m_factories.end() ? nullptr : found->second.get();
62}
63
Junxiao Shi38b24c72017-01-05 02:59:31 +000064ProtocolFactory*
65FaceSystem::getFactoryByScheme(const std::string& scheme)
66{
67 auto found = m_factoryByScheme.find(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +000068 return found == m_factoryByScheme.end() ? nullptr : found->second;
Junxiao Shi38b24c72017-01-05 02:59:31 +000069}
70
Junxiao Shib8590312016-12-29 21:22:25 +000071void
72FaceSystem::setConfigFile(ConfigFile& configFile)
73{
74 configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
75}
76
77void
78FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
79{
Junxiao Shi38b24c72017-01-05 02:59:31 +000080 ConfigContext context;
81 context.isDryRun = isDryRun;
82 context.addFace = bind(&FaceTable::add, &m_faceTable, _1);
Junxiao Shi7003c602017-01-10 13:35:28 +000083 context.m_netifs = listNetworkInterfaces();
Junxiao Shi38b24c72017-01-05 02:59:31 +000084
85 // process sections in protocol factories
86 for (const auto& pair : m_factories) {
87 const std::string& sectionName = pair.first;
Junxiao Shib47247d2017-01-24 15:09:16 +000088 ProtocolFactory* factory = pair.second.get();
Junxiao Shi38b24c72017-01-05 02:59:31 +000089
90 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
91 factory->processConfig(configSection.get_child_optional(sectionName), context);
92
93 if (!isDryRun) {
94 for (const std::string& scheme : factory->getProvidedSchemes()) {
95 m_factoryByScheme[scheme] = factory;
Junxiao Shib47247d2017-01-24 15:09:16 +000096 if (oldProvidedSchemes.erase(scheme) == 0) {
97 NFD_LOG_TRACE("factory " << sectionName <<
98 " provides " << scheme << " FaceUri scheme");
99 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000100 }
101 for (const std::string& scheme : oldProvidedSchemes) {
102 m_factoryByScheme.erase(scheme);
Junxiao Shib47247d2017-01-24 15:09:16 +0000103 NFD_LOG_TRACE("factory " << sectionName <<
104 " no longer provides " << scheme << " FaceUri scheme");
Junxiao Shi38b24c72017-01-05 02:59:31 +0000105 }
106 }
107 }
108
109 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000110 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000111 for (const auto& pair : configSection) {
112 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000113 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000114
Junxiao Shi38b24c72017-01-05 02:59:31 +0000115 if (!seenSections.insert(sectionName).second) {
116 BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000117 }
118
Junxiao Shi38b24c72017-01-05 02:59:31 +0000119 if (m_factories.count(sectionName) > 0) {
120 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000121 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000122
123 ///\todo #3521 nicfaces
124
Junxiao Shi64d99f22017-01-21 23:06:36 +0000125 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000126 }
127}
128
Junxiao Shib8590312016-12-29 21:22:25 +0000129} // namespace face
130} // namespace nfd