blob: fdff3ab5fe78aa299547fa22cdf0b21c8558cf38 [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
52 * \param isExact true to require exact match, false to permit unversioned strategyName
53 * \return true if strategy is installed
Junxiao Shibb5105f2014-03-03 12:06:45 -070054 */
55 bool
Junxiao Shie93d6a32014-09-07 16:13:22 -070056 hasStrategy(const Name& strategyName, bool isExact = false) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -070057
58 /** \brief install a strategy
59 * \return true if installed; false if not installed due to duplicate strategyName
Junxiao Shi838c4f12014-11-03 18:55:24 -070060 * \note shared_ptr is passed by value because StrategyChoice takes ownership of strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070061 */
62 bool
63 install(shared_ptr<fw::Strategy> strategy);
64
65public: // Strategy Choice table
66 /** \brief set strategy of prefix to be strategyName
Junxiao Shie93d6a32014-09-07 16:13:22 -070067 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070068 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070069 *
70 * This method set a strategy onto a Name prefix.
71 * The strategy must have been installed.
72 * The strategyName can either be exact (contains version component),
73 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -070074 */
75 bool
76 insert(const Name& prefix, const Name& strategyName);
77
78 /** \brief make prefix to inherit strategy from its parent
79 *
80 * not allowed for root prefix (ndn:/)
81 */
82 void
83 erase(const Name& prefix);
84
85 /** \brief get strategy Name of prefix
Junxiao Shi838c4f12014-11-03 18:55:24 -070086 * \return true and strategyName at exact match, or false
Junxiao Shibb5105f2014-03-03 12:06:45 -070087 */
Junxiao Shi838c4f12014-11-03 18:55:24 -070088 std::pair<bool, Name>
Junxiao Shibb5105f2014-03-03 12:06:45 -070089 get(const Name& prefix) const;
90
Junxiao Shib5888d22014-05-26 07:35:22 -070091public: // effective strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070092 /// get effective strategy for prefix
93 fw::Strategy&
94 findEffectiveStrategy(const Name& prefix) const;
95
96 /// get effective strategy for pitEntry
97 fw::Strategy&
98 findEffectiveStrategy(const pit::Entry& pitEntry) const;
99
Junxiao Shi7bb01512014-03-05 21:34:09 -0700100 /// get effective strategy for measurementsEntry
101 fw::Strategy&
102 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
103
Junxiao Shib5888d22014-05-26 07:35:22 -0700104public: // enumeration
105 class const_iterator
106 : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
107 {
108 public:
109 explicit
110 const_iterator(const NameTree::const_iterator& it);
111
112 ~const_iterator();
113
114 const strategy_choice::Entry&
115 operator*() const;
116
117 shared_ptr<strategy_choice::Entry>
118 operator->() const;
119
120 const_iterator&
121 operator++();
122
123 const_iterator
124 operator++(int);
125
126 bool
127 operator==(const const_iterator& other) const;
128
129 bool
130 operator!=(const const_iterator& other) const;
131
132 private:
133 NameTree::const_iterator m_nameTreeIterator;
134 };
135
Junxiao Shibb5105f2014-03-03 12:06:45 -0700136 /// number of entries stored
137 size_t
138 size() const;
139
Junxiao Shib5888d22014-05-26 07:35:22 -0700140 const_iterator
141 begin() const;
142
143 const_iterator
144 end() const;
145
Junxiao Shibb5105f2014-03-03 12:06:45 -0700146private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700147 /** \brief get Strategy instance by strategyName
148 * \param strategyName a versioned or unversioned strategyName
149 */
Junxiao Shi838c4f12014-11-03 18:55:24 -0700150 fw::Strategy*
Junxiao Shie93d6a32014-09-07 16:13:22 -0700151 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152
153 void
154 setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
155
156 void
Junxiao Shi838c4f12014-11-03 18:55:24 -0700157 changeStrategy(strategy_choice::Entry& entry,
158 fw::Strategy& oldStrategy,
159 fw::Strategy& newStrategy);
Junxiao Shibb5105f2014-03-03 12:06:45 -0700160
HangZhangcb4fc832014-03-11 16:57:11 +0800161 fw::Strategy&
Junxiao Shi838c4f12014-11-03 18:55:24 -0700162 findEffectiveStrategy(shared_ptr<name_tree::Entry> nte) const;
HangZhangcb4fc832014-03-11 16:57:11 +0800163
Junxiao Shibb5105f2014-03-03 12:06:45 -0700164private:
165 NameTree& m_nameTree;
166 size_t m_nItems;
167
168 typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
169 StrategyInstanceTable m_strategyInstances;
170};
171
172inline size_t
173StrategyChoice::size() const
174{
175 return m_nItems;
176}
177
Junxiao Shib5888d22014-05-26 07:35:22 -0700178inline StrategyChoice::const_iterator
179StrategyChoice::end() const
180{
181 return const_iterator(m_nameTree.end());
182}
183
184inline
185StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
186 : m_nameTreeIterator(it)
187{
188}
189
190inline
191StrategyChoice::const_iterator::~const_iterator()
192{
193}
194
195inline
196StrategyChoice::const_iterator
197StrategyChoice::const_iterator::operator++(int)
198{
199 StrategyChoice::const_iterator temp(*this);
200 ++(*this);
201 return temp;
202}
203
204inline StrategyChoice::const_iterator&
205StrategyChoice::const_iterator::operator++()
206{
207 ++m_nameTreeIterator;
208 return *this;
209}
210
211inline const strategy_choice::Entry&
212StrategyChoice::const_iterator::operator*() const
213{
214 return *(m_nameTreeIterator->getStrategyChoiceEntry());
215}
216
217inline shared_ptr<strategy_choice::Entry>
218StrategyChoice::const_iterator::operator->() const
219{
220 return m_nameTreeIterator->getStrategyChoiceEntry();
221}
222
223inline bool
224StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
225{
226 return m_nameTreeIterator == other.m_nameTreeIterator;
227}
228
229inline bool
230StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
231{
232 return m_nameTreeIterator != other.m_nameTreeIterator;
233}
234
Junxiao Shibb5105f2014-03-03 12:06:45 -0700235} // namespace nfd
236
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700237#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP