blob: 8267930a560b5b92912b549eb63ab86daa92fdf8 [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"
akmhoque05d5fcf2014-04-15 14:58:45 -050011#include "utility/nlsr_logger.hpp"
12
13#define THIS_FILE "nlsr_rt.cpp"
akmhoqueba094742014-02-28 11:47:21 -060014
15namespace nlsr
16{
17
akmhoque5a44dd42014-03-12 18:11:32 -050018 using namespace std;
akmhoqueba094742014-02-28 11:47:21 -060019
akmhoque5a44dd42014-03-12 18:11:32 -050020 void
21 RoutingTable::calculate(Nlsr& pnlsr)
22 {
23 //debugging purpose
24 std::cout<<pnlsr.getConfParameter()<<std::endl;
akmhoque05d5fcf2014-04-15 14:58:45 -050025 pnlsr.getNpt().print();
akmhoque5a44dd42014-03-12 18:11:32 -050026 pnlsr.getLsdb().printAdjLsdb();
27 pnlsr.getLsdb().printCorLsdb();
28 pnlsr.getLsdb().printNameLsdb();
29 if ( pnlsr.getIsRoutingTableCalculating() == 0 )
akmhoqueba094742014-02-28 11:47:21 -060030 {
akmhoque5a44dd42014-03-12 18:11:32 -050031 pnlsr.setIsRoutingTableCalculating(1); //setting routing table calculation
32 if ( pnlsr.getLsdb().doesLsaExist(
33 pnlsr.getConfParameter().getRouterPrefix()+"/"+"2",2) )
34 {
35 if(pnlsr.getIsBuildAdjLsaSheduled() != 1)
akmhoqueba094742014-02-28 11:47:21 -060036 {
akmhoque5a44dd42014-03-12 18:11:32 -050037 cout<<"CLearing old routing table ....."<<endl;
38 clearRoutingTable();
39 clearDryRoutingTable(); // for dry run options
40 // calculate Link State routing
41 if( (pnlsr.getConfParameter().getIsHyperbolicCalc() == 0 )
42 || (pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 ) )
43 {
44 calculateLsRoutingTable(pnlsr);
45 }
46 //calculate hyperbolic routing
47 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 1 )
48 {
49 calculateHypRoutingTable(pnlsr);
50 }
51 //calculate dry hyperbolic routing
52 if ( pnlsr.getConfParameter().getIsHyperbolicCalc() == 2 )
53 {
54 calculateHypDryRoutingTable(pnlsr);
55 }
56 //need to update NPT here
akmhoque05d5fcf2014-04-15 14:58:45 -050057 pnlsr.getNpt().updateWithNewRoute(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -050058 //debugging purpose
59 printRoutingTable();
akmhoque05d5fcf2014-04-15 14:58:45 -050060 pnlsr.getNpt().print();
61 pnlsr.getFib().print();
akmhoque5a44dd42014-03-12 18:11:32 -050062 //debugging purpose end
akmhoqueba094742014-02-28 11:47:21 -060063 }
64 else
65 {
akmhoque5a44dd42014-03-12 18:11:32 -050066 cout<<"Adjacency building is scheduled, so ";
67 cout<<"routing table can not be calculated :("<<endl;
akmhoqueba094742014-02-28 11:47:21 -060068 }
akmhoque5a44dd42014-03-12 18:11:32 -050069 }
70 else
71 {
72 cout<<"No Adj LSA of router itself,";
73 cout<< " so Routing table can not be calculated :("<<endl;
74 clearRoutingTable();
75 clearDryRoutingTable(); // for dry run options
76 // need to update NPT here
77 std::cout<<"Calling Update NPT With new Route"<<std::endl;
akmhoque05d5fcf2014-04-15 14:58:45 -050078 pnlsr.getNpt().updateWithNewRoute(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -050079 //debugging purpose
80 printRoutingTable();
akmhoque05d5fcf2014-04-15 14:58:45 -050081 pnlsr.getNpt().print();
82 pnlsr.getFib().print();
akmhoque5a44dd42014-03-12 18:11:32 -050083 //debugging purpose end
84 }
85 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
86 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
akmhoqueba094742014-02-28 11:47:21 -060087 }
akmhoque5a44dd42014-03-12 18:11:32 -050088 else
akmhoqueba094742014-02-28 11:47:21 -060089 {
akmhoque5a44dd42014-03-12 18:11:32 -050090 scheduleRoutingTableCalculation(pnlsr);
akmhoqueba094742014-02-28 11:47:21 -060091 }
akmhoque5a44dd42014-03-12 18:11:32 -050092 }
akmhoqueba094742014-02-28 11:47:21 -060093
akmhoqueba094742014-02-28 11:47:21 -060094
akmhoque5a44dd42014-03-12 18:11:32 -050095 void
96 RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
97 {
98 cout<<"RoutingTable::calculateLsRoutingTable Called"<<endl;
99 Map vMap;
akmhoque05d5fcf2014-04-15 14:58:45 -0500100 vMap.createFromAdjLsdb(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -0500101 int numOfRouter=vMap.getMapSize();
102 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
103 lsrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
104 }
akmhoqueba094742014-02-28 11:47:21 -0600105
akmhoque5a44dd42014-03-12 18:11:32 -0500106 void
107 RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
108 {
109 Map vMap;
akmhoque05d5fcf2014-04-15 14:58:45 -0500110 vMap.createFromAdjLsdb(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -0500111 int numOfRouter=vMap.getMapSize();
112 HypRoutingTableCalculator hrtc(numOfRouter,0);
113 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
114 }
akmhoqueba094742014-02-28 11:47:21 -0600115
akmhoque5a44dd42014-03-12 18:11:32 -0500116 void
117 RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
118 {
119 Map vMap;
akmhoque05d5fcf2014-04-15 14:58:45 -0500120 vMap.createFromAdjLsdb(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -0500121 int numOfRouter=vMap.getMapSize();
122 HypRoutingTableCalculator hrtc(numOfRouter,1);
123 hrtc.calculatePath(vMap,boost::ref(*this),pnlsr);
124 }
125
126 void
127 RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
128 {
129 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
akmhoqueba094742014-02-28 11:47:21 -0600130 {
akmhoque5a44dd42014-03-12 18:11:32 -0500131 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
132 ndn::bind(&RoutingTable::calculate,this,boost::ref(pnlsr)));
133 pnlsr.setIsRouteCalculationScheduled(1);
akmhoqueba094742014-02-28 11:47:21 -0600134 }
akmhoque5a44dd42014-03-12 18:11:32 -0500135 }
136
137 static bool
138 routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
139 {
140 return rte.getDestination()==destRouter;
141 }
akmhoqueba094742014-02-28 11:47:21 -0600142
143// function related to manipulation of routing table
akmhoque5a44dd42014-03-12 18:11:32 -0500144 void
145 RoutingTable::addNextHop(string destRouter, NextHop& nh)
146 {
147 std::pair<RoutingTableEntry&, bool> rte=findRoutingTableEntry(destRouter);
148 if( !rte.second )
akmhoqueba094742014-02-28 11:47:21 -0600149 {
akmhoque5a44dd42014-03-12 18:11:32 -0500150 RoutingTableEntry rte(destRouter);
151 rte.getNhl().addNextHop(nh);
akmhoque05d5fcf2014-04-15 14:58:45 -0500152 m_rTable.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -0600153 }
akmhoque5a44dd42014-03-12 18:11:32 -0500154 else
155 {
156 (rte.first).getNhl().addNextHop(nh);
157 }
158 }
akmhoqueba094742014-02-28 11:47:21 -0600159
akmhoque5a44dd42014-03-12 18:11:32 -0500160 std::pair<RoutingTableEntry&, bool>
161 RoutingTable::findRoutingTableEntry(string destRouter)
162 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500163 std::list<RoutingTableEntry >::iterator it = std::find_if( m_rTable.begin(),
164 m_rTable.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500165 bind(&routingTableEntryCompare, _1, destRouter));
akmhoque05d5fcf2014-04-15 14:58:45 -0500166 if ( it != m_rTable.end() )
akmhoqueba094742014-02-28 11:47:21 -0600167 {
akmhoque5a44dd42014-03-12 18:11:32 -0500168 return std::make_pair(boost::ref((*it)),true);
akmhoqueba094742014-02-28 11:47:21 -0600169 }
akmhoque5a44dd42014-03-12 18:11:32 -0500170 RoutingTableEntry rteEmpty;
171 return std::make_pair(boost::ref(rteEmpty),false);
172 }
akmhoqueba094742014-02-28 11:47:21 -0600173
akmhoque5a44dd42014-03-12 18:11:32 -0500174 void
175 RoutingTable::printRoutingTable()
176 {
177 cout<<"---------------Routing Table------------------"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -0500178 for(std::list<RoutingTableEntry>::iterator it=m_rTable.begin() ;
179 it != m_rTable.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -0600180 {
akmhoque5a44dd42014-03-12 18:11:32 -0500181 cout<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600182 }
akmhoque5a44dd42014-03-12 18:11:32 -0500183 }
akmhoqueba094742014-02-28 11:47:21 -0600184
185
186//function related to manipulation of dry routing table
akmhoque5a44dd42014-03-12 18:11:32 -0500187 void
188 RoutingTable::addNextHopToDryTable(string destRouter, NextHop& nh)
189 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500190 std::list<RoutingTableEntry >::iterator it = std::find_if( m_dryTable.begin(),
191 m_dryTable.end(),
akmhoque5a44dd42014-03-12 18:11:32 -0500192 bind(&routingTableEntryCompare, _1, destRouter));
akmhoque05d5fcf2014-04-15 14:58:45 -0500193 if ( it == m_dryTable.end() )
akmhoqueba094742014-02-28 11:47:21 -0600194 {
akmhoque5a44dd42014-03-12 18:11:32 -0500195 RoutingTableEntry rte(destRouter);
196 rte.getNhl().addNextHop(nh);
akmhoque05d5fcf2014-04-15 14:58:45 -0500197 m_dryTable.push_back(rte);
akmhoqueba094742014-02-28 11:47:21 -0600198 }
akmhoque5a44dd42014-03-12 18:11:32 -0500199 else
200 {
201 (*it).getNhl().addNextHop(nh);
202 }
203 }
akmhoqueba094742014-02-28 11:47:21 -0600204
akmhoque5a44dd42014-03-12 18:11:32 -0500205 void
206 RoutingTable::printDryRoutingTable()
207 {
208 cout<<"--------Dry Run's Routing Table--------------"<<endl;
akmhoque05d5fcf2014-04-15 14:58:45 -0500209 for(std::list<RoutingTableEntry>::iterator it=m_dryTable.begin() ;
210 it != m_dryTable.end(); ++it)
akmhoqueba094742014-02-28 11:47:21 -0600211 {
akmhoque5a44dd42014-03-12 18:11:32 -0500212 cout<<(*it)<<endl;
akmhoqueba094742014-02-28 11:47:21 -0600213 }
akmhoque5a44dd42014-03-12 18:11:32 -0500214 }
akmhoqueba094742014-02-28 11:47:21 -0600215
216
akmhoque5a44dd42014-03-12 18:11:32 -0500217 void
218 RoutingTable::clearRoutingTable()
219 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500220 if( m_rTable.size() > 0 )
akmhoqueba094742014-02-28 11:47:21 -0600221 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500222 m_rTable.clear();
akmhoqueba094742014-02-28 11:47:21 -0600223 }
akmhoque5a44dd42014-03-12 18:11:32 -0500224 }
akmhoqueba094742014-02-28 11:47:21 -0600225
akmhoque5a44dd42014-03-12 18:11:32 -0500226 void
227 RoutingTable::clearDryRoutingTable()
228 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500229 if (m_dryTable.size()>0 )
akmhoqueba094742014-02-28 11:47:21 -0600230 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500231 m_dryTable.clear();
akmhoqueba094742014-02-28 11:47:21 -0600232 }
akmhoque5a44dd42014-03-12 18:11:32 -0500233 }
akmhoqueba094742014-02-28 11:47:21 -0600234
235}//namespace nlsr
236