blob: 1388cbb2c6625ecc0b4e841c36b0b7411a016175 [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
32namespace nfd {
Junxiao Shiff10da62016-07-13 17:57:43 +000033namespace strategy_choice {
Junxiao Shibb5105f2014-03-03 12:06:45 -070034
Junxiao Shie93d6a32014-09-07 16:13:22 -070035/** \brief represents the Strategy Choice table
36 *
37 * The Strategy Choice table maintains available Strategy types,
38 * and associates Name prefixes with Strategy types.
39 *
40 * Each strategy is identified by a strategyName.
41 * It's recommended to include a version number as the last component of strategyName.
42 *
43 * A Name prefix is owned by a strategy if a longest prefix match on the
44 * Strategy Choice table returns that strategy.
45 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070046class StrategyChoice : noncopyable
47{
48public:
Junxiao Shia49a1ab2016-07-15 18:24:36 +000049 StrategyChoice(NameTree& nameTree, unique_ptr<fw::Strategy> defaultStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -070050
51public: // available Strategy types
Junxiao Shie93d6a32014-09-07 16:13:22 -070052 /** \brief determines if a strategy is installed
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050053 * \param strategyName name of the strategy
Junxiao Shie93d6a32014-09-07 16:13:22 -070054 * \param isExact true to require exact match, false to permit unversioned strategyName
55 * \return true if strategy is installed
Junxiao Shibb5105f2014-03-03 12:06:45 -070056 */
57 bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070058 hasStrategy(const Name& strategyName, bool isExact = false) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -070059
60 /** \brief install a strategy
Junxiao Shia49a1ab2016-07-15 18:24:36 +000061 * \return if installed, true, and a pointer to the strategy instance;
62 * if not installed due to duplicate strategyName, false,
63 * and a pointer to the existing strategy instance
Junxiao Shibb5105f2014-03-03 12:06:45 -070064 */
Junxiao Shia49a1ab2016-07-15 18:24:36 +000065 std::pair<bool, fw::Strategy*>
66 install(unique_ptr<fw::Strategy> strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -070067
68public: // Strategy Choice table
69 /** \brief set strategy of prefix to be strategyName
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050070 * \param prefix the name prefix for which \p strategyName should be used
Junxiao Shie93d6a32014-09-07 16:13:22 -070071 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070072 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070073 *
74 * This method set a strategy onto a Name prefix.
75 * The strategy must have been installed.
76 * The strategyName can either be exact (contains version component),
77 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -070078 */
79 bool
80 insert(const Name& prefix, const Name& strategyName);
81
82 /** \brief make prefix to inherit strategy from its parent
83 *
84 * not allowed for root prefix (ndn:/)
85 */
86 void
87 erase(const Name& prefix);
88
89 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -070090 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -070091 */
Junxiao Shi838c4f12014-11-03 18:55:24 -070092 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -070093 get(const Name& prefix) const;
94
Junxiao Shib5888d22014-05-26 07:35:22 -070095public: // effective strategy
Junxiao Shia49a1ab2016-07-15 18:24:36 +000096 /** \brief get effective strategy for prefix
97 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070098 fw::Strategy&
99 findEffectiveStrategy(const Name& prefix) const;
100
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000101 /** \brief get effective strategy for pitEntry
102 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700103 fw::Strategy&
104 findEffectiveStrategy(const pit::Entry& pitEntry) const;
105
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000106 /** \brief get effective strategy for measurementsEntry
107 */
Junxiao Shi7bb01512014-03-05 21:34:09 -0700108 fw::Strategy&
109 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
110
Junxiao Shib5888d22014-05-26 07:35:22 -0700111public: // enumeration
112 class const_iterator
Junxiao Shiff10da62016-07-13 17:57:43 +0000113 : public std::iterator<std::forward_iterator_tag, const Entry>
Junxiao Shib5888d22014-05-26 07:35:22 -0700114 {
115 public:
116 explicit
117 const_iterator(const NameTree::const_iterator& it);
118
119 ~const_iterator();
120
Junxiao Shiff10da62016-07-13 17:57:43 +0000121 const Entry&
Junxiao Shib5888d22014-05-26 07:35:22 -0700122 operator*() const;
123
Junxiao Shiff10da62016-07-13 17:57:43 +0000124 const Entry*
Junxiao Shib5888d22014-05-26 07:35:22 -0700125 operator->() const;
126
127 const_iterator&
128 operator++();
129
130 const_iterator
131 operator++(int);
132
133 bool
134 operator==(const const_iterator& other) const;
135
136 bool
137 operator!=(const const_iterator& other) const;
138
139 private:
140 NameTree::const_iterator m_nameTreeIterator;
141 };
142
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000143 /** \return number of entries stored
144 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145 size_t
146 size() const;
147
Junxiao Shib5888d22014-05-26 07:35:22 -0700148 const_iterator
149 begin() const;
150
151 const_iterator
152 end() const;
153
Junxiao Shibb5105f2014-03-03 12:06:45 -0700154private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700155 /** \brief get Strategy instance by strategyName
156 * \param strategyName a versioned or unversioned strategyName
157 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700158 fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700159 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700160
161 void
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000162 setDefaultStrategy(unique_ptr<fw::Strategy> strategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163
164 void
Junxiao Shiff10da62016-07-13 17:57:43 +0000165 changeStrategy(Entry& entry,
Junxiao Shi838c4f12014-11-03 18:55:24 -0700166 fw::Strategy& oldStrategy,
167 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700168
HangZhangcb4fc832014-03-11 16:57:11 +0800169 fw::Strategy&
Junxiao Shif2420fc2016-08-11 13:18:21 +0000170 findEffectiveStrategy(const name_tree::Entry& nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800171
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172private:
173 NameTree& m_nameTree;
174 size_t m_nItems;
175
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000176 typedef std::map<Name, unique_ptr<fw::Strategy>> StrategyInstanceTable;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700177 StrategyInstanceTable m_strategyInstances;
178};
179
180inline size_t
181StrategyChoice::size() const
182{
183 return m_nItems;
184}
185
Junxiao Shib5888d22014-05-26 07:35:22 -0700186inline StrategyChoice::const_iterator
187StrategyChoice::end() const
188{
189 return const_iterator(m_nameTree.end());
190}
191
192inline
193StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
194 : m_nameTreeIterator(it)
195{
196}
197
198inline
199StrategyChoice::const_iterator::~const_iterator()
200{
201}
202
203inline
204StrategyChoice::const_iterator
205StrategyChoice::const_iterator::operator++(int)
206{
207 StrategyChoice::const_iterator temp(*this);
208 ++(*this);
209 return temp;
210}
211
212inline StrategyChoice::const_iterator&
213StrategyChoice::const_iterator::operator++()
214{
215 ++m_nameTreeIterator;
216 return *this;
217}
218
Junxiao Shiff10da62016-07-13 17:57:43 +0000219inline const Entry&
Junxiao Shib5888d22014-05-26 07:35:22 -0700220StrategyChoice::const_iterator::operator*() const
221{
222 return *(m_nameTreeIterator->getStrategyChoiceEntry());
223}
224
Junxiao Shiff10da62016-07-13 17:57:43 +0000225inline const Entry*
Junxiao Shib5888d22014-05-26 07:35:22 -0700226StrategyChoice::const_iterator::operator->() const
227{
228 return m_nameTreeIterator->getStrategyChoiceEntry();
229}
230
231inline bool
232StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
233{
234 return m_nameTreeIterator == other.m_nameTreeIterator;
235}
236
237inline bool
238StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
239{
240 return m_nameTreeIterator != other.m_nameTreeIterator;
241}
242
Junxiao Shiff10da62016-07-13 17:57:43 +0000243} // namespace strategy_choice
244
245using strategy_choice::StrategyChoice;
246
Junxiao Shibb5105f2014-03-03 12:06:45 -0700247} // namespace nfd
248
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700249#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP