blob: 2c492cdf35c3bc49e373e53e8e6dd78f862bade4 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shiff10da62016-07-13 17:57:43 +00003 * 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.
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 Shie93d6a32014-09-07 16:13:22 -070070 /** \brief determines if a strategy is installed
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050071 * \param strategyName name of the strategy
Junxiao Shie93d6a32014-09-07 16:13:22 -070072 * \param isExact true to require exact match, false to permit unversioned strategyName
73 * \return true if strategy is installed
Junxiao Shibb5105f2014-03-03 12:06:45 -070074 */
Junxiao Shif15058a2016-12-11 20:15:05 +000075 DEPRECATED(
Junxiao Shibb5105f2014-03-03 12:06:45 -070076 bool
Junxiao Shif15058a2016-12-11 20:15:05 +000077 hasStrategy(const Name& strategyName, bool isExact = false) const);
78
79 // DEPRECATED macro does not work when this type appears inline on install function.
80 typedef std::pair<bool, fw::Strategy*> InstallResult;
Junxiao Shibb5105f2014-03-03 12:06:45 -070081
82 /** \brief install a strategy
Junxiao Shia49a1ab2016-07-15 18:24:36 +000083 * \return if installed, true, and a pointer to the strategy instance;
84 * if not installed due to duplicate strategyName, false,
85 * and a pointer to the existing strategy instance
Junxiao Shibb5105f2014-03-03 12:06:45 -070086 */
Junxiao Shif15058a2016-12-11 20:15:05 +000087 DEPRECATED(
88 InstallResult
89 install(unique_ptr<fw::Strategy> strategy));
Junxiao Shibb5105f2014-03-03 12:06:45 -070090
91public: // Strategy Choice table
92 /** \brief set strategy of prefix to be strategyName
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050093 * \param prefix the name prefix for which \p strategyName should be used
Junxiao Shie93d6a32014-09-07 16:13:22 -070094 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070095 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070096 *
97 * This method set a strategy onto a Name prefix.
98 * The strategy must have been installed.
99 * The strategyName can either be exact (contains version component),
100 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -0700101 */
102 bool
103 insert(const Name& prefix, const Name& strategyName);
104
105 /** \brief make prefix to inherit strategy from its parent
106 *
107 * not allowed for root prefix (ndn:/)
108 */
109 void
110 erase(const Name& prefix);
111
112 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -0700113 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -0700114 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700115 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -0700116 get(const Name& prefix) const;
117
Junxiao Shib5888d22014-05-26 07:35:22 -0700118public: // effective strategy
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000119 /** \brief get effective strategy for prefix
120 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700121 fw::Strategy&
122 findEffectiveStrategy(const Name& prefix) const;
123
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000124 /** \brief get effective strategy for pitEntry
Junxiao Shib30863e2016-08-15 21:32:01 +0000125 *
126 * This is equivalent to .findEffectiveStrategy(pitEntry.getName())
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000127 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700128 fw::Strategy&
129 findEffectiveStrategy(const pit::Entry& pitEntry) const;
130
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000131 /** \brief get effective strategy for measurementsEntry
Junxiao Shib30863e2016-08-15 21:32:01 +0000132 *
133 * This is equivalent to .findEffectiveStrategy(measurementsEntry.getName())
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000134 */
Junxiao Shi7bb01512014-03-05 21:34:09 -0700135 fw::Strategy&
136 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
137
Junxiao Shib5888d22014-05-26 07:35:22 -0700138public: // enumeration
Junxiao Shib30863e2016-08-15 21:32:01 +0000139 typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range;
140 typedef boost::range_iterator<Range>::type const_iterator;
Junxiao Shib5888d22014-05-26 07:35:22 -0700141
Junxiao Shib30863e2016-08-15 21:32:01 +0000142 /** \return an iterator to the beginning
143 * \note Iteration order is implementation-defined.
144 * \warning Undefined behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
145 * is inserted or erased during enumeration.
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000146 */
Junxiao Shib5888d22014-05-26 07:35:22 -0700147 const_iterator
Junxiao Shib30863e2016-08-15 21:32:01 +0000148 begin() const
149 {
150 return this->getRange().begin();
151 }
Junxiao Shib5888d22014-05-26 07:35:22 -0700152
Junxiao Shib30863e2016-08-15 21:32:01 +0000153 /** \return an iterator to the end
154 * \sa begin()
155 */
Junxiao Shib5888d22014-05-26 07:35:22 -0700156 const_iterator
Junxiao Shib30863e2016-08-15 21:32:01 +0000157 end() const
158 {
159 return this->getRange().end();
160 }
Junxiao Shib5888d22014-05-26 07:35:22 -0700161
Junxiao Shibb5105f2014-03-03 12:06:45 -0700162private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700163 /** \brief get Strategy instance by strategyName
164 * \param strategyName a versioned or unversioned strategyName
165 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700166 fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700167 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700168
169 void
Junxiao Shiff10da62016-07-13 17:57:43 +0000170 changeStrategy(Entry& entry,
Junxiao Shi838c4f12014-11-03 18:55:24 -0700171 fw::Strategy& oldStrategy,
172 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700173
Junxiao Shidd8f6612016-08-12 15:42:52 +0000174 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
175 */
176 template<typename K>
HangZhangcb4fc832014-03-11 16:57:11 +0800177 fw::Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000178 findEffectiveStrategyImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800179
Junxiao Shib30863e2016-08-15 21:32:01 +0000180 Range
181 getRange() const;
182
Junxiao Shibb5105f2014-03-03 12:06:45 -0700183private:
Junxiao Shic34d1672016-12-09 15:57:59 +0000184 Forwarder& m_forwarder;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700185 NameTree& m_nameTree;
186 size_t m_nItems;
187
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000188 typedef std::map<Name, unique_ptr<fw::Strategy>> StrategyInstanceTable;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700189 StrategyInstanceTable m_strategyInstances;
190};
191
Junxiao Shiff10da62016-07-13 17:57:43 +0000192} // namespace strategy_choice
193
194using strategy_choice::StrategyChoice;
195
Junxiao Shibb5105f2014-03-03 12:06:45 -0700196} // namespace nfd
197
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700198#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP