blob: 5e15604ebf7dcc6c3f2bd161d6cad385422f142c [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
akmhoqueb1710aa2014-02-19 17:13:36 -060012namespace nlsr {
13
akmhoque298385a2014-02-13 14:13:09 -060014using namespace std;
15
16void
akmhoque1a481092014-02-19 16:34:22 -060017RoutingTable::calculate(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060018{
19 //debugging purpose
20 pnlsr.getNpt().printNpt();
21
22 if ( pnlsr.getIsRoutingTableCalculating() == 0 )
23 {
24 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
25
26 if ( pnlsr.getLsdb().doesLsaExist(
27 pnlsr.getConfParameter().getRouterPrefix()+"/"+"2",2) )
28 {
29 if(pnlsr.getIsBuildAdjLsaSheduled() != 1)
30 {
31 cout<<"CLearing old routing table ....."<<endl;
32 clearRoutingTable();
33 clearDryRoutingTable(); // for dry run options
34 // calculate Link State routing
35 if( (pnlsr.getConfParameter().getIsHyperbolicCalc() == 0 )
36 || (pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 ) )
37 {
38 calculateLsRoutingTable(pnlsr);
39 }
40 //calculate hyperbolic routing
41 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 1 )
42 {
43 calculateHypRoutingTable(pnlsr);
44 }
45 //calculate dry hyperbolic routing
46 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 )
47 {
48 calculateHypDryRoutingTable(pnlsr);
49 }
50
51 //need to update NPT here
52 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
53 //debugging purpose
54 printRoutingTable();
55 pnlsr.getNpt().printNpt();
56 pnlsr.getFib().printFib();
57 //debugging purpose end
58 }
59 else
60 {
61 cout<<"Adjacency building is scheduled, so ";
62 cout<<"routing table can not be calculated :("<<endl;
63 }
64 }
65 else
66 {
67 cout<<"No Adj LSA of router itself,";
68 cout<< " so Routing table can not be calculated :("<<endl;
69 clearRoutingTable();
70 clearDryRoutingTable(); // for dry run options
71 // need to update NPT here
72 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
73 //debugging purpose
74 printRoutingTable();
75 pnlsr.getNpt().printNpt();
76 pnlsr.getFib().printFib();
77 //debugging purpose end
78 }
79
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
92void
akmhoque1a481092014-02-19 16:34:22 -060093RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060094{
95 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
96 Map vMap;
97 vMap.createMapFromAdjLsdb(pnlsr);
98 int numOfRouter=vMap.getMapSize();
99
100 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
101 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
102}
103
104void
akmhoque1a481092014-02-19 16:34:22 -0600105RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600106{
107 Map vMap;
108 vMap.createMapFromAdjLsdb(pnlsr);
109 int numOfRouter=vMap.getMapSize();
110 HypRoutingTableCalculator hrtc(numOfRouter,0);
111 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
112}
113
114void
akmhoque1a481092014-02-19 16:34:22 -0600115RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600116{
117 Map vMap;
118 vMap.createMapFromAdjLsdb(pnlsr);
119 int numOfRouter=vMap.getMapSize();
120 HypRoutingTableCalculator hrtc(numOfRouter,1);
121 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
122}
123
124void
akmhoque1a481092014-02-19 16:34:22 -0600125RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600126{
127 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
128 {
129 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
130 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
131 pnlsr.setIsRouteCalculationScheduled(1);
132 }
133}
134
135static bool
136routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter){
137 return rte.getDestination()==destRouter;
138}
139
140// function related to manipulation of routing table
141void
142RoutingTable::addNextHop(string destRouter, NextHop& nh)
143{
144 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
145 if( !rte.second )
146 {
147 RoutingTableEntry rte(destRouter);
148 rte.getNhl().addNextHop(nh);
149 rTable.push_back(rte);
150 }
151 else
152 {
153 (rte.first).getNhl().addNextHop(nh);
154 }
155}
156
157std::pair<RoutingTableEntry&, bool>
158RoutingTable::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() )
164 {
165 return std::make_pair(boost::ref((*it)),true);
166 }
167 RoutingTableEntry rteEmpty;
168 return std::make_pair(boost::ref(rteEmpty),false);
169}
170
171void
172RoutingTable::printRoutingTable()
173{
174 cout<<"---------------Routing Table------------------"<<endl;
175 for(std::list<RoutingTableEntry>::iterator it=rTable.begin() ;
176 it != rTable.end(); ++it)
177 {
178 cout<<(*it)<<endl;
179 }
180}
181
182
183//function related to manipulation of dry routing table
184void
185RoutingTable::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() ){
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
202void
203RoutingTable::printDryRoutingTable()
204{
205 cout<<"--------Dry Run's Routing Table--------------"<<endl;
206 for(std::list<RoutingTableEntry>::iterator it=dryTable.begin() ;
207 it != dryTable.end(); ++it)
208 {
209 cout<<(*it)<<endl;
210 }
211}
212
213
214void
215RoutingTable::clearRoutingTable()
216{
217 if( rTable.size() > 0 )
218 {
219 rTable.clear();
220 }
221}
222
223void
224RoutingTable::clearDryRoutingTable()
225{
226 if (dryTable.size()>0 )
227 {
228 dryTable.clear();
229 }
230}
231
akmhoqueb1710aa2014-02-19 17:13:36 -0600232}//namespace nlsr
233