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