protocol-factory.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
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 #ifndef NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
27 #define NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
28 
29 #include "channel.hpp"
30 #include "face.hpp"
31 #include "face-system.hpp"
32 
33 #include <boost/range/adaptor/map.hpp>
34 #include <boost/range/algorithm/copy.hpp>
35 
36 namespace nfd::face {
37 
48 {
50  shared_ptr<ndn::net::NetworkMonitor> netmon;
51 };
52 
62 class ProtocolFactory : noncopyable
63 {
64 public: // registry
66 
72  template<typename PF>
73  static void
74  registerType(const std::string& id = PF::getId())
75  {
76  BOOST_ASSERT(!id.empty());
77  auto r = getRegistry().insert_or_assign(id, [] (auto&&... p) {
78  return make_unique<PF>(std::forward<decltype(p)>(p)...);
79  });
80  BOOST_VERIFY(r.second);
81  }
82 
87  static unique_ptr<ProtocolFactory>
88  create(const std::string& id, const CtorParams& params);
89 
93  [[nodiscard]] static std::set<std::string>
95 
96 public:
100  class Error : public std::runtime_error
101  {
102  public:
103  using std::runtime_error::runtime_error;
104  };
105 
106  explicit
107  ProtocolFactory(const CtorParams& params);
108 
109  virtual
111 
112 #ifdef DOXYGEN
118  static const std::string&
119  getId() noexcept;
120 #endif
121 
125  const std::set<std::string>&
126  getProvidedSchemes() const noexcept
127  {
128  return providedSchemes;
129  }
130 
136  void
138 
145  {
146  FaceUri remoteUri;
147  std::optional<FaceUri> localUri;
149  };
150 
157  void
158  createFace(const CreateFaceRequest& req,
159  const FaceCreatedCallback& onCreated,
160  const FaceCreationFailedCallback& onFailure);
161 
170  shared_ptr<Face>
171  createNetdevBoundFace(const FaceUri& remote,
172  const shared_ptr<const ndn::net::NetworkInterface>& netdev);
173 
176  std::vector<shared_ptr<const Channel>>
177  getChannels() const;
178 
179 protected:
180  template<typename ChannelMap>
181  static std::vector<shared_ptr<const Channel>>
182  getChannelsFromMap(const ChannelMap& channelMap)
183  {
184  std::vector<shared_ptr<const Channel>> channels;
185  boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels));
186  return channels;
187  }
188 
189 private:
200  virtual void
201  doProcessConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext& context);
202 
209  virtual void
210  doCreateFace(const CreateFaceRequest& req,
211  const FaceCreatedCallback& onCreated,
212  const FaceCreationFailedCallback& onFailure);
213 
229  virtual shared_ptr<Face>
230  doCreateNetdevBoundFace(const FaceUri& remote,
231  const shared_ptr<const ndn::net::NetworkInterface>& netif);
232 
238  virtual std::vector<shared_ptr<const Channel>>
239  doGetChannels() const;
240 
241 private: // registry
242  using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>;
243  using Registry = std::map<std::string, CreateFunc>; // indexed by factory id
244 
245  static Registry&
246  getRegistry();
247 
248 protected:
249  std::set<std::string> providedSchemes;
251 
257  shared_ptr<ndn::net::NetworkMonitor> netmon;
258 };
259 
260 } // namespace nfd::face
261 
266 #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
267 static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
268 { \
269 public: \
270  NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
271  { \
272  ::nfd::face::ProtocolFactory::registerType<PF>(); \
273  } \
274 } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
275 
276 #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
Context for processing a config section in ProtocolFactory.
Definition: face-system.hpp:99
Base class for all exceptions thrown by ProtocolFactory subclasses.
Provides support for an underlying protocol.
shared_ptr< Face > createNetdevBoundFace(const FaceUri &remote, const shared_ptr< const ndn::net::NetworkInterface > &netdev)
Create a netdev-bound face.
ProtocolFactory(const CtorParams &params)
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure)
Create a unicast face.
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
Process face_system subsection that corresponds to this protocol factory id.
std::set< std::string > providedSchemes
FaceUri schemes provided by this protocol factory.
FaceCreatedCallback addFace
callback when a new face is created
static const std::string & getId() noexcept
Get the ID of this protocol factory.
static std::set< std::string > listRegistered()
Get all registered protocol factory IDs.
ProtocolFactoryCtorParams CtorParams
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
std::vector< shared_ptr< const Channel > > getChannels() const
Get list of open channels (listening + non-listening)
const std::set< std::string > & getProvidedSchemes() const noexcept
Get FaceUri schemes accepted by this protocol factory.
static void registerType(const std::string &id=PF::getId())
Register a protocol factory type.
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
std::function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when a face fails to be created.
Definition: channel.hpp:92
std::function< void(const shared_ptr< Face > &)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:88
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:43
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:85
Encapsulates a face creation request and all its parameters.
Parameters to ProtocolFactory constructor.
shared_ptr< ndn::net::NetworkMonitor > netmon