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