blob: 21ac00cbd6ae29f2f7c5631e162e0ca4ce1af88f [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"
akmhoque157b0a42014-05-13 00:26:37 -05008#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -05009#include "routing-table-calculator.hpp"
10#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050011#include "name-prefix-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050012#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050013
14namespace nlsr {
15
akmhoque674b0b12014-05-20 14:33:28 -050016INIT_LOGGER("RoutingTable");
17
akmhoque53353462014-04-22 08:43:45 -050018using namespace std;
19
20void
21RoutingTable::calculate(Nlsr& pnlsr)
22{
23 //debugging purpose
akmhoquec8a10f72014-04-25 18:42:55 -050024 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050025 pnlsr.getLsdb().printAdjLsdb();
26 pnlsr.getLsdb().printCorLsdb();
27 pnlsr.getLsdb().printNameLsdb();
akmhoque674b0b12014-05-20 14:33:28 -050028 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050029 if (pnlsr.getIsRoutingTableCalculating() == false) {
30 //setting routing table calculation
31 pnlsr.setIsRoutingTableCalculating(true);
akmhoque53353462014-04-22 08:43:45 -050032 if (pnlsr.getLsdb().doesLsaExist(
akmhoque31d1d4b2014-05-05 22:08:14 -050033 pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
akmhoque157b0a42014-05-13 00:26:37 -050034 std::string("adjacency"))) {
35 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
akmhoque53353462014-04-22 08:43:45 -050036 std::cout << "CLearing old routing table ....." << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050037 _LOG_DEBUG("CLearing old routing table .....");
akmhoque53353462014-04-22 08:43:45 -050038 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050039 // for dry run options
40 clearDryRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050041 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050042 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
43 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050044 calculateLsRoutingTable(pnlsr);
45 }
46 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050047 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050048 calculateHypRoutingTable(pnlsr);
49 }
50 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050051 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050052 calculateHypDryRoutingTable(pnlsr);
53 }
54 //need to update NPT here
akmhoque31d1d4b2014-05-05 22:08:14 -050055 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -050056 //debugging purpose
57 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050058 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050059 pnlsr.getFib().print();
akmhoque674b0b12014-05-20 14:33:28 -050060 writeLog();
61 pnlsr.getNamePrefixTable().writeLog();
62 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050063 //debugging purpose end
64 }
akmhoque157b0a42014-05-13 00:26:37 -050065 else {
akmhoque53353462014-04-22 08:43:45 -050066 std::cout << "Adjacency building is scheduled, so ";
67 std::cout << "routing table can not be calculated :(" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050068 _LOG_DEBUG("Adjacency building is scheduled, so"
69 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050070 }
71 }
akmhoque157b0a42014-05-13 00:26:37 -050072 else {
akmhoque53353462014-04-22 08:43:45 -050073 std::cout << "No Adj LSA of router itself,";
74 std::cout << " so Routing table can not be calculated :(" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050075 _LOG_DEBUG("No Adj LSA of router itself,"
76 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050077 clearRoutingTable();
78 clearDryRoutingTable(); // for dry run options
79 // need to update NPT here
80 std::cout << "Calling Update NPT With new Route" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050081 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -050082 //debugging purpose
83 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050084 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050085 pnlsr.getFib().print();
akmhoque674b0b12014-05-20 14:33:28 -050086 writeLog();
87 pnlsr.getNamePrefixTable().writeLog();
88 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050089 //debugging purpose end
90 }
akmhoque157b0a42014-05-13 00:26:37 -050091 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
92 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -050093 }
akmhoque157b0a42014-05-13 00:26:37 -050094 else {
akmhoque53353462014-04-22 08:43:45 -050095 scheduleRoutingTableCalculation(pnlsr);
96 }
97}
98
99
100void
101RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
102{
103 std::cout << "RoutingTable::calculateLsRoutingTable Called" << std::endl;
104 Map vMap;
105 vMap.createFromAdjLsdb(pnlsr);
106 int numOfRouter = vMap.getMapSize();
107 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500108 lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500109}
110
111void
112RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
113{
114 Map vMap;
115 vMap.createFromAdjLsdb(pnlsr);
116 int numOfRouter = vMap.getMapSize();
117 HypRoutingTableCalculator hrtc(numOfRouter, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500118 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500119}
120
121void
122RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
123{
124 Map vMap;
125 vMap.createFromAdjLsdb(pnlsr);
126 int numOfRouter = vMap.getMapSize();
127 HypRoutingTableCalculator hrtc(numOfRouter, 1);
akmhoque157b0a42014-05-13 00:26:37 -0500128 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500129}
130
131void
132RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
133{
akmhoque157b0a42014-05-13 00:26:37 -0500134 if (pnlsr.getIsRouteCalculationScheduled() != true) {
akmhoque53353462014-04-22 08:43:45 -0500135 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
akmhoque157b0a42014-05-13 00:26:37 -0500136 ndn::bind(&RoutingTable::calculate, this,
137 ndn::ref(pnlsr)));
138 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500139 }
140}
141
142static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500143routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500144{
145 return rte.getDestination() == destRouter;
146}
147
148// function related to manipulation of routing table
149void
akmhoque31d1d4b2014-05-05 22:08:14 -0500150RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500151{
akmhoqueb6450b12014-04-24 00:01:03 -0500152 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500153 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500154 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500155 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500156 m_rTable.push_back(rte);
157 }
akmhoque157b0a42014-05-13 00:26:37 -0500158 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500159 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500160 }
161}
162
akmhoqueb6450b12014-04-24 00:01:03 -0500163RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500164RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500165{
166 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
167 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500168 ndn::bind(&routingTableEntryCompare,
169 _1, destRouter));
170 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500171 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500172 }
akmhoqueb6450b12014-04-24 00:01:03 -0500173 return 0;
akmhoque53353462014-04-22 08:43:45 -0500174}
175
176void
akmhoque674b0b12014-05-20 14:33:28 -0500177RoutingTable::writeLog()
178{
179 _LOG_DEBUG("---------------Routing Table------------------");
180 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
181 it != m_rTable.end(); ++it) {
182 _LOG_DEBUG("Destination: " << (*it).getDestination());
183 _LOG_DEBUG("Nexthops: ");
184 (*it).getNexthopList().writeLog();
185 }
186}
187
188void
akmhoque53353462014-04-22 08:43:45 -0500189RoutingTable::printRoutingTable()
190{
191 std::cout << "---------------Routing Table------------------" << std::endl;
192 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500193 it != m_rTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500194 std::cout << (*it) << std::endl;
195 }
196}
197
198
199//function related to manipulation of dry routing table
200void
akmhoque31d1d4b2014-05-05 22:08:14 -0500201RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500202{
203 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
204 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500205 ndn::bind(&routingTableEntryCompare,
206 _1, destRouter));
207 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500208 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500209 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500210 m_dryTable.push_back(rte);
211 }
akmhoque157b0a42014-05-13 00:26:37 -0500212 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500213 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500214 }
215}
216
217void
218RoutingTable::printDryRoutingTable()
219{
220 std::cout << "--------Dry Run's Routing Table--------------" << std::endl;
221 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500222 it != m_dryTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500223 cout << (*it) << endl;
224 }
225}
226
227
228void
229RoutingTable::clearRoutingTable()
230{
akmhoque157b0a42014-05-13 00:26:37 -0500231 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500232 m_rTable.clear();
233 }
234}
235
236void
237RoutingTable::clearDryRoutingTable()
238{
akmhoque157b0a42014-05-13 00:26:37 -0500239 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500240 m_dryTable.clear();
241 }
242}
243
244}//namespace nlsr
245