blob: 734a85ee7bdff52a3ca7d5fdefdf06b0663b5901 [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();
22
23 if ( pnlsr.getIsRoutingTableCalculating() == 0 )
24 {
25 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
26
27 if ( pnlsr.getLsdb().doesLsaExist(
28 pnlsr.getConfParameter().getRouterPrefix()+"/"+"2",2) )
29 {
30 if(pnlsr.getIsBuildAdjLsaSheduled() != 1)
31 {
32 cout<<"CLearing old routing table ....."<<endl;
33 clearRoutingTable();
34 clearDryRoutingTable(); // for dry run options
35 // calculate Link State routing
36 if( (pnlsr.getConfParameter().getIsHyperbolicCalc() == 0 )
37 || (pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 ) )
38 {
39 calculateLsRoutingTable(pnlsr);
40 }
41 //calculate hyperbolic routing
42 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 1 )
43 {
44 calculateHypRoutingTable(pnlsr);
45 }
46 //calculate dry hyperbolic routing
47 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 )
48 {
49 calculateHypDryRoutingTable(pnlsr);
50 }
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
73 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
74 //debugging purpose
75 printRoutingTable();
76 pnlsr.getNpt().printNpt();
77 pnlsr.getFib().printFib();
78 //debugging purpose end
79 }
akmhoque298385a2014-02-13 14:13:09 -060080
81
akmhoque1fd8c1e2014-02-19 19:41:49 -060082 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
83 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
84 }
85 else
86 {
87 scheduleRoutingTableCalculation(pnlsr);
88 }
akmhoque298385a2014-02-13 14:13:09 -060089
akmhoque1fd8c1e2014-02-19 19:41:49 -060090 }
akmhoque298385a2014-02-13 14:13:09 -060091
92
akmhoque1fd8c1e2014-02-19 19:41:49 -060093 void
94 RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
95 {
96 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
97 Map vMap;
98 vMap.createMapFromAdjLsdb(pnlsr);
99 int numOfRouter=vMap.getMapSize();
akmhoque298385a2014-02-13 14:13:09 -0600100
akmhoque1fd8c1e2014-02-19 19:41:49 -0600101 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
102 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
103 }
akmhoque298385a2014-02-13 14:13:09 -0600104
akmhoque1fd8c1e2014-02-19 19:41:49 -0600105 void
106 RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
107 {
108 Map vMap;
109 vMap.createMapFromAdjLsdb(pnlsr);
110 int numOfRouter=vMap.getMapSize();
111 HypRoutingTableCalculator hrtc(numOfRouter,0);
112 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
113 }
akmhoque298385a2014-02-13 14:13:09 -0600114
akmhoque1fd8c1e2014-02-19 19:41:49 -0600115 void
116 RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
117 {
118 Map vMap;
119 vMap.createMapFromAdjLsdb(pnlsr);
120 int numOfRouter=vMap.getMapSize();
121 HypRoutingTableCalculator hrtc(numOfRouter,1);
122 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
123 }
akmhoque298385a2014-02-13 14:13:09 -0600124
akmhoque1fd8c1e2014-02-19 19:41:49 -0600125 void
126 RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
127 {
128 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
129 {
130 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
131 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
132 pnlsr.setIsRouteCalculationScheduled(1);
133 }
134 }
akmhoque298385a2014-02-13 14:13:09 -0600135
akmhoque1fd8c1e2014-02-19 19:41:49 -0600136 static bool
137 routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
138 {
139 return rte.getDestination()==destRouter;
140 }
akmhoque298385a2014-02-13 14:13:09 -0600141
142// function related to manipulation of routing table
akmhoque1fd8c1e2014-02-19 19:41:49 -0600143 void
144 RoutingTable::addNextHop(string destRouter, NextHop& nh)
145 {
146 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
147 if( !rte.second )
148 {
149 RoutingTableEntry rte(destRouter);
150 rte.getNhl().addNextHop(nh);
151 rTable.push_back(rte);
152 }
153 else
154 {
155 (rte.first).getNhl().addNextHop(nh);
156 }
157 }
akmhoque298385a2014-02-13 14:13:09 -0600158
akmhoque1fd8c1e2014-02-19 19:41:49 -0600159 std::pair<RoutingTableEntry&, bool>
160 RoutingTable::findRoutingTableEntry(string destRouter)
161 {
162 std::list<RoutingTableEntry >::iterator it = std::find_if( rTable.begin(),
163 rTable.end(),
164 bind(&routingTableEntryCompare, _1, destRouter));
165 if ( it != rTable.end() )
166 {
167 return std::make_pair(boost::ref((*it)),true);
168 }
169 RoutingTableEntry rteEmpty;
170 return std::make_pair(boost::ref(rteEmpty),false);
171 }
akmhoque298385a2014-02-13 14:13:09 -0600172
akmhoque1fd8c1e2014-02-19 19:41:49 -0600173 void
174 RoutingTable::printRoutingTable()
175 {
176 cout<<"---------------Routing Table------------------"<<endl;
177 for(std::list<RoutingTableEntry>::iterator it=rTable.begin() ;
178 it != rTable.end(); ++it)
179 {
180 cout<<(*it)<<endl;
181 }
182 }
akmhoque298385a2014-02-13 14:13:09 -0600183
184
185//function related to manipulation of dry routing table
akmhoque1fd8c1e2014-02-19 19:41:49 -0600186 void
187 RoutingTable::addNextHopToDryTable(string destRouter, NextHop& nh)
188 {
189 std::list<RoutingTableEntry >::iterator it = std::find_if( dryTable.begin(),
190 dryTable.end(),
191 bind(&routingTableEntryCompare, _1, destRouter));
192 if ( it == dryTable.end() )
193 {
194 RoutingTableEntry rte(destRouter);
195 rte.getNhl().addNextHop(nh);
196 dryTable.push_back(rte);
197 }
198 else
199 {
200 (*it).getNhl().addNextHop(nh);
201 }
akmhoque298385a2014-02-13 14:13:09 -0600202
akmhoque1fd8c1e2014-02-19 19:41:49 -0600203 }
204
205 void
206 RoutingTable::printDryRoutingTable()
207 {
208 cout<<"--------Dry Run's Routing Table--------------"<<endl;
209 for(std::list<RoutingTableEntry>::iterator it=dryTable.begin() ;
210 it != dryTable.end(); ++it)
211 {
212 cout<<(*it)<<endl;
213 }
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
216
akmhoque1fd8c1e2014-02-19 19:41:49 -0600217 void
218 RoutingTable::clearRoutingTable()
219 {
220 if( rTable.size() > 0 )
221 {
222 rTable.clear();
223 }
224 }
akmhoque298385a2014-02-13 14:13:09 -0600225
akmhoque1fd8c1e2014-02-19 19:41:49 -0600226 void
227 RoutingTable::clearDryRoutingTable()
228 {
229 if (dryTable.size()>0 )
230 {
231 dryTable.clear();
232 }
233 }
akmhoque298385a2014-02-13 14:13:09 -0600234
akmhoqueb1710aa2014-02-19 17:13:36 -0600235}//namespace nlsr
236