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