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 | { |
| 45 | //debugging purpose |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 46 | pnlsr.getNamePrefixTable().print(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | pnlsr.getLsdb().printAdjLsdb(); |
| 48 | pnlsr.getLsdb().printCorLsdb(); |
| 49 | pnlsr.getLsdb().printNameLsdb(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 50 | pnlsr.getNamePrefixTable().writeLog(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 51 | if (pnlsr.getIsRoutingTableCalculating() == false) { |
| 52 | //setting routing table calculation |
| 53 | pnlsr.setIsRoutingTableCalculating(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | if (pnlsr.getLsdb().doesLsaExist( |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 55 | pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency", |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 56 | std::string("adjacency"))) { |
| 57 | if (pnlsr.getIsBuildAdjLsaSheduled() != 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | std::cout << "CLearing old routing table ....." << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 59 | _LOG_DEBUG("CLearing old routing table ....."); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 60 | clearRoutingTable(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | // for dry run options |
| 62 | clearDryRoutingTable(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | // calculate Link State routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 64 | if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) |
| 65 | || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | calculateLsRoutingTable(pnlsr); |
| 67 | } |
| 68 | //calculate hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | calculateHypRoutingTable(pnlsr); |
| 71 | } |
| 72 | //calculate dry hyperbolic routing |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 73 | if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 74 | calculateHypDryRoutingTable(pnlsr); |
| 75 | } |
| 76 | //need to update NPT here |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 77 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | //debugging purpose |
| 79 | printRoutingTable(); |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 80 | pnlsr.getNamePrefixTable().print(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | pnlsr.getFib().print(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 82 | writeLog(); |
| 83 | pnlsr.getNamePrefixTable().writeLog(); |
| 84 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | //debugging purpose end |
| 86 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 87 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 88 | std::cout << "Adjacency building is scheduled, so "; |
| 89 | std::cout << "routing table can not be calculated :(" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 90 | _LOG_DEBUG("Adjacency building is scheduled, so" |
| 91 | " routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 92 | } |
| 93 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 94 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | std::cout << "No Adj LSA of router itself,"; |
| 96 | std::cout << " so Routing table can not be calculated :(" << std::endl; |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 97 | _LOG_DEBUG("No Adj LSA of router itself," |
| 98 | " so Routing table can not be calculated :("); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | clearRoutingTable(); |
| 100 | clearDryRoutingTable(); // for dry run options |
| 101 | // need to update NPT here |
| 102 | std::cout << "Calling Update NPT With new Route" << std::endl; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 103 | pnlsr.getNamePrefixTable().updateWithNewRoute(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | //debugging purpose |
| 105 | printRoutingTable(); |
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 106 | pnlsr.getNamePrefixTable().print(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 107 | pnlsr.getFib().print(); |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 108 | writeLog(); |
| 109 | pnlsr.getNamePrefixTable().writeLog(); |
| 110 | pnlsr.getFib().writeLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | //debugging purpose end |
| 112 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 113 | pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag |
| 114 | pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 115 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 116 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | scheduleRoutingTableCalculation(pnlsr); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void |
| 123 | RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr) |
| 124 | { |
| 125 | std::cout << "RoutingTable::calculateLsRoutingTable Called" << std::endl; |
| 126 | Map vMap; |
| 127 | vMap.createFromAdjLsdb(pnlsr); |
| 128 | int numOfRouter = vMap.getMapSize(); |
| 129 | LinkStateRoutingTableCalculator lsrtc(numOfRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 130 | lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr) |
| 135 | { |
| 136 | Map vMap; |
| 137 | vMap.createFromAdjLsdb(pnlsr); |
| 138 | int numOfRouter = vMap.getMapSize(); |
| 139 | HypRoutingTableCalculator hrtc(numOfRouter, 0); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 140 | hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
| 144 | RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr) |
| 145 | { |
| 146 | Map vMap; |
| 147 | vMap.createFromAdjLsdb(pnlsr); |
| 148 | int numOfRouter = vMap.getMapSize(); |
| 149 | HypRoutingTableCalculator hrtc(numOfRouter, 1); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 150 | hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
| 154 | RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr) |
| 155 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 156 | if (pnlsr.getIsRouteCalculationScheduled() != true) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 158 | ndn::bind(&RoutingTable::calculate, this, |
| 159 | ndn::ref(pnlsr))); |
| 160 | pnlsr.setIsRouteCalculationScheduled(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | static bool |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 165 | routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | { |
| 167 | return rte.getDestination() == destRouter; |
| 168 | } |
| 169 | |
| 170 | // function related to manipulation of routing table |
| 171 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 172 | RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 173 | { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 174 | RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 175 | if (rteChk == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 177 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | m_rTable.push_back(rte); |
| 179 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 180 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 181 | rteChk->getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 185 | RoutingTableEntry* |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 186 | RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 187 | { |
| 188 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(), |
| 189 | m_rTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 190 | ndn::bind(&routingTableEntryCompare, |
| 191 | _1, destRouter)); |
| 192 | if (it != m_rTable.end()) { |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 193 | return &(*it); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 194 | } |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 195 | return 0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void |
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame] | 199 | RoutingTable::writeLog() |
| 200 | { |
| 201 | _LOG_DEBUG("---------------Routing Table------------------"); |
| 202 | for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ; |
| 203 | it != m_rTable.end(); ++it) { |
| 204 | _LOG_DEBUG("Destination: " << (*it).getDestination()); |
| 205 | _LOG_DEBUG("Nexthops: "); |
| 206 | (*it).getNexthopList().writeLog(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | RoutingTable::printRoutingTable() |
| 212 | { |
| 213 | std::cout << "---------------Routing Table------------------" << std::endl; |
| 214 | for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 215 | it != m_rTable.end(); ++it) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | std::cout << (*it) << std::endl; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | //function related to manipulation of dry routing table |
| 222 | void |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 223 | RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | { |
| 225 | std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(), |
| 226 | m_dryTable.end(), |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 227 | ndn::bind(&routingTableEntryCompare, |
| 228 | _1, destRouter)); |
| 229 | if (it == m_dryTable.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | RoutingTableEntry rte(destRouter); |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 231 | rte.getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 232 | m_dryTable.push_back(rte); |
| 233 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 234 | else { |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 235 | (*it).getNexthopList().addNextHop(nh); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | RoutingTable::printDryRoutingTable() |
| 241 | { |
| 242 | std::cout << "--------Dry Run's Routing Table--------------" << std::endl; |
| 243 | for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 244 | it != m_dryTable.end(); ++it) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 245 | cout << (*it) << endl; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | void |
| 251 | RoutingTable::clearRoutingTable() |
| 252 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 253 | if (m_rTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | m_rTable.clear(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | RoutingTable::clearDryRoutingTable() |
| 260 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 261 | if (m_dryTable.size() > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 262 | m_dryTable.clear(); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | }//namespace nlsr |
| 267 | |