blob: ab2e307788603d08da10057e5f72b4951c3d920f [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
HYuana9b85752014-02-26 02:32:30 -060024
25// Name Tree Entry (i.e., Name Prefix Entry)
26
27#ifndef NFD_TABLE_NAME_TREE_ENTRY_HPP
28#define NFD_TABLE_NAME_TREE_ENTRY_HPP
29
30#include "common.hpp"
31#include "table/fib-entry.hpp"
32#include "table/pit-entry.hpp"
33#include "table/measurements-entry.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070034#include "table/strategy-choice-entry.hpp"
HYuana9b85752014-02-26 02:32:30 -060035
36namespace nfd {
37
38class NameTree;
39
40namespace name_tree {
41
Haowei Yuane1079fc2014-03-08 14:41:25 -060042// Forward declarations
HYuana9b85752014-02-26 02:32:30 -060043class Node;
44class Entry;
45
46/**
Haowei Yuane1079fc2014-03-08 14:41:25 -060047 * \brief Name Tree Node Class
HYuana9b85752014-02-26 02:32:30 -060048 */
49class Node
50{
51public:
52 Node();
53
54 ~Node();
55
56public:
57 // variables are in public as this is just a data structure
58 shared_ptr<Entry> m_entry; // Name Tree Entry (i.e., Name Prefix Entry)
59 Node* m_prev; // Previous Name Tree Node (to resolve hash collision)
60 Node* m_next; // Next Name Tree Node (to resolve hash collision)
61};
62
63/**
Haowei Yuane1079fc2014-03-08 14:41:25 -060064 * \brief Name Tree Entry Class
HYuana9b85752014-02-26 02:32:30 -060065 */
Junxiao Shiefceadc2014-03-09 18:52:57 -070066class Entry : public enable_shared_from_this<Entry>, noncopyable
HYuana9b85752014-02-26 02:32:30 -060067{
HYuana9b85752014-02-26 02:32:30 -060068public:
69 explicit
70 Entry(const Name& prefix);
71
72 ~Entry();
73
74 const Name&
75 getPrefix() const;
76
77 void
Haowei Yuanf52dac72014-03-24 23:35:03 -050078 setHash(size_t hash);
HYuana9b85752014-02-26 02:32:30 -060079
Haowei Yuanf52dac72014-03-24 23:35:03 -050080 size_t
HYuana9b85752014-02-26 02:32:30 -060081 getHash() const;
82
83 void
84 setParent(shared_ptr<Entry> parent);
85
86 shared_ptr<Entry>
87 getParent() const;
88
89 std::vector<shared_ptr<Entry> >&
90 getChildren();
Haowei Yuane1079fc2014-03-08 14:41:25 -060091
92 bool
93 hasChildren() const;
94
Junxiao Shi40631842014-03-01 13:52:37 -070095 bool
96 isEmpty() const;
HYuana9b85752014-02-26 02:32:30 -060097
98 void
Junxiao Shiefceadc2014-03-09 18:52:57 -070099 setFibEntry(shared_ptr<fib::Entry> fibEntry);
HYuana9b85752014-02-26 02:32:30 -0600100
101 shared_ptr<fib::Entry>
102 getFibEntry() const;
103
HYuana9b85752014-02-26 02:32:30 -0600104 void
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700105 insertPitEntry(shared_ptr<pit::Entry> pitEntry);
106
107 void
108 erasePitEntry(shared_ptr<pit::Entry> pitEntry);
HYuana9b85752014-02-26 02:32:30 -0600109
Haowei Yuane1079fc2014-03-08 14:41:25 -0600110 bool
111 hasPitEntries() const;
112
Junxiao Shie349ea12014-03-12 01:32:42 -0700113 const std::vector<shared_ptr<pit::Entry> >&
114 getPitEntries() const;
115
HYuana9b85752014-02-26 02:32:30 -0600116 void
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700117 setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry);
HYuana9b85752014-02-26 02:32:30 -0600118
119 shared_ptr<measurements::Entry>
120 getMeasurementsEntry() const;
121
Junxiao Shibb5105f2014-03-03 12:06:45 -0700122 void
123 setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry);
124
125 shared_ptr<strategy_choice::Entry>
126 getStrategyChoiceEntry() const;
127
HYuana9b85752014-02-26 02:32:30 -0600128private:
Haowei Yuanf52dac72014-03-24 23:35:03 -0500129 // Benefits of storing m_hash
130 // 1. m_hash is compared before m_prefix is compared
131 // 2. fast hash table resize support
132 size_t m_hash;
HYuana9b85752014-02-26 02:32:30 -0600133 Name m_prefix;
134 shared_ptr<Entry> m_parent; // Pointing to the parent entry.
135 std::vector<shared_ptr<Entry> > m_children; // Children pointers.
136 shared_ptr<fib::Entry> m_fibEntry;
137 std::vector<shared_ptr<pit::Entry> > m_pitEntries;
138 shared_ptr<measurements::Entry> m_measurementsEntry;
Junxiao Shibb5105f2014-03-03 12:06:45 -0700139 shared_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
HYuana9b85752014-02-26 02:32:30 -0600140
141 // get the Name Tree Node that is associated with this Name Tree Entry
142 Node* m_node;
Haowei Yuanf52dac72014-03-24 23:35:03 -0500143
Junxiao Shi9f7455b2014-04-07 21:02:16 -0700144 // Make private members accessible by Name Tree
145 friend class nfd::NameTree;
HYuana9b85752014-02-26 02:32:30 -0600146};
147
148inline const Name&
149Entry::getPrefix() const
150{
151 return m_prefix;
152}
153
Haowei Yuanf52dac72014-03-24 23:35:03 -0500154inline size_t
HYuana9b85752014-02-26 02:32:30 -0600155Entry::getHash() const
156{
157 return m_hash;
158}
159
Haowei Yuanf52dac72014-03-24 23:35:03 -0500160inline void
161Entry::setHash(size_t hash)
162{
163 m_hash = hash;
164}
165
HYuana9b85752014-02-26 02:32:30 -0600166inline shared_ptr<Entry>
167Entry::getParent() const
168{
169 return m_parent;
170}
171
Haowei Yuanf52dac72014-03-24 23:35:03 -0500172inline void
173Entry::setParent(shared_ptr<Entry> parent)
174{
175 m_parent = parent;
176}
177
HYuana9b85752014-02-26 02:32:30 -0600178inline std::vector<shared_ptr<name_tree::Entry> >&
179Entry::getChildren()
180{
181 return m_children;
182}
183
Junxiao Shi40631842014-03-01 13:52:37 -0700184inline bool
Haowei Yuane1079fc2014-03-08 14:41:25 -0600185Entry::hasChildren() const
186{
187 return !m_children.empty();
188}
189
190inline bool
Junxiao Shi40631842014-03-01 13:52:37 -0700191Entry::isEmpty() const
192{
193 return m_children.empty() &&
194 !static_cast<bool>(m_fibEntry) &&
195 m_pitEntries.empty() &&
196 !static_cast<bool>(m_measurementsEntry);
197}
198
HYuana9b85752014-02-26 02:32:30 -0600199inline shared_ptr<fib::Entry>
200Entry::getFibEntry() const
201{
202 return m_fibEntry;
203}
204
Haowei Yuanf52dac72014-03-24 23:35:03 -0500205inline void
206Entry::setFibEntry(shared_ptr<fib::Entry> fibEntry)
207{
208 if (static_cast<bool>(fibEntry)) {
209 BOOST_ASSERT(!static_cast<bool>(fibEntry->m_nameTreeEntry));
210 }
211
212 if (static_cast<bool>(m_fibEntry)) {
213 m_fibEntry->m_nameTreeEntry.reset();
214 }
215 m_fibEntry = fibEntry;
216 if (static_cast<bool>(m_fibEntry)) {
217 m_fibEntry->m_nameTreeEntry = this->shared_from_this();
218 }
219}
220
Haowei Yuane1079fc2014-03-08 14:41:25 -0600221inline bool
222Entry::hasPitEntries() const
223{
224 return !m_pitEntries.empty();
225}
226
Junxiao Shie349ea12014-03-12 01:32:42 -0700227inline const std::vector<shared_ptr<pit::Entry> >&
228Entry::getPitEntries() const
229{
230 return m_pitEntries;
231}
232
Haowei Yuanf52dac72014-03-24 23:35:03 -0500233inline void
234Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry)
235{
236 BOOST_ASSERT(static_cast<bool>(pitEntry));
237 BOOST_ASSERT(!static_cast<bool>(pitEntry->m_nameTreeEntry));
238
239 m_pitEntries.push_back(pitEntry);
240 pitEntry->m_nameTreeEntry = this->shared_from_this();
241}
242
HYuana9b85752014-02-26 02:32:30 -0600243inline shared_ptr<measurements::Entry>
244Entry::getMeasurementsEntry() const
245{
246 return m_measurementsEntry;
247}
248
Haowei Yuanf52dac72014-03-24 23:35:03 -0500249inline void
250Entry::setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry)
251{
252 if (static_cast<bool>(measurementsEntry)) {
253 BOOST_ASSERT(!static_cast<bool>(measurementsEntry->m_nameTreeEntry));
254 }
255
256 if (static_cast<bool>(m_measurementsEntry)) {
257 m_measurementsEntry->m_nameTreeEntry.reset();
258 }
259 m_measurementsEntry = measurementsEntry;
260 if (static_cast<bool>(m_measurementsEntry)) {
261 m_measurementsEntry->m_nameTreeEntry = this->shared_from_this();
262 }
263}
264
Junxiao Shibb5105f2014-03-03 12:06:45 -0700265inline shared_ptr<strategy_choice::Entry>
266Entry::getStrategyChoiceEntry() const
267{
268 return m_strategyChoiceEntry;
269}
270
Haowei Yuanf52dac72014-03-24 23:35:03 -0500271inline void
272Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
273{
274 if (static_cast<bool>(strategyChoiceEntry)) {
275 BOOST_ASSERT(!static_cast<bool>(strategyChoiceEntry->m_nameTreeEntry));
276 }
277
278 if (static_cast<bool>(m_strategyChoiceEntry)) {
279 m_strategyChoiceEntry->m_nameTreeEntry.reset();
280 }
281 m_strategyChoiceEntry = strategyChoiceEntry;
282 if (static_cast<bool>(m_strategyChoiceEntry)) {
283 m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this();
284 }
285}
286
HYuana9b85752014-02-26 02:32:30 -0600287} // namespace name_tree
288} // namespace nfd
289
290#endif // NFD_TABLE_NAME_TREE_ENTRY_HPP