Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 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" |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame^] | 27 | // #include "core/logger.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 28 | #include "fw/face-table.hpp" |
| 29 | |
| 30 | // ProtocolFactory includes, sorted alphabetically |
| 31 | #ifdef HAVE_LIBPCAP |
| 32 | #include "ethernet-factory.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 33 | #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 | |
| 43 | namespace nfd { |
| 44 | namespace face { |
| 45 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame^] | 46 | // NFD_LOG_INIT("FaceSystem"); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 47 | |
| 48 | FaceSystem::FaceSystem(FaceTable& faceTable) |
| 49 | : m_faceTable(faceTable) |
| 50 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 51 | ///\todo #3904 make a registry, and construct instances from registry |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef HAVE_LIBPCAP |
| 54 | m_factories["ether"] = make_shared<EthernetFactory>(); |
| 55 | #endif // HAVE_LIBPCAP |
| 56 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 57 | m_factories["tcp"] = make_shared<TcpFactory>(); |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 58 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame^] | 59 | m_factories["udp"] = make_shared<UdpFactory>(); |
| 60 | |
Junxiao Shi | 406deb5 | 2017-01-05 20:07:44 +0000 | [diff] [blame] | 61 | #ifdef HAVE_UNIX_SOCKETS |
| 62 | m_factories["unix"] = make_shared<UnixStreamFactory>(); |
| 63 | #endif // HAVE_UNIX_SOCKETS |
Junxiao Shi | 3409cd3 | 2017-01-18 15:31:27 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef HAVE_WEBSOCKET |
| 66 | m_factories["websocket"] = make_shared<WebSocketFactory>(); |
| 67 | #endif // HAVE_WEBSOCKET |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::set<const ProtocolFactory*> |
| 71 | FaceSystem::listProtocolFactories() const |
| 72 | { |
| 73 | std::set<const ProtocolFactory*> factories; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 74 | for (const auto& p : m_factoryByScheme) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 75 | factories.insert(p.second.get()); |
| 76 | } |
| 77 | return factories; |
| 78 | } |
| 79 | |
| 80 | ProtocolFactory* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 81 | FaceSystem::getFactoryById(const std::string& id) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 82 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 83 | auto found = m_factories.find(id); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 84 | return found == m_factories.end() ? nullptr : found->second.get(); |
| 85 | } |
| 86 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 87 | ProtocolFactory* |
| 88 | FaceSystem::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 Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 94 | void |
| 95 | FaceSystem::setConfigFile(ConfigFile& configFile) |
| 96 | { |
| 97 | configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3)); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename) |
| 102 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 103 | ConfigContext context; |
| 104 | context.isDryRun = isDryRun; |
| 105 | context.addFace = bind(&FaceTable::add, &m_faceTable, _1); |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 106 | context.m_netifs = listNetworkInterfaces(); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 107 | |
| 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 Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 128 | std::set<std::string> seenSections; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 129 | for (const auto& pair : configSection) { |
| 130 | const std::string& sectionName = pair.first; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame^] | 131 | // const ConfigSection& subSection = pair.second; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 132 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 133 | if (!seenSections.insert(sectionName).second) { |
| 134 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 137 | if (m_factories.count(sectionName) > 0) { |
| 138 | continue; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 139 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 140 | |
| 141 | ///\todo #3521 nicfaces |
| 142 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame^] | 143 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 147 | } // namespace face |
| 148 | } // namespace nfd |