blob: 75d3a47e8ada09d057599ec20d5514d7eb42e58a [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
Junxiao Shidd8f6612016-08-12 15:42:52 +0000169 /** \tparam K a parameter acceptable to NameTree::findLongestPrefixMatch
170 */
171 template<typename K>
HangZhangcb4fc832014-03-11 16:57:11 +0800172 fw::Strategy&
Junxiao Shidd8f6612016-08-12 15:42:52 +0000173 findEffectiveStrategyImpl(const K& key) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800174
Junxiao Shibb5105f2014-03-03 12:06:45 -0700175private:
176 NameTree& m_nameTree;
177 size_t m_nItems;
178
Junxiao Shia49a1ab2016-07-15 18:24:36 +0000179 typedef std::map<Name, unique_ptr<fw::Strategy>> StrategyInstanceTable;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700180 StrategyInstanceTable m_strategyInstances;
181};
182
183inline size_t
184StrategyChoice::size() const
185{
186 return m_nItems;
187}
188
Junxiao Shib5888d22014-05-26 07:35:22 -0700189inline StrategyChoice::const_iterator
190StrategyChoice::end() const
191{
192 return const_iterator(m_nameTree.end());
193}
194
195inline
196StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
197 : m_nameTreeIterator(it)
198{
199}
200
201inline
202StrategyChoice::const_iterator::~const_iterator()
203{
204}
205
206inline
207StrategyChoice::const_iterator
208StrategyChoice::const_iterator::operator++(int)
209{
210 StrategyChoice::const_iterator temp(*this);
211 ++(*this);
212 return temp;
213}
214
215inline StrategyChoice::const_iterator&
216StrategyChoice::const_iterator::operator++()
217{
218 ++m_nameTreeIterator;
219 return *this;
220}
221
Junxiao Shiff10da62016-07-13 17:57:43 +0000222inline const Entry&
Junxiao Shib5888d22014-05-26 07:35:22 -0700223StrategyChoice::const_iterator::operator*() const
224{
225 return *(m_nameTreeIterator->getStrategyChoiceEntry());
226}
227
Junxiao Shiff10da62016-07-13 17:57:43 +0000228inline const Entry*
Junxiao Shib5888d22014-05-26 07:35:22 -0700229StrategyChoice::const_iterator::operator->() const
230{
231 return m_nameTreeIterator->getStrategyChoiceEntry();
232}
233
234inline bool
235StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
236{
237 return m_nameTreeIterator == other.m_nameTreeIterator;
238}
239
240inline bool
241StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
242{
243 return m_nameTreeIterator != other.m_nameTreeIterator;
244}
245
Junxiao Shiff10da62016-07-13 17:57:43 +0000246} // namespace strategy_choice
247
248using strategy_choice::StrategyChoice;
249
Junxiao Shibb5105f2014-03-03 12:06:45 -0700250} // namespace nfd
251
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700252#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP