blob: 5359e2d2cd0fa102c575211c6122b9d66b765688 [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 Shi64d99f22017-01-21 23:06:36 +000027// #include "core/logger.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000028#include "fw/face-table.hpp"
29
30// ProtocolFactory includes, sorted alphabetically
31#ifdef HAVE_LIBPCAP
32#include "ethernet-factory.hpp"
Junxiao Shib8590312016-12-29 21:22:25 +000033#endif // HAVE_LIBPCAP
34#include "tcp-factory.hpp"
35#include "udp-factory.hpp"
36#ifdef HAVE_UNIX_SOCKETS
37#include "unix-stream-factory.hpp"
38#endif // HAVE_UNIX_SOCKETS
39#ifdef HAVE_WEBSOCKET
40#include "websocket-factory.hpp"
41#endif // HAVE_WEBSOCKET
42
43namespace nfd {
44namespace face {
45
Junxiao Shi64d99f22017-01-21 23:06:36 +000046// NFD_LOG_INIT("FaceSystem");
Junxiao Shib8590312016-12-29 21:22:25 +000047
48FaceSystem::FaceSystem(FaceTable& faceTable)
49 : m_faceTable(faceTable)
50{
Junxiao Shi38b24c72017-01-05 02:59:31 +000051 ///\todo #3904 make a registry, and construct instances from registry
Junxiao Shi7003c602017-01-10 13:35:28 +000052
53#ifdef HAVE_LIBPCAP
54 m_factories["ether"] = make_shared<EthernetFactory>();
55#endif // HAVE_LIBPCAP
56
Junxiao Shi38b24c72017-01-05 02:59:31 +000057 m_factories["tcp"] = make_shared<TcpFactory>();
Junxiao Shi406deb52017-01-05 20:07:44 +000058
Junxiao Shi64d99f22017-01-21 23:06:36 +000059 m_factories["udp"] = make_shared<UdpFactory>();
60
Junxiao Shi406deb52017-01-05 20:07:44 +000061#ifdef HAVE_UNIX_SOCKETS
62 m_factories["unix"] = make_shared<UnixStreamFactory>();
63#endif // HAVE_UNIX_SOCKETS
Junxiao Shi3409cd32017-01-18 15:31:27 +000064
65#ifdef HAVE_WEBSOCKET
66 m_factories["websocket"] = make_shared<WebSocketFactory>();
67#endif // HAVE_WEBSOCKET
Junxiao Shib8590312016-12-29 21:22:25 +000068}
69
70std::set<const ProtocolFactory*>
71FaceSystem::listProtocolFactories() const
72{
73 std::set<const ProtocolFactory*> factories;
Junxiao Shi38b24c72017-01-05 02:59:31 +000074 for (const auto& p : m_factoryByScheme) {
Junxiao Shib8590312016-12-29 21:22:25 +000075 factories.insert(p.second.get());
76 }
77 return factories;
78}
79
80ProtocolFactory*
Junxiao Shi38b24c72017-01-05 02:59:31 +000081FaceSystem::getFactoryById(const std::string& id)
Junxiao Shib8590312016-12-29 21:22:25 +000082{
Junxiao Shi38b24c72017-01-05 02:59:31 +000083 auto found = m_factories.find(id);
Junxiao Shib8590312016-12-29 21:22:25 +000084 return found == m_factories.end() ? nullptr : found->second.get();
85}
86
Junxiao Shi38b24c72017-01-05 02:59:31 +000087ProtocolFactory*
88FaceSystem::getFactoryByScheme(const std::string& scheme)
89{
90 auto found = m_factoryByScheme.find(scheme);
91 return found == m_factoryByScheme.end() ? nullptr : found->second.get();
92}
93
Junxiao Shib8590312016-12-29 21:22:25 +000094void
95FaceSystem::setConfigFile(ConfigFile& configFile)
96{
97 configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3));
98}
99
100void
101FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
102{
Junxiao Shi38b24c72017-01-05 02:59:31 +0000103 ConfigContext context;
104 context.isDryRun = isDryRun;
105 context.addFace = bind(&FaceTable::add, &m_faceTable, _1);
Junxiao Shi7003c602017-01-10 13:35:28 +0000106 context.m_netifs = listNetworkInterfaces();
Junxiao Shi38b24c72017-01-05 02:59:31 +0000107
108 // process sections in protocol factories
109 for (const auto& pair : m_factories) {
110 const std::string& sectionName = pair.first;
111 shared_ptr<ProtocolFactory> factory = pair.second;
112
113 std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes();
114 factory->processConfig(configSection.get_child_optional(sectionName), context);
115
116 if (!isDryRun) {
117 for (const std::string& scheme : factory->getProvidedSchemes()) {
118 m_factoryByScheme[scheme] = factory;
119 oldProvidedSchemes.erase(scheme);
120 }
121 for (const std::string& scheme : oldProvidedSchemes) {
122 m_factoryByScheme.erase(scheme);
123 }
124 }
125 }
126
127 // process other sections
Junxiao Shib8590312016-12-29 21:22:25 +0000128 std::set<std::string> seenSections;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000129 for (const auto& pair : configSection) {
130 const std::string& sectionName = pair.first;
Junxiao Shi64d99f22017-01-21 23:06:36 +0000131 // const ConfigSection& subSection = pair.second;
Junxiao Shib8590312016-12-29 21:22:25 +0000132
Junxiao Shi38b24c72017-01-05 02:59:31 +0000133 if (!seenSections.insert(sectionName).second) {
134 BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000135 }
136
Junxiao Shi38b24c72017-01-05 02:59:31 +0000137 if (m_factories.count(sectionName) > 0) {
138 continue;
Junxiao Shib8590312016-12-29 21:22:25 +0000139 }
Junxiao Shi38b24c72017-01-05 02:59:31 +0000140
141 ///\todo #3521 nicfaces
142
Junxiao Shi64d99f22017-01-21 23:06:36 +0000143 BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName));
Junxiao Shib8590312016-12-29 21:22:25 +0000144 }
145}
146
Junxiao Shib8590312016-12-29 21:22:25 +0000147} // namespace face
148} // namespace nfd