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 | /* |
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 | 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 | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 36 | FaceSystem::FaceSystem(FaceTable& faceTable, const shared_ptr<ndn::net::NetworkMonitor>& netmon) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 37 | : m_faceTable(faceTable) |
| 38 | { |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 39 | BOOST_ASSERT(netmon != nullptr); |
| 40 | |
| 41 | auto addFace = bind(&FaceTable::add, &m_faceTable, _1); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 42 | for (const std::string& id : ProtocolFactory::listRegistered()) { |
| 43 | NFD_LOG_TRACE("creating factory " << id); |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 44 | m_factories[id] = ProtocolFactory::create(id, netmon, addFace); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 45 | } |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 48 | FaceSystem::FaceSystem(FaceTable& faceTable) |
| 49 | : FaceSystem(faceTable, make_shared<ndn::net::NetworkMonitor>(getGlobalIoService())) |
| 50 | { |
| 51 | } |
| 52 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 53 | FaceSystem::~FaceSystem() = default; |
| 54 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 55 | std::set<const ProtocolFactory*> |
| 56 | FaceSystem::listProtocolFactories() const |
| 57 | { |
| 58 | std::set<const ProtocolFactory*> factories; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 59 | for (const auto& p : m_factories) { |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 60 | factories.insert(p.second.get()); |
| 61 | } |
| 62 | return factories; |
| 63 | } |
| 64 | |
| 65 | ProtocolFactory* |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 66 | FaceSystem::getFactoryById(const std::string& id) |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 67 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 68 | auto found = m_factories.find(id); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 69 | return found == m_factories.end() ? nullptr : found->second.get(); |
| 70 | } |
| 71 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 72 | ProtocolFactory* |
| 73 | FaceSystem::getFactoryByScheme(const std::string& scheme) |
| 74 | { |
| 75 | auto found = m_factoryByScheme.find(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 76 | return found == m_factoryByScheme.end() ? nullptr : found->second; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 79 | void |
| 80 | FaceSystem::setConfigFile(ConfigFile& configFile) |
| 81 | { |
| 82 | configFile.addSectionHandler("face_system", bind(&FaceSystem::processConfig, this, _1, _2, _3)); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | FaceSystem::processConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename) |
| 87 | { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 88 | ConfigContext context; |
| 89 | context.isDryRun = isDryRun; |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 90 | context.m_netifs = listNetworkInterfaces(); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 91 | |
| 92 | // process sections in protocol factories |
| 93 | for (const auto& pair : m_factories) { |
| 94 | const std::string& sectionName = pair.first; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 95 | ProtocolFactory* factory = pair.second.get(); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 96 | |
| 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 Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 103 | if (oldProvidedSchemes.erase(scheme) == 0) { |
| 104 | NFD_LOG_TRACE("factory " << sectionName << |
| 105 | " provides " << scheme << " FaceUri scheme"); |
| 106 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 107 | } |
| 108 | for (const std::string& scheme : oldProvidedSchemes) { |
| 109 | m_factoryByScheme.erase(scheme); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 110 | NFD_LOG_TRACE("factory " << sectionName << |
| 111 | " no longer provides " << scheme << " FaceUri scheme"); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // process other sections |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 117 | std::set<std::string> seenSections; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 118 | for (const auto& pair : configSection) { |
| 119 | const std::string& sectionName = pair.first; |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 120 | // const ConfigSection& subSection = pair.second; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 121 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 122 | if (!seenSections.insert(sectionName).second) { |
| 123 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Duplicate section face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 126 | if (m_factories.count(sectionName) > 0) { |
| 127 | continue; |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 128 | } |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 129 | |
| 130 | ///\todo #3521 nicfaces |
| 131 | |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 132 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option face_system." + sectionName)); |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Junxiao Shi | b859031 | 2016-12-29 21:22:25 +0000 | [diff] [blame] | 136 | } // namespace face |
| 137 | } // namespace nfd |