blob: 9e2c19c1b61ffb99815b8a02c08fded79415ef45 [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"
10#include "npt.hpp"
11
12namespace nlsr {
13
14using namespace std;
15
16void
17RoutingTable::calculate(Nlsr& pnlsr)
18{
19 //debugging purpose
20 std::cout << pnlsr.getConfParameter() << std::endl;
21 pnlsr.getNpt().print();
22 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
53 pnlsr.getNpt().updateWithNewRoute(pnlsr);
54 //debugging purpose
55 printRoutingTable();
56 pnlsr.getNpt().print();
57 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;
74 pnlsr.getNpt().updateWithNewRoute(pnlsr);
75 //debugging purpose
76 printRoutingTable();
77 pnlsr.getNpt().print();
78 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{
143 std::pair<RoutingTableEntry&, bool> rte = findRoutingTableEntry(destRouter);
144 if (!rte.second)
145 {
146 RoutingTableEntry rte(destRouter);
147 rte.getNhl().addNextHop(nh);
148 m_rTable.push_back(rte);
149 }
150 else
151 {
152 (rte.first).getNhl().addNextHop(nh);
153 }
154}
155
156std::pair<RoutingTableEntry&, bool>
157RoutingTable::findRoutingTableEntry(string destRouter)
158{
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 {
164 return std::make_pair(boost::ref((*it)), true);
165 }
166 RoutingTableEntry rteEmpty;
167 return std::make_pair(boost::ref(rteEmpty), false);
168}
169
170void
171RoutingTable::printRoutingTable()
172{
173 std::cout << "---------------Routing Table------------------" << std::endl;
174 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
175 it != m_rTable.end(); ++it)
176 {
177 std::cout << (*it) << std::endl;
178 }
179}
180
181
182//function related to manipulation of dry routing table
183void
184RoutingTable::addNextHopToDryTable(string destRouter, NextHop& nh)
185{
186 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
187 m_dryTable.end(),
188 bind(&routingTableEntryCompare, _1, destRouter));
189 if (it == m_dryTable.end())
190 {
191 RoutingTableEntry rte(destRouter);
192 rte.getNhl().addNextHop(nh);
193 m_dryTable.push_back(rte);
194 }
195 else
196 {
197 (*it).getNhl().addNextHop(nh);
198 }
199}
200
201void
202RoutingTable::printDryRoutingTable()
203{
204 std::cout << "--------Dry Run's Routing Table--------------" << std::endl;
205 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
206 it != m_dryTable.end(); ++it)
207 {
208 cout << (*it) << endl;
209 }
210}
211
212
213void
214RoutingTable::clearRoutingTable()
215{
216 if (m_rTable.size() > 0)
217 {
218 m_rTable.clear();
219 }
220}
221
222void
223RoutingTable::clearDryRoutingTable()
224{
225 if (m_dryTable.size() > 0)
226 {
227 m_dryTable.clear();
228 }
229}
230
231}//namespace nlsr
232