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