Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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 | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 27 | #include "protocol-factory.hpp" |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 28 | #include "core/global-io.hpp" |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 29 | #include "fw/face-table.hpp" |
| 30 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 31 | namespace nfd { |
| 32 | namespace face { |
| 33 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 34 | NFD_LOG_INIT("FaceSystem"); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 35 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 36 | FaceSystem::FaceSystem(FaceTable& faceTable, shared_ptr<ndn::net::NetworkMonitor> netmon) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 37 | : m_faceTable(faceTable) |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 38 | , m_netmon(std::move(netmon)) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 39 | { |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 40 | auto pfCtorParams = this->makePFCtorParams(); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 41 | for (const std::string& id : ProtocolFactory::listRegistered()) { |
| 42 | NFD_LOG_TRACE("creating factory " << id); |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 43 | m_factories[id] = ProtocolFactory::create(id, pfCtorParams); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 44 | } |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 47 | ProtocolFactoryCtorParams |
| 48 | FaceSystem::makePFCtorParams() |
| 49 | { |
| 50 | auto addFace = bind(&FaceTable::add, &m_faceTable, _1); |
| 51 | return {addFace, m_netmon}; |
| 52 | } |
| 53 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 54 | FaceSystem::~FaceSystem() = default; |
| 55 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 56 | std::set<const ProtocolFactory*> |
| 57 | FaceSystem::listProtocolFactories() const |
| 58 | { |
| 59 | std::set<const ProtocolFactory*> factories; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 60 | for (const auto& p : m_factories) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 61 | factories.insert(p.second.get()); |
| 62 | } |
| 63 | return factories; |
| 64 | } |
| 65 | |
| 66 | ProtocolFactory* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 67 | FaceSystem::getFactoryById(const std::string& id) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 68 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 69 | auto found = m_factories.find(id); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 70 | return found == m_factories.end() ? nullptr : found->second.get(); |
| 71 | } |
| 72 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 73 | ProtocolFactory* |
| 74 | FaceSystem::getFactoryByScheme(const std::string& scheme) |
| 75 | { |
| 76 | auto found = m_factoryByScheme.find(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 77 | return found == m_factoryByScheme.end() ? nullptr : found->second; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 80 | void |
| 81 | FaceSystem::setConfigFile(ConfigFile& configFile) |
| 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 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 89 | ConfigContext context; |
| 90 | context.isDryRun = isDryRun; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 91 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 92 | // process general protocol factory config section |
| 93 | auto generalSection = configSection.get_child_optional("general"); |
| 94 | if (generalSection) { |
| 95 | for (const auto& pair : *generalSection) { |
| 96 | const std::string& key = pair.first; |
| 97 | if (key == "enable_congestion_marking") { |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 98 | context.generalConfig.wantCongestionMarking = ConfigFile::parseYesNo(pair, "face_system.general"); |
| 99 | } |
| 100 | else { |
| 101 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system.general." + key)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 106 | // process sections in protocol factories |
| 107 | for (const auto& pair : m_factories) { |
| 108 | const std::string& sectionName = pair.first; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 109 | ProtocolFactory* factory = pair.second.get(); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 110 | |
| 111 | std::set<std::string> oldProvidedSchemes = factory->getProvidedSchemes(); |
| 112 | factory->processConfig(configSection.get_child_optional(sectionName), context); |
| 113 | |
| 114 | if (!isDryRun) { |
| 115 | for (const std::string& scheme : factory->getProvidedSchemes()) { |
| 116 | m_factoryByScheme[scheme] = factory; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 117 | if (oldProvidedSchemes.erase(scheme) == 0) { |
| 118 | NFD_LOG_TRACE("factory " << sectionName << |
| 119 | " provides " << scheme << " FaceUri scheme"); |
| 120 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 121 | } |
| 122 | for (const std::string& scheme : oldProvidedSchemes) { |
| 123 | m_factoryByScheme.erase(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 124 | NFD_LOG_TRACE("factory " << sectionName << |
| 125 | " no longer provides " << scheme << " FaceUri scheme"); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // process other sections |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 131 | std::set<std::string> seenSections; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 132 | for (const auto& pair : configSection) { |
| 133 | const std::string& sectionName = pair.first; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 134 | // const ConfigSection& subSection = pair.second; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 135 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 136 | if (!seenSections.insert(sectionName).second) { |
| 137 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Eric Newberry | 0c84164 | 2018-01-17 15:01:00 -0700 | [diff] [blame] | 140 | if (sectionName == "general" || m_factories.count(sectionName) > 0) { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 141 | continue; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 142 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 143 | |
| 144 | ///\todo #3521 nicfaces |
| 145 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 146 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 150 | } // namespace face |
| 151 | } // namespace nfd |