blob: 48f755a10217021cb738e977eef9c40d32538332 [file] [log] [blame]
Alexander Afanasyeva9034b02014-01-26 18:32:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +02004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060024 */
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
27#define NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080028
Davide Pesavento47ab0292015-11-02 18:45:39 +010029#include "channel.hpp"
Junxiao Shi38b24c72017-01-05 02:59:31 +000030#include "face-system.hpp"
Junxiao Shi0de23a22015-12-03 20:07:02 +000031#include <ndn-cxx/encoding/nfd-constants.hpp>
Junxiao Shi38b24c72017-01-05 02:59:31 +000032#include <boost/range/adaptor/map.hpp>
33#include <boost/range/algorithm/copy.hpp>
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080034
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080035namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000036namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080037
Junxiao Shi38b24c72017-01-05 02:59:31 +000038/** \brief provide support for an underlying protocol
39 * \sa FaceSystem
40 *
41 * A protocol factory provides support for an underlying protocol and owns Channel objects.
42 * It can process a subsection of face_system config section and create channels and multicast
43 * faces accordingly.
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020044 */
Junxiao Shi38b24c72017-01-05 02:59:31 +000045class ProtocolFactory : noncopyable
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080046{
Junxiao Shib47247d2017-01-24 15:09:16 +000047public: // registry
48 /** \brief register a protocol factory type
49 * \tparam S subclass of ProtocolFactory
50 * \param id factory identifier
51 */
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
61 /** \return a protocol factory instance
62 * \retval nullptr if factory with \p id is not registered
63 */
64 static unique_ptr<ProtocolFactory>
65 create(const std::string& id);
66
67 /** \return registered protocol factory ids
68 */
69 static std::set<std::string>
70 listRegistered();
71
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080072public:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080073 /**
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020074 * \brief Base class for all exceptions thrown by protocol factories
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080075 */
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020076 class Error : public std::runtime_error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080077 {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020078 public:
79 explicit
80 Error(const std::string& what)
81 : std::runtime_error(what)
82 {
83 }
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080084 };
Alexander Afanasyevd6655302014-02-28 08:41:28 -080085
Junxiao Shib47247d2017-01-24 15:09:16 +000086 virtual
87 ~ProtocolFactory() = default;
88
89#ifdef DOXYGEN
90 /** \return protocol factory id
91 *
92 * face_system.factory-id config section is processed by the protocol factory.
93 */
94 static const std::string&
95 getId();
96#endif
97
Junxiao Shi38b24c72017-01-05 02:59:31 +000098 /** \brief process face_system subsection that corresponds to this ProtocolFactory type
99 * \param configSection the configuration section or boost::null to indicate it is omitted
100 * \param context provides access to data structures and contextual information
101 * \throw ConfigFile::Error invalid configuration
102 *
103 * This function updates \p providedSchemes
104 */
105 virtual void
106 processConfig(OptionalConfigSection configSection,
107 FaceSystem::ConfigContext& context)
108 {
109 ///\todo implement in every subclass and make this pure-virtual
110 BOOST_THROW_EXCEPTION(Error("processConfig is not implemented"));
111 }
112
113 /** \return FaceUri schemes accepted by this ProtocolFactory
114 */
115 const std::set<std::string>&
116 getProvidedSchemes()
117 {
118 return providedSchemes;
119 }
120
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800121 /** \brief Try to create Face using the supplied FaceUri
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800122 *
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800123 * This method should automatically choose channel, based on supplied FaceUri
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800124 * and create face.
125 *
Eric Newberryf40551a2016-09-05 15:41:16 -0700126 * \param uri remote URI of the new face
127 * \param persistency persistency of the new face
128 * \param wantLocalFieldsEnabled whether local fields should be enabled on the face
129 * \param onCreated callback if face creation succeeds
130 * If a face with the same remote URI already exists, its persistency and
131 * LocalFieldsEnabled setting will not be modified.
132 * \param onFailure callback if face creation fails
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800133 */
134 virtual void
135 createFace(const FaceUri& uri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800136 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700137 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800138 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700139 const FaceCreationFailedCallback& onFailure) = 0;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600140
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200141 virtual std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600142 getChannels() const = 0;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000143
144protected:
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
Junxiao Shib47247d2017-01-24 15:09:16 +0000154private: // 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
Junxiao Shi38b24c72017-01-05 02:59:31 +0000161protected:
162 /** \brief FaceUri schemes provided by this ProtocolFactory
163 */
164 std::set<std::string> providedSchemes;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800165};
166
Junxiao Shi38b24c72017-01-05 02:59:31 +0000167} // namespace face
168
169using face::ProtocolFactory;
170
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800171} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800172
Junxiao Shib47247d2017-01-24 15:09:16 +0000173/** \brief registers a protocol factory
174 *
175 * This macro should appear once in .cpp of each protocol factory.
176 */
177#define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
178static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
179{ \
180public: \
181 NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
182 { \
183 ::nfd::face::ProtocolFactory::registerType<PF>(); \
184 } \
185} g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
186
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700187#endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP