blob: 98901013fa9321088b2a270d085f690830d22226 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento4b9d30f2020-05-01 02:48:34 -04002/*
3 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehmancae33b62015-06-05 09:21:30 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmancae33b62015-06-05 09:21:30 -050021
22#include "name-prefix-table-entry.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050023
24#include "common.hpp"
akmhoque53353462014-04-22 08:43:45 -050025#include "nexthop.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050026#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050027
28namespace nlsr {
29
dmcoomescf8d0ed2017-02-21 11:39:01 -060030INIT_LOGGER(route.NamePrefixTableEntry);
akmhoque674b0b12014-05-20 14:33:28 -050031
akmhoque53353462014-04-22 08:43:45 -050032void
akmhoquec8a10f72014-04-25 18:42:55 -050033NamePrefixTableEntry::generateNhlfromRteList()
akmhoque53353462014-04-22 08:43:45 -050034{
akmhoquefdbddb12014-05-02 18:35:19 -050035 m_nexthopList.reset();
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050036 for (auto iterator = m_rteList.begin(); iterator != m_rteList.end(); ++iterator) {
37 for (auto nhItr = (*iterator)->getNexthopList().getNextHops().begin();
38 nhItr != (*iterator)->getNexthopList().getNextHops().end();
Nick Gordonb50e51b2016-07-22 16:05:57 -050039 ++nhItr) {
40 m_nexthopList.addNextHop((*nhItr));
akmhoque53353462014-04-22 08:43:45 -050041 }
42 }
43}
44
Nick Gordonb50e51b2016-07-22 16:05:57 -050045uint64_t
Davide Pesavento4b9d30f2020-05-01 02:48:34 -040046NamePrefixTableEntry::removeRoutingTableEntry(std::shared_ptr<RoutingTablePoolEntry> entryPtr)
akmhoque53353462014-04-22 08:43:45 -050047{
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050048 auto iterator = std::find(m_rteList.begin(), m_rteList.end(), entryPtr);
Nick Gordonb50e51b2016-07-22 16:05:57 -050049
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050050 if (iterator != m_rteList.end()) {
51 (*iterator)->decrementUseCount();
Nick Gordonc0c6bcf2017-08-15 18:11:21 -050052 // Remove this NamePrefixEntry from the RoutingTablePoolEntry
53 (*iterator)->namePrefixTableEntries.erase(getNamePrefix());
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050054 m_rteList.erase(iterator);
Nick Gordonb50e51b2016-07-22 16:05:57 -050055 }
56 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -050057 NLSR_LOG_ERROR("Routing entry for: " << entryPtr->getDestination()
Nick Gordonb50e51b2016-07-22 16:05:57 -050058 << " not found in NPT entry: " << getNamePrefix());
59 }
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050060 return entryPtr->getUseCount();
akmhoque53353462014-04-22 08:43:45 -050061}
62
63void
Davide Pesavento4b9d30f2020-05-01 02:48:34 -040064NamePrefixTableEntry::addRoutingTableEntry(std::shared_ptr<RoutingTablePoolEntry> entryPtr)
akmhoque53353462014-04-22 08:43:45 -050065{
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050066 auto iterator = std::find(m_rteList.begin(), m_rteList.end(), entryPtr);
akmhoque53353462014-04-22 08:43:45 -050067
Nick Gordonb50e51b2016-07-22 16:05:57 -050068 // Ensure that this is a new entry
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050069 if (iterator == m_rteList.end()) {
Nick Gordonb50e51b2016-07-22 16:05:57 -050070 // Adding a new routing entry to the NPT entry
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050071 entryPtr->incrementUseCount();
72 m_rteList.push_back(entryPtr);
akmhoque53353462014-04-22 08:43:45 -050073 }
Nick Gordonb50e51b2016-07-22 16:05:57 -050074 // Note: we don't need to update in the else case because these are
75 // pointers, and they are centrally-located in the NPT and will all
76 // be updated there.
akmhoque53353462014-04-22 08:43:45 -050077}
78
akmhoque674b0b12014-05-20 14:33:28 -050079void
80NamePrefixTableEntry::writeLog()
81{
dmcoomes5bcb39e2017-10-31 15:07:55 -050082 NLSR_LOG_DEBUG("Name: " << m_namePrefix);
Nick Gordonb50e51b2016-07-22 16:05:57 -050083 for (auto it = m_rteList.begin(); it != m_rteList.end(); ++it) {
dmcoomes5bcb39e2017-10-31 15:07:55 -050084 NLSR_LOG_DEBUG("Destination: " << (*it)->getDestination());
85 NLSR_LOG_DEBUG("Nexthops: ");
Nick Gordonb50e51b2016-07-22 16:05:57 -050086 (*it)->getNexthopList().writeLog();
akmhoque674b0b12014-05-20 14:33:28 -050087 }
88 m_nexthopList.writeLog();
89}
90
Nick Gordonb50e51b2016-07-22 16:05:57 -050091bool
92operator==(const NamePrefixTableEntry& lhs, const NamePrefixTableEntry& rhs)
93{
Davide Pesavento4b9d30f2020-05-01 02:48:34 -040094 return lhs.getNamePrefix() == rhs.getNamePrefix();
Nick Gordonb50e51b2016-07-22 16:05:57 -050095}
96
97bool
98operator==(const NamePrefixTableEntry& lhs, const ndn::Name& rhs)
99{
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400100 return lhs.getNamePrefix() == rhs;
Nick Gordonb50e51b2016-07-22 16:05:57 -0500101}
102
Vince Lehmancae33b62015-06-05 09:21:30 -0500103std::ostream&
104operator<<(std::ostream& os, const NamePrefixTableEntry& entry)
105{
106 os << "Name: " << entry.getNamePrefix() << "\n";
Davide Pesavento4b9d30f2020-05-01 02:48:34 -0400107 for (const auto& entryPtr : entry.getRteList()) {
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500108 os << "Destination: " << entryPtr->getDestination() << "\n";
Vince Lehmancae33b62015-06-05 09:21:30 -0500109 }
Vince Lehmancae33b62015-06-05 09:21:30 -0500110 return os;
111}
112
113} // namespace nlsr