NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019, 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 {
37 namespace face {
38 
47 {
49  shared_ptr<ndn::net::NetworkMonitor> netmon;
50 };
51 
59 class ProtocolFactory : noncopyable
60 {
61 public: // registry
63 
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);
74  registry[id] = [] (const CtorParams& p) { return make_unique<PF>(p); };
75  }
76 
80  static unique_ptr<ProtocolFactory>
81  create(const std::string& id, const CtorParams& params);
82 
85  static std::set<std::string>
87 
88 public:
91  class Error : public std::runtime_error
92  {
93  public:
94  using std::runtime_error::runtime_error;
95  };
96 
97  explicit
98  ProtocolFactory(const CtorParams& params);
99 
100  virtual
102 
103 #ifdef DOXYGEN
104 
108  static const std::string&
109  getId() noexcept;
110 #endif
111 
114  const std::set<std::string>&
116  {
117  return providedSchemes;
118  }
119 
125  void
127 
134  {
136  optional<FaceUri> localUri;
138  };
139 
146  void
147  createFace(const CreateFaceRequest& req,
148  const FaceCreatedCallback& onCreated,
149  const FaceCreationFailedCallback& onFailure);
150 
159  shared_ptr<Face>
160  createNetdevBoundFace(const FaceUri& remote,
161  const shared_ptr<const ndn::net::NetworkInterface>& netdev);
162 
165  std::vector<shared_ptr<const Channel>>
166  getChannels() const;
167 
168 protected:
169  template<typename ChannelMap>
170  static std::vector<shared_ptr<const Channel>>
171  getChannelsFromMap(const ChannelMap& channelMap)
172  {
173  std::vector<shared_ptr<const Channel>> channels;
174  boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels));
175  return channels;
176  }
177 
178 private:
189  virtual void
190  doProcessConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext& context);
191 
198  virtual void
199  doCreateFace(const CreateFaceRequest& req,
200  const FaceCreatedCallback& onCreated,
201  const FaceCreationFailedCallback& onFailure);
202 
218  virtual shared_ptr<Face>
219  doCreateNetdevBoundFace(const FaceUri& remote,
220  const shared_ptr<const ndn::net::NetworkInterface>& netif);
221 
227  virtual std::vector<shared_ptr<const Channel>>
228  doGetChannels() const;
229 
230 private: // registry
231  using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>;
232  using Registry = std::map<std::string, CreateFunc>; // indexed by factory id
233 
234  static Registry&
235  getRegistry();
236 
237 protected:
238  std::set<std::string> providedSchemes;
240 
246  shared_ptr<ndn::net::NetworkMonitor> netmon;
247 };
248 
249 } // namespace face
250 } // namespace nfd
251 
256 #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
257 static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
258 { \
259 public: \
260  NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
261  { \
262  ::nfd::face::ProtocolFactory::registerType<PF>(); \
263  } \
264 } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
265 
266 #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
nfd::face::ProtocolFactory::netmon
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
Definition: protocol-factory.hpp:246
nfd::face::ProtocolFactory::createFace
void createFace(const CreateFaceRequest &req, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure)
Create a unicast face.
Definition: protocol-factory.cpp:86
ndn::FaceUri
represents the underlying protocol and address used by a Face
Definition: face-uri.hpp:45
nfd::face::ProtocolFactory::providedSchemes
std::set< std::string > providedSchemes
FaceUri schemes provided by this protocol factory.
Definition: protocol-factory.hpp:238
nfd::face::ProtocolFactory::CreateFaceRequest::localUri
optional< FaceUri > localUri
Definition: protocol-factory.hpp:136
nfd::face::ProtocolFactoryCtorParams::addFace
FaceCreatedCallback addFace
Definition: protocol-factory.hpp:48
nfd::face::ProtocolFactory::ProtocolFactory
ProtocolFactory(const CtorParams &params)
Definition: protocol-factory.cpp:62
nfd::face::ProtocolFactory
Provides support for an underlying protocol.
Definition: protocol-factory.hpp:60
nfd::face::ProtocolFactory::processConfig
void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
Process face_system subsection that corresponds to this protocol factory id.
Definition: protocol-factory.cpp:73
nfd::face::ProtocolFactory::CreateFaceRequest::remoteUri
FaceUri remoteUri
Definition: protocol-factory.hpp:135
nfd::face::ProtocolFactory::registerType
static void registerType(const std::string &id=PF::getId())
Register a protocol factory type.
Definition: protocol-factory.hpp:70
nfd::OptionalConfigSection
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
face-system.hpp
nfd
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
nfd::face::ProtocolFactory::addFace
FaceCreatedCallback addFace
callback when a new face is created
Definition: protocol-factory.hpp:239
nfd::face::ProtocolFactory::create
static unique_ptr< ProtocolFactory > create(const std::string &id, const CtorParams &params)
Create a protocol factory instance.
Definition: protocol-factory.cpp:42
nfd::face::ProtocolFactory::~ProtocolFactory
virtual ~ProtocolFactory()=0
nfd::face::ProtocolFactory::getProvidedSchemes
const std::set< std::string > & getProvidedSchemes() const
Get FaceUri schemes accepted by this protocol factory.
Definition: protocol-factory.hpp:115
nfd::face::ProtocolFactoryCtorParams
Parameters to ProtocolFactory constructor.
Definition: protocol-factory.hpp:47
nfd::face::ProtocolFactory::CreateFaceRequest
Encapsulates a face creation request and all its parameters.
Definition: protocol-factory.hpp:134
nfd::face::ProtocolFactoryCtorParams::netmon
shared_ptr< ndn::net::NetworkMonitor > netmon
Definition: protocol-factory.hpp:49
nfd::face::ProtocolFactory::Error
Base class for all exceptions thrown by ProtocolFactory subclasses.
Definition: protocol-factory.hpp:92
nfd::face::ProtocolFactory::getId
static const std::string & getId() noexcept
Get id for this protocol factory.
nfd::face::FaceCreatedCallback
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:74
nfd::face::ProtocolFactory::CtorParams
ProtocolFactoryCtorParams CtorParams
Definition: protocol-factory.hpp:62
face.hpp
nfd::face::ProtocolFactory::createNetdevBoundFace
shared_ptr< Face > createNetdevBoundFace(const FaceUri &remote, const shared_ptr< const ndn::net::NetworkInterface > &netdev)
Create a netdev-bound face.
Definition: protocol-factory.cpp:106
nfd::face::FaceParams
Parameters used to set Transport properties or LinkService options on a newly created face.
Definition: face-common.hpp:73
nfd::face::ProtocolFactory::CreateFaceRequest::params
FaceParams params
Definition: protocol-factory.hpp:137
nfd::face::ProtocolFactory::listRegistered
static std::set< std::string > listRegistered()
Get all registered protocol factory ids.
Definition: protocol-factory.cpp:54
nfd::face::ProtocolFactory::getChannels
std::vector< shared_ptr< const Channel > > getChannels() const
Get list of open channels (listening + non-listening)
Definition: protocol-factory.cpp:121
nfd::face::ProtocolFactory::getChannelsFromMap
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Definition: protocol-factory.hpp:171
nfd::face::FaceCreationFailedCallback
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:78
nfd::face::FaceSystem::ConfigContext
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:97
channel.hpp