blob: 62b64dc428660b3be951de61ec013b0bef16b534 [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:
53 virtual ~UnsolicitedDataPolicy() = default;
54
55 virtual UnsolicitedDataDecision
56 decide(const Face& inFace, const Data& data) const = 0;
Junxiao Shi58320f72016-09-04 04:40:34 +000057
58public: // 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
73private:
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 Shifbe8efe2016-08-22 16:02:30 +000079};
80
Junxiao Shi36b22252016-08-25 13:22:21 +000081/** \brief drops all unsolicited Data
82 */
83class DropAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
84{
85public:
86 virtual UnsolicitedDataDecision
87 decide(const Face& inFace, const Data& data) const final;
88};
89
Junxiao Shifbe8efe2016-08-22 16:02:30 +000090/** \brief admits unsolicited Data from local faces
91 */
92class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy
93{
94public:
95 virtual UnsolicitedDataDecision
96 decide(const Face& inFace, const Data& data) const final;
97};
98
Junxiao Shi36b22252016-08-25 13:22:21 +000099/** \brief admits unsolicited Data from non-local faces
100 */
101class AdmitNetworkUnsolicitedDataPolicy : public UnsolicitedDataPolicy
102{
103public:
104 virtual UnsolicitedDataDecision
105 decide(const Face& inFace, const Data& data) const final;
106};
107
108/** \brief admits all unsolicited Data
109 */
110class AdmitAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
111{
112public:
113 virtual UnsolicitedDataDecision
114 decide(const Face& inFace, const Data& data) const final;
115};
116
Junxiao Shi9685cc52016-08-29 12:47:05 +0000117/** \brief the default UnsolicitedDataPolicy
118 */
Junxiao Shi88f5f732016-09-03 07:11:37 +0000119typedef DropAllUnsolicitedDataPolicy DefaultUnsolicitedDataPolicy;
Junxiao Shi9685cc52016-08-29 12:47:05 +0000120
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000121} // namespace fw
122} // namespace nfd
123
Junxiao Shi58320f72016-09-04 04:40:34 +0000124/** \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) \
129static class NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass \
130{ \
131public: \
132 NfdAuto ## P ## UnsolicitedDataPolicyRegistrationClass() \
133 { \
134 ::nfd::fw::UnsolicitedDataPolicy::registerPolicy<P>(key); \
135 } \
136} g_nfdAuto ## P ## UnsolicitedDataPolicyRegistrationVariable
137
Junxiao Shifbe8efe2016-08-22 16:02:30 +0000138#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP