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: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 53 | virtual |
| 54 | ~UnsolicitedDataPolicy() = default; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 55 | |
| 56 | virtual UnsolicitedDataDecision |
| 57 | decide(const Face& inFace, const Data& data) const = 0; |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 58 | |
| 59 | public: // registry |
| 60 | template<typename P> |
| 61 | static void |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 62 | registerPolicy(const std::string& policyName = P::POLICY_NAME) |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 63 | { |
| 64 | Registry& registry = getRegistry(); |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 65 | BOOST_ASSERT(registry.count(policyName) == 0); |
| 66 | registry[policyName] = [] { return make_unique<P>(); }; |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 69 | /** \return an UnsolicitedDataPolicy identified by \p policyName, |
| 70 | * or nullptr if \p policyName is unknown |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 71 | */ |
| 72 | static unique_ptr<UnsolicitedDataPolicy> |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 73 | create(const std::string& policyName); |
| 74 | |
| 75 | /** \return a list of available policy names |
| 76 | */ |
| 77 | static std::set<std::string> |
| 78 | getPolicyNames(); |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 79 | |
| 80 | private: |
Junxiao Shi | c29c0b0 | 2016-12-04 21:11:44 +0000 | [diff] [blame] | 81 | typedef std::function<unique_ptr<UnsolicitedDataPolicy>()> CreateFunc; |
| 82 | typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name |
| 83 | |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 84 | static Registry& |
| 85 | getRegistry(); |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 88 | /** \brief drops all unsolicited Data |
| 89 | */ |
| 90 | class DropAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 91 | { |
| 92 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 93 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 94 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 95 | |
| 96 | public: |
| 97 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 100 | /** \brief admits unsolicited Data from local faces |
| 101 | */ |
| 102 | class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 103 | { |
| 104 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 105 | UnsolicitedDataDecision |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 106 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 107 | |
| 108 | public: |
| 109 | static const std::string POLICY_NAME; |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 112 | /** \brief admits unsolicited Data from non-local faces |
| 113 | */ |
| 114 | class AdmitNetworkUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 115 | { |
| 116 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 117 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 118 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 119 | |
| 120 | public: |
| 121 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /** \brief admits all unsolicited Data |
| 125 | */ |
| 126 | class AdmitAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy |
| 127 | { |
| 128 | public: |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 129 | UnsolicitedDataDecision |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 130 | decide(const Face& inFace, const Data& data) const final; |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 131 | |
| 132 | public: |
| 133 | static const std::string POLICY_NAME; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 136 | /** \brief the default UnsolicitedDataPolicy |
| 137 | */ |
Junxiao Shi | 88f5f73 | 2016-09-03 07:11:37 +0000 | [diff] [blame] | 138 | typedef DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy; |
Junxiao Shi | 9685cc5 | 2016-08-29 12:47:05 +0000 | [diff] [blame] | 139 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 140 | } // namespace fw |
| 141 | } // namespace nfd |
| 142 | |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 143 | /** \brief registers an unsolicited data policy |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 144 | * \param P a subclass of nfd::fw::UnsolicitedDataPolicy; |
| 145 | * P::POLICY_NAME must be a string that contains policy name |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 146 | */ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 147 | #define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 148 | static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \ |
| 149 | { \ |
| 150 | public: \ |
| 151 | NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \ |
| 152 | { \ |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 153 | ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \ |
Junxiao Shi | 58320f7 | 2016-09-04 04:40:34 +0000 | [diff] [blame] | 154 | } \ |
| 155 | } g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable |
| 156 | |
Junxiao Shi | fbe8efe | 2016-08-22 16:02:30 +0000 | [diff] [blame] | 157 | #endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP |