akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | d6922b5 | 2024-01-14 19:50:34 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 19 | */ |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 20 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 21 | #include "routing-table.hpp" |
| 22 | #include "nlsr.hpp" |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 23 | #include "name-map.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 24 | #include "conf-parameter.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | #include "routing-table-calculator.hpp" |
| 26 | #include "routing-table-entry.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 28 | #include "tlv-nlsr.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
| 31 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 32 | INIT_LOGGER(route.RoutingTable); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 33 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 34 | RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Lsdb& lsdb, ConfParameter& confParam) |
| 35 | : m_scheduler(scheduler) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 36 | , m_lsdb(lsdb) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 37 | , m_routingCalcInterval{confParam.getRoutingCalcInterval()} |
| 38 | , m_isRoutingTableCalculating(false) |
| 39 | , m_isRouteCalculationScheduled(false) |
| 40 | , m_confParam(confParam) |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 41 | , m_hyperbolicState(m_confParam.getHyperbolicState()) |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 42 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 43 | m_afterLsdbModified = lsdb.onLsdbModified.connect( |
| 44 | [this] (std::shared_ptr<Lsa> lsa, LsdbUpdate updateType, |
| 45 | const auto& namesToAdd, const auto& namesToRemove) { |
| 46 | auto type = lsa->getType(); |
| 47 | bool updateForOwnAdjacencyLsa = lsa->getOriginRouter() == m_confParam.getRouterPrefix() && |
| 48 | type == Lsa::Type::ADJACENCY; |
| 49 | bool scheduleCalculation = false; |
| 50 | |
| 51 | if (updateType == LsdbUpdate::REMOVED && updateForOwnAdjacencyLsa) { |
| 52 | // If own Adjacency LSA is removed then we have no ACTIVE neighbors. |
| 53 | // (Own Coordinate LSA is never removed. But routing table calculation is scheduled |
| 54 | // in HelloProtocol. The routing table calculator for HR takes into account |
| 55 | // the INACTIVE status of the link). |
| 56 | NLSR_LOG_DEBUG("No Adj LSA of router itself, routing table can not be calculated :("); |
| 57 | clearRoutingTable(); |
| 58 | clearDryRoutingTable(); |
| 59 | NLSR_LOG_DEBUG("Calling Update NPT With new Route"); |
| 60 | afterRoutingChange(m_rTable); |
| 61 | NLSR_LOG_DEBUG(*this); |
| 62 | m_ownAdjLsaExist = false; |
| 63 | } |
| 64 | |
| 65 | if (updateType == LsdbUpdate::INSTALLED && updateForOwnAdjacencyLsa) { |
| 66 | m_ownAdjLsaExist = true; |
| 67 | } |
| 68 | |
| 69 | // Don;t do anything on removal, wait for HelloProtocol to confirm and then react |
| 70 | if (updateType == LsdbUpdate::INSTALLED || updateType == LsdbUpdate::UPDATED) { |
| 71 | if ((type == Lsa::Type::ADJACENCY && m_hyperbolicState != HYPERBOLIC_STATE_ON) || |
| 72 | (type == Lsa::Type::COORDINATE && m_hyperbolicState != HYPERBOLIC_STATE_OFF)) { |
| 73 | scheduleCalculation = true; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (scheduleCalculation) { |
| 78 | scheduleRoutingTableCalculation(); |
| 79 | } |
| 80 | } |
| 81 | ); |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 82 | } |
| 83 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 85 | RoutingTable::calculate() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 87 | m_lsdb.writeLog(); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 88 | NLSR_LOG_TRACE("Calculating routing table"); |
| 89 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 90 | if (m_isRoutingTableCalculating == false) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 91 | m_isRoutingTableCalculating = true; |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 92 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 93 | if (m_hyperbolicState == HYPERBOLIC_STATE_OFF) { |
| 94 | calculateLsRoutingTable(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | } |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 96 | else if (m_hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 97 | calculateLsRoutingTable(); |
| 98 | calculateHypRoutingTable(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | } |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 100 | else if (m_hyperbolicState == HYPERBOLIC_STATE_ON) { |
| 101 | calculateHypRoutingTable(false); |
| 102 | } |
| 103 | |
| 104 | m_isRouteCalculationScheduled = false; |
| 105 | m_isRoutingTableCalculating = false; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 106 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 107 | else { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 108 | scheduleRoutingTableCalculation(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 112 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 113 | RoutingTable::calculateLsRoutingTable() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 115 | NLSR_LOG_TRACE("CalculateLsRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 116 | |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 117 | if (m_lsdb.getIsBuildAdjLsaScheduled()) { |
| 118 | NLSR_LOG_DEBUG("Adjacency build is scheduled, routing table can not be calculated :("); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // We only check this in LS since we never remove our own Coordinate LSA, |
| 123 | // whereas we remove our own Adjacency LSA if we don't have any neighbors |
| 124 | if (!m_ownAdjLsaExist) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | clearRoutingTable(); |
| 129 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 130 | auto lsaRange = m_lsdb.getLsdbIterator<AdjLsa>(); |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 131 | auto map = NameMap::createFromAdjLsdb(lsaRange.first, lsaRange.second); |
Junxiao Shi | d6922b5 | 2024-01-14 19:50:34 +0000 | [diff] [blame] | 132 | NLSR_LOG_DEBUG(map); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 133 | |
Junxiao Shi | d6922b5 | 2024-01-14 19:50:34 +0000 | [diff] [blame] | 134 | size_t nRouters = map.size(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 135 | |
| 136 | LinkStateRoutingTableCalculator calculator(nRouters); |
| 137 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 138 | calculator.calculatePath(map, *this, m_confParam, m_lsdb); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 139 | |
| 140 | NLSR_LOG_DEBUG("Calling Update NPT With new Route"); |
| 141 | afterRoutingChange(m_rTable); |
| 142 | NLSR_LOG_DEBUG(*this); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 146 | RoutingTable::calculateHypRoutingTable(bool isDryRun) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 148 | if (isDryRun) { |
| 149 | clearDryRoutingTable(); |
| 150 | } |
| 151 | else { |
| 152 | clearRoutingTable(); |
| 153 | } |
| 154 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 155 | auto lsaRange = m_lsdb.getLsdbIterator<CoordinateLsa>(); |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 156 | auto map = NameMap::createFromCoordinateLsdb(lsaRange.first, lsaRange.second); |
Junxiao Shi | d6922b5 | 2024-01-14 19:50:34 +0000 | [diff] [blame] | 157 | NLSR_LOG_DEBUG(map); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 158 | |
Junxiao Shi | d6922b5 | 2024-01-14 19:50:34 +0000 | [diff] [blame] | 159 | size_t nRouters = map.size(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 160 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 161 | HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix()); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 162 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 163 | calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList()); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 164 | |
| 165 | if (!isDryRun) { |
| 166 | NLSR_LOG_DEBUG("Calling Update NPT With new Route"); |
| 167 | afterRoutingChange(m_rTable); |
| 168 | NLSR_LOG_DEBUG(*this); |
| 169 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 173 | RoutingTable::scheduleRoutingTableCalculation() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 175 | if (!m_isRouteCalculationScheduled) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 176 | NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval); |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 177 | m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); }); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 178 | m_isRouteCalculationScheduled = true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 183 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | { |
| 185 | return rte.getDestination() == destRouter; |
| 186 | } |
| 187 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 189 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 191 | NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 192 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 193 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 194 | if (rteChk == nullptr) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 196 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 197 | m_rTable.push_back(rte); |
| 198 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 199 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 200 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 201 | } |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 202 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 203 | } |
| 204 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 205 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 206 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 207 | { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 208 | auto it = std::find_if(m_rTable.begin(), m_rTable.end(), |
| 209 | std::bind(&routingTableEntryCompare, _1, destRouter)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 210 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 211 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 212 | } |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 213 | return nullptr; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 217 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 219 | NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 220 | |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 221 | auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(), |
| 222 | std::bind(&routingTableEntryCompare, _1, destRouter)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 225 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 226 | m_dryTable.push_back(rte); |
| 227 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 228 | else { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 229 | it->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | } |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 231 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | RoutingTable::clearRoutingTable() |
| 236 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 237 | m_rTable.clear(); |
| 238 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void |
| 242 | RoutingTable::clearDryRoutingTable() |
| 243 | { |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 244 | m_dryTable.clear(); |
| 245 | m_wire.reset(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 248 | template<ndn::encoding::Tag TAG> |
| 249 | size_t |
| 250 | RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 251 | { |
| 252 | size_t totalLength = 0; |
| 253 | |
| 254 | for (auto it = m_dryTable.rbegin(); it != m_dryTable.rend(); ++it) { |
| 255 | totalLength += it->wireEncode(block); |
| 256 | } |
| 257 | |
| 258 | for (auto it = m_rTable.rbegin(); it != m_rTable.rend(); ++it) { |
| 259 | totalLength += it->wireEncode(block); |
| 260 | } |
| 261 | |
| 262 | totalLength += block.prependVarNumber(totalLength); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 263 | totalLength += block.prependVarNumber(nlsr::tlv::RoutingTable); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 264 | |
| 265 | return totalLength; |
| 266 | } |
| 267 | |
| 268 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus); |
| 269 | |
| 270 | const ndn::Block& |
| 271 | RoutingTableStatus::wireEncode() const |
| 272 | { |
| 273 | if (m_wire.hasWire()) { |
| 274 | return m_wire; |
| 275 | } |
| 276 | |
| 277 | ndn::EncodingEstimator estimator; |
| 278 | size_t estimatedSize = wireEncode(estimator); |
| 279 | |
| 280 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 281 | wireEncode(buffer); |
| 282 | |
| 283 | m_wire = buffer.block(); |
| 284 | |
| 285 | return m_wire; |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | RoutingTableStatus::wireDecode(const ndn::Block& wire) |
| 290 | { |
| 291 | m_rTable.clear(); |
| 292 | |
| 293 | m_wire = wire; |
| 294 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 295 | if (m_wire.type() != nlsr::tlv::RoutingTable) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 296 | NDN_THROW(Error("RoutingTable", m_wire.type())); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | m_wire.parse(); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 300 | auto val = m_wire.elements_begin(); |
| 301 | |
| 302 | std::set<ndn::Name> destinations; |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 303 | for (; val != m_wire.elements_end() && val->type() == nlsr::tlv::RoutingTableEntry; ++val) { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 304 | auto entry = RoutingTableEntry(*val); |
| 305 | |
| 306 | if (destinations.emplace(entry.getDestination()).second) { |
| 307 | m_rTable.push_back(entry); |
| 308 | } |
| 309 | else { |
| 310 | // If destination already exists then this is the start of dry HR table |
| 311 | m_dryTable.push_back(entry); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (val != m_wire.elements_end()) { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 316 | NDN_THROW(Error("Unrecognized TLV of type " + ndn::to_string(val->type()) + " in RoutingTable")); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | std::ostream& |
| 321 | operator<<(std::ostream& os, const RoutingTableStatus& rts) |
| 322 | { |
| 323 | os << "Routing Table:\n"; |
| 324 | for (const auto& rte : rts.getRoutingTableEntry()) { |
| 325 | os << rte; |
| 326 | } |
| 327 | |
| 328 | if (!rts.getDryRoutingTableEntry().empty()) { |
| 329 | os << "Dry-Run Hyperbolic Routing Table:\n"; |
| 330 | for (const auto& rte : rts.getDryRoutingTableEntry()) { |
| 331 | os << rte; |
| 332 | } |
| 333 | } |
| 334 | return os; |
| 335 | } |
| 336 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 337 | } // namespace nlsr |