blob: f9923f260be252909186a525c6d6f3812d1d2bad [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
73 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
74 //debugging purpose
75 printRoutingTable();
76 pnlsr.getNpt().printNpt();
77 pnlsr.getFib().printFib();
78 //debugging purpose end
79 }
80 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
81 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
82 }
83 else
84 {
85 scheduleRoutingTableCalculation(pnlsr);
86 }
87 }
88
89
90 void
91 RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
92 {
93 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
94 Map vMap;
95 vMap.createMapFromAdjLsdb(pnlsr);
96 int numOfRouter=vMap.getMapSize();
97 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
98 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
99 }
100
101 void
102 RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
103 {
104 Map vMap;
105 vMap.createMapFromAdjLsdb(pnlsr);
106 int numOfRouter=vMap.getMapSize();
107 HypRoutingTableCalculator hrtc(numOfRouter,0);
108 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
109 }
110
111 void
112 RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
113 {
114 Map vMap;
115 vMap.createMapFromAdjLsdb(pnlsr);
116 int numOfRouter=vMap.getMapSize();
117 HypRoutingTableCalculator hrtc(numOfRouter,1);
118 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
119 }
120
121 void
122 RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
123 {
124 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
125 {
126 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
127 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
128 pnlsr.setIsRouteCalculationScheduled(1);
129 }
130 }
131
132 static bool
133 routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
134 {
135 return rte.getDestination()==destRouter;
136 }
137
138// function related to manipulation of routing table
139 void
140 RoutingTable::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
155 std::pair<RoutingTableEntry&, bool>
156 RoutingTable::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
169 void
170 RoutingTable::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
182 void
183 RoutingTable::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 {
190 RoutingTableEntry rte(destRouter);
191 rte.getNhl().addNextHop(nh);
192 dryTable.push_back(rte);
193 }
194 else
195 {
196 (*it).getNhl().addNextHop(nh);
197 }
198 }
199
200 void
201 RoutingTable::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
212 void
213 RoutingTable::clearRoutingTable()
214 {
215 if( rTable.size() > 0 )
216 {
217 rTable.clear();
218 }
219 }
220
221 void
222 RoutingTable::clearDryRoutingTable()
223 {
224 if (dryTable.size()>0 )
225 {
226 dryTable.clear();
227 }
228 }
229
230}//namespace nlsr
231