Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [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, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |
| 27 | #define NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 29 | #include "channel.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 30 | #include "face-system.hpp" |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 31 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 32 | #include <boost/range/adaptor/map.hpp> |
| 33 | #include <boost/range/algorithm/copy.hpp> |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 34 | #include <ndn-cxx/encoding/nfd-constants.hpp> |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 36 | namespace nfd { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 37 | namespace face { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 38 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 39 | /** \brief Provides support for an underlying protocol |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 40 | * \sa FaceSystem |
| 41 | * |
| 42 | * A protocol factory provides support for an underlying protocol and owns Channel objects. |
| 43 | * It can process a subsection of face_system config section and create channels and multicast |
| 44 | * faces accordingly. |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 45 | */ |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 46 | class ProtocolFactory : noncopyable |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 47 | { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 48 | public: // registry |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 49 | /** \brief Register a protocol factory type |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 50 | * \tparam S subclass of ProtocolFactory |
| 51 | * \param id factory identifier |
| 52 | */ |
| 53 | template<typename PF> |
| 54 | static void |
| 55 | registerType(const std::string& id = PF::getId()) |
| 56 | { |
| 57 | Registry& registry = getRegistry(); |
| 58 | BOOST_ASSERT(registry.count(id) == 0); |
| 59 | registry[id] = &make_unique<PF>; |
| 60 | } |
| 61 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 62 | /** \brief Create a protocol factory instance |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 63 | * \retval nullptr if factory with \p id is not registered |
| 64 | */ |
| 65 | static unique_ptr<ProtocolFactory> |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 66 | create(const std::string& id, shared_ptr<ndn::net::NetworkMonitor> netmon, |
| 67 | const FaceCreatedCallback& addFace); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 68 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 69 | /** \brief Get registered protocol factory ids |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 70 | */ |
| 71 | static std::set<std::string> |
| 72 | listRegistered(); |
| 73 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 74 | public: |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 75 | /** |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 76 | * \brief Base class for all exceptions thrown by ProtocolFactory subclasses |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 77 | */ |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 78 | class Error : public std::runtime_error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 79 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 80 | public: |
| 81 | explicit |
| 82 | Error(const std::string& what) |
| 83 | : std::runtime_error(what) |
| 84 | { |
| 85 | } |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 86 | }; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 87 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 88 | virtual |
| 89 | ~ProtocolFactory() = default; |
| 90 | |
| 91 | #ifdef DOXYGEN |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 92 | /** \brief Get id for this ProtocolFactory |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 93 | * |
| 94 | * face_system.factory-id config section is processed by the protocol factory. |
| 95 | */ |
| 96 | static const std::string& |
| 97 | getId(); |
| 98 | #endif |
| 99 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 100 | /** \brief Process face_system subsection that corresponds to this ProtocolFactory type |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 101 | * \param configSection the configuration section or boost::null to indicate it is omitted |
| 102 | * \param context provides access to data structures and contextual information |
| 103 | * \throw ConfigFile::Error invalid configuration |
| 104 | * |
| 105 | * This function updates \p providedSchemes |
| 106 | */ |
| 107 | virtual void |
| 108 | processConfig(OptionalConfigSection configSection, |
Junxiao Shi | c344304 | 2017-01-26 17:21:35 +0000 | [diff] [blame] | 109 | FaceSystem::ConfigContext& context) = 0; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 110 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 111 | /** \brief Get FaceUri schemes accepted by this ProtocolFactory |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 112 | */ |
| 113 | const std::set<std::string>& |
| 114 | getProvidedSchemes() |
| 115 | { |
| 116 | return providedSchemes; |
| 117 | } |
| 118 | |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 119 | /** \brief Try to create Face using the supplied FaceUri |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 120 | * |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 121 | * \param remoteUri remote URI of the new face |
| 122 | * \param localUri local URI of the new face |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 123 | * \param persistency persistency of the new face |
| 124 | * \param wantLocalFieldsEnabled whether local fields should be enabled on the face |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 125 | * \param onCreated callback if face creation succeeds or face already exists; |
| 126 | * persistency and local fields settings are not updated on an existing face |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 127 | * \param onFailure callback if face creation fails |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 128 | */ |
| 129 | virtual void |
Eric Newberry | 78e32b0 | 2017-04-01 14:34:44 +0000 | [diff] [blame] | 130 | createFace(const FaceUri& remoteUri, |
| 131 | const ndn::optional<FaceUri>& localUri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 132 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 133 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 134 | const FaceCreatedCallback& onCreated, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 135 | const FaceCreationFailedCallback& onFailure) = 0; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 136 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 137 | virtual std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 138 | getChannels() const = 0; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 139 | |
| 140 | protected: |
| 141 | template<typename ChannelMap> |
| 142 | static std::vector<shared_ptr<const Channel>> |
| 143 | getChannelsFromMap(const ChannelMap& channelMap) |
| 144 | { |
| 145 | std::vector<shared_ptr<const Channel>> channels; |
| 146 | boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels)); |
| 147 | return channels; |
| 148 | } |
| 149 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 150 | private: // registry |
| 151 | typedef std::function<unique_ptr<ProtocolFactory>()> CreateFunc; |
| 152 | typedef std::map<std::string, CreateFunc> Registry; // indexed by factory id |
| 153 | |
| 154 | static Registry& |
| 155 | getRegistry(); |
| 156 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 157 | protected: |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 158 | std::set<std::string> providedSchemes; ///< FaceUri schemes provided by this ProtocolFactory |
| 159 | |
| 160 | /** \brief NetworkMonitor for listing available network interfaces and monitoring their changes |
| 161 | * |
| 162 | * ProtocolFactory subclass should check the NetworkMonitor has sufficient capabilities prior |
| 163 | * to usage. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 164 | */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame^] | 165 | shared_ptr<ndn::net::NetworkMonitor> netmon; |
| 166 | |
| 167 | FaceCreatedCallback addFace; ///< callback when a new face is created |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 170 | } // namespace face |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 171 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 172 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 173 | /** \brief registers a protocol factory |
| 174 | * |
| 175 | * This macro should appear once in .cpp of each protocol factory. |
| 176 | */ |
| 177 | #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \ |
| 178 | static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \ |
| 179 | { \ |
| 180 | public: \ |
| 181 | NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \ |
| 182 | { \ |
| 183 | ::nfd::face::ProtocolFactory::registerType<PF>(); \ |
| 184 | } \ |
| 185 | } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable |
| 186 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 187 | #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |