blob: cb192ebf910d305645c1e6552e10f0eac7e605a3 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventod90338d2021-01-07 17:50:05 -05002/*
Davide Pesaventofd1e9402023-11-13 15:40:41 -05003 * Copyright (c) 2014-2023, The University of Memphis,
Vince Lehmanf7eec4f2015-05-08 19:02:31 -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 Lehman0a7da612014-10-29 14:39:29 -050021
akmhoquec8a10f72014-04-25 18:42:55 -050022#include "adjacency-list.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050023#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050024
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050025#include <algorithm>
26
akmhoque53353462014-04-22 08:43:45 -050027namespace nlsr {
28
dmcoomescf8d0ed2017-02-21 11:39:01 -060029INIT_LOGGER(AdjacencyList);
akmhoque674b0b12014-05-20 14:33:28 -050030
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080031bool
Davide Pesaventod90338d2021-01-07 17:50:05 -050032AdjacencyList::insert(const Adjacent& adjacent)
akmhoque53353462014-04-22 08:43:45 -050033{
Davide Pesaventod90338d2021-01-07 17:50:05 -050034 auto it = find(adjacent.getName());
akmhoque157b0a42014-05-13 00:26:37 -050035 if (it != m_adjList.end()) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080036 return false;
akmhoque53353462014-04-22 08:43:45 -050037 }
akmhoquefdbddb12014-05-02 18:35:19 -050038 m_adjList.push_back(adjacent);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080039 return true;
akmhoque53353462014-04-22 08:43:45 -050040}
41
akmhoque53353462014-04-22 08:43:45 -050042Adjacent
akmhoque31d1d4b2014-05-05 22:08:14 -050043AdjacencyList::getAdjacent(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -050044{
45 Adjacent adj(adjName);
Davide Pesaventofd1e9402023-11-13 15:40:41 -050046 auto it = find(adjName);
akmhoque157b0a42014-05-13 00:26:37 -050047 if (it != m_adjList.end()) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -050048 return *it;
akmhoque53353462014-04-22 08:43:45 -050049 }
50 return adj;
51}
52
akmhoque53353462014-04-22 08:43:45 -050053bool
Nick Gordon49648702018-01-22 11:57:33 -060054AdjacencyList::operator==(const AdjacencyList& adl) const
akmhoque53353462014-04-22 08:43:45 -050055{
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050056 auto theirList = adl.getAdjList();
57 if (m_adjList.size() != theirList.size()) {
akmhoque53353462014-04-22 08:43:45 -050058 return false;
59 }
Nick Gordon2a1ac612017-10-06 15:36:49 -050060
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050061 std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend());
62 std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend());
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050063 return ourSet == theirSet;
akmhoque53353462014-04-22 08:43:45 -050064}
65
akmhoque53353462014-04-22 08:43:45 -050066bool
Nick Gordon49648702018-01-22 11:57:33 -060067AdjacencyList::isNeighbor(const ndn::Name& adjName) const
akmhoque53353462014-04-22 08:43:45 -050068{
Davide Pesaventofd1e9402023-11-13 15:40:41 -050069 return find(adjName) != m_adjList.end();
akmhoque53353462014-04-22 08:43:45 -050070}
71
72void
akmhoque31d1d4b2014-05-05 22:08:14 -050073AdjacencyList::incrementTimedOutInterestCount(const ndn::Name& neighbor)
akmhoque53353462014-04-22 08:43:45 -050074{
Davide Pesaventofd1e9402023-11-13 15:40:41 -050075 auto it = find(neighbor);
76 if (it != m_adjList.end()) {
77 it->setInterestTimedOutNo(it->getInterestTimedOutNo() + 1);
akmhoque53353462014-04-22 08:43:45 -050078 }
akmhoque53353462014-04-22 08:43:45 -050079}
80
81void
Davide Pesaventofd1e9402023-11-13 15:40:41 -050082AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, uint32_t count)
akmhoque53353462014-04-22 08:43:45 -050083{
Davide Pesaventofd1e9402023-11-13 15:40:41 -050084 auto it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050085 if (it != m_adjList.end()) {
Davide Pesaventofd1e9402023-11-13 15:40:41 -050086 it->setInterestTimedOutNo(count);
akmhoque53353462014-04-22 08:43:45 -050087 }
88}
89
akmhoquefdbddb12014-05-02 18:35:19 -050090int32_t
Nick Gordon49648702018-01-22 11:57:33 -060091AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -050092{
Davide Pesaventofd1e9402023-11-13 15:40:41 -050093 auto it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -050094 if (it == m_adjList.end()) {
akmhoque53353462014-04-22 08:43:45 -050095 return -1;
96 }
Davide Pesaventofd1e9402023-11-13 15:40:41 -050097 return it->getInterestTimedOutNo();
akmhoque53353462014-04-22 08:43:45 -050098}
99
Vince Lehmancb76ade2014-08-28 21:24:41 -0500100Adjacent::Status
Nick Gordon49648702018-01-22 11:57:33 -0600101AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
akmhoque53353462014-04-22 08:43:45 -0500102{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500103 auto it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500104 if (it == m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500105 return Adjacent::STATUS_UNKNOWN;
akmhoque53353462014-04-22 08:43:45 -0500106 }
Vince Lehmancb76ade2014-08-28 21:24:41 -0500107 else {
108 return it->getStatus();
109 }
akmhoque53353462014-04-22 08:43:45 -0500110}
111
112void
Vince Lehmancb76ade2014-08-28 21:24:41 -0500113AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
akmhoque53353462014-04-22 08:43:45 -0500114{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500115 auto it = find(neighbor);
akmhoque157b0a42014-05-13 00:26:37 -0500116 if (it != m_adjList.end()) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500117 it->setStatus(status);
akmhoque53353462014-04-22 08:43:45 -0500118 }
119}
120
121std::list<Adjacent>&
akmhoquec8a10f72014-04-25 18:42:55 -0500122AdjacencyList::getAdjList()
akmhoque53353462014-04-22 08:43:45 -0500123{
124 return m_adjList;
125}
126
Nick Gordon22b5c952017-08-10 17:48:15 -0500127const std::list<Adjacent>&
128AdjacencyList::getAdjList() const
129{
130 return m_adjList;
131}
132
akmhoque53353462014-04-22 08:43:45 -0500133bool
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500134AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
akmhoque53353462014-04-22 08:43:45 -0500135{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500136 uint32_t nTimedOutNeighbors = 0;
Vince Lehmancb76ade2014-08-28 21:24:41 -0500137
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500138 for (const auto& adjacency : m_adjList) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500139 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
140 return true;
akmhoque53353462014-04-22 08:43:45 -0500141 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500142 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
143 nTimedOutNeighbors++;
akmhoque53353462014-04-22 08:43:45 -0500144 }
145 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500146
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500147 return nTimedOutNeighbors == m_adjList.size();
akmhoque53353462014-04-22 08:43:45 -0500148}
149
akmhoquefdbddb12014-05-02 18:35:19 -0500150int32_t
Nick Gordon49648702018-01-22 11:57:33 -0600151AdjacencyList::getNumOfActiveNeighbor() const
akmhoque53353462014-04-22 08:43:45 -0500152{
akmhoquefdbddb12014-05-02 18:35:19 -0500153 int32_t actNbrCount = 0;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500154 for (const auto& adjacent: m_adjList) {
155 if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500156 actNbrCount++;
157 }
158 }
159 return actNbrCount;
160}
161
162std::list<Adjacent>::iterator
akmhoque31d1d4b2014-05-05 22:08:14 -0500163AdjacencyList::find(const ndn::Name& adjName)
akmhoque53353462014-04-22 08:43:45 -0500164{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500165 return std::find_if(m_adjList.begin(), m_adjList.end(),
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500166 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
akmhoque53353462014-04-22 08:43:45 -0500167}
168
Nick Gordon49648702018-01-22 11:57:33 -0600169std::list<Adjacent>::const_iterator
170AdjacencyList::find(const ndn::Name& adjName) const
171{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500172 return std::find_if(m_adjList.cbegin(), m_adjList.cend(),
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500173 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
Nick Gordon49648702018-01-22 11:57:33 -0600174}
175
Nick Gordonc780a692017-04-27 18:03:02 -0500176AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500177AdjacencyList::findAdjacent(const ndn::Name& adjName)
178{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500179 return std::find_if(m_adjList.begin(), m_adjList.end(),
180 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
akmhoquec04e7272014-07-02 11:00:14 -0500181}
182
Nick Gordonc780a692017-04-27 18:03:02 -0500183AdjacencyList::iterator
akmhoquec04e7272014-07-02 11:00:14 -0500184AdjacencyList::findAdjacent(uint64_t faceId)
185{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500186 return std::find_if(m_adjList.begin(), m_adjList.end(),
187 std::bind(&Adjacent::compareFaceId, _1, faceId));
akmhoquec04e7272014-07-02 11:00:14 -0500188}
189
Nick Gordone9733ed2017-04-26 10:48:39 -0500190AdjacencyList::iterator
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500191AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
Nick Gordone9733ed2017-04-26 10:48:39 -0500192{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500193 return std::find_if(m_adjList.begin(), m_adjList.end(),
194 std::bind(&Adjacent::compareFaceUri, _1, faceUri));
Nick Gordone9733ed2017-04-26 10:48:39 -0500195}
196
akmhoque102aea42014-08-04 10:22:12 -0500197uint64_t
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500198AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
akmhoque102aea42014-08-04 10:22:12 -0500199{
Davide Pesaventofd1e9402023-11-13 15:40:41 -0500200 auto it = std::find_if(m_adjList.begin(), m_adjList.end(),
201 std::bind(&Adjacent::compareFaceUri, _1, faceUri));
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500202 return it != m_adjList.end() ? it->getFaceId() : 0;
akmhoque102aea42014-08-04 10:22:12 -0500203}
204
akmhoque674b0b12014-05-20 14:33:28 -0500205void
206AdjacencyList::writeLog()
207{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500208 NLSR_LOG_DEBUG("-------Adjacency List--------");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500209 for (const auto& adjacent : m_adjList) {
210 NLSR_LOG_DEBUG(adjacent);
akmhoque674b0b12014-05-20 14:33:28 -0500211 }
212}
213
Nick Gordonfad8e252016-08-11 14:21:38 -0500214} // namespace nlsr