blob: 2bc827cbcb4bc1d1f52b037cbf4e91c7cde245fe [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <iostream>
2#include <string>
3#include <list>
4
5#include "routing-table.hpp"
6#include "nlsr.hpp"
7#include "map.hpp"
8#include "routing-table-calculator.hpp"
9#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050010#include "name-prefix-table.hpp"
akmhoque53353462014-04-22 08:43:45 -050011
12namespace nlsr {
13
14using namespace std;
15
16void
17RoutingTable::calculate(Nlsr& pnlsr)
18{
19 //debugging purpose
20 std::cout << pnlsr.getConfParameter() << std::endl;
akmhoquec8a10f72014-04-25 18:42:55 -050021 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050022 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 std::cout << "CLearing old routing table ....." << std::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
akmhoquec8a10f72014-04-25 18:42:55 -050053 pnlsr.getNamePrefixTable().updateWithNewRoute(pnlsr);
akmhoque53353462014-04-22 08:43:45 -050054 //debugging purpose
55 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050056 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050057 pnlsr.getFib().print();
58 //debugging purpose end
59 }
60 else
61 {
62 std::cout << "Adjacency building is scheduled, so ";
63 std::cout << "routing table can not be calculated :(" << std::endl;
64 }
65 }
66 else
67 {
68 std::cout << "No Adj LSA of router itself,";
69 std::cout << " so Routing table can not be calculated :(" << std::endl;
70 clearRoutingTable();
71 clearDryRoutingTable(); // for dry run options
72 // need to update NPT here
73 std::cout << "Calling Update NPT With new Route" << std::endl;
akmhoquec8a10f72014-04-25 18:42:55 -050074 pnlsr.getNamePrefixTable().updateWithNewRoute(pnlsr);
akmhoque53353462014-04-22 08:43:45 -050075 //debugging purpose
76 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050077 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050078 pnlsr.getFib().print();
79 //debugging purpose end
80 }
81 pnlsr.setIsRouteCalculationScheduled(0); //clear scheduled flag
82 pnlsr.setIsRoutingTableCalculating(0); //unsetting routing table calculation
83 }
84 else
85 {
86 scheduleRoutingTableCalculation(pnlsr);
87 }
88}
89
90
91void
92RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
93{
94 std::cout << "RoutingTable::calculateLsRoutingTable Called" << std::endl;
95 Map vMap;
96 vMap.createFromAdjLsdb(pnlsr);
97 int numOfRouter = vMap.getMapSize();
98 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
99 lsrtc.calculatePath(vMap, boost::ref(*this), pnlsr);
100}
101
102void
103RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
104{
105 Map vMap;
106 vMap.createFromAdjLsdb(pnlsr);
107 int numOfRouter = vMap.getMapSize();
108 HypRoutingTableCalculator hrtc(numOfRouter, 0);
109 hrtc.calculatePath(vMap, boost::ref(*this), pnlsr);
110}
111
112void
113RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
114{
115 Map vMap;
116 vMap.createFromAdjLsdb(pnlsr);
117 int numOfRouter = vMap.getMapSize();
118 HypRoutingTableCalculator hrtc(numOfRouter, 1);
119 hrtc.calculatePath(vMap, boost::ref(*this), pnlsr);
120}
121
122void
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
133static bool
134routingTableEntryCompare(RoutingTableEntry& rte, string& destRouter)
135{
136 return rte.getDestination() == destRouter;
137}
138
139// function related to manipulation of routing table
140void
141RoutingTable::addNextHop(string destRouter, NextHop& nh)
142{
akmhoqueb6450b12014-04-24 00:01:03 -0500143 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
144 if (rteChk == 0)
akmhoque53353462014-04-22 08:43:45 -0500145 {
146 RoutingTableEntry rte(destRouter);
147 rte.getNhl().addNextHop(nh);
148 m_rTable.push_back(rte);
149 }
150 else
151 {
akmhoqueb6450b12014-04-24 00:01:03 -0500152 rteChk->getNhl().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500153 }
154}
155
akmhoqueb6450b12014-04-24 00:01:03 -0500156RoutingTableEntry*
157RoutingTable::findRoutingTableEntry(const string destRouter)
akmhoque53353462014-04-22 08:43:45 -0500158{
159 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
160 m_rTable.end(),
161 bind(&routingTableEntryCompare, _1, destRouter));
162 if (it != m_rTable.end())
163 {
akmhoqueb6450b12014-04-24 00:01:03 -0500164 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500165 }
akmhoqueb6450b12014-04-24 00:01:03 -0500166 return 0;
akmhoque53353462014-04-22 08:43:45 -0500167}
168
169void
170RoutingTable::printRoutingTable()
171{
172 std::cout << "---------------Routing Table------------------" << std::endl;
173 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
174 it != m_rTable.end(); ++it)
175 {
176 std::cout << (*it) << std::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(m_dryTable.begin(),
186 m_dryTable.end(),
187 bind(&routingTableEntryCompare, _1, destRouter));
188 if (it == m_dryTable.end())
189 {
190 RoutingTableEntry rte(destRouter);
191 rte.getNhl().addNextHop(nh);
192 m_dryTable.push_back(rte);
193 }
194 else
195 {
196 (*it).getNhl().addNextHop(nh);
197 }
198}
199
200void
201RoutingTable::printDryRoutingTable()
202{
203 std::cout << "--------Dry Run's Routing Table--------------" << std::endl;
204 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
205 it != m_dryTable.end(); ++it)
206 {
207 cout << (*it) << endl;
208 }
209}
210
211
212void
213RoutingTable::clearRoutingTable()
214{
215 if (m_rTable.size() > 0)
216 {
217 m_rTable.clear();
218 }
219}
220
221void
222RoutingTable::clearDryRoutingTable()
223{
224 if (m_dryTable.size() > 0)
225 {
226 m_dryTable.clear();
227 }
228}
229
230}//namespace nlsr
231