blob: ae7fc88b5feda1cb4dca0196fb71ae8eed5bda5f [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 {
84 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
85 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
86 pnlsr.setIsRouteCalculationScheduled(1);
87 }
akmhoque62a8e402014-02-08 12:00:27 -060088
akmhoque79d355f2014-02-04 15:11:16 -060089}
90
91
92void
93RoutingTable::calculateLsRoutingTable(nlsr& pnlsr)
94{
akmhoque3fdf7612014-02-04 21:18:23 -060095 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060096 Map vMap;
97 vMap.createMapFromAdjLsdb(pnlsr);
98 int numOfRouter=vMap.getMapSize();
99
100 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque79d355f2014-02-04 15:11:16 -0600101 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -0600102}
103
104void
105RoutingTable::calculateHypRoutingTable(nlsr& pnlsr)
106{
akmhoquef7c2c7c2014-02-06 11:32:43 -0600107 Map vMap;
108 vMap.createMapFromAdjLsdb(pnlsr);
109 int numOfRouter=vMap.getMapSize();
110 HypRoutingTableCalculator hrtc(numOfRouter,0);
111 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -0600112}
113
114void
akmhoquef7c2c7c2014-02-06 11:32:43 -0600115RoutingTable::calculateHypDryRoutingTable(nlsr& pnlsr)
akmhoque79d355f2014-02-04 15:11:16 -0600116{
akmhoquef7c2c7c2014-02-06 11:32:43 -0600117 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
124static bool
125routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter){
126 return rte.getDestination()==destRouter;
127}
128
129// function related to manipulation of routing table
130void
131RoutingTable::addNextHop(string destRouter, NextHop& nh)
132{
133 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
134 if( !rte.second )
135 {
136 RoutingTableEntry rte(destRouter);
137 rte.getNhl().addNextHop(nh);
138 rTable.push_back(rte);
139 }
140 else
141 {
142 (rte.first).getNhl().addNextHop(nh);
143 }
144}
145
146std::pair<RoutingTableEntry&, bool>
147RoutingTable::findRoutingTableEntry(string destRouter)
148{
149 std::list<RoutingTableEntry >::iterator it = std::find_if( rTable.begin(),
150 rTable.end(),
151 bind(&routingTableEntryCompare, _1, destRouter));
152 if ( it != rTable.end() )
153 {
154 return std::make_pair(boost::ref((*it)),true);
155 }
156 RoutingTableEntry rteEmpty;
157 return std::make_pair(boost::ref(rteEmpty),false);
158}
159
160void
161RoutingTable::printRoutingTable()
162{
163 cout<<"---------------Routing Table------------------"<<endl;
164 for(std::list<RoutingTableEntry>::iterator it=rTable.begin() ;
165 it != rTable.end(); ++it)
166 {
167 cout<<(*it)<<endl;
168 }
169}
170
171
172//function related to manipulation of dry routing table
173void
174RoutingTable::addNextHopToDryTable(string destRouter, NextHop& nh)
175{
176 std::list<RoutingTableEntry >::iterator it = std::find_if( dryTable.begin(),
177 dryTable.end(),
178 bind(&routingTableEntryCompare, _1, destRouter));
179 if ( it == dryTable.end() ){
180 RoutingTableEntry rte(destRouter);
181 rte.getNhl().addNextHop(nh);
182 dryTable.push_back(rte);
183 }
184 else
185 {
186 (*it).getNhl().addNextHop(nh);
187 }
akmhoque79d355f2014-02-04 15:11:16 -0600188
189}
190
akmhoquef7c2c7c2014-02-06 11:32:43 -0600191void
192RoutingTable::printDryRoutingTable()
193{
194 cout<<"--------Dry Run's Routing Table--------------"<<endl;
195 for(std::list<RoutingTableEntry>::iterator it=dryTable.begin() ;
196 it != dryTable.end(); ++it)
197 {
198 cout<<(*it)<<endl;
199 }
200}
201
202
akmhoque79d355f2014-02-04 15:11:16 -0600203void
204RoutingTable::clearRoutingTable()
205{
akmhoque3fdf7612014-02-04 21:18:23 -0600206 if( rTable.size() > 0 )
207 {
208 rTable.clear();
209 }
akmhoque79d355f2014-02-04 15:11:16 -0600210}
211
212void
213RoutingTable::clearDryRoutingTable()
214{
akmhoque3fdf7612014-02-04 21:18:23 -0600215 if (dryTable.size()>0 )
216 {
217 dryTable.clear();
218 }
akmhoque79d355f2014-02-04 15:11:16 -0600219}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600220