blob: 68ed39538b24f25e026d9f2e5fbc4f6625bf9dd2 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib5888d22014-05-26 07:35:22 -07003 * Copyright (c) 2014, 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 {
33
Junxiao Shie93d6a32014-09-07 16:13:22 -070034/** \brief represents the Strategy Choice table
35 *
36 * The Strategy Choice table maintains available Strategy types,
37 * and associates Name prefixes with Strategy types.
38 *
39 * Each strategy is identified by a strategyName.
40 * It's recommended to include a version number as the last component of strategyName.
41 *
42 * A Name prefix is owned by a strategy if a longest prefix match on the
43 * Strategy Choice table returns that strategy.
44 */
Junxiao Shibb5105f2014-03-03 12:06:45 -070045class StrategyChoice : noncopyable
46{
47public:
48 StrategyChoice(NameTree& nameTree, shared_ptr<fw::Strategy> defaultStrategy);
49
50public: // available Strategy types
Junxiao Shie93d6a32014-09-07 16:13:22 -070051 /** \brief determines if a strategy is installed
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050052 * \param strategyName name of the strategy
Junxiao Shie93d6a32014-09-07 16:13:22 -070053 * \param isExact true to require exact match, false to permit unversioned strategyName
54 * \return true if strategy is installed
Junxiao Shibb5105f2014-03-03 12:06:45 -070055 */
56 bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070057 hasStrategy(const Name& strategyName, bool isExact = false) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -070058
59 /** \brief install a strategy
60 * \return true if installed; false if not installed due to duplicate strategyName
Junxiao Shi838c4f12014-11-03 18:55:24 -070061 * \note shared_ptr is passed by value because StrategyChoice takes ownership of strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070062 */
63 bool
64 install(shared_ptr<fw::Strategy> strategy);
65
66public: // Strategy Choice table
67 /** \brief set strategy of prefix to be strategyName
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050068 * \param prefix the name prefix for which \p strategyName should be used
Junxiao Shie93d6a32014-09-07 16:13:22 -070069 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070070 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070071 *
72 * This method set a strategy onto a Name prefix.
73 * The strategy must have been installed.
74 * The strategyName can either be exact (contains version component),
75 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -070076 */
77 bool
78 insert(const Name& prefix, const Name& strategyName);
79
80 /** \brief make prefix to inherit strategy from its parent
81 *
82 * not allowed for root prefix (ndn:/)
83 */
84 void
85 erase(const Name& prefix);
86
87 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -070089 */
Junxiao Shi838c4f12014-11-03 18:55:24 -070090 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -070091 get(const Name& prefix) const;
92
Junxiao Shib5888d22014-05-26 07:35:22 -070093public: // effective strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070094 /// get effective strategy for prefix
95 fw::Strategy&
96 findEffectiveStrategy(const Name& prefix) const;
97
98 /// get effective strategy for pitEntry
99 fw::Strategy&
100 findEffectiveStrategy(const pit::Entry& pitEntry) const;
101
Junxiao Shi7bb01512014-03-05 21:34:09 -0700102 /// get effective strategy for measurementsEntry
103 fw::Strategy&
104 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
105
Junxiao Shib5888d22014-05-26 07:35:22 -0700106public: // enumeration
107 class const_iterator
108 : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
109 {
110 public:
111 explicit
112 const_iterator(const NameTree::const_iterator& it);
113
114 ~const_iterator();
115
116 const strategy_choice::Entry&
117 operator*() const;
118
119 shared_ptr<strategy_choice::Entry>
120 operator->() const;
121
122 const_iterator&
123 operator++();
124
125 const_iterator
126 operator++(int);
127
128 bool
129 operator==(const const_iterator& other) const;
130
131 bool
132 operator!=(const const_iterator& other) const;
133
134 private:
135 NameTree::const_iterator m_nameTreeIterator;
136 };
137
Junxiao Shibb5105f2014-03-03 12:06:45 -0700138 /// number of entries stored
139 size_t
140 size() const;
141
Junxiao Shib5888d22014-05-26 07:35:22 -0700142 const_iterator
143 begin() const;
144
145 const_iterator
146 end() const;
147
Junxiao Shibb5105f2014-03-03 12:06:45 -0700148private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700149 /** \brief get Strategy instance by strategyName
150 * \param strategyName a versioned or unversioned strategyName
151 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700152 fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700153 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700154
155 void
156 setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
157
158 void
Junxiao Shi838c4f12014-11-03 18:55:24 -0700159 changeStrategy(strategy_choice::Entry& entry,
160 fw::Strategy& oldStrategy,
161 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700162
HangZhangcb4fc832014-03-11 16:57:11 +0800163 fw::Strategy&
Junxiao Shi838c4f12014-11-03 18:55:24 -0700164 findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800165
Junxiao Shibb5105f2014-03-03 12:06:45 -0700166private:
167 NameTree& m_nameTree;
168 size_t m_nItems;
169
170 typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
171 StrategyInstanceTable m_strategyInstances;
172};
173
174inline size_t
175StrategyChoice::size() const
176{
177 return m_nItems;
178}
179
Junxiao Shib5888d22014-05-26 07:35:22 -0700180inline StrategyChoice::const_iterator
181StrategyChoice::end() const
182{
183 return const_iterator(m_nameTree.end());
184}
185
186inline
187StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
188 : m_nameTreeIterator(it)
189{
190}
191
192inline
193StrategyChoice::const_iterator::~const_iterator()
194{
195}
196
197inline
198StrategyChoice::const_iterator
199StrategyChoice::const_iterator::operator++(int)
200{
201 StrategyChoice::const_iterator temp(*this);
202 ++(*this);
203 return temp;
204}
205
206inline StrategyChoice::const_iterator&
207StrategyChoice::const_iterator::operator++()
208{
209 ++m_nameTreeIterator;
210 return *this;
211}
212
213inline const strategy_choice::Entry&
214StrategyChoice::const_iterator::operator*() const
215{
216 return *(m_nameTreeIterator->getStrategyChoiceEntry());
217}
218
219inline shared_ptr<strategy_choice::Entry>
220StrategyChoice::const_iterator::operator->() const
221{
222 return m_nameTreeIterator->getStrategyChoiceEntry();
223}
224
225inline bool
226StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
227{
228 return m_nameTreeIterator == other.m_nameTreeIterator;
229}
230
231inline bool
232StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
233{
234 return m_nameTreeIterator != other.m_nameTreeIterator;
235}
236
Junxiao Shibb5105f2014-03-03 12:06:45 -0700237} // namespace nfd
238
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700239#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP