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