blob: 2cd9b28000851cafb3bad04f6a5f21d9e88254b7 [file] [log] [blame]
akmhoqueba094742014-02-28 11:47:21 -06001#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
12namespace nlsr
13{
14
akmhoque5a44dd42014-03-12 18:11:32 -050015 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060016
akmhoque5a44dd42014-03-12 18:11:32 -050017 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 )
akmhoqueba094742014-02-28 11:47:21 -060027 {
akmhoque5a44dd42014-03-12 18:11:32 -050028 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
29 if ( pnlsr.getLsdb().doesLsaExist(
30 pnlsr.getConfParameter().getRouterPrefix()+"/"+"2",2) )
31 {
32 if(pnlsr.getIsBuildAdjLsaSheduled() != 1)
akmhoqueba094742014-02-28 11:47:21 -060033 {
akmhoque5a44dd42014-03-12 18:11:32 -050034 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
akmhoqueba094742014-02-28 11:47:21 -060060 }
61 else
62 {
akmhoque5a44dd42014-03-12 18:11:32 -050063 cout<<"Adjacency building is scheduled, so ";
64 cout<<"routing table can not be calculated :("<<endl;
akmhoqueba094742014-02-28 11:47:21 -060065 }
akmhoque5a44dd42014-03-12 18:11:32 -050066 }
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
akmhoqueba094742014-02-28 11:47:21 -060084 }
akmhoque5a44dd42014-03-12 18:11:32 -050085 else
akmhoqueba094742014-02-28 11:47:21 -060086 {
akmhoque5a44dd42014-03-12 18:11:32 -050087 scheduleRoutingTableCalculation(pnlsr);
akmhoqueba094742014-02-28 11:47:21 -060088 }
akmhoque5a44dd42014-03-12 18:11:32 -050089 }
akmhoqueba094742014-02-28 11:47:21 -060090
akmhoqueba094742014-02-28 11:47:21 -060091
akmhoque5a44dd42014-03-12 18:11:32 -050092 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 }
akmhoqueba094742014-02-28 11:47:21 -0600102
akmhoque5a44dd42014-03-12 18:11:32 -0500103 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 }
akmhoqueba094742014-02-28 11:47:21 -0600112
akmhoque5a44dd42014-03-12 18:11:32 -0500113 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 )
akmhoqueba094742014-02-28 11:47:21 -0600127 {
akmhoque5a44dd42014-03-12 18:11:32 -0500128 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
129 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
130 pnlsr.setIsRouteCalculationScheduled(1);
akmhoqueba094742014-02-28 11:47:21 -0600131 }
akmhoque5a44dd42014-03-12 18:11:32 -0500132 }
133
134 static bool
135 routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
136 {
137 return rte.getDestination()==destRouter;
138 }
akmhoqueba094742014-02-28 11:47:21 -0600139
140// function related to manipulation of routing table
akmhoque5a44dd42014-03-12 18:11:32 -0500141 void
142 RoutingTable::addNextHop(string destRouter, NextHop& nh)
143 {
144 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
145 if( !rte.second )
akmhoqueba094742014-02-28 11:47:21 -0600146 {
akmhoque5a44dd42014-03-12 18:11:32 -0500147 RoutingTableEntry rte(destRouter);
148 rte.getNhl().addNextHop(nh);
149 rTable.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -0600150 }
akmhoque5a44dd42014-03-12 18:11:32 -0500151 else
152 {
153 (rte.first).getNhl().addNextHop(nh);
154 }
155 }
akmhoqueba094742014-02-28 11:47:21 -0600156
akmhoque5a44dd42014-03-12 18:11:32 -0500157 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() )
akmhoqueba094742014-02-28 11:47:21 -0600164 {
akmhoque5a44dd42014-03-12 18:11:32 -0500165 return std::make_pair(boost::ref((*it)),true);
akmhoqueba094742014-02-28 11:47:21 -0600166 }
akmhoque5a44dd42014-03-12 18:11:32 -0500167 RoutingTableEntry rteEmpty;
168 return std::make_pair(boost::ref(rteEmpty),false);
169 }
akmhoqueba094742014-02-28 11:47:21 -0600170
akmhoque5a44dd42014-03-12 18:11:32 -0500171 void
172 RoutingTable::printRoutingTable()
173 {
174 cout<<"---------------Routing Table------------------"<<endl;
175 for(std::list<RoutingTableEntry>::iterator it=rTable.begin() ;
176 it != rTable.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -0600177 {
akmhoque5a44dd42014-03-12 18:11:32 -0500178 cout<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600179 }
akmhoque5a44dd42014-03-12 18:11:32 -0500180 }
akmhoqueba094742014-02-28 11:47:21 -0600181
182
183//function related to manipulation of dry routing table
akmhoque5a44dd42014-03-12 18:11:32 -0500184 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() )
akmhoqueba094742014-02-28 11:47:21 -0600191 {
akmhoque5a44dd42014-03-12 18:11:32 -0500192 RoutingTableEntry rte(destRouter);
193 rte.getNhl().addNextHop(nh);
194 dryTable.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -0600195 }
akmhoque5a44dd42014-03-12 18:11:32 -0500196 else
197 {
198 (*it).getNhl().addNextHop(nh);
199 }
200 }
akmhoqueba094742014-02-28 11:47:21 -0600201
akmhoque5a44dd42014-03-12 18:11:32 -0500202 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)
akmhoqueba094742014-02-28 11:47:21 -0600208 {
akmhoque5a44dd42014-03-12 18:11:32 -0500209 cout<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600210 }
akmhoque5a44dd42014-03-12 18:11:32 -0500211 }
akmhoqueba094742014-02-28 11:47:21 -0600212
213
akmhoque5a44dd42014-03-12 18:11:32 -0500214 void
215 RoutingTable::clearRoutingTable()
216 {
217 if( rTable.size() > 0 )
akmhoqueba094742014-02-28 11:47:21 -0600218 {
akmhoque5a44dd42014-03-12 18:11:32 -0500219 rTable.clear();
akmhoqueba094742014-02-28 11:47:21 -0600220 }
akmhoque5a44dd42014-03-12 18:11:32 -0500221 }
akmhoqueba094742014-02-28 11:47:21 -0600222
akmhoque5a44dd42014-03-12 18:11:32 -0500223 void
224 RoutingTable::clearDryRoutingTable()
225 {
226 if (dryTable.size()>0 )
akmhoqueba094742014-02-28 11:47:21 -0600227 {
akmhoque5a44dd42014-03-12 18:11:32 -0500228 dryTable.clear();
akmhoqueba094742014-02-28 11:47:21 -0600229 }
akmhoque5a44dd42014-03-12 18:11:32 -0500230 }
akmhoqueba094742014-02-28 11:47:21 -0600231
232}//namespace nlsr
233