blob: a8071bc932928e0d516ae96d38375e79f096b1cf [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"
akmhoque674b0b12014-05-20 14:33:28 -05009#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050010
11namespace nlsr {
12
akmhoque674b0b12014-05-20 14:33:28 -050013INIT_LOGGER("NamePrefixTable");
14
akmhoque53353462014-04-22 08:43:45 -050015using namespace std;
16
17static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050018npteCompare(NamePrefixTableEntry& npte, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050019{
20 return npte.getNamePrefix() == name;
21}
22
23
24
25void
akmhoque31d1d4b2014-05-05 22:08:14 -050026NamePrefixTable::addEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050027{
akmhoquefdbddb12014-05-02 18:35:19 -050028 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050029 m_table.end(),
30 ndn::bind(&npteCompare, _1, name));
31 if (it == m_table.end()) {
akmhoquec8a10f72014-04-25 18:42:55 -050032 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050033 newEntry.addRoutingTableEntry(rte);
34 newEntry.generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050035 newEntry.getNexthopList().sort();
36 m_table.push_back(newEntry);
akmhoque157b0a42014-05-13 00:26:37 -050037 if (rte.getNexthopList().getSize() > 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050038 m_nlsr.getFib().update(name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050039 }
40 }
akmhoque157b0a42014-05-13 00:26:37 -050041 else {
42 if (rte.getNexthopList().getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -050043 (*it).addRoutingTableEntry(rte);
44 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050045 (*it).getNexthopList().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -050046 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050047 }
akmhoque157b0a42014-05-13 00:26:37 -050048 else {
akmhoque53353462014-04-22 08:43:45 -050049 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050050 (*it).getNexthopList().reset();
akmhoque31d1d4b2014-05-05 22:08:14 -050051 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050052 }
53 }
54}
55
56void
akmhoque31d1d4b2014-05-05 22:08:14 -050057NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050058{
akmhoquefdbddb12014-05-02 18:35:19 -050059 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050060 m_table.end(),
61 ndn::bind(&npteCompare, _1, name));
62 if (it != m_table.end()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050063 ndn::Name destRouter = rte.getDestination();
akmhoque53353462014-04-22 08:43:45 -050064 (*it).removeRoutingTableEntry(rte);
65 if (((*it).getRteListSize() == 0) &&
akmhoque31d1d4b2014-05-05 22:08:14 -050066 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/name"),
67 std::string("name"))) &&
68 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/adjacency"),
69 std::string("adjacency"))) &&
70 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/coordinate"),
akmhoque157b0a42014-05-13 00:26:37 -050071 std::string("coordinate")))) {
akmhoquefdbddb12014-05-02 18:35:19 -050072 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050073 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050074 }
akmhoque157b0a42014-05-13 00:26:37 -050075 else {
akmhoque53353462014-04-22 08:43:45 -050076 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -050077 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050078 }
79 }
80}
81
82
83void
akmhoque31d1d4b2014-05-05 22:08:14 -050084NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -050085{
akmhoque31d1d4b2014-05-05 22:08:14 -050086 //
akmhoqueb6450b12014-04-24 00:01:03 -050087 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -050088 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -050089 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050090 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -050091 }
akmhoque157b0a42014-05-13 00:26:37 -050092 else {
akmhoque53353462014-04-22 08:43:45 -050093 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -050094 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -050095 }
96}
97
98void
akmhoque31d1d4b2014-05-05 22:08:14 -050099NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500100{
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 //
akmhoqueb6450b12014-04-24 00:01:03 -0500102 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500106 }
akmhoque157b0a42014-05-13 00:26:37 -0500107 else {
akmhoque53353462014-04-22 08:43:45 -0500108 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500110 }
111}
112
113void
akmhoque31d1d4b2014-05-05 22:08:14 -0500114NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500115{
akmhoquefdbddb12014-05-02 18:35:19 -0500116 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500117 it != m_table.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500118 std::list<RoutingTableEntry> rteList = (*it).getRteList();
119 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500120 rteit != rteList.end(); ++rteit) {
akmhoqueb6450b12014-04-24 00:01:03 -0500121 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoque157b0a42014-05-13 00:26:37 -0500123 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500125 }
akmhoque157b0a42014-05-13 00:26:37 -0500126 else {
akmhoque53353462014-04-22 08:43:45 -0500127 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500129 }
130 }
131 }
132}
133
134void
akmhoque674b0b12014-05-20 14:33:28 -0500135NamePrefixTable::writeLog()
136{
137 _LOG_DEBUG("----------------NPT----------------------");
138 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
139 it != m_table.end();
140 ++it) {
141 (*it).writeLog();
142 }
143}
144
145void
akmhoquec8a10f72014-04-25 18:42:55 -0500146NamePrefixTable::print()
akmhoque53353462014-04-22 08:43:45 -0500147{
148 std::cout << "----------------NPT----------------------" << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500149 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
150 it != m_table.end();
akmhoque157b0a42014-05-13 00:26:37 -0500151 ++it) {
akmhoque53353462014-04-22 08:43:45 -0500152 cout << (*it) << endl;
153 }
154}
155
156} //namespace nlsr