blob: b8bba3d938063add6df436a9eb187fb6ca6e3af4 [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"
Davide Pesaventoe5eebad2017-04-06 20:23:26 -040031
Junxiao Shi0de23a22015-12-03 20:07:02 +000032#include <ndn-cxx/encoding/nfd-constants.hpp>
Junxiao Shi38b24c72017-01-05 02:59:31 +000033#include <boost/range/adaptor/map.hpp>
34#include <boost/range/algorithm/copy.hpp>
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080035
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080036namespace nfd {
Junxiao Shi38b24c72017-01-05 02:59:31 +000037namespace face {
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080038
Junxiao Shi38b24c72017-01-05 02:59:31 +000039/** \brief provide support for an underlying protocol
40 * \sa FaceSystem
41 *
42 * A protocol factory provides support for an underlying protocol and owns Channel objects.
43 * It can process a subsection of face_system config section and create channels and multicast
44 * faces accordingly.
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020045 */
Junxiao Shi38b24c72017-01-05 02:59:31 +000046class ProtocolFactory : noncopyable
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080047{
Junxiao Shib47247d2017-01-24 15:09:16 +000048public: // registry
49 /** \brief register a protocol factory type
50 * \tparam S subclass of ProtocolFactory
51 * \param id factory identifier
52 */
53 template<typename PF>
54 static void
55 registerType(const std::string& id = PF::getId())
56 {
57 Registry& registry = getRegistry();
58 BOOST_ASSERT(registry.count(id) == 0);
59 registry[id] = &make_unique<PF>;
60 }
61
62 /** \return a protocol factory instance
63 * \retval nullptr if factory with \p id is not registered
64 */
65 static unique_ptr<ProtocolFactory>
66 create(const std::string& id);
67
68 /** \return registered protocol factory ids
69 */
70 static std::set<std::string>
71 listRegistered();
72
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080073public:
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080074 /**
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020075 * \brief Base class for all exceptions thrown by protocol factories
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080076 */
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020077 class Error : public std::runtime_error
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080078 {
Davide Pesavento1d7e7af2015-10-10 23:54:08 +020079 public:
80 explicit
81 Error(const std::string& what)
82 : std::runtime_error(what)
83 {
84 }
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080085 };
Alexander Afanasyevd6655302014-02-28 08:41:28 -080086
Junxiao Shib47247d2017-01-24 15:09:16 +000087 virtual
88 ~ProtocolFactory() = default;
89
90#ifdef DOXYGEN
91 /** \return protocol factory id
92 *
93 * face_system.factory-id config section is processed by the protocol factory.
94 */
95 static const std::string&
96 getId();
97#endif
98
Junxiao Shi38b24c72017-01-05 02:59:31 +000099 /** \brief process face_system subsection that corresponds to this ProtocolFactory type
100 * \param configSection the configuration section or boost::null to indicate it is omitted
101 * \param context provides access to data structures and contextual information
102 * \throw ConfigFile::Error invalid configuration
103 *
104 * This function updates \p providedSchemes
105 */
106 virtual void
107 processConfig(OptionalConfigSection configSection,
Junxiao Shic3443042017-01-26 17:21:35 +0000108 FaceSystem::ConfigContext& context) = 0;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000109
110 /** \return FaceUri schemes accepted by this ProtocolFactory
111 */
112 const std::set<std::string>&
113 getProvidedSchemes()
114 {
115 return providedSchemes;
116 }
117
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800118 /** \brief Try to create Face using the supplied FaceUri
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800119 *
Eric Newberry78e32b02017-04-01 14:34:44 +0000120 * \param remoteUri remote URI of the new face
121 * \param localUri local URI of the new face
Eric Newberryf40551a2016-09-05 15:41:16 -0700122 * \param persistency persistency of the new face
123 * \param wantLocalFieldsEnabled whether local fields should be enabled on the face
Eric Newberry78e32b02017-04-01 14:34:44 +0000124 * \param onCreated callback if face creation succeeds or face already exists;
125 * persistency and local fields settings are not updated on an existing face
Eric Newberryf40551a2016-09-05 15:41:16 -0700126 * \param onFailure callback if face creation fails
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800127 */
128 virtual void
Eric Newberry78e32b02017-04-01 14:34:44 +0000129 createFace(const FaceUri& remoteUri,
130 const ndn::optional<FaceUri>& localUri,
Yukai Tu7c90e6d2015-07-11 12:21:46 +0800131 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700132 bool wantLocalFieldsEnabled,
Alexander Afanasyevd6655302014-02-28 08:41:28 -0800133 const FaceCreatedCallback& onCreated,
Eric Newberryf40551a2016-09-05 15:41:16 -0700134 const FaceCreationFailedCallback& onFailure) = 0;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600135
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200136 virtual std::vector<shared_ptr<const Channel>>
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600137 getChannels() const = 0;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000138
139protected:
140 template<typename ChannelMap>
141 static std::vector<shared_ptr<const Channel>>
142 getChannelsFromMap(const ChannelMap& channelMap)
143 {
144 std::vector<shared_ptr<const Channel>> channels;
145 boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels));
146 return channels;
147 }
148
Junxiao Shib47247d2017-01-24 15:09:16 +0000149private: // registry
150 typedef std::function<unique_ptr<ProtocolFactory>()> CreateFunc;
151 typedef std::map<std::string, CreateFunc> Registry; // indexed by factory id
152
153 static Registry&
154 getRegistry();
155
Junxiao Shi38b24c72017-01-05 02:59:31 +0000156protected:
157 /** \brief FaceUri schemes provided by this ProtocolFactory
158 */
159 std::set<std::string> providedSchemes;
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800160};
161
Junxiao Shi38b24c72017-01-05 02:59:31 +0000162} // namespace face
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800163} // namespace nfd
Alexander Afanasyeva9034b02014-01-26 18:32:02 -0800164
Junxiao Shib47247d2017-01-24 15:09:16 +0000165/** \brief registers a protocol factory
166 *
167 * This macro should appear once in .cpp of each protocol factory.
168 */
169#define NFD_REGISTER_PROTOCOL_FACTORY(PF) \
170static class NfdAuto ## PF ## ProtocolFactoryRegistrationClass \
171{ \
172public: \
173 NfdAuto ## PF ## ProtocolFactoryRegistrationClass() \
174 { \
175 ::nfd::face::ProtocolFactory::registerType<PF>(); \
176 } \
177} g_nfdAuto ## PF ## ProtocolFactoryRegistrationVariable
178
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700179#endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP