akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 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/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | #include <list> |
| 26 | |
| 27 | #include "routing-table.hpp" |
| 28 | #include "nlsr.hpp" |
| 29 | #include "map.hpp" |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 30 | #include "conf-parameter.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | #include "routing-table-calculator.hpp" |
| 32 | #include "routing-table-entry.hpp" |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 33 | #include "name-prefix-table.hpp" |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 34 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | |
| 36 | namespace nlsr { |
| 37 | |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 38 | INIT_LOGGER("RoutingTable"); |
| 39 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 40 | using namespace std; |
| 41 | |
| 42 | void |
| 43 | RoutingTable::calculate(Nlsr& pnlsr) |
| 44 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 45 | pnlsr.getLsdb().writeCorLsdbLog(); |
| 46 | pnlsr.getLsdb().writeNameLsdbLog(); |
| 47 | pnlsr.getLsdb().writeAdjLsdbLog(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 48 | pnlsr.getNamePrefixTable().writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 49 | if (pnlsr.getIsRoutingTableCalculating() == false) { |
| 50 | //setting routing table calculation |
| 51 | pnlsr.setIsRoutingTableCalculating(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | if (pnlsr.getLsdb().doesLsaExist( |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 53 | pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency", |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 54 | std::string("adjacency"))) { |
| 55 | if (pnlsr.getIsBuildAdjLsaSheduled() != 1) { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 56 | _LOG_DEBUG("CLearing old routing table ....."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | clearRoutingTable(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 58 | // for dry run options |
| 59 | clearDryRoutingTable(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | // calculate Link State routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) |
| 62 | || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | calculateLsRoutingTable(pnlsr); |
| 64 | } |
| 65 | //calculate hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | calculateHypRoutingTable(pnlsr); |
| 68 | } |
| 69 | //calculate dry hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 70 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | calculateHypDryRoutingTable(pnlsr); |
| 72 | } |
| 73 | //need to update NPT here |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 74 | _LOG_DEBUG("Calling Update NPT With new Route"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 75 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 76 | writeLog(pnlsr.getConfParameter().getHyperbolicState()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 77 | pnlsr.getNamePrefixTable().writeLog(); |
| 78 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 81 | _LOG_DEBUG("Adjacency building is scheduled, so" |
| 82 | " routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 85 | else { |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 86 | _LOG_DEBUG("No Adj LSA of router itself," |
| 87 | " so Routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | clearRoutingTable(); |
| 89 | clearDryRoutingTable(); // for dry run options |
| 90 | // need to update NPT here |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 91 | _LOG_DEBUG("Calling Update NPT With new Route"); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 92 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 93 | writeLog(pnlsr.getConfParameter().getHyperbolicState()); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 94 | pnlsr.getNamePrefixTable().writeLog(); |
| 95 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 96 | //debugging purpose end |
| 97 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 98 | pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag |
| 99 | pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 101 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 102 | scheduleRoutingTableCalculation(pnlsr); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 108 | RoutingTable::calculateLsRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 109 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 110 | _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 111 | |
| 112 | Map map; |
| 113 | map.createFromAdjLsdb(nlsr); |
| 114 | map.writeLog(); |
| 115 | |
| 116 | size_t nRouters = map.getMapSize(); |
| 117 | |
| 118 | LinkStateRoutingTableCalculator calculator(nRouters); |
| 119 | |
| 120 | calculator.calculatePath(map, ndn::ref(*this), nlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 124 | RoutingTable::calculateHypRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 126 | Map map; |
| 127 | map.createFromAdjLsdb(nlsr); |
| 128 | map.writeLog(); |
| 129 | |
| 130 | size_t nRouters = map.getMapSize(); |
| 131 | |
| 132 | HyperbolicRoutingCalculator calculator(nRouters, false, |
| 133 | nlsr.getConfParameter().getRouterPrefix()); |
| 134 | |
| 135 | calculator.calculatePaths(map, ndn::ref(*this), |
| 136 | nlsr.getLsdb(), nlsr.getAdjacencyList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 140 | RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 142 | Map map; |
| 143 | map.createFromAdjLsdb(nlsr); |
| 144 | map.writeLog(); |
| 145 | |
| 146 | size_t nRouters = map.getMapSize(); |
| 147 | |
| 148 | HyperbolicRoutingCalculator calculator(nRouters, true, |
| 149 | nlsr.getConfParameter().getRouterPrefix()); |
| 150 | |
| 151 | calculator.calculatePaths(map, ndn::ref(*this), |
| 152 | nlsr.getLsdb(), nlsr.getAdjacencyList()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
| 156 | RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr) |
| 157 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 158 | if (pnlsr.getIsRouteCalculationScheduled() != true) { |
Vince Lehman | 7b61658 | 2014-10-17 16:25:39 -0500 | [diff] [blame] | 159 | m_scheduler.scheduleEvent(m_routingCalcInterval, |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 160 | ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr))); |
| 161 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 162 | pnlsr.setIsRouteCalculationScheduled(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 167 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | { |
| 169 | return rte.getDestination() == destRouter; |
| 170 | } |
| 171 | |
| 172 | // function related to manipulation of routing table |
| 173 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 174 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 176 | _LOG_DEBUG("Adding " << nh << " for destination: " << destRouter); |
| 177 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 178 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 179 | if (rteChk == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 180 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 181 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | m_rTable.push_back(rte); |
| 183 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 184 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 185 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 189 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 190 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | { |
| 192 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(), |
| 193 | m_rTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 194 | ndn::bind(&routingTableEntryCompare, |
| 195 | _1, destRouter)); |
| 196 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 197 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 199 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 203 | RoutingTable::writeLog(int hyperbolicState) |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 204 | { |
| 205 | _LOG_DEBUG("---------------Routing Table------------------"); |
| 206 | for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ; |
| 207 | it != m_rTable.end(); ++it) { |
| 208 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 209 | _LOG_DEBUG("Nexthops: "); |
| 210 | (*it).getNexthopList().writeLog(); |
| 211 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 212 | |
| 213 | if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 214 | _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------"); |
| 215 | for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ; |
| 216 | it != m_dryTable.end(); ++it) { |
| 217 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 218 | _LOG_DEBUG("Nexthops: "); |
| 219 | (*it).getNexthopList().writeLog(); |
| 220 | } |
| 221 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 222 | } |
| 223 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | //function related to manipulation of dry routing table |
| 225 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 226 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 228 | _LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter); |
| 229 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(), |
| 231 | m_dryTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 232 | ndn::bind(&routingTableEntryCompare, |
| 233 | _1, destRouter)); |
| 234 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 236 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 237 | m_dryTable.push_back(rte); |
| 238 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 239 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 240 | (*it).getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 245 | RoutingTable::clearRoutingTable() |
| 246 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 247 | if (m_rTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 248 | m_rTable.clear(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | RoutingTable::clearDryRoutingTable() |
| 254 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 255 | if (m_dryTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 256 | m_dryTable.clear(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | }//namespace nlsr |
| 261 | |