blob: 7082092f076f105119daa64ba9729ed9694fba85 [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:
49 StrategyChoice(NameTree& nameTree, shared_ptr<fw::Strategy> defaultStrategy);
50
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
61 * \return true if installed; false if not installed due to duplicate strategyName
Junxiao Shi838c4f12014-11-03 18:55:24 -070062 * \note shared_ptr is passed by value because StrategyChoice takes ownership of strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070063 */
64 bool
65 install(shared_ptr<fw::Strategy> strategy);
66
67public: // Strategy Choice table
68 /** \brief set strategy of prefix to be strategyName
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050069 * \param prefix the name prefix for which \p strategyName should be used
Junxiao Shie93d6a32014-09-07 16:13:22 -070070 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070071 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070072 *
73 * This method set a strategy onto a Name prefix.
74 * The strategy must have been installed.
75 * The strategyName can either be exact (contains version component),
76 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -070077 */
78 bool
79 insert(const Name& prefix, const Name& strategyName);
80
81 /** \brief make prefix to inherit strategy from its parent
82 *
83 * not allowed for root prefix (ndn:/)
84 */
85 void
86 erase(const Name& prefix);
87
88 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -070089 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -070090 */
Junxiao Shi838c4f12014-11-03 18:55:24 -070091 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -070092 get(const Name& prefix) const;
93
Junxiao Shib5888d22014-05-26 07:35:22 -070094public: // effective strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070095 /// get effective strategy for prefix
96 fw::Strategy&
97 findEffectiveStrategy(const Name& prefix) const;
98
99 /// get effective strategy for pitEntry
100 fw::Strategy&
101 findEffectiveStrategy(const pit::Entry& pitEntry) const;
102
Junxiao Shi7bb01512014-03-05 21:34:09 -0700103 /// get effective strategy for measurementsEntry
104 fw::Strategy&
105 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
106
Junxiao Shib5888d22014-05-26 07:35:22 -0700107public: // enumeration
108 class const_iterator
Junxiao Shiff10da62016-07-13 17:57:43 +0000109 : public std::iterator<std::forward_iterator_tag, const Entry>
Junxiao Shib5888d22014-05-26 07:35:22 -0700110 {
111 public:
112 explicit
113 const_iterator(const NameTree::const_iterator& it);
114
115 ~const_iterator();
116
Junxiao Shiff10da62016-07-13 17:57:43 +0000117 const Entry&
Junxiao Shib5888d22014-05-26 07:35:22 -0700118 operator*() const;
119
Junxiao Shiff10da62016-07-13 17:57:43 +0000120 const Entry*
Junxiao Shib5888d22014-05-26 07:35:22 -0700121 operator->() const;
122
123 const_iterator&
124 operator++();
125
126 const_iterator
127 operator++(int);
128
129 bool
130 operator==(const const_iterator& other) const;
131
132 bool
133 operator!=(const const_iterator& other) const;
134
135 private:
136 NameTree::const_iterator m_nameTreeIterator;
137 };
138
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139 /// number of entries stored
140 size_t
141 size() const;
142
Junxiao Shib5888d22014-05-26 07:35:22 -0700143 const_iterator
144 begin() const;
145
146 const_iterator
147 end() const;
148
Junxiao Shibb5105f2014-03-03 12:06:45 -0700149private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700150 /** \brief get Strategy instance by strategyName
151 * \param strategyName a versioned or unversioned strategyName
152 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700153 fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700154 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700155
156 void
157 setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
158
159 void
Junxiao Shiff10da62016-07-13 17:57:43 +0000160 changeStrategy(Entry& entry,
Junxiao Shi838c4f12014-11-03 18:55:24 -0700161 fw::Strategy& oldStrategy,
162 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163
HangZhangcb4fc832014-03-11 16:57:11 +0800164 fw::Strategy&
Junxiao Shi838c4f12014-11-03 18:55:24 -0700165 findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800166
Junxiao Shibb5105f2014-03-03 12:06:45 -0700167private:
168 NameTree& m_nameTree;
169 size_t m_nItems;
170
Junxiao Shiff10da62016-07-13 17:57:43 +0000171 typedef std::map<Name, shared_ptr<fw::Strategy>> StrategyInstanceTable;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700172 StrategyInstanceTable m_strategyInstances;
173};
174
175inline size_t
176StrategyChoice::size() const
177{
178 return m_nItems;
179}
180
Junxiao Shib5888d22014-05-26 07:35:22 -0700181inline StrategyChoice::const_iterator
182StrategyChoice::end() const
183{
184 return const_iterator(m_nameTree.end());
185}
186
187inline
188StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
189 : m_nameTreeIterator(it)
190{
191}
192
193inline
194StrategyChoice::const_iterator::~const_iterator()
195{
196}
197
198inline
199StrategyChoice::const_iterator
200StrategyChoice::const_iterator::operator++(int)
201{
202 StrategyChoice::const_iterator temp(*this);
203 ++(*this);
204 return temp;
205}
206
207inline StrategyChoice::const_iterator&
208StrategyChoice::const_iterator::operator++()
209{
210 ++m_nameTreeIterator;
211 return *this;
212}
213
Junxiao Shiff10da62016-07-13 17:57:43 +0000214inline const Entry&
Junxiao Shib5888d22014-05-26 07:35:22 -0700215StrategyChoice::const_iterator::operator*() const
216{
217 return *(m_nameTreeIterator->getStrategyChoiceEntry());
218}
219
Junxiao Shiff10da62016-07-13 17:57:43 +0000220inline const Entry*
Junxiao Shib5888d22014-05-26 07:35:22 -0700221StrategyChoice::const_iterator::operator->() const
222{
223 return m_nameTreeIterator->getStrategyChoiceEntry();
224}
225
226inline bool
227StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
228{
229 return m_nameTreeIterator == other.m_nameTreeIterator;
230}
231
232inline bool
233StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
234{
235 return m_nameTreeIterator != other.m_nameTreeIterator;
236}
237
Junxiao Shiff10da62016-07-13 17:57:43 +0000238} // namespace strategy_choice
239
240using strategy_choice::StrategyChoice;
241
Junxiao Shibb5105f2014-03-03 12:06:45 -0700242} // namespace nfd
243
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700244#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP