blob: 1d8a90c214e487d4e086e10b85158d3307b14af7 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <list>
24#include <utility>
25#include <algorithm>
26
akmhoque53353462014-04-22 08:43:45 -050027#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050028#include "name-prefix-table.hpp"
29#include "name-prefix-table-entry.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050030#include "routing-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050031#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050032
33namespace nlsr {
34
akmhoque674b0b12014-05-20 14:33:28 -050035INIT_LOGGER("NamePrefixTable");
36
akmhoque53353462014-04-22 08:43:45 -050037using namespace std;
38
39static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050040npteCompare(NamePrefixTableEntry& npte, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050041{
42 return npte.getNamePrefix() == name;
43}
44
45
46
47void
akmhoque31d1d4b2014-05-05 22:08:14 -050048NamePrefixTable::addEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050049{
akmhoquefdbddb12014-05-02 18:35:19 -050050 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050051 m_table.end(),
52 ndn::bind(&npteCompare, _1, name));
53 if (it == m_table.end()) {
akmhoquec8a10f72014-04-25 18:42:55 -050054 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050055 newEntry.addRoutingTableEntry(rte);
56 newEntry.generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050057 newEntry.getNexthopList().sort();
58 m_table.push_back(newEntry);
akmhoque157b0a42014-05-13 00:26:37 -050059 if (rte.getNexthopList().getSize() > 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050060 m_nlsr.getFib().update(name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050061 }
62 }
akmhoque157b0a42014-05-13 00:26:37 -050063 else {
64 if (rte.getNexthopList().getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -050065 (*it).addRoutingTableEntry(rte);
66 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050067 (*it).getNexthopList().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -050068 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050069 }
akmhoque157b0a42014-05-13 00:26:37 -050070 else {
akmhoque53353462014-04-22 08:43:45 -050071 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050072 (*it).getNexthopList().reset();
akmhoque31d1d4b2014-05-05 22:08:14 -050073 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050074 }
75 }
76}
77
78void
akmhoque31d1d4b2014-05-05 22:08:14 -050079NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050080{
akmhoquefdbddb12014-05-02 18:35:19 -050081 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050082 m_table.end(),
83 ndn::bind(&npteCompare, _1, name));
84 if (it != m_table.end()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050085 ndn::Name destRouter = rte.getDestination();
akmhoque53353462014-04-22 08:43:45 -050086 (*it).removeRoutingTableEntry(rte);
87 if (((*it).getRteListSize() == 0) &&
alvy49b1c0c2014-12-19 13:57:46 -060088 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + NameLsa::TYPE_STRING),
89 (NameLsa::TYPE_STRING))) &&
90 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + AdjLsa::TYPE_STRING),
91 (AdjLsa::TYPE_STRING))) &&
92 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + CoordinateLsa::TYPE_STRING),
93 (CoordinateLsa::TYPE_STRING)))) {
akmhoquefdbddb12014-05-02 18:35:19 -050094 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050095 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050096 }
akmhoque157b0a42014-05-13 00:26:37 -050097 else {
akmhoque53353462014-04-22 08:43:45 -050098 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -050099 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -0500100 }
101 }
102}
103
104
105void
akmhoque31d1d4b2014-05-05 22:08:14 -0500106NamePrefixTable::addEntry(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);
akmhoque157b0a42014-05-13 00:26:37 -0500111 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500113 }
akmhoque157b0a42014-05-13 00:26:37 -0500114 else {
akmhoque53353462014-04-22 08:43:45 -0500115 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500116 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500117 }
118}
119
120void
akmhoque31d1d4b2014-05-05 22:08:14 -0500121NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500122{
akmhoque31d1d4b2014-05-05 22:08:14 -0500123 //
akmhoqueb6450b12014-04-24 00:01:03 -0500124 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500126 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500127 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500128 }
akmhoque157b0a42014-05-13 00:26:37 -0500129 else {
akmhoque53353462014-04-22 08:43:45 -0500130 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500131 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500132 }
133}
134
135void
akmhoque31d1d4b2014-05-05 22:08:14 -0500136NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500137{
akmhoquefdbddb12014-05-02 18:35:19 -0500138 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500139 it != m_table.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500140 std::list<RoutingTableEntry> rteList = (*it).getRteList();
141 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500142 rteit != rteList.end(); ++rteit) {
akmhoqueb6450b12014-04-24 00:01:03 -0500143 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500144 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoque157b0a42014-05-13 00:26:37 -0500145 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500147 }
akmhoque157b0a42014-05-13 00:26:37 -0500148 else {
akmhoque53353462014-04-22 08:43:45 -0500149 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500151 }
152 }
153 }
154}
155
156void
akmhoque674b0b12014-05-20 14:33:28 -0500157NamePrefixTable::writeLog()
158{
159 _LOG_DEBUG("----------------NPT----------------------");
160 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
161 it != m_table.end();
162 ++it) {
163 (*it).writeLog();
164 }
165}
166
akmhoque53353462014-04-22 08:43:45 -0500167} //namespace nlsr