blob: 2487db5077f149f450f10ed0c0ebd19d5c2d2801 [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"
akmhoquedfa4a5b2014-02-03 20:12:29 -060010
11using namespace std;
12
akmhoque79d355f2014-02-04 15:11:16 -060013void
14RoutingTable::calculate(nlsr& pnlsr)
15{
akmhoquedfa4a5b2014-02-03 20:12:29 -060016
akmhoque79d355f2014-02-04 15:11:16 -060017 if ( pnlsr.getIsRoutingTableCalculating() == 0 )
18 {
19 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
20
21 if ( pnlsr.getLsdb().doesLsaExist(
22 pnlsr.getConfParameter().getRouterPrefix()+"/"+"2",2) )
23 {
24 if(pnlsr.getIsBuildAdjLsaSheduled() != 1)
25 {
akmhoque3fdf7612014-02-04 21:18:23 -060026 cout<<"CLearing old routing table ....."<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060027 clearRoutingTable();
28 clearDryRoutingTable(); // for dry run options
29 // calculate Link State routing
30 if( (pnlsr.getConfParameter().getIsHyperbolicCalc() == 0 )
31 || (pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 ) )
32 {
33 calculateLsRoutingTable(pnlsr);
34 }
35 //calculate hyperbolic routing
36 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 1 )
37 {
38 calculateHypRoutingTable(pnlsr);
39 }
40 //calculate dry hyperbolic routing
41 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 )
42 {
43 calculateHypDryRoutingTable(pnlsr);
44 }
45
46 //need to update NPT here
47 }
48 else
49 {
50 cout<<"Adjacency building is scheduled, so ";
51 cout<<"routing table can not be calculated :("<<endl;
52 }
53 }
54 else
55 {
56 cout<<"No Adj LSA of router itself,";
akmhoque3fdf7612014-02-04 21:18:23 -060057 cout<< " so Routing table can not be calculated :("<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060058 clearRoutingTable();
59 clearDryRoutingTable(); // for dry run options
60 // need to update NPT here
61 }
62
akmhoquef7c2c7c2014-02-06 11:32:43 -060063 printRoutingTable();
64
akmhoque79d355f2014-02-04 15:11:16 -060065 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
66 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
67 }
68 else
69 {
70 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
71 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
72 pnlsr.setIsRouteCalculationScheduled(1);
73 }
74}
75
76
77void
78RoutingTable::calculateLsRoutingTable(nlsr& pnlsr)
79{
akmhoque3fdf7612014-02-04 21:18:23 -060080 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
akmhoque79d355f2014-02-04 15:11:16 -060081 Map vMap;
82 vMap.createMapFromAdjLsdb(pnlsr);
83 int numOfRouter=vMap.getMapSize();
84
85 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque79d355f2014-02-04 15:11:16 -060086 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -060087}
88
89void
90RoutingTable::calculateHypRoutingTable(nlsr& pnlsr)
91{
akmhoquef7c2c7c2014-02-06 11:32:43 -060092 Map vMap;
93 vMap.createMapFromAdjLsdb(pnlsr);
94 int numOfRouter=vMap.getMapSize();
95 HypRoutingTableCalculator hrtc(numOfRouter,0);
96 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
akmhoque79d355f2014-02-04 15:11:16 -060097}
98
99void
akmhoquef7c2c7c2014-02-06 11:32:43 -0600100RoutingTable::calculateHypDryRoutingTable(nlsr& pnlsr)
akmhoque79d355f2014-02-04 15:11:16 -0600101{
akmhoquef7c2c7c2014-02-06 11:32:43 -0600102 Map vMap;
103 vMap.createMapFromAdjLsdb(pnlsr);
104 int numOfRouter=vMap.getMapSize();
105 HypRoutingTableCalculator hrtc(numOfRouter,1);
106 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
107}
108
109static bool
110routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter){
111 return rte.getDestination()==destRouter;
112}
113
114// function related to manipulation of routing table
115void
116RoutingTable::addNextHop(string destRouter, NextHop& nh)
117{
118 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
119 if( !rte.second )
120 {
121 RoutingTableEntry rte(destRouter);
122 rte.getNhl().addNextHop(nh);
123 rTable.push_back(rte);
124 }
125 else
126 {
127 (rte.first).getNhl().addNextHop(nh);
128 }
129}
130
131std::pair<RoutingTableEntry&, bool>
132RoutingTable::findRoutingTableEntry(string destRouter)
133{
134 std::list<RoutingTableEntry >::iterator it = std::find_if( rTable.begin(),
135 rTable.end(),
136 bind(&routingTableEntryCompare, _1, destRouter));
137 if ( it != rTable.end() )
138 {
139 return std::make_pair(boost::ref((*it)),true);
140 }
141 RoutingTableEntry rteEmpty;
142 return std::make_pair(boost::ref(rteEmpty),false);
143}
144
145void
146RoutingTable::printRoutingTable()
147{
148 cout<<"---------------Routing Table------------------"<<endl;
149 for(std::list<RoutingTableEntry>::iterator it=rTable.begin() ;
150 it != rTable.end(); ++it)
151 {
152 cout<<(*it)<<endl;
153 }
154}
155
156
157//function related to manipulation of dry routing table
158void
159RoutingTable::addNextHopToDryTable(string destRouter, NextHop& nh)
160{
161 std::list<RoutingTableEntry >::iterator it = std::find_if( dryTable.begin(),
162 dryTable.end(),
163 bind(&routingTableEntryCompare, _1, destRouter));
164 if ( it == dryTable.end() ){
165 RoutingTableEntry rte(destRouter);
166 rte.getNhl().addNextHop(nh);
167 dryTable.push_back(rte);
168 }
169 else
170 {
171 (*it).getNhl().addNextHop(nh);
172 }
akmhoque79d355f2014-02-04 15:11:16 -0600173
174}
175
akmhoquef7c2c7c2014-02-06 11:32:43 -0600176void
177RoutingTable::printDryRoutingTable()
178{
179 cout<<"--------Dry Run's Routing Table--------------"<<endl;
180 for(std::list<RoutingTableEntry>::iterator it=dryTable.begin() ;
181 it != dryTable.end(); ++it)
182 {
183 cout<<(*it)<<endl;
184 }
185}
186
187
akmhoque79d355f2014-02-04 15:11:16 -0600188void
189RoutingTable::clearRoutingTable()
190{
akmhoque3fdf7612014-02-04 21:18:23 -0600191 if( rTable.size() > 0 )
192 {
193 rTable.clear();
194 }
akmhoque79d355f2014-02-04 15:11:16 -0600195}
196
197void
198RoutingTable::clearDryRoutingTable()
199{
akmhoque3fdf7612014-02-04 21:18:23 -0600200 if (dryTable.size()>0 )
201 {
202 dryTable.clear();
203 }
akmhoque79d355f2014-02-04 15:11:16 -0600204}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600205