protocol-factory.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
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 #include <ndn-cxx/encoding/nfd-constants.hpp>
32 #include <boost/range/adaptor/map.hpp>
33 #include <boost/range/algorithm/copy.hpp>
34 
35 namespace nfd {
36 namespace face {
37 
45 class ProtocolFactory : noncopyable
46 {
47 public: // registry
52  template<typename PF>
53  static void
54  registerType(const std::string& id = PF::getId())
55  {
56  Registry& registry = getRegistry();
57  BOOST_ASSERT(registry.count(id) == 0);
58  registry[id] = &make_unique<PF>;
59  }
60 
64  static unique_ptr<ProtocolFactory>
65  create(const std::string& id);
66 
69  static std::set<std::string>
71 
72 public:
76  class Error : public std::runtime_error
77  {
78  public:
79  explicit
80  Error(const std::string& what)
81  : std::runtime_error(what)
82  {
83  }
84  };
85 
86  virtual
87  ~ProtocolFactory() = default;
88 
89 #ifdef DOXYGEN
90 
94  static const std::string&
95  getId();
96 #endif
97 
105  virtual void
107  FaceSystem::ConfigContext& context)
108  {
110  BOOST_THROW_EXCEPTION(Error("processConfig is not implemented"));
111  }
112 
115  const std::set<std::string>&
117  {
118  return providedSchemes;
119  }
120 
134  virtual void
135  createFace(const FaceUri& uri,
136  ndn::nfd::FacePersistency persistency,
137  bool wantLocalFieldsEnabled,
138  const FaceCreatedCallback& onCreated,
139  const FaceCreationFailedCallback& onFailure) = 0;
140 
141  virtual std::vector<shared_ptr<const Channel>>
142  getChannels() const = 0;
143 
144 protected:
145  template<typename ChannelMap>
146  static std::vector<shared_ptr<const Channel>>
147  getChannelsFromMap(const ChannelMap& channelMap)
148  {
149  std::vector<shared_ptr<const Channel>> channels;
150  boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels));
151  return channels;
152  }
153 
154 private: // registry
155  typedef std::function<unique_ptr<ProtocolFactory>()> CreateFunc;
156  typedef std::map<std::string, CreateFunc> Registry; // indexed by factory id
157 
158  static Registry&
159  getRegistry();
160 
161 protected:
164  std::set<std::string> providedSchemes;
165 };
166 
167 } // namespace face
168 
170 
171 } // namespace nfd
172 
177 #define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
178 static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
179 { \
180 public: \
181  NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
182  { \
183  ::nfd::face::ProtocolFactory::registerType<PF>(); \
184  } \
185 } g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
186 
187 #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
static std::set< std::string > listRegistered()
virtual ~ProtocolFactory()=default
std::set< std::string > providedSchemes
FaceUri schemes provided by this ProtocolFactory.
virtual std::vector< shared_ptr< const Channel > > getChannels() const =0
STL namespace.
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:77
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...
boost::optional< const ConfigSection & > OptionalConfigSection
an optional config file section
Definition: config-file.hpp:41
static unique_ptr< ProtocolFactory > create(const std::string &id)
Error(const std::string &what)
virtual void createFace(const FaceUri &uri, ndn::nfd::FacePersistency persistency, bool wantLocalFieldsEnabled, const FaceCreatedCallback &onCreated, const FaceCreationFailedCallback &onFailure)=0
Try to create Face using the supplied FaceUri.
virtual void processConfig(OptionalConfigSection configSection, FaceSystem::ConfigContext &context)
process face_system subsection that corresponds to this ProtocolFactory type
function< void(uint32_t status, const std::string &reason)> FaceCreationFailedCallback
Prototype for the callback that is invoked when the face fails to be created.
Definition: channel.hpp:44
provide support for an underlying protocol
const std::set< std::string > & getProvidedSchemes()
function< void(const shared_ptr< Face > &newFace)> FaceCreatedCallback
Prototype for the callback that is invoked when the face is created (as a response to incoming connec...
Definition: channel.hpp:38
static const std::string & getId()
Base class for all exceptions thrown by protocol factories.