blob: 4e59716ee08dfd60627c0543aa8e33d9de205285 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi530cf002017-01-03 14:43:16 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shiff10da62016-07-13 17:57:43 +00004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shie93d6a32014-09-07 16:13:22 -070024 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP
27#define NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP
Junxiao Shibb5105f2014-03-03 12:06:45 -070028
29#include "strategy-choice-entry.hpp"
30#include "name-tree.hpp"
31
Junxiao Shib30863e2016-08-15 21:32:01 +000032#include <boost/range/adaptor/transformed.hpp>
33
Junxiao Shibb5105f2014-03-03 12:06:45 -070034namespace nfd {
Junxiao Shic34d1672016-12-09 15:57:59 +000035
36class Forwarder;
37
Junxiao Shiff10da62016-07-13 17:57:43 +000038namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070039
Junxiao Shie93d6a32014-09-07 16:13:22 -070040/** \brief represents the Strategy Choice table
41 *
42 * The Strategy Choice table maintains available Strategy types,
43 * and associates Name prefixes with Strategy types.
44 *
45 * Each strategy is identified by a strategyName.
46 * It's recommended to include a version number as the last component of strategyName.
47 *
48 * A Name prefix is owned by a strategy if a longest prefix match on the
49 * Strategy Choice table returns that strategy.
50 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070051class StrategyChoice : noncopyable
52{
53public:
Junxiao Shic34d1672016-12-09 15:57:59 +000054 explicit
55 StrategyChoice(Forwarder& forwarder);
Junxiao Shibb5105f2014-03-03 12:06:45 -070056
Junxiao Shib30863e2016-08-15 21:32:01 +000057 size_t
58 size() const
59 {
60 return m_nItems;
61 }
62
Junxiao Shic34d1672016-12-09 15:57:59 +000063 /** \brief set the default strategy
64 *
65 * This must be called by forwarder constructor.
66 */
67 void
68 setDefaultStrategy(const Name& strategyName);
69
Junxiao Shibb5105f2014-03-03 12:06:45 -070070public: // Strategy Choice table
Junxiao Shi530cf002017-01-03 14:43:16 +000071 class InsertResult
72 {
73 public:
74 explicit
75 operator bool() const
76 {
77 return m_status == OK;
78 }
79
80 bool
81 isRegistered() const
82 {
83 return m_status != NOT_REGISTERED;
84 }
85
86 private:
87 enum Status {
88 OK,
89 NOT_REGISTERED,
90 EXCEPTION
91 };
92
93 // implicitly constructible from Status
94 InsertResult(Status status, const std::string& exceptionMessage = "");
95
96 private:
97 Status m_status;
98 std::string m_exceptionMessage;
99
100 friend class StrategyChoice;
101 friend std::ostream& operator<<(std::ostream&, const InsertResult&);
102 };
103
104 /** \brief set strategy of \p prefix to be \p strategyName
105 * \param prefix the name prefix to change strategy
106 * \param strategyName strategy instance name, may contain version and parameters;
107 * strategy must have been registered
108 * \return convertible to true on success; convertible to false on failure
Junxiao Shibb5105f2014-03-03 12:06:45 -0700109 */
Junxiao Shi530cf002017-01-03 14:43:16 +0000110 InsertResult
Junxiao Shibb5105f2014-03-03 12:06:45 -0700111 insert(const Name& prefix, const Name& strategyName);
112
113 /** \brief make prefix to inherit strategy from its parent
114 *
115 * not allowed for root prefix (ndn:/)
116 */
117 void
118 erase(const Name& prefix);
119
120 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -0700121 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -0700122 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700123 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700124 get(const Name& prefix) const;
125
Junxiao Shib5888d22014-05-26 07:35:22 -0700126public: // effective strategy
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000127 /** \brief get effective strategy for prefix
128 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700129 fw::Strategy&
130 findEffectiveStrategy(const Name& prefix) const;
131
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000132 /** \brief get effective strategy for pitEntry
Junxiao Shib30863e2016-08-15 21:32:01 +0000133 *
134 * This is equivalent to .findEffectiveStrategy(pitEntry.getName())
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000135 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700136 fw::Strategy&
137 findEffectiveStrategy(const pit::Entry& pitEntry) const;
138
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000139 /** \brief get effective strategy for measurementsEntry
Junxiao Shib30863e2016-08-15 21:32:01 +0000140 *
141 * This is equivalent to .findEffectiveStrategy(measurementsEntry.getName())
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000142 */
Junxiao Shi7bb01512014-03-05 21:34:09 -0700143 fw::Strategy&
144 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
145
Junxiao Shib5888d22014-05-26 07:35:22 -0700146public: // enumeration
Junxiao Shib30863e2016-08-15 21:32:01 +0000147 typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range;
148 typedef boost::range_iterator<Range>::type const_iterator;
Junxiao Shib5888d22014-05-26 07:35:22 -0700149
Junxiao Shib30863e2016-08-15 21:32:01 +0000150 /** \return an iterator to the beginning
151 * \note Iteration order is implementation-defined.
152 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
153 * is inserted or erased during enumeration.
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000154 */
Junxiao Shib5888d22014-05-26 07:35:22 -0700155 const_iterator
Junxiao Shib30863e2016-08-15 21:32:01 +0000156 begin() const
157 {
158 return this->getRange().begin();
159 }
Junxiao Shib5888d22014-05-26 07:35:22 -0700160
Junxiao Shib30863e2016-08-15 21:32:01 +0000161 /** \return an iterator to the end
162 * \sa begin()
163 */
Junxiao Shib5888d22014-05-26 07:35:22 -0700164 const_iterator
Junxiao Shib30863e2016-08-15 21:32:01 +0000165 end() const
166 {
167 return this->getRange().end();
168 }
Junxiao Shib5888d22014-05-26 07:35:22 -0700169
Junxiao Shibb5105f2014-03-03 12:06:45 -0700170private:
Junxiao Shibb5105f2014-03-03 12:06:45 -0700171 void
Junxiao Shiff10da62016-07-13 17:57:43 +0000172 changeStrategy(Entry& entry,
Junxiao Shi838c4f12014-11-03 18:55:24 -0700173 fw::Strategy& oldStrategy,
174 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700175
Junxiao Shidd8f6612016-08-12 15:42:52 +0000176 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
177 */
178 template<typename K>
HangZhangcb4fc832014-03-11 16:57:11 +0800179 fw::Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000180 findEffectiveStrategyImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800181
Junxiao Shib30863e2016-08-15 21:32:01 +0000182 Range
183 getRange() const;
184
Junxiao Shibb5105f2014-03-03 12:06:45 -0700185private:
Junxiao Shic34d1672016-12-09 15:57:59 +0000186 Forwarder& m_forwarder;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700187 NameTree& m_nameTree;
188 size_t m_nItems;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700189};
190
Junxiao Shi530cf002017-01-03 14:43:16 +0000191std::ostream&
192operator<<(std::ostream& os, const StrategyChoice::InsertResult& res);
193
Junxiao Shiff10da62016-07-13 17:57:43 +0000194} // namespace strategy_choice
195
196using strategy_choice::StrategyChoice;
197
Junxiao Shibb5105f2014-03-03 12:06:45 -0700198} // namespace nfd
199
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700200#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP