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-2017, 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-system.hpp"
31 
32 #include <boost/range/adaptor/map.hpp>
33 #include <boost/range/algorithm/copy.hpp>
34 #include <ndn-cxx/encoding/nfd-constants.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] = &make_unique<PF, const CtorParams&>;
75  }
76 
80  static unique_ptr<ProtocolFactory>
81  create(const std::string& id, const CtorParams& params);
82 
85  static std::set<std::string>
86  listRegistered();
87 
88 public:
91  class Error : public std::runtime_error
92  {
93  public:
94  explicit
95  Error(const std::string& what)
96  : std::runtime_error(what)
97  {
98  }
99  };
100 
101  virtual
102  ~ProtocolFactory() = default;
103 
104 #ifdef DOXYGEN
105 
109  static const std::string&
110  getId();
111 #endif
112 
120  virtual void
121  processConfig(OptionalConfigSection configSection,
122  FaceSystem::ConfigContext& context) = 0;
123 
126  const std::set<std::string>&
128  {
129  return providedSchemes;
130  }
131 
138  {
139  FaceUri remoteUri;
140  ndn::optional<FaceUri> localUri;
141  ndn::nfd::FacePersistency persistency;
144  };
145 
153  virtual void
154  createFace(const CreateFaceParams& params,
155  const FaceCreatedCallback& onCreated,
156  const FaceCreationFailedCallback& onFailure) = 0;
157 
158  virtual std::vector<shared_ptr<const Channel>>
159  getChannels() const = 0;
160 
161 protected:
162  explicit
163  ProtocolFactory(const CtorParams& params);
164 
165  template<typename ChannelMap>
166  static std::vector<shared_ptr<const Channel>>
167  getChannelsFromMap(const ChannelMap& channelMap)
168  {
169  std::vector<shared_ptr<const Channel>> channels;
170  boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels));
171  return channels;
172  }
173 
174 private: // registry
175  using CreateFunc = std::function<unique_ptr<ProtocolFactory>(const CtorParams&)>;
176  using Registry = std::map<std::string, CreateFunc>; // indexed by factory id
177 
178  static Registry&
179  getRegistry();
180 
181 protected:
182  std::set<std::string> providedSchemes;
183 
185 
191  shared_ptr<ndn::net::NetworkMonitor> netmon;
192 };
193 
194 } // namespace face
195 } // namespace nfd
196 
201 #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
202 static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
203 { \
204 public: \
205  NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
206  { \
207  ::nfd::face::ProtocolFactory::registerType<PF>(); \
208  } \
209 } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
210 
211 #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
FaceCreatedCallback addFace
callback when a new face is created
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
STL namespace.
shared_ptr< ndn::net::NetworkMonitor > netmon
NetworkMonitor for listing available network interfaces and monitoring their changes.
static void registerType(const std::string &id=PF::getId())
Register a protocol factory type.
context for processing a config section in ProtocolFactory
Definition: face-system.hpp:85
static std::vector< shared_ptr< const Channel > > getChannelsFromMap(const ChannelMap &channelMap)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
Error(const std::string &what)
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:44
Provides support for an underlying protocol.
Parameters to ProtocolFactory constructor.
shared_ptr< ndn::net::NetworkMonitor > netmon
const std::set< std::string > & getProvidedSchemes()
Get FaceUri schemes accepted by this ProtocolFactory.
Parameters to ProtocolFactory::createFace.
Base class for all exceptions thrown by ProtocolFactory subclasses.
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when a face is created (in response to an incoming connect...
Definition: channel.hpp:35