blob: efa423a866877cfb2cb40d4b65a914892d1fb997 [file] [log] [blame]
akmhoquedfa4a5b2014-02-03 20:12:29 -06001#include<iostream>
2#include<string>
akmhoque79d355f2014-02-04 15:11:16 -06003#include<list>
akmhoquedfa4a5b2014-02-03 20:12:29 -06004
5#include "nlsr_rt.hpp"
6#include "nlsr.hpp"
akmhoque79d355f2014-02-04 15:11:16 -06007#include "nlsr_map.hpp"
8#include "nlsr_rtc.hpp"
akmhoquef7c2c7c2014-02-06 11:32:43 -06009#include "nlsr_rte.hpp"
akmhoque62a8e402014-02-08 12:00:27 -060010#include "nlsr_npt.hpp"
akmhoquedfa4a5b2014-02-03 20:12:29 -060011
12using namespace std;
13
akmhoque79d355f2014-02-04 15:11:16 -060014void
15RoutingTable::calculate(nlsr& pnlsr)
16{
akmhoque62a8e402014-02-08 12:00:27 -060017 //debugging purpose
18 pnlsr.getNpt().printNpt();
akmhoquedfa4a5b2014-02-03 20:12:29 -060019
akmhoque79d355f2014-02-04 15:11:16 -060020 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 {
akmhoque3fdf7612014-02-04 21:18:23 -060029 cout<<"CLearing old routing table ....."<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060030 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
akmhoque62a8e402014-02-08 12:00:27 -060050 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
51 //debugging purpose
52 printRoutingTable();
53 pnlsr.getNpt().printNpt();
54 pnlsr.getFib().printFib();
55 //debugging purpose end
akmhoque79d355f2014-02-04 15:11:16 -060056 }
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,";
akmhoque3fdf7612014-02-04 21:18:23 -060066 cout<< " so Routing table can not be calculated :("<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060067 clearRoutingTable();
68 clearDryRoutingTable(); // for dry run options
69 // need to update NPT here
akmhoque62a8e402014-02-08 12:00:27 -060070 pnlsr.getNpt().updateNptWithNewRoute(pnlsr);
71 //debugging purpose
72 printRoutingTable();
73 pnlsr.getNpt().printNpt();
74 pnlsr.getFib().printFib();
75 //debugging purpose end
akmhoque79d355f2014-02-04 15:11:16 -060076 }
77
akmhoquef7c2c7c2014-02-06 11:32:43 -060078
akmhoque79d355f2014-02-04 15:11:16 -060079 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
80 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
81 }
82 else
83 {
akmhoquee77d8142014-02-11 11:59:57 -060084 scheduleRoutingTableCalculation(pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -060085 }
akmhoque62a8e402014-02-08 12:00:27 -060086
akmhoque79d355f2014-02-04 15:11:16 -060087}
88
89
90void
91RoutingTable::calculateLsRoutingTable(nlsr& pnlsr)
92{
akmhoque3fdf7612014-02-04 21:18:23 -060093 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060094 Map vMap;
95 vMap.createMapFromAdjLsdb(pnlsr);
96 int numOfRouter=vMap.getMapSize();
97
98 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque79d355f2014-02-04 15:11:16 -060099 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -0600100}
101
102void
103RoutingTable::calculateHypRoutingTable(nlsr& pnlsr)
104{
akmhoquef7c2c7c2014-02-06 11:32:43 -0600105 Map vMap;
106 vMap.createMapFromAdjLsdb(pnlsr);
107 int numOfRouter=vMap.getMapSize();
108 HypRoutingTableCalculator hrtc(numOfRouter,0);
109 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -0600110}
111
112void
akmhoquef7c2c7c2014-02-06 11:32:43 -0600113RoutingTable::calculateHypDryRoutingTable(nlsr& pnlsr)
akmhoque79d355f2014-02-04 15:11:16 -0600114{
akmhoquef7c2c7c2014-02-06 11:32:43 -0600115 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
akmhoquee77d8142014-02-11 11:59:57 -0600122void
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
akmhoquef7c2c7c2014-02-06 11:32:43 -0600133static 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 }
akmhoque79d355f2014-02-04 15:11:16 -0600197
198}
199
akmhoquef7c2c7c2014-02-06 11:32:43 -0600200void
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
akmhoque79d355f2014-02-04 15:11:16 -0600212void
213RoutingTable::clearRoutingTable()
214{
akmhoque3fdf7612014-02-04 21:18:23 -0600215 if( rTable.size() > 0 )
216 {
217 rTable.clear();
218 }
akmhoque79d355f2014-02-04 15:11:16 -0600219}
220
221void
222RoutingTable::clearDryRoutingTable()
223{
akmhoque3fdf7612014-02-04 21:18:23 -0600224 if (dryTable.size()>0 )
225 {
226 dryTable.clear();
227 }
akmhoque79d355f2014-02-04 15:11:16 -0600228}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600229