blob: f791d0a871af6e81e52476cf78d60243748d8fa8 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
2#include <utility>
3#include <algorithm>
4
akmhoque53353462014-04-22 08:43:45 -05005#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05006#include "name-prefix-table.hpp"
7#include "name-prefix-table-entry.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -05008#include "routing-table.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05009
akmhoque53353462014-04-22 08:43:45 -050010
11
12namespace nlsr {
13
14using namespace std;
15
16static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050017npteCompare(NamePrefixTableEntry& npte, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050018{
19 return npte.getNamePrefix() == name;
20}
21
22
23
24void
akmhoque31d1d4b2014-05-05 22:08:14 -050025NamePrefixTable::addEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050026{
akmhoquefdbddb12014-05-02 18:35:19 -050027 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
28 m_table.end(), bind(&npteCompare, _1, name));
29 if (it == m_table.end())
akmhoque53353462014-04-22 08:43:45 -050030 {
akmhoquec8a10f72014-04-25 18:42:55 -050031 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050032 newEntry.addRoutingTableEntry(rte);
33 newEntry.generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050034 newEntry.getNexthopList().sort();
35 m_table.push_back(newEntry);
36 if (rte.getNexthopList().getSize() > 0)
akmhoque53353462014-04-22 08:43:45 -050037 {
akmhoque31d1d4b2014-05-05 22:08:14 -050038 m_nlsr.getFib().update(name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050039 }
40 }
41 else
42 {
akmhoquefdbddb12014-05-02 18:35:19 -050043 if (rte.getNexthopList().getSize() > 0)
akmhoque53353462014-04-22 08:43:45 -050044 {
45 (*it).addRoutingTableEntry(rte);
46 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050047 (*it).getNexthopList().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -050048 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050049 }
50 else
51 {
52 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050053 (*it).getNexthopList().reset();
akmhoque31d1d4b2014-05-05 22:08:14 -050054 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050055 }
56 }
57}
58
59void
akmhoque31d1d4b2014-05-05 22:08:14 -050060NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050061{
akmhoquefdbddb12014-05-02 18:35:19 -050062 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
63 m_table.end(), bind(&npteCompare, _1, name));
64 if (it != m_table.end())
akmhoque53353462014-04-22 08:43:45 -050065 {
akmhoque31d1d4b2014-05-05 22:08:14 -050066 ndn::Name destRouter = rte.getDestination();
akmhoque53353462014-04-22 08:43:45 -050067 (*it).removeRoutingTableEntry(rte);
68 if (((*it).getRteListSize() == 0) &&
akmhoque31d1d4b2014-05-05 22:08:14 -050069 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/name"),
70 std::string("name"))) &&
71 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/adjacency"),
72 std::string("adjacency"))) &&
73 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/coordinate"),
74 std::string("coordinate"))))
akmhoque53353462014-04-22 08:43:45 -050075 {
akmhoquefdbddb12014-05-02 18:35:19 -050076 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050077 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050078 }
79 else
80 {
81 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -050082 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050083 }
84 }
85}
86
87
88void
akmhoque31d1d4b2014-05-05 22:08:14 -050089NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -050090{
akmhoque31d1d4b2014-05-05 22:08:14 -050091 //
akmhoqueb6450b12014-04-24 00:01:03 -050092 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -050093 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoqueb6450b12014-04-24 00:01:03 -050094 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -050095 {
akmhoque31d1d4b2014-05-05 22:08:14 -050096 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -050097 }
98 else
99 {
100 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500102 }
103}
104
105void
akmhoque31d1d4b2014-05-05 22:08:14 -0500106NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500107{
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 //
akmhoqueb6450b12014-04-24 00:01:03 -0500109 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500110 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoqueb6450b12014-04-24 00:01:03 -0500111 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500112 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500114 }
115 else
116 {
117 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500118 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500119 }
120}
121
122void
akmhoque31d1d4b2014-05-05 22:08:14 -0500123NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500124{
akmhoquefdbddb12014-05-02 18:35:19 -0500125 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
126 it != m_table.end(); ++it)
akmhoque53353462014-04-22 08:43:45 -0500127 {
128 std::list<RoutingTableEntry> rteList = (*it).getRteList();
129 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
130 rteit != rteList.end(); ++rteit)
131 {
akmhoqueb6450b12014-04-24 00:01:03 -0500132 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500133 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoqueb6450b12014-04-24 00:01:03 -0500134 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500135 {
akmhoque31d1d4b2014-05-05 22:08:14 -0500136 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500137 }
138 else
139 {
140 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500141 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500142 }
143 }
144 }
145}
146
147void
akmhoquec8a10f72014-04-25 18:42:55 -0500148NamePrefixTable::print()
akmhoque53353462014-04-22 08:43:45 -0500149{
150 std::cout << "----------------NPT----------------------" << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500151 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
152 it != m_table.end();
akmhoque53353462014-04-22 08:43:45 -0500153 ++it)
154 {
155 cout << (*it) << endl;
156 }
157}
158
159} //namespace nlsr