HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
| 26 | #include "name-tree-entry.hpp" |
| 27 | |
| 28 | namespace nfd { |
| 29 | namespace name_tree { |
| 30 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 31 | Node::Node() |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 32 | : m_prev(nullptr) |
| 33 | , m_next(nullptr) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | Node::~Node() |
| 38 | { |
| 39 | // erase the Name Tree Nodes that were created to |
| 40 | // resolve hash collisions |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 41 | // So before erasing a single node, make sure its m_next == nullptr |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 42 | // See eraseEntryIfEmpty in name-tree.cpp |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame] | 43 | if (m_next != nullptr) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 44 | delete m_next; |
| 45 | } |
| 46 | |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 47 | Entry::Entry(const Name& name) |
| 48 | : m_hash(0) |
| 49 | , m_prefix(name) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 53 | bool |
| 54 | Entry::isEmpty() const |
| 55 | { |
| 56 | return m_children.empty() && |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 57 | m_fibEntry == nullptr && |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 58 | m_pitEntries.empty() && |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 59 | m_measurementsEntry == nullptr && |
| 60 | m_strategyChoiceEntry == nullptr; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 64 | Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 65 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 66 | BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry.expired()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 67 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 68 | if (m_fibEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 69 | m_fibEntry->m_nameTreeEntry.reset(); |
| 70 | } |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 71 | m_fibEntry = std::move(fibEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 72 | |
| 73 | if (m_fibEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 74 | m_fibEntry->m_nameTreeEntry = this->shared_from_this(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry) |
| 80 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 81 | BOOST_ASSERT(pitEntry != nullptr); |
| 82 | BOOST_ASSERT(pitEntry->m_nameTreeEntry.expired()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 83 | |
| 84 | m_pitEntries.push_back(pitEntry); |
| 85 | pitEntry->m_nameTreeEntry = this->shared_from_this(); |
| 86 | } |
| 87 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 88 | void |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 89 | Entry::erasePitEntry(shared_ptr<pit::Entry> pitEntry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 90 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 91 | BOOST_ASSERT(pitEntry != nullptr); |
| 92 | BOOST_ASSERT(pitEntry->m_nameTreeEntry.lock().get() == this); |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 94 | auto it = std::find(m_pitEntries.begin(), m_pitEntries.end(), pitEntry); |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 95 | BOOST_ASSERT(it != m_pitEntries.end()); |
| 96 | |
| 97 | *it = m_pitEntries.back(); |
| 98 | m_pitEntries.pop_back(); |
| 99 | pitEntry->m_nameTreeEntry.reset(); |
| 100 | } |
| 101 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 102 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 103 | Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 104 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 105 | BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry.expired()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 106 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 107 | if (m_measurementsEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 108 | m_measurementsEntry->m_nameTreeEntry.reset(); |
| 109 | } |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 110 | m_measurementsEntry = std::move(measurementsEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 111 | |
| 112 | if (m_measurementsEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 113 | m_measurementsEntry->m_nameTreeEntry = this->shared_from_this(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 118 | Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 119 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 120 | BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry.expired()); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 122 | if (m_strategyChoiceEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 123 | m_strategyChoiceEntry->m_nameTreeEntry.reset(); |
| 124 | } |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 125 | m_strategyChoiceEntry = std::move(strategyChoiceEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 126 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 127 | if (m_strategyChoiceEntry != nullptr) { |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 128 | m_strategyChoiceEntry->m_nameTreeEntry = this->shared_from_this(); |
| 129 | } |
| 130 | } |
| 131 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 132 | } // namespace name_tree |
| 133 | } // namespace nfd |