blob: 87dcd26a23dcabc149c0ebab4e8f482fd8fd85f8 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -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
akmhoque1fd8c1e2014-02-19 19:41:49 -060012namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060013{
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060016
akmhoque1fd8c1e2014-02-19 19:41:49 -060017 void
18 RoutingTable::calculate(Nlsr& pnlsr)
19 {
20 //debugging purpose
21 pnlsr.getNpt().printNpt();
akmhoque1fd8c1e2014-02-19 19:41:49 -060022 if ( pnlsr.getIsRoutingTableCalculating() == 0 )
23 {
24 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
akmhoque1fd8c1e2014-02-19 19:41:49 -060025 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 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060049 //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 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060077 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
78 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
79 }
80 else
81 {
82 scheduleRoutingTableCalculation(pnlsr);
83 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060084 }
akmhoque298385a2014-02-13 14:13:09 -060085
86
akmhoque1fd8c1e2014-02-19 19:41:49 -060087 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();
akmhoque1fd8c1e2014-02-19 19:41:49 -060094 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
95 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
96 }
akmhoque298385a2014-02-13 14:13:09 -060097
akmhoque1fd8c1e2014-02-19 19:41:49 -060098 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 }
akmhoque298385a2014-02-13 14:13:09 -0600107
akmhoque1fd8c1e2014-02-19 19:41:49 -0600108 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 }
akmhoque298385a2014-02-13 14:13:09 -0600117
akmhoque1fd8c1e2014-02-19 19:41:49 -0600118 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 }
akmhoque298385a2014-02-13 14:13:09 -0600128
akmhoque1fd8c1e2014-02-19 19:41:49 -0600129 static bool
130 routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
131 {
132 return rte.getDestination()==destRouter;
133 }
akmhoque298385a2014-02-13 14:13:09 -0600134
135// function related to manipulation of routing table
akmhoque1fd8c1e2014-02-19 19:41:49 -0600136 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 }
akmhoque298385a2014-02-13 14:13:09 -0600151
akmhoque1fd8c1e2014-02-19 19:41:49 -0600152 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 }
akmhoque298385a2014-02-13 14:13:09 -0600165
akmhoque1fd8c1e2014-02-19 19:41:49 -0600166 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 }
akmhoque298385a2014-02-13 14:13:09 -0600176
177
178//function related to manipulation of dry routing table
akmhoque1fd8c1e2014-02-19 19:41:49 -0600179 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 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600195 }
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 }
akmhoque298385a2014-02-13 14:13:09 -0600207
208
akmhoque1fd8c1e2014-02-19 19:41:49 -0600209 void
210 RoutingTable::clearRoutingTable()
211 {
212 if( rTable.size() > 0 )
213 {
214 rTable.clear();
215 }
216 }
akmhoque298385a2014-02-13 14:13:09 -0600217
akmhoque1fd8c1e2014-02-19 19:41:49 -0600218 void
219 RoutingTable::clearDryRoutingTable()
220 {
221 if (dryTable.size()>0 )
222 {
223 dryTable.clear();
224 }
225 }
akmhoque298385a2014-02-13 14:13:09 -0600226
akmhoqueb1710aa2014-02-19 17:13:36 -0600227}//namespace nlsr
228