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