HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * 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 | **/ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 24 | |
| 25 | // Name Tree Entry (i.e., Name Prefix Entry) |
| 26 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 27 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |
| 28 | #define NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 29 | |
| 30 | #include "common.hpp" |
| 31 | #include "table/fib-entry.hpp" |
| 32 | #include "table/pit-entry.hpp" |
| 33 | #include "table/measurements-entry.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 34 | #include "table/strategy-choice-entry.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | |
| 38 | class NameTree; |
| 39 | |
| 40 | namespace name_tree { |
| 41 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 42 | // Forward declarations |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 43 | class Node; |
| 44 | class Entry; |
| 45 | |
| 46 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 47 | * \brief Name Tree Node Class |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 48 | */ |
| 49 | class Node |
| 50 | { |
| 51 | public: |
| 52 | Node(); |
| 53 | |
| 54 | ~Node(); |
| 55 | |
| 56 | public: |
| 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 64 | * \brief Name Tree Entry Class |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 65 | */ |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 66 | class Entry : public enable_shared_from_this<Entry>, noncopyable |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 67 | { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 68 | public: |
| 69 | explicit |
| 70 | Entry(const Name& prefix); |
| 71 | |
| 72 | ~Entry(); |
| 73 | |
| 74 | const Name& |
| 75 | getPrefix() const; |
| 76 | |
| 77 | void |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 78 | setHash(size_t hash); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 79 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 80 | size_t |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 81 | 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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 91 | |
| 92 | bool |
| 93 | hasChildren() const; |
| 94 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 95 | bool |
| 96 | isEmpty() const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 97 | |
| 98 | void |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 99 | setFibEntry(shared_ptr<fib::Entry> fibEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 100 | |
| 101 | shared_ptr<fib::Entry> |
| 102 | getFibEntry() const; |
| 103 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 104 | void |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 105 | insertPitEntry(shared_ptr<pit::Entry> pitEntry); |
| 106 | |
| 107 | void |
| 108 | erasePitEntry(shared_ptr<pit::Entry> pitEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 109 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 110 | bool |
| 111 | hasPitEntries() const; |
| 112 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 113 | const std::vector<shared_ptr<pit::Entry> >& |
| 114 | getPitEntries() const; |
| 115 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 116 | void |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 117 | setMeasurementsEntry(shared_ptr<measurements::Entry> measurementsEntry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 118 | |
| 119 | shared_ptr<measurements::Entry> |
| 120 | getMeasurementsEntry() const; |
| 121 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 122 | void |
| 123 | setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry); |
| 124 | |
| 125 | shared_ptr<strategy_choice::Entry> |
| 126 | getStrategyChoiceEntry() const; |
| 127 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 128 | private: |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 129 | // 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; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 133 | 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 Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 139 | shared_ptr<strategy_choice::Entry> m_strategyChoiceEntry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 140 | |
| 141 | // get the Name Tree Node that is associated with this Name Tree Entry |
| 142 | Node* m_node; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 143 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 144 | // Make private members accessible by Name Tree |
| 145 | friend class nfd::NameTree; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | inline const Name& |
| 149 | Entry::getPrefix() const |
| 150 | { |
| 151 | return m_prefix; |
| 152 | } |
| 153 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 154 | inline size_t |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 155 | Entry::getHash() const |
| 156 | { |
| 157 | return m_hash; |
| 158 | } |
| 159 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 160 | inline void |
| 161 | Entry::setHash(size_t hash) |
| 162 | { |
| 163 | m_hash = hash; |
| 164 | } |
| 165 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 166 | inline shared_ptr<Entry> |
| 167 | Entry::getParent() const |
| 168 | { |
| 169 | return m_parent; |
| 170 | } |
| 171 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 172 | inline void |
| 173 | Entry::setParent(shared_ptr<Entry> parent) |
| 174 | { |
| 175 | m_parent = parent; |
| 176 | } |
| 177 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 178 | inline std::vector<shared_ptr<name_tree::Entry> >& |
| 179 | Entry::getChildren() |
| 180 | { |
| 181 | return m_children; |
| 182 | } |
| 183 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 184 | inline bool |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 185 | Entry::hasChildren() const |
| 186 | { |
| 187 | return !m_children.empty(); |
| 188 | } |
| 189 | |
| 190 | inline bool |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 191 | Entry::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 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 199 | inline shared_ptr<fib::Entry> |
| 200 | Entry::getFibEntry() const |
| 201 | { |
| 202 | return m_fibEntry; |
| 203 | } |
| 204 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 205 | inline void |
| 206 | Entry::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 Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 221 | inline bool |
| 222 | Entry::hasPitEntries() const |
| 223 | { |
| 224 | return !m_pitEntries.empty(); |
| 225 | } |
| 226 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 227 | inline const std::vector<shared_ptr<pit::Entry> >& |
| 228 | Entry::getPitEntries() const |
| 229 | { |
| 230 | return m_pitEntries; |
| 231 | } |
| 232 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 233 | inline void |
| 234 | Entry::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 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 243 | inline shared_ptr<measurements::Entry> |
| 244 | Entry::getMeasurementsEntry() const |
| 245 | { |
| 246 | return m_measurementsEntry; |
| 247 | } |
| 248 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 249 | inline void |
| 250 | Entry::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 Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 265 | inline shared_ptr<strategy_choice::Entry> |
| 266 | Entry::getStrategyChoiceEntry() const |
| 267 | { |
| 268 | return m_strategyChoiceEntry; |
| 269 | } |
| 270 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 271 | inline void |
| 272 | Entry::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 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 287 | } // namespace name_tree |
| 288 | } // namespace nfd |
| 289 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame^] | 290 | #endif // NFD_DAEMON_TABLE_NAME_TREE_ENTRY_HPP |