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