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 |
| 108 | RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr) |
| 109 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 110 | _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | Map vMap; |
| 112 | vMap.createFromAdjLsdb(pnlsr); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 113 | vMap.writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 114 | int numOfRouter = vMap.getMapSize(); |
| 115 | LinkStateRoutingTableCalculator lsrtc(numOfRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 116 | lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void |
| 120 | RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr) |
| 121 | { |
| 122 | Map vMap; |
| 123 | vMap.createFromAdjLsdb(pnlsr); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 124 | vMap.writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | int numOfRouter = vMap.getMapSize(); |
| 126 | HypRoutingTableCalculator hrtc(numOfRouter, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
| 131 | RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr) |
| 132 | { |
| 133 | Map vMap; |
| 134 | vMap.createFromAdjLsdb(pnlsr); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 135 | vMap.writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 136 | int numOfRouter = vMap.getMapSize(); |
| 137 | HypRoutingTableCalculator hrtc(numOfRouter, 1); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 138 | hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr) |
| 143 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 144 | if (pnlsr.getIsRouteCalculationScheduled() != true) { |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame^] | 145 | m_scheduler.scheduleEvent(ndn::time::seconds(15), |
| 146 | ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr))); |
| 147 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 148 | pnlsr.setIsRouteCalculationScheduled(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 153 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 154 | { |
| 155 | return rte.getDestination() == destRouter; |
| 156 | } |
| 157 | |
| 158 | // function related to manipulation of routing table |
| 159 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 160 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 162 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 163 | if (rteChk == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 164 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 165 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | m_rTable.push_back(rte); |
| 167 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 168 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 169 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 173 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 174 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | { |
| 176 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(), |
| 177 | m_rTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 178 | ndn::bind(&routingTableEntryCompare, |
| 179 | _1, destRouter)); |
| 180 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 181 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 183 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 187 | RoutingTable::writeLog(int hyperbolicState) |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 188 | { |
| 189 | _LOG_DEBUG("---------------Routing Table------------------"); |
| 190 | for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ; |
| 191 | it != m_rTable.end(); ++it) { |
| 192 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 193 | _LOG_DEBUG("Nexthops: "); |
| 194 | (*it).getNexthopList().writeLog(); |
| 195 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 196 | |
| 197 | if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) { |
| 198 | _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------"); |
| 199 | for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ; |
| 200 | it != m_dryTable.end(); ++it) { |
| 201 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 202 | _LOG_DEBUG("Nexthops: "); |
| 203 | (*it).getNexthopList().writeLog(); |
| 204 | } |
| 205 | } |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 206 | } |
| 207 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 208 | //function related to manipulation of dry routing table |
| 209 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 210 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | { |
| 212 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(), |
| 213 | m_dryTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 214 | ndn::bind(&routingTableEntryCompare, |
| 215 | _1, destRouter)); |
| 216 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 217 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 218 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | m_dryTable.push_back(rte); |
| 220 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 221 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 222 | (*it).getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | RoutingTable::clearRoutingTable() |
| 228 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 229 | if (m_rTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | m_rTable.clear(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | RoutingTable::clearDryRoutingTable() |
| 236 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 237 | if (m_dryTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | m_dryTable.clear(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | }//namespace nlsr |
| 243 | |