blob: a5f24d98f44ab84e09c5901633be6100657c5f47 [file] [log] [blame]
Junxiao Shifbe8efe2016-08-22 16:02:30 +00001/* -*- 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
31namespace nfd {
32namespace fw {
33
34/** \brief a decision made by UnsolicitedDataPolicy
35 */
36enum class UnsolicitedDataDecision {
37 DROP, ///< the Data should be dropped
38 CACHE ///< the Data should be cached in the ContentStore
39};
40
41std::ostream&
42operator<<(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 */
50class UnsolicitedDataPolicy : noncopyable
51{
52public:
Junxiao Shi9db73592016-12-01 23:43:11 +000053 virtual
54 ~UnsolicitedDataPolicy() = default;
Junxiao Shifbe8efe2016-08-22 16:02:30 +000055
56 virtual UnsolicitedDataDecision
57 decide(const Face& inFace, const Data& data) const = 0;
Junxiao Shi58320f72016-09-04 04:40:34 +000058
59public: // registry
Junxiao Shi9db73592016-12-01 23:43:11 +000060 typedef std::function<unique_ptr<UnsolicitedDataPolicy>()> CreateFunc;
61 typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name
62
Junxiao Shi58320f72016-09-04 04:40:34 +000063 template<typename P>
64 static void
Junxiao Shi9db73592016-12-01 23:43:11 +000065 registerPolicy(const std::string& policyName = P::POLICY_NAME)
Junxiao Shi58320f72016-09-04 04:40:34 +000066 {
67 Registry& registry = getRegistry();
Junxiao Shi9db73592016-12-01 23:43:11 +000068 BOOST_ASSERT(registry.count(policyName) == 0);
69 registry[policyName] = [] { return make_unique<P>(); };
Junxiao Shi58320f72016-09-04 04:40:34 +000070 }
71
Junxiao Shi9db73592016-12-01 23:43:11 +000072 /** \return an UnsolicitedDataPolicy identified by \p policyName,
73 * or nullptr if \p policyName is unknown
Junxiao Shi58320f72016-09-04 04:40:34 +000074 */
75 static unique_ptr<UnsolicitedDataPolicy>
Junxiao Shi9db73592016-12-01 23:43:11 +000076 create(const std::string& policyName);
77
78 /** \return a list of available policy names
79 */
80 static std::set<std::string>
81 getPolicyNames();
Junxiao Shi58320f72016-09-04 04:40:34 +000082
83private:
Junxiao Shi58320f72016-09-04 04:40:34 +000084 static Registry&
85 getRegistry();
Junxiao Shifbe8efe2016-08-22 16:02:30 +000086};
87
Junxiao Shi36b22252016-08-25 13:22:21 +000088/** \brief drops all unsolicited Data
89 */
90class DropAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
91{
92public:
Junxiao Shi9db73592016-12-01 23:43:11 +000093 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +000094 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +000095
96public:
97 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +000098};
99
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000100/** \brief admits unsolicited Data from local faces
101 */
102class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy
103{
104public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000105 UnsolicitedDataDecision
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000106 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000107
108public:
109 static const std::string POLICY_NAME;
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000110};
111
Junxiao Shi36b22252016-08-25 13:22:21 +0000112/** \brief admits unsolicited Data from non-local faces
113 */
114class AdmitNetworkUnsolicitedDataPolicy : public UnsolicitedDataPolicy
115{
116public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000117 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000118 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000119
120public:
121 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +0000122};
123
124/** \brief admits all unsolicited Data
125 */
126class AdmitAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
127{
128public:
Junxiao Shi9db73592016-12-01 23:43:11 +0000129 UnsolicitedDataDecision
Junxiao Shi36b22252016-08-25 13:22:21 +0000130 decide(const Face& inFace, const Data& data) const final;
Junxiao Shi9db73592016-12-01 23:43:11 +0000131
132public:
133 static const std::string POLICY_NAME;
Junxiao Shi36b22252016-08-25 13:22:21 +0000134};
135
Junxiao Shi9685cc52016-08-29 12:47:05 +0000136/** \brief the default UnsolicitedDataPolicy
137 */
Junxiao Shi88f5f732016-09-03 07:11:37 +0000138typedef DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy;
Junxiao Shi9685cc52016-08-29 12:47:05 +0000139
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000140} // namespace fw
141} // namespace nfd
142
Junxiao Shi58320f72016-09-04 04:40:34 +0000143/** \brief registers an unsolicited data policy
Junxiao Shi9db73592016-12-01 23:43:11 +0000144 * \param P a subclass of nfd::fw::UnsolicitedDataPolicy;
145 * P::POLICY_NAME must be a string that contains policy name
Junxiao Shi58320f72016-09-04 04:40:34 +0000146 */
Junxiao Shi9db73592016-12-01 23:43:11 +0000147#define NFD_REGISTER_UNSOLICITED_DATA_POLICY(P) \
Junxiao Shi58320f72016-09-04 04:40:34 +0000148static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \
149{ \
150public: \
151 NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \
152 { \
Junxiao Shi9db73592016-12-01 23:43:11 +0000153 ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(); \
Junxiao Shi58320f72016-09-04 04:40:34 +0000154 } \
155} g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable
156
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000157#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP