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 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, 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" |
Davide Pesavento | cb425e8 | 2019-07-14 21:48:22 -0400 | [diff] [blame] | 30 | #include "face.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 31 | #include "face-system.hpp" |
Davide Pesavento | e5eebad | 2017-04-06 20:23:26 -0400 | [diff] [blame] | 32 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 33 | #include <boost/range/adaptor/map.hpp> |
| 34 | #include <boost/range/algorithm/copy.hpp> |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame^] | 36 | #include <functional> |
| 37 | #include <map> |
| 38 | #include <set> |
| 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | namespace nfd::face { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 41 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 42 | /** |
| 43 | * \brief Parameters to ProtocolFactory constructor. |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 44 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 45 | * Every ProtocolFactory subclass is expected to have a constructor that accepts |
| 46 | * ProtocolFactory::CtorParams, which in turn passes it to ProtocolFactory base class |
| 47 | * constructor. Parameters are passed as a struct rather than individually, so that |
| 48 | * any future changes in the list of parameters will not require updates to all subclass |
| 49 | * constructors. |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 50 | */ |
| 51 | struct ProtocolFactoryCtorParams |
| 52 | { |
| 53 | FaceCreatedCallback addFace; |
| 54 | shared_ptr<ndn::net::NetworkMonitor> netmon; |
| 55 | }; |
| 56 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 57 | /** |
| 58 | * \brief Provides support for an underlying protocol. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 59 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 60 | * A protocol factory provides support for an underlying protocol and owns Channel objects. |
| 61 | * It can process a subsection of the `face_system` configuration section and create channels |
| 62 | * and multicast faces accordingly. |
| 63 | * |
| 64 | * \sa FaceSystem |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 65 | */ |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 66 | class ProtocolFactory : noncopyable |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 67 | { |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 68 | public: // registry |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 69 | using CtorParams = ProtocolFactoryCtorParams; |
| 70 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 71 | /** |
| 72 | * \brief Register a protocol factory type. |
| 73 | * \tparam PF subclass of ProtocolFactory |
| 74 | * \param id factory identifier |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 75 | */ |
| 76 | template<typename PF> |
| 77 | static void |
| 78 | registerType(const std::string& id = PF::getId()) |
| 79 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 80 | BOOST_ASSERT(!id.empty()); |
| 81 | auto r = getRegistry().insert_or_assign(id, [] (auto&&... p) { |
| 82 | return make_unique<PF>(std::forward<decltype(p)>(p)...); |
| 83 | }); |
| 84 | BOOST_VERIFY(r.second); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 87 | /** |
| 88 | * \brief Create a protocol factory instance. |
| 89 | * \retval nullptr if a factory with the given \p id is not registered |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 90 | */ |
| 91 | static unique_ptr<ProtocolFactory> |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 92 | create(const std::string& id, const CtorParams& params); |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 93 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 94 | /** |
| 95 | * \brief Get all registered protocol factory IDs. |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 96 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 97 | [[nodiscard]] static std::set<std::string> |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 98 | listRegistered(); |
| 99 | |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 100 | public: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 101 | /** |
| 102 | * \brief Base class for all exceptions thrown by ProtocolFactory subclasses. |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 103 | */ |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 104 | class Error : public std::runtime_error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 105 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 106 | public: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 107 | using std::runtime_error::runtime_error; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 108 | }; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 109 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 110 | explicit |
| 111 | ProtocolFactory(const CtorParams& params); |
| 112 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 113 | virtual |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 114 | ~ProtocolFactory() = 0; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 115 | |
| 116 | #ifdef DOXYGEN |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 117 | /** |
| 118 | * \brief Get the ID of this protocol factory. |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 119 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 120 | * `face_system.<factory-id>` config section is processed by the protocol factory. |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 121 | */ |
| 122 | static const std::string& |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 123 | getId() noexcept; |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 124 | #endif |
| 125 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 126 | /** |
| 127 | * \brief Get FaceUri schemes accepted by this protocol factory. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 128 | */ |
| 129 | const std::set<std::string>& |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 130 | getProvidedSchemes() const noexcept |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 131 | { |
| 132 | return providedSchemes; |
| 133 | } |
| 134 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 135 | /** \brief Process face_system subsection that corresponds to this protocol factory id |
| 136 | * \param configSection the configuration section or boost::none to indicate it is omitted |
| 137 | * \param context provides access to data structures and contextual information |
| 138 | * \throw ConfigFile::Error invalid configuration |
| 139 | */ |
| 140 | void |
| 141 | processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext& context); |
| 142 | |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 143 | /** \brief Encapsulates a face creation request and all its parameters |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 144 | * |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 145 | * Parameters are passed as a struct rather than individually, so that a future change in the list |
| 146 | * of parameters does not require an update to the method signature in all subclasses. |
| 147 | */ |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 148 | struct CreateFaceRequest |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 149 | { |
| 150 | FaceUri remoteUri; |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 151 | std::optional<FaceUri> localUri; |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 152 | FaceParams params; |
Eric Newberry | 944f38b | 2017-07-20 20:54:22 -0400 | [diff] [blame] | 153 | }; |
| 154 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 155 | /** \brief Create a unicast face |
| 156 | * \param req request object containing the face creation parameters |
| 157 | * \param onCreated callback if face creation succeeds or face already exists; the settings |
| 158 | * of an existing face are not updated if they differ from the request |
| 159 | * \param onFailure callback if face creation fails |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 160 | */ |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 161 | void |
Davide Pesavento | 15b5505 | 2018-01-27 19:09:28 -0500 | [diff] [blame] | 162 | createFace(const CreateFaceRequest& req, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 163 | const FaceCreatedCallback& onCreated, |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 164 | const FaceCreationFailedCallback& onFailure); |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 165 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 166 | /** \brief Create a netdev-bound face |
| 167 | * \param remote remote FaceUri, must be canonical |
| 168 | * \param netdev local network interface |
| 169 | * \return new face |
| 170 | * \throw Error cannot create a face using specified arguments |
| 171 | * \note The caller must ensure there is no existing netdev-bound face with same remote FaceUri |
| 172 | * on the same local network interface. |
| 173 | */ |
| 174 | shared_ptr<Face> |
| 175 | createNetdevBoundFace(const FaceUri& remote, |
| 176 | const shared_ptr<const ndn::net::NetworkInterface>& netdev); |
| 177 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 178 | /** \brief Get list of open channels (listening + non-listening) |
| 179 | */ |
| 180 | std::vector<shared_ptr<const Channel>> |
| 181 | getChannels() const; |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 182 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 183 | protected: |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 184 | template<typename ChannelMap> |
| 185 | static std::vector<shared_ptr<const Channel>> |
| 186 | getChannelsFromMap(const ChannelMap& channelMap) |
| 187 | { |
| 188 | std::vector<shared_ptr<const Channel>> channels; |
| 189 | boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels)); |
| 190 | return channels; |
| 191 | } |
| 192 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 193 | private: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 194 | /** \brief Process face_system subsection that corresponds to this protocol factory id |
| 195 | * \sa processConfig |
| 196 | * |
| 197 | * A subclass should override this method if it supports configuration options in the config |
| 198 | * file, and do all the required processing here. The subclass should throw ConfigFile::Error |
| 199 | * if it encounters unrecognized options or invalid values. It should also update |
| 200 | * \p providedSchemes as needed. |
| 201 | * |
| 202 | * The base class implementation does nothing. |
| 203 | */ |
| 204 | virtual void |
| 205 | doProcessConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext& context); |
| 206 | |
| 207 | /** \brief Create a unicast face |
| 208 | * \sa createFace |
| 209 | * |
| 210 | * The base class implementation always invokes the failure callback with error code 406, |
| 211 | * indicating unicast face creation is not supported. |
| 212 | */ |
| 213 | virtual void |
| 214 | doCreateFace(const CreateFaceRequest& req, |
| 215 | const FaceCreatedCallback& onCreated, |
| 216 | const FaceCreationFailedCallback& onFailure); |
| 217 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 218 | /** \brief Create a netdev-bound face |
| 219 | * \sa createNetdevBoundFace |
| 220 | * |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 221 | * The base class implementation always throws Error, indicating netdev-bound faces are not |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 222 | * supported. |
| 223 | * |
| 224 | * A subclass that offers netdev-bound faces should override this method, and also expose |
| 225 | * "scheme+dev" in providedSchemes. For example, UdpFactory should provide "udp4+dev" scheme, |
| 226 | * in addition to "udp4" scheme. |
| 227 | * |
| 228 | * The face should be constructed immediately. Face persistency shall be reported as PERMANENT. |
| 229 | * Face state shall remain DOWN until underlying transport is connected. The face shall remain |
| 230 | * open until after .close() is invoked, and survive all socket errors; in case the network |
| 231 | * interface disappears, the face shall remain DOWN until .close() is invoked. |
| 232 | */ |
| 233 | virtual shared_ptr<Face> |
| 234 | doCreateNetdevBoundFace(const FaceUri& remote, |
| 235 | const shared_ptr<const ndn::net::NetworkInterface>& netif); |
| 236 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 237 | /** \brief Get list of open channels (listening + non-listening) |
| 238 | * \sa getChannels |
| 239 | * |
| 240 | * The base class implementation returns an empty list. |
| 241 | */ |
| 242 | virtual std::vector<shared_ptr<const Channel>> |
| 243 | doGetChannels() const; |
| 244 | |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 245 | private: // registry |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 246 | using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>; |
| 247 | using Registry = std::map<std::string, CreateFunc>; // indexed by factory id |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 248 | |
| 249 | static Registry& |
| 250 | getRegistry(); |
| 251 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 252 | protected: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 253 | std::set<std::string> providedSchemes; ///< FaceUri schemes provided by this protocol factory |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 254 | FaceCreatedCallback addFace; ///< callback when a new face is created |
| 255 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 256 | /** \brief NetworkMonitor for listing available network interfaces and monitoring their changes |
| 257 | * |
| 258 | * ProtocolFactory subclass should check the NetworkMonitor has sufficient capabilities prior |
| 259 | * to usage. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 260 | */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 261 | shared_ptr<ndn::net::NetworkMonitor> netmon; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 264 | } // namespace nfd::face |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 265 | |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 266 | /** \brief Registers a protocol factory |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 267 | * |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 268 | * This macro should appear once in the .cpp file of each protocol factory. |
Junxiao Shi | b47247d | 2017-01-24 15:09:16 +0000 | [diff] [blame] | 269 | */ |
| 270 | #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \ |
| 271 | static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \ |
| 272 | { \ |
| 273 | public: \ |
| 274 | NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \ |
| 275 | { \ |
| 276 | ::nfd::face::ProtocolFactory::registerType<PF>(); \ |
| 277 | } \ |
| 278 | } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable |
| 279 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 280 | #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |