Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |
| 27 | #define NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 47ab029 | 2015-11-02 18:45:39 +0100 | [diff] [blame] | 29 | #include "channel.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 30 | #include "face-system.hpp" |
Junxiao Shi | 0de23a2 | 2015-12-03 20:07:02 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/encoding/nfd-constants.hpp> |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 32 | #include <boost/range/adaptor/map.hpp> |
| 33 | #include <boost/range/algorithm/copy.hpp> |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 35 | namespace nfd { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 36 | namespace face { |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 37 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 38 | /** \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 Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 44 | */ |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 45 | class ProtocolFactory : noncopyable |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 46 | { |
| 47 | public: |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 48 | /** |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 49 | * \brief Base class for all exceptions thrown by protocol factories |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 50 | */ |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 51 | class Error : public std::runtime_error |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 52 | { |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | } |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 59 | }; |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 60 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 61 | /** \brief process face_system subsection that corresponds to this ProtocolFactory type |
| 62 | * \param configSection the configuration section or boost::null to indicate it is omitted |
| 63 | * \param context provides access to data structures and contextual information |
| 64 | * \throw ConfigFile::Error invalid configuration |
| 65 | * |
| 66 | * This function updates \p providedSchemes |
| 67 | */ |
| 68 | virtual void |
| 69 | processConfig(OptionalConfigSection configSection, |
| 70 | FaceSystem::ConfigContext& context) |
| 71 | { |
| 72 | ///\todo implement in every subclass and make this pure-virtual |
| 73 | BOOST_THROW_EXCEPTION(Error("processConfig is not implemented")); |
| 74 | } |
| 75 | |
| 76 | /** \return FaceUri schemes accepted by this ProtocolFactory |
| 77 | */ |
| 78 | const std::set<std::string>& |
| 79 | getProvidedSchemes() |
| 80 | { |
| 81 | return providedSchemes; |
| 82 | } |
| 83 | |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 84 | /** \brief Try to create Face using the supplied FaceUri |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 85 | * |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 86 | * This method should automatically choose channel, based on supplied FaceUri |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 87 | * and create face. |
| 88 | * |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 89 | * \param uri remote URI of the new face |
| 90 | * \param persistency persistency of the new face |
| 91 | * \param wantLocalFieldsEnabled whether local fields should be enabled on the face |
| 92 | * \param onCreated callback if face creation succeeds |
| 93 | * If a face with the same remote URI already exists, its persistency and |
| 94 | * LocalFieldsEnabled setting will not be modified. |
| 95 | * \param onFailure callback if face creation fails |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 96 | */ |
| 97 | virtual void |
| 98 | createFace(const FaceUri& uri, |
Yukai Tu | 7c90e6d | 2015-07-11 12:21:46 +0800 | [diff] [blame] | 99 | ndn::nfd::FacePersistency persistency, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 100 | bool wantLocalFieldsEnabled, |
Alexander Afanasyev | d665530 | 2014-02-28 08:41:28 -0800 | [diff] [blame] | 101 | const FaceCreatedCallback& onCreated, |
Eric Newberry | f40551a | 2016-09-05 15:41:16 -0700 | [diff] [blame] | 102 | const FaceCreationFailedCallback& onFailure) = 0; |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 103 | |
Davide Pesavento | 1d7e7af | 2015-10-10 23:54:08 +0200 | [diff] [blame] | 104 | virtual std::vector<shared_ptr<const Channel>> |
Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame] | 105 | getChannels() const = 0; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 106 | |
| 107 | protected: |
| 108 | template<typename ChannelMap> |
| 109 | static std::vector<shared_ptr<const Channel>> |
| 110 | getChannelsFromMap(const ChannelMap& channelMap) |
| 111 | { |
| 112 | std::vector<shared_ptr<const Channel>> channels; |
| 113 | boost::copy(channelMap | boost::adaptors::map_values, std::back_inserter(channels)); |
| 114 | return channels; |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | /** \brief FaceUri schemes provided by this ProtocolFactory |
| 119 | */ |
| 120 | std::set<std::string> providedSchemes; |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 123 | } // namespace face |
| 124 | |
| 125 | using face::ProtocolFactory; |
| 126 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 127 | } // namespace nfd |
Alexander Afanasyev | a9034b0 | 2014-01-26 18:32:02 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 129 | #endif // NFD_DAEMON_FACE_PROTOCOL_FACTORY_HPP |