blob: 9441dcc90b157fb174cc6bacc5595d5e62b5233c [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/>.
24 **/
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
34class StrategyChoice : noncopyable
35{
36public:
37 StrategyChoice(NameTree& nameTree, shared_ptr<fw::Strategy> defaultStrategy);
38
39public: // available Strategy types
40 /** \return true if strategy is installed
41 */
42 bool
43 hasStrategy(const Name& strategyName) const;
44
45 /** \brief install a strategy
46 * \return true if installed; false if not installed due to duplicate strategyName
47 */
48 bool
49 install(shared_ptr<fw::Strategy> strategy);
50
51public: // Strategy Choice table
52 /** \brief set strategy of prefix to be strategyName
53 * \param strategyName the strategy to be used, must be installed
54 * \return true on success
55 */
56 bool
57 insert(const Name& prefix, const Name& strategyName);
58
59 /** \brief make prefix to inherit strategy from its parent
60 *
61 * not allowed for root prefix (ndn:/)
62 */
63 void
64 erase(const Name& prefix);
65
66 /** \brief get strategy Name of prefix
67 * \return strategyName at exact match, or nullptr
68 */
69 shared_ptr<const Name>
70 get(const Name& prefix) const;
71
Junxiao Shib5888d22014-05-26 07:35:22 -070072public: // effective strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070073 /// get effective strategy for prefix
74 fw::Strategy&
75 findEffectiveStrategy(const Name& prefix) const;
76
77 /// get effective strategy for pitEntry
78 fw::Strategy&
79 findEffectiveStrategy(const pit::Entry& pitEntry) const;
80
Junxiao Shi7bb01512014-03-05 21:34:09 -070081 /// get effective strategy for measurementsEntry
82 fw::Strategy&
83 findEffectiveStrategy(const measurements::Entry& measurementsEntry) const;
84
Junxiao Shib5888d22014-05-26 07:35:22 -070085public: // enumeration
86 class const_iterator
87 : public std::iterator<std::forward_iterator_tag, const strategy_choice::Entry>
88 {
89 public:
90 explicit
91 const_iterator(const NameTree::const_iterator& it);
92
93 ~const_iterator();
94
95 const strategy_choice::Entry&
96 operator*() const;
97
98 shared_ptr<strategy_choice::Entry>
99 operator->() const;
100
101 const_iterator&
102 operator++();
103
104 const_iterator
105 operator++(int);
106
107 bool
108 operator==(const const_iterator& other) const;
109
110 bool
111 operator!=(const const_iterator& other) const;
112
113 private:
114 NameTree::const_iterator m_nameTreeIterator;
115 };
116
Junxiao Shibb5105f2014-03-03 12:06:45 -0700117 /// number of entries stored
118 size_t
119 size() const;
120
Junxiao Shib5888d22014-05-26 07:35:22 -0700121 const_iterator
122 begin() const;
123
124 const_iterator
125 end() const;
126
Junxiao Shibb5105f2014-03-03 12:06:45 -0700127private:
128 shared_ptr<fw::Strategy>
129 getStrategy(const Name& strategyName);
130
131 void
132 setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
133
134 void
135 changeStrategy(shared_ptr<strategy_choice::Entry> entry,
136 shared_ptr<fw::Strategy> oldStrategy,
137 shared_ptr<fw::Strategy> newStrategy);
138
HangZhangcb4fc832014-03-11 16:57:11 +0800139 fw::Strategy&
140 findEffectiveStrategy(shared_ptr<name_tree::Entry> nameTreeEntry) const;
141
Junxiao Shibb5105f2014-03-03 12:06:45 -0700142private:
143 NameTree& m_nameTree;
144 size_t m_nItems;
145
146 typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
147 StrategyInstanceTable m_strategyInstances;
148};
149
150inline size_t
151StrategyChoice::size() const
152{
153 return m_nItems;
154}
155
Junxiao Shib5888d22014-05-26 07:35:22 -0700156inline StrategyChoice::const_iterator
157StrategyChoice::end() const
158{
159 return const_iterator(m_nameTree.end());
160}
161
162inline
163StrategyChoice::const_iterator::const_iterator(const NameTree::const_iterator& it)
164 : m_nameTreeIterator(it)
165{
166}
167
168inline
169StrategyChoice::const_iterator::~const_iterator()
170{
171}
172
173inline
174StrategyChoice::const_iterator
175StrategyChoice::const_iterator::operator++(int)
176{
177 StrategyChoice::const_iterator temp(*this);
178 ++(*this);
179 return temp;
180}
181
182inline StrategyChoice::const_iterator&
183StrategyChoice::const_iterator::operator++()
184{
185 ++m_nameTreeIterator;
186 return *this;
187}
188
189inline const strategy_choice::Entry&
190StrategyChoice::const_iterator::operator*() const
191{
192 return *(m_nameTreeIterator->getStrategyChoiceEntry());
193}
194
195inline shared_ptr<strategy_choice::Entry>
196StrategyChoice::const_iterator::operator->() const
197{
198 return m_nameTreeIterator->getStrategyChoiceEntry();
199}
200
201inline bool
202StrategyChoice::const_iterator::operator==(const StrategyChoice::const_iterator& other) const
203{
204 return m_nameTreeIterator == other.m_nameTreeIterator;
205}
206
207inline bool
208StrategyChoice::const_iterator::operator!=(const StrategyChoice::const_iterator& other) const
209{
210 return m_nameTreeIterator != other.m_nameTreeIterator;
211}
212
Junxiao Shibb5105f2014-03-03 12:06:45 -0700213} // namespace nfd
214
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700215#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_HPP