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