HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 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" |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame^] | 27 | #include "name-tree.hpp" |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
| 30 | namespace name_tree { |
| 31 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 32 | Entry::Entry(const Name& name, Node* node) |
| 33 | : m_name(name) |
| 34 | , m_node(node) |
| 35 | , m_parent(nullptr) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 36 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 37 | BOOST_ASSERT(node != nullptr); |
Junxiao Shi | 057d149 | 2018-03-20 17:14:18 +0000 | [diff] [blame^] | 38 | BOOST_ASSERT(name.size() <= NameTree::getMaxDepth()); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 39 | } |
| 40 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 41 | void |
| 42 | Entry::setParent(Entry& entry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 43 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 44 | BOOST_ASSERT(this->getParent() == nullptr); |
| 45 | BOOST_ASSERT(!this->getName().empty()); |
| 46 | BOOST_ASSERT(entry.getName() == this->getName().getPrefix(-1)); |
| 47 | |
| 48 | m_parent = &entry; |
| 49 | |
| 50 | m_parent->m_children.push_back(this); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 51 | } |
| 52 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 53 | void |
| 54 | Entry::unsetParent() |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 55 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 56 | BOOST_ASSERT(this->getParent() != nullptr); |
| 57 | |
| 58 | auto i = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this); |
| 59 | BOOST_ASSERT(i != m_parent->m_children.end()); |
| 60 | m_parent->m_children.erase(i); |
| 61 | |
| 62 | m_parent = nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 65 | bool |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 66 | Entry::hasTableEntries() const |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 67 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 68 | return m_fibEntry != nullptr || |
| 69 | !m_pitEntries.empty() || |
| 70 | m_measurementsEntry != nullptr || |
| 71 | m_strategyChoiceEntry != nullptr; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 75 | Entry::setFibEntry(unique_ptr<fib::Entry> fibEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 76 | { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 77 | BOOST_ASSERT(fibEntry == nullptr || fibEntry->m_nameTreeEntry == nullptr); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 79 | if (m_fibEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 80 | m_fibEntry->m_nameTreeEntry = nullptr; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 81 | } |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 82 | m_fibEntry = std::move(fibEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 83 | |
| 84 | if (m_fibEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 85 | m_fibEntry->m_nameTreeEntry = this; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Entry::insertPitEntry(shared_ptr<pit::Entry> pitEntry) |
| 91 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 92 | BOOST_ASSERT(pitEntry != nullptr); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 93 | BOOST_ASSERT(pitEntry->m_nameTreeEntry == nullptr); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 94 | |
| 95 | m_pitEntries.push_back(pitEntry); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 96 | pitEntry->m_nameTreeEntry = this; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 97 | } |
| 98 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 99 | void |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 100 | Entry::erasePitEntry(pit::Entry* pitEntry) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 101 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 102 | BOOST_ASSERT(pitEntry != nullptr); |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 103 | BOOST_ASSERT(pitEntry->m_nameTreeEntry == this); |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 104 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 105 | auto it = std::find_if(m_pitEntries.begin(), m_pitEntries.end(), |
| 106 | [pitEntry] (const shared_ptr<pit::Entry>& pitEntry2) { return pitEntry2.get() == pitEntry; }); |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 107 | BOOST_ASSERT(it != m_pitEntries.end()); |
| 108 | |
Junxiao Shi | dbef6dc | 2016-08-15 02:58:36 +0000 | [diff] [blame] | 109 | pitEntry->m_nameTreeEntry = nullptr; // must be done before pitEntry is deallocated |
| 110 | *it = m_pitEntries.back(); // may deallocate pitEntry |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 111 | m_pitEntries.pop_back(); |
Junxiao Shi | 9f7455b | 2014-04-07 21:02:16 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 114 | void |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 115 | Entry::setMeasurementsEntry(unique_ptr<measurements::Entry> measurementsEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 116 | { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 117 | BOOST_ASSERT(measurementsEntry == nullptr || measurementsEntry->m_nameTreeEntry == nullptr); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 119 | if (m_measurementsEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 120 | m_measurementsEntry->m_nameTreeEntry = nullptr; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 121 | } |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 122 | m_measurementsEntry = std::move(measurementsEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 123 | |
| 124 | if (m_measurementsEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 125 | m_measurementsEntry->m_nameTreeEntry = this; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | void |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 130 | Entry::setStrategyChoiceEntry(unique_ptr<strategy_choice::Entry> strategyChoiceEntry) |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 131 | { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 132 | BOOST_ASSERT(strategyChoiceEntry == nullptr || strategyChoiceEntry->m_nameTreeEntry == nullptr); |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 133 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 134 | if (m_strategyChoiceEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 135 | m_strategyChoiceEntry->m_nameTreeEntry = nullptr; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 136 | } |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 137 | m_strategyChoiceEntry = std::move(strategyChoiceEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 138 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 139 | if (m_strategyChoiceEntry != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 140 | m_strategyChoiceEntry->m_nameTreeEntry = this; |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 144 | } // namespace name_tree |
| 145 | } // namespace nfd |