blob: 61b27224aecc5df9207dfb06ae96e5849d5e9318 [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(),
akmhoque157b0a42014-05-13 00:26:37 -050028 m_table.end(),
29 ndn::bind(&npteCompare, _1, name));
30 if (it == m_table.end()) {
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);
akmhoque157b0a42014-05-13 00:26:37 -050036 if (rte.getNexthopList().getSize() > 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050037 m_nlsr.getFib().update(name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050038 }
39 }
akmhoque157b0a42014-05-13 00:26:37 -050040 else {
41 if (rte.getNexthopList().getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -050042 (*it).addRoutingTableEntry(rte);
43 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050044 (*it).getNexthopList().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -050045 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050046 }
akmhoque157b0a42014-05-13 00:26:37 -050047 else {
akmhoque53353462014-04-22 08:43:45 -050048 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050049 (*it).getNexthopList().reset();
akmhoque31d1d4b2014-05-05 22:08:14 -050050 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050051 }
52 }
53}
54
55void
akmhoque31d1d4b2014-05-05 22:08:14 -050056NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050057{
akmhoquefdbddb12014-05-02 18:35:19 -050058 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050059 m_table.end(),
60 ndn::bind(&npteCompare, _1, name));
61 if (it != m_table.end()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050062 ndn::Name destRouter = rte.getDestination();
akmhoque53353462014-04-22 08:43:45 -050063 (*it).removeRoutingTableEntry(rte);
64 if (((*it).getRteListSize() == 0) &&
akmhoque31d1d4b2014-05-05 22:08:14 -050065 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/name"),
66 std::string("name"))) &&
67 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/adjacency"),
68 std::string("adjacency"))) &&
69 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -050070 std::string("coordinate")))) {
akmhoquefdbddb12014-05-02 18:35:19 -050071 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050072 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050073 }
akmhoque157b0a42014-05-13 00:26:37 -050074 else {
akmhoque53353462014-04-22 08:43:45 -050075 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -050076 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050077 }
78 }
79}
80
81
82void
akmhoque31d1d4b2014-05-05 22:08:14 -050083NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -050084{
akmhoque31d1d4b2014-05-05 22:08:14 -050085 //
akmhoqueb6450b12014-04-24 00:01:03 -050086 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -050087 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -050088 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050089 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -050090 }
akmhoque157b0a42014-05-13 00:26:37 -050091 else {
akmhoque53353462014-04-22 08:43:45 -050092 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -050093 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -050094 }
95}
96
97void
akmhoque31d1d4b2014-05-05 22:08:14 -050098NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -050099{
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 //
akmhoqueb6450b12014-04-24 00:01:03 -0500101 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500103 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500105 }
akmhoque157b0a42014-05-13 00:26:37 -0500106 else {
akmhoque53353462014-04-22 08:43:45 -0500107 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500109 }
110}
111
112void
akmhoque31d1d4b2014-05-05 22:08:14 -0500113NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500114{
akmhoquefdbddb12014-05-02 18:35:19 -0500115 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500116 it != m_table.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500117 std::list<RoutingTableEntry> rteList = (*it).getRteList();
118 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500119 rteit != rteList.end(); ++rteit) {
akmhoqueb6450b12014-04-24 00:01:03 -0500120 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoque157b0a42014-05-13 00:26:37 -0500122 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500124 }
akmhoque157b0a42014-05-13 00:26:37 -0500125 else {
akmhoque53353462014-04-22 08:43:45 -0500126 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500128 }
129 }
130 }
131}
132
133void
akmhoquec8a10f72014-04-25 18:42:55 -0500134NamePrefixTable::print()
akmhoque53353462014-04-22 08:43:45 -0500135{
136 std::cout << "----------------NPT----------------------" << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500137 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
138 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500139 ++it) {
akmhoque53353462014-04-22 08:43:45 -0500140 cout << (*it) << endl;
141 }
142}
143
144} //namespace nlsr