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-2024, 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 #include <functional>
37 #include <map>
38 #include <set>
39 
40 namespace nfd::face {
41 
52 {
54  shared_ptr<ndn::net::NetworkMonitor> netmon;
55 };
56 
66 class ProtocolFactory : noncopyable
67 {
68 public: // registry
70 
76  template<typename PF>
77  static void
78  registerType(const std::string& id = PF::getId())
79  {
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);
85  }
86 
91  static unique_ptr<ProtocolFactory>
92  create(const std::string& id, const CtorParams& params);
93 
97  [[nodiscard]] static std::set<std::string>
99 
100 public:
104  class Error : public std::runtime_error
105  {
106  public:
107  using std::runtime_error::runtime_error;
108  };
109 
110  explicit
111  ProtocolFactory(const CtorParams& params);
112 
113  virtual
115 
116 #ifdef DOXYGEN
122  static const std::string&
123  getId() noexcept;
124 #endif
125 
129  const std::set<std::string>&
130  getProvidedSchemes() const noexcept
131  {
132  return providedSchemes;
133  }
134 
140  void
142 
149  {
150  FaceUri remoteUri;
151  std::optional<FaceUri> localUri;
153  };
154 
161  void
162  createFace(const CreateFaceRequest& req,
163  const FaceCreatedCallback& onCreated,
164  const FaceCreationFailedCallback& onFailure);
165 
174  shared_ptr<Face>
175  createNetdevBoundFace(const FaceUri& remote,
176  const shared_ptr<const ndn::net::NetworkInterface>& netdev);
177 
180  std::vector<shared_ptr<const Channel>>
181  getChannels() const;
182 
183 protected:
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 
193 private:
204  virtual void
205  doProcessConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext& context);
206 
213  virtual void
214  doCreateFace(const CreateFaceRequest& req,
215  const FaceCreatedCallback& onCreated,
216  const FaceCreationFailedCallback& onFailure);
217 
233  virtual shared_ptr<Face>
234  doCreateNetdevBoundFace(const FaceUri& remote,
235  const shared_ptr<const ndn::net::NetworkInterface>& netif);
236 
242  virtual std::vector<shared_ptr<const Channel>>
243  doGetChannels() const;
244 
245 private: // registry
246  using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>;
247  using Registry = std::map<std::string, CreateFunc>; // indexed by factory id
248 
249  static Registry&
250  getRegistry();
251 
252 protected:
253  std::set<std::string> providedSchemes;
255 
261  shared_ptr<ndn::net::NetworkMonitor> netmon;
262 };
263 
264 } // namespace nfd::face
265 
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 
280 #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
Context for processing a config section in ProtocolFactory.
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:94
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:90
boost::optional< const ConfigSection & > OptionalConfigSection
An optional configuration file section.
Definition: config-file.hpp:46
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:87
Encapsulates a face creation request and all its parameters.
Parameters to ProtocolFactory constructor.
shared_ptr< ndn::net::NetworkMonitor > netmon