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