blob: bcfb61c525bc939f731aa193a5c330fa089c4702 [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"
akmhoque53353462014-04-22 08:43:45 -050012
13namespace nlsr {
14
15using namespace std;
16
17void
18RoutingTable::calculate(Nlsr& pnlsr)
19{
20 //debugging purpose
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();
akmhoque157b0a42014-05-13 00:26:37 -050025 if (pnlsr.getIsRoutingTableCalculating() == false) {
26 //setting routing table calculation
27 pnlsr.setIsRoutingTableCalculating(true);
akmhoque53353462014-04-22 08:43:45 -050028 if (pnlsr.getLsdb().doesLsaExist(
akmhoque31d1d4b2014-05-05 22:08:14 -050029 pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
akmhoque157b0a42014-05-13 00:26:37 -050030 std::string("adjacency"))) {
31 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
akmhoque53353462014-04-22 08:43:45 -050032 std::cout << "CLearing old routing table ....." << std::endl;
33 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050034 // for dry run options
35 clearDryRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050036 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050037 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
38 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050039 calculateLsRoutingTable(pnlsr);
40 }
41 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050042 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050043 calculateHypRoutingTable(pnlsr);
44 }
45 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050046 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050047 calculateHypDryRoutingTable(pnlsr);
48 }
49 //need to update NPT here
akmhoque31d1d4b2014-05-05 22:08:14 -050050 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -050051 //debugging purpose
52 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050053 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050054 pnlsr.getFib().print();
55 //debugging purpose end
56 }
akmhoque157b0a42014-05-13 00:26:37 -050057 else {
akmhoque53353462014-04-22 08:43:45 -050058 std::cout << "Adjacency building is scheduled, so ";
59 std::cout << "routing table can not be calculated :(" << std::endl;
60 }
61 }
akmhoque157b0a42014-05-13 00:26:37 -050062 else {
akmhoque53353462014-04-22 08:43:45 -050063 std::cout << "No Adj LSA of router itself,";
64 std::cout << " so Routing table can not be calculated :(" << std::endl;
65 clearRoutingTable();
66 clearDryRoutingTable(); // for dry run options
67 // need to update NPT here
68 std::cout << "Calling Update NPT With new Route" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -050069 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -050070 //debugging purpose
71 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050072 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050073 pnlsr.getFib().print();
74 //debugging purpose end
75 }
akmhoque157b0a42014-05-13 00:26:37 -050076 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
77 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -050078 }
akmhoque157b0a42014-05-13 00:26:37 -050079 else {
akmhoque53353462014-04-22 08:43:45 -050080 scheduleRoutingTableCalculation(pnlsr);
81 }
82}
83
84
85void
86RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
87{
88 std::cout << "RoutingTable::calculateLsRoutingTable Called" << std::endl;
89 Map vMap;
90 vMap.createFromAdjLsdb(pnlsr);
91 int numOfRouter = vMap.getMapSize();
92 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque157b0a42014-05-13 00:26:37 -050093 lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -050094}
95
96void
97RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
98{
99 Map vMap;
100 vMap.createFromAdjLsdb(pnlsr);
101 int numOfRouter = vMap.getMapSize();
102 HypRoutingTableCalculator hrtc(numOfRouter, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500103 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500104}
105
106void
107RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
108{
109 Map vMap;
110 vMap.createFromAdjLsdb(pnlsr);
111 int numOfRouter = vMap.getMapSize();
112 HypRoutingTableCalculator hrtc(numOfRouter, 1);
akmhoque157b0a42014-05-13 00:26:37 -0500113 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500114}
115
116void
117RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
118{
akmhoque157b0a42014-05-13 00:26:37 -0500119 if (pnlsr.getIsRouteCalculationScheduled() != true) {
akmhoque53353462014-04-22 08:43:45 -0500120 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
akmhoque157b0a42014-05-13 00:26:37 -0500121 ndn::bind(&RoutingTable::calculate, this,
122 ndn::ref(pnlsr)));
123 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500124 }
125}
126
127static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500128routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500129{
130 return rte.getDestination() == destRouter;
131}
132
133// function related to manipulation of routing table
134void
akmhoque31d1d4b2014-05-05 22:08:14 -0500135RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500136{
akmhoqueb6450b12014-04-24 00:01:03 -0500137 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500138 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500139 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500140 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500141 m_rTable.push_back(rte);
142 }
akmhoque157b0a42014-05-13 00:26:37 -0500143 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500144 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500145 }
146}
147
akmhoqueb6450b12014-04-24 00:01:03 -0500148RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500149RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500150{
151 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
152 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500153 ndn::bind(&routingTableEntryCompare,
154 _1, destRouter));
155 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500156 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500157 }
akmhoqueb6450b12014-04-24 00:01:03 -0500158 return 0;
akmhoque53353462014-04-22 08:43:45 -0500159}
160
161void
162RoutingTable::printRoutingTable()
163{
164 std::cout << "---------------Routing Table------------------" << std::endl;
165 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500166 it != m_rTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500167 std::cout << (*it) << std::endl;
168 }
169}
170
171
172//function related to manipulation of dry routing table
173void
akmhoque31d1d4b2014-05-05 22:08:14 -0500174RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500175{
176 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
177 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500178 ndn::bind(&routingTableEntryCompare,
179 _1, destRouter));
180 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500181 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500182 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500183 m_dryTable.push_back(rte);
184 }
akmhoque157b0a42014-05-13 00:26:37 -0500185 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500186 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500187 }
188}
189
190void
191RoutingTable::printDryRoutingTable()
192{
193 std::cout << "--------Dry Run's Routing Table--------------" << std::endl;
194 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500195 it != m_dryTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500196 cout << (*it) << endl;
197 }
198}
199
200
201void
202RoutingTable::clearRoutingTable()
203{
akmhoque157b0a42014-05-13 00:26:37 -0500204 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500205 m_rTable.clear();
206 }
207}
208
209void
210RoutingTable::clearDryRoutingTable()
211{
akmhoque157b0a42014-05-13 00:26:37 -0500212 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500213 m_dryTable.clear();
214 }
215}
216
217}//namespace nlsr
218