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