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