blob: d454a291f34d718d1c13a46c982f3c6e9876e3be [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
60 */
61 bool
62 install(shared_ptr<fw::Strategy> strategy);
63
64public: // Strategy Choice table
65 /** \brief set strategy of prefix to be strategyName
Junxiao Shie93d6a32014-09-07 16:13:22 -070066 * \param strategyName the strategy to be used
Junxiao Shibb5105f2014-03-03 12:06:45 -070067 * \return true on success
Junxiao Shie93d6a32014-09-07 16:13:22 -070068 *
69 * This method set a strategy onto a Name prefix.
70 * The strategy must have been installed.
71 * The strategyName can either be exact (contains version component),
72 * or omit the version component to pick the latest version.
Junxiao Shibb5105f2014-03-03 12:06:45 -070073 */
74 bool
75 insert(const Name& prefix, const Name& strategyName);
76
77 /** \brief make prefix to inherit strategy from its parent
78 *
79 * not allowed for root prefix (ndn:/)
80 */
81 void
82 erase(const Name& prefix);
83
84 /** \brief get strategy Name of prefix
85 * \return strategyName at exact match, or nullptr
86 */
87 shared_ptr<const Name>
88 get(const Name& prefix) const;
89
Junxiao Shib5888d22014-05-26 07:35:22 -070090public: // effective strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070091 /// get effective strategy for prefix
92 fw::Strategy&
93 findEffectiveStrategy(const Name& prefix) const;
94
95 /// get effective strategy for pitEntry
96 fw::Strategy&
97 findEffectiveStrategy(const pit::Entry& pitEntry) const;
98
Junxiao Shi7bb01512014-03-05 21:34:09 -070099 /// get effective strategy for measurementsEntry
100 fw::Strategy&
101 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
102
Junxiao Shib5888d22014-05-26 07:35:22 -0700103public: // enumeration
104 class const_iterator
105 : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
106 {
107 public:
108 explicit
109 const_iterator(const NameTree::const_iterator& it);
110
111 ~const_iterator();
112
113 const strategy_choice::Entry&
114 operator*() const;
115
116 shared_ptr<strategy_choice::Entry>
117 operator->() const;
118
119 const_iterator&
120 operator++();
121
122 const_iterator
123 operator++(int);
124
125 bool
126 operator==(const const_iterator& other) const;
127
128 bool
129 operator!=(const const_iterator& other) const;
130
131 private:
132 NameTree::const_iterator m_nameTreeIterator;
133 };
134
Junxiao Shibb5105f2014-03-03 12:06:45 -0700135 /// number of entries stored
136 size_t
137 size() const;
138
Junxiao Shib5888d22014-05-26 07:35:22 -0700139 const_iterator
140 begin() const;
141
142 const_iterator
143 end() const;
144
Junxiao Shibb5105f2014-03-03 12:06:45 -0700145private:
Junxiao Shie93d6a32014-09-07 16:13:22 -0700146 /** \brief get Strategy instance by strategyName
147 * \param strategyName a versioned or unversioned strategyName
148 */
Junxiao Shibb5105f2014-03-03 12:06:45 -0700149 shared_ptr<fw::Strategy>
Junxiao Shie93d6a32014-09-07 16:13:22 -0700150 getStrategy(const Name& strategyName) const;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700151
152 void
153 setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
154
155 void
156 changeStrategy(shared_ptr<strategy_choice::Entry> entry,
157 shared_ptr<fw::Strategy> oldStrategy,
158 shared_ptr<fw::Strategy> newStrategy);
159
HangZhangcb4fc832014-03-11 16:57:11 +0800160 fw::Strategy&
161 findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const;
162
Junxiao Shibb5105f2014-03-03 12:06:45 -0700163private:
164 NameTree& m_nameTree;
165 size_t m_nItems;
166
167 typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
168 StrategyInstanceTable m_strategyInstances;
169};
170
171inline size_t
172StrategyChoice::size() const
173{
174 return m_nItems;
175}
176
Junxiao Shib5888d22014-05-26 07:35:22 -0700177inline StrategyChoice::const_iterator
178StrategyChoice::end() const
179{
180 return const_iterator(m_nameTree.end());
181}
182
183inline
184StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
185 : m_nameTreeIterator(it)
186{
187}
188
189inline
190StrategyChoice::const_iterator::~const_iterator()
191{
192}
193
194inline
195StrategyChoice::const_iterator
196StrategyChoice::const_iterator::operator++(int)
197{
198 StrategyChoice::const_iterator temp(*this);
199 ++(*this);
200 return temp;
201}
202
203inline StrategyChoice::const_iterator&
204StrategyChoice::const_iterator::operator++()
205{
206 ++m_nameTreeIterator;
207 return *this;
208}
209
210inline const strategy_choice::Entry&
211StrategyChoice::const_iterator::operator*() const
212{
213 return *(m_nameTreeIterator->getStrategyChoiceEntry());
214}
215
216inline shared_ptr<strategy_choice::Entry>
217StrategyChoice::const_iterator::operator->() const
218{
219 return m_nameTreeIterator->getStrategyChoiceEntry();
220}
221
222inline bool
223StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
224{
225 return m_nameTreeIterator == other.m_nameTreeIterator;
226}
227
228inline bool
229StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
230{
231 return m_nameTreeIterator != other.m_nameTreeIterator;
232}
233
Junxiao Shibb5105f2014-03-03 12:06:45 -0700234} // namespace nfd
235
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700236#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP