Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, 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_FW_UNSOLICITED_DATA_POLICY_HPP |
| 27 | #define NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |
| 28 | |
| 29 | #include "face/face.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | |
| 34 | /** \brief a decision made by UnsolicitedDataPolicy |
| 35 | */ |
| 36 | enum class UnsolicitedDataDecision { |
| 37 | DROP, ///< the Data should be dropped |
| 38 | CACHE ///< the Data should be cached in the ContentStore |
| 39 | }; |
| 40 | |
| 41 | std::ostream& |
| 42 | operator<<(std::ostream& os, UnsolicitedDataDecision d); |
| 43 | |
| 44 | /** \brief determines how to process an unsolicited Data |
| 45 | * |
| 46 | * An incoming Data is unsolicited if it does not match any PIT entry. |
| 47 | * This class assists forwarding pipelines to decide whether to drop an unsolicited Data |
| 48 | * or admit it into the ContentStore. |
| 49 | */ |
| 50 | class UnsolicitedDataPolicy : noncopyable |
| 51 | { |
| 52 | public: |
| 53 | virtual ~UnsolicitedDataPolicy() = default; |
| 54 | |
| 55 | virtual UnsolicitedDataDecision |
| 56 | decide(const Face& inFace, const Data& data) const = 0; |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame^] | 57 | |
| 58 | public: // registry |
| 59 | template<typename P> |
| 60 | static void |
| 61 | registerPolicy(const std::string& key) |
| 62 | { |
| 63 | Registry& registry = getRegistry(); |
| 64 | BOOST_ASSERT(registry.count(key) == 0); |
| 65 | registry[key] = [] { return make_unique<P>(); }; |
| 66 | } |
| 67 | |
| 68 | /** \return an UnsolicitedDataPolicy identified by \p key, or nullptr if \p key is unknown |
| 69 | */ |
| 70 | static unique_ptr<UnsolicitedDataPolicy> |
| 71 | create(const std::string& key); |
| 72 | |
| 73 | private: |
| 74 | typedef std::function<unique_ptr<UnsolicitedDataPolicy>()> CreateFunc; |
| 75 | typedef std::map<std::string, CreateFunc> Registry; // indexed by key |
| 76 | |
| 77 | static Registry& |
| 78 | getRegistry(); |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 81 | /** \brief drops all unsolicited Data |
| 82 | */ |
| 83 | class DropAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 84 | { |
| 85 | public: |
| 86 | virtual UnsolicitedDataDecision |
| 87 | decide(const Face& inFace, const Data& data) const final; |
| 88 | }; |
| 89 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 90 | /** \brief admits unsolicited Data from local faces |
| 91 | */ |
| 92 | class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 93 | { |
| 94 | public: |
| 95 | virtual UnsolicitedDataDecision |
| 96 | decide(const Face& inFace, const Data& data) const final; |
| 97 | }; |
| 98 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 99 | /** \brief admits unsolicited Data from non-local faces |
| 100 | */ |
| 101 | class AdmitNetworkUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 102 | { |
| 103 | public: |
| 104 | virtual UnsolicitedDataDecision |
| 105 | decide(const Face& inFace, const Data& data) const final; |
| 106 | }; |
| 107 | |
| 108 | /** \brief admits all unsolicited Data |
| 109 | */ |
| 110 | class AdmitAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 111 | { |
| 112 | public: |
| 113 | virtual UnsolicitedDataDecision |
| 114 | decide(const Face& inFace, const Data& data) const final; |
| 115 | }; |
| 116 | |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 117 | /** \brief the default UnsolicitedDataPolicy |
| 118 | */ |
Junxiao Shi | 88f5f73 | 2016-09-03 07:11:37 +0000 | [diff] [blame] | 119 | typedef DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy; |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 120 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 121 | } // namespace fw |
| 122 | } // namespace nfd |
| 123 | |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame^] | 124 | /** \brief registers an unsolicited data policy |
| 125 | * \param P a subclass of nfd::fw::UnsolicitedDataPolicy |
| 126 | * \param key the policy keyword, which is available for selection in NFD config file |
| 127 | */ |
| 128 | #define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P, key) \ |
| 129 | static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \ |
| 130 | { \ |
| 131 | public: \ |
| 132 | NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \ |
| 133 | { \ |
| 134 | ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(key); \ |
| 135 | } \ |
| 136 | } g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable |
| 137 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 138 | #endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |