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 | /* |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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" |
| 23 | #include "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 | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 27 | #include "name-prefix-table.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 28 | #include "logger.hpp" |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 29 | #include "tlv-nlsr.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 31 | #include <list> |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 32 | #include <string> |
Nick Gordon | 22b5c95 | 2017-08-10 17:48:15 -0500 | [diff] [blame] | 33 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | namespace nlsr { |
| 35 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 36 | INIT_LOGGER(route.RoutingTable); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 37 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 38 | RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Fib& fib, Lsdb& lsdb, |
| 39 | NamePrefixTable& namePrefixTable, ConfParameter& confParam) |
Davide Pesavento | a08dc3f | 2018-05-24 00:40:28 -0400 | [diff] [blame] | 40 | : afterRoutingChange{std::make_unique<AfterRoutingChange>()} |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 41 | , m_scheduler(scheduler) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 42 | , m_fib(fib) |
| 43 | , m_lsdb(lsdb) |
| 44 | , m_namePrefixTable(namePrefixTable) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 45 | , m_routingCalcInterval{confParam.getRoutingCalcInterval()} |
| 46 | , m_isRoutingTableCalculating(false) |
| 47 | , m_isRouteCalculationScheduled(false) |
| 48 | , m_confParam(confParam) |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 53 | RoutingTable::calculate() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 55 | m_lsdb.writeLog(); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 56 | m_namePrefixTable.writeLog(); |
| 57 | if (m_isRoutingTableCalculating == false) { |
| 58 | // setting routing table calculation |
| 59 | m_isRoutingTableCalculating = true; |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 60 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 61 | bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF; |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 62 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 63 | if ((!isHrEnabled && |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 64 | m_lsdb.doesLsaExist(m_confParam.getRouterPrefix(), Lsa::Type::ADJACENCY)) |
Nick Gordon | e8e03ac | 2016-07-07 14:24:38 -0500 | [diff] [blame] | 65 | || |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 66 | (isHrEnabled && |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 67 | m_lsdb.doesLsaExist(m_confParam.getRouterPrefix(), Lsa::Type::COORDINATE))) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 68 | if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 69 | NLSR_LOG_TRACE("Clearing old routing table"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | clearRoutingTable(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 71 | // for dry run options |
| 72 | clearDryRoutingTable(); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 73 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 74 | NLSR_LOG_DEBUG("Calculating routing table"); |
Vince Lehman | 50df6b7 | 2015-03-03 12:06:40 -0600 | [diff] [blame] | 75 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | // calculate Link State routing |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 77 | if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) || |
| 78 | (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 79 | calculateLsRoutingTable(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | } |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 81 | // calculate hyperbolic routing |
| 82 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
| 83 | calculateHypRoutingTable(false); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 84 | } |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 85 | // calculate dry hyperbolic routing |
| 86 | if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) { |
| 87 | calculateHypRoutingTable(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 89 | // Inform the NPT that updates have been made |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 90 | NLSR_LOG_DEBUG("Calling Update NPT With new Route"); |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 91 | (*afterRoutingChange)(m_rTable); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 92 | NLSR_LOG_DEBUG(*this); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 93 | m_namePrefixTable.writeLog(); |
| 94 | m_fib.writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 96 | else { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 97 | NLSR_LOG_DEBUG("Adjacency building is scheduled, so routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | else { |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 101 | NLSR_LOG_DEBUG("No Adj LSA of router itself, so Routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | clearRoutingTable(); |
| 103 | clearDryRoutingTable(); // for dry run options |
| 104 | // need to update NPT here |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 105 | NLSR_LOG_DEBUG("Calling Update NPT With new Route"); |
Nick Gordon | b7b5839 | 2017-08-17 16:29:21 -0500 | [diff] [blame] | 106 | (*afterRoutingChange)(m_rTable); |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 107 | NLSR_LOG_DEBUG(*this); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 108 | m_namePrefixTable.writeLog(); |
| 109 | m_fib.writeLog(); |
| 110 | // debugging purpose end |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 112 | m_isRouteCalculationScheduled = false; // clear scheduled flag |
| 113 | m_isRoutingTableCalculating = false; // unsetting routing table calculation |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | else { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 116 | scheduleRoutingTableCalculation(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 120 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 121 | RoutingTable::calculateLsRoutingTable() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 122 | { |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 123 | NLSR_LOG_TRACE("CalculateLsRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 124 | |
| 125 | Map map; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 126 | auto lsaRange = m_lsdb.getLsdbIterator<AdjLsa>(); |
| 127 | map.createFromAdjLsdb(lsaRange.first, lsaRange.second); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 128 | map.writeLog(); |
| 129 | |
| 130 | size_t nRouters = map.getMapSize(); |
| 131 | |
| 132 | LinkStateRoutingTableCalculator calculator(nRouters); |
| 133 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 134 | calculator.calculatePath(map, *this, m_confParam, m_lsdb); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 138 | RoutingTable::calculateHypRoutingTable(bool isDryRun) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 140 | Map map; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 141 | auto lsaRange = m_lsdb.getLsdbIterator<CoordinateLsa>(); |
| 142 | map.createFromCoordinateLsdb(lsaRange.first, lsaRange.second); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 143 | map.writeLog(); |
| 144 | |
| 145 | size_t nRouters = map.getMapSize(); |
| 146 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 147 | HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix()); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 148 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 149 | calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 153 | RoutingTable::scheduleRoutingTableCalculation() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 155 | if (!m_isRouteCalculationScheduled) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 156 | NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval); |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 157 | m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); }); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 158 | m_isRouteCalculationScheduled = true; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 163 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | { |
| 165 | return rte.getDestination() == destRouter; |
| 166 | } |
| 167 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 169 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 171 | NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 172 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 173 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 174 | if (rteChk == nullptr) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 176 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | m_rTable.push_back(rte); |
| 178 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 179 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 180 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 184 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 185 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 187 | auto it = std::find_if(m_rTable.begin(), m_rTable.end(), |
| 188 | std::bind(&routingTableEntryCompare, _1, destRouter)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 189 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 190 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | } |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 192 | return nullptr; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 196 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 197 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 198 | NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 199 | |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 200 | auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(), |
| 201 | std::bind(&routingTableEntryCompare, _1, destRouter)); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 202 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 203 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 204 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 205 | m_dryTable.push_back(rte); |
| 206 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 207 | else { |
Davide Pesavento | af7a211 | 2019-03-19 14:55:20 -0400 | [diff] [blame] | 208 | it->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | RoutingTable::clearRoutingTable() |
| 214 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 215 | if (m_rTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | m_rTable.clear(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void |
| 221 | RoutingTable::clearDryRoutingTable() |
| 222 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | if (m_dryTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | m_dryTable.clear(); |
| 225 | } |
| 226 | } |
| 227 | |
Ashlesh Gawande | 0421bc6 | 2020-05-08 20:42:19 -0700 | [diff] [blame] | 228 | template<ndn::encoding::Tag TAG> |
| 229 | size_t |
| 230 | RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 231 | { |
| 232 | size_t totalLength = 0; |
| 233 | |
| 234 | for (auto it = m_dryTable.rbegin(); it != m_dryTable.rend(); ++it) { |
| 235 | totalLength += it->wireEncode(block); |
| 236 | } |
| 237 | |
| 238 | for (auto it = m_rTable.rbegin(); it != m_rTable.rend(); ++it) { |
| 239 | totalLength += it->wireEncode(block); |
| 240 | } |
| 241 | |
| 242 | totalLength += block.prependVarNumber(totalLength); |
| 243 | totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable); |
| 244 | |
| 245 | return totalLength; |
| 246 | } |
| 247 | |
| 248 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus); |
| 249 | |
| 250 | const ndn::Block& |
| 251 | RoutingTableStatus::wireEncode() const |
| 252 | { |
| 253 | if (m_wire.hasWire()) { |
| 254 | return m_wire; |
| 255 | } |
| 256 | |
| 257 | ndn::EncodingEstimator estimator; |
| 258 | size_t estimatedSize = wireEncode(estimator); |
| 259 | |
| 260 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 261 | wireEncode(buffer); |
| 262 | |
| 263 | m_wire = buffer.block(); |
| 264 | |
| 265 | return m_wire; |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | RoutingTableStatus::wireDecode(const ndn::Block& wire) |
| 270 | { |
| 271 | m_rTable.clear(); |
| 272 | |
| 273 | m_wire = wire; |
| 274 | |
| 275 | if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) { |
| 276 | std::stringstream error; |
| 277 | error << "Expected RoutingTableStatus Block, but Block is of a different type: #" |
| 278 | << m_wire.type(); |
| 279 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 280 | } |
| 281 | |
| 282 | m_wire.parse(); |
| 283 | |
| 284 | auto val = m_wire.elements_begin(); |
| 285 | |
| 286 | std::set<ndn::Name> destinations; |
| 287 | for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RoutingTableEntry; ++val) { |
| 288 | auto entry = RoutingTableEntry(*val); |
| 289 | |
| 290 | if (destinations.emplace(entry.getDestination()).second) { |
| 291 | m_rTable.push_back(entry); |
| 292 | } |
| 293 | else { |
| 294 | // If destination already exists then this is the start of dry HR table |
| 295 | m_dryTable.push_back(entry); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (val != m_wire.elements_end()) { |
| 300 | std::stringstream error; |
| 301 | error << "Expected the end of elements, but Block is of a different type: #" |
| 302 | << val->type(); |
| 303 | BOOST_THROW_EXCEPTION(Error(error.str())); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | std::ostream& |
| 308 | operator<<(std::ostream& os, const RoutingTableStatus& rts) |
| 309 | { |
| 310 | os << "Routing Table:\n"; |
| 311 | for (const auto& rte : rts.getRoutingTableEntry()) { |
| 312 | os << rte; |
| 313 | } |
| 314 | |
| 315 | if (!rts.getDryRoutingTableEntry().empty()) { |
| 316 | os << "Dry-Run Hyperbolic Routing Table:\n"; |
| 317 | for (const auto& rte : rts.getDryRoutingTableEntry()) { |
| 318 | os << rte; |
| 319 | } |
| 320 | } |
| 321 | return os; |
| 322 | } |
| 323 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 324 | } // namespace nlsr |