Loading...
Searching...
No Matches
adjacency-list.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2024, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
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/>.
20 **/
21
22#include "adjacency-list.hpp"
23#include "logger.hpp"
24
25#include <algorithm>
26
27namespace nlsr {
28
30
31bool
33{
34 auto it = find(adjacent.getName());
35 if (it != m_adjList.end()) {
36 return false;
37 }
38 m_adjList.push_back(adjacent);
39 return true;
40}
41
43AdjacencyList::getAdjacent(const ndn::Name& adjName) const
44{
45 Adjacent adj(adjName);
46 auto it = find(adjName);
47 if (it != m_adjList.end()) {
48 return *it;
49 }
50 return adj;
51}
52
53bool
55{
56 auto theirList = adl.getAdjList();
57 if (m_adjList.size() != theirList.size()) {
58 return false;
59 }
60
61 std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend());
62 std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend());
63 return ourSet == theirSet;
64}
65
66bool
67AdjacencyList::isNeighbor(const ndn::Name& adjName) const
68{
69 return find(adjName) != m_adjList.end();
70}
71
72void
74{
75 auto it = find(neighbor);
76 if (it != m_adjList.end()) {
77 it->setInterestTimedOutNo(it->getInterestTimedOutNo() + 1);
78 }
79}
80
81void
82AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor, uint32_t count)
83{
84 auto it = find(neighbor);
85 if (it != m_adjList.end()) {
86 it->setInterestTimedOutNo(count);
87 }
88}
89
90int32_t
91AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
92{
93 auto it = find(neighbor);
94 if (it == m_adjList.end()) {
95 return -1;
96 }
97 return it->getInterestTimedOutNo();
98}
99
101AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
102{
103 auto it = find(neighbor);
104 if (it == m_adjList.end()) {
106 }
107 else {
108 return it->getStatus();
109 }
110}
111
112void
113AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
114{
115 auto it = find(neighbor);
116 if (it != m_adjList.end()) {
117 it->setStatus(status);
118 }
119}
120
121std::list<Adjacent>&
123{
124 return m_adjList;
125}
126
127const std::list<Adjacent>&
129{
130 return m_adjList;
131}
132
133bool
134AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
135{
136 uint32_t nTimedOutNeighbors = 0;
137
138 for (const auto& adjacency : m_adjList) {
139 if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
140 return true;
141 }
142 else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
143 nTimedOutNeighbors++;
144 }
145 }
146
147 return nTimedOutNeighbors == m_adjList.size();
148}
149
150int32_t
152{
153 int32_t actNbrCount = 0;
154 for (const auto& adjacent: m_adjList) {
155 if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
156 actNbrCount++;
157 }
158 }
159 return actNbrCount;
160}
161
162std::list<Adjacent>::iterator
163AdjacencyList::find(const ndn::Name& adjName)
164{
165 return std::find_if(m_adjList.begin(), m_adjList.end(),
166 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
167}
168
169std::list<Adjacent>::const_iterator
170AdjacencyList::find(const ndn::Name& adjName) const
171{
172 return std::find_if(m_adjList.cbegin(), m_adjList.cend(),
173 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
174}
175
177AdjacencyList::findAdjacent(const ndn::Name& adjName)
178{
179 return std::find_if(m_adjList.begin(), m_adjList.end(),
180 std::bind(&Adjacent::compare, _1, std::cref(adjName)));
181}
182
185{
186 return std::find_if(m_adjList.begin(), m_adjList.end(),
187 std::bind(&Adjacent::compareFaceId, _1, faceId));
188}
189
191AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
192{
193 return std::find_if(m_adjList.begin(), m_adjList.end(),
194 std::bind(&Adjacent::compareFaceUri, _1, faceUri));
195}
196
197uint64_t
198AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
199{
200 auto it = std::find_if(m_adjList.begin(), m_adjList.end(),
201 std::bind(&Adjacent::compareFaceUri, _1, faceUri));
202 return it != m_adjList.end() ? it->getFaceId() : 0;
203}
204
205void
207{
208 NLSR_LOG_DEBUG("-------Adjacency List--------");
209 for (const auto& adjacent : m_adjList) {
210 NLSR_LOG_DEBUG(adjacent);
211 }
212}
213
214} // namespace nlsr
int32_t getNumOfActiveNeighbor() const
int32_t getTimedOutInterestCount(const ndn::Name &neighbor) const
void incrementTimedOutInterestCount(const ndn::Name &neighbor)
void setTimedOutInterestCount(const ndn::Name &neighbor, uint32_t count)
bool operator==(const AdjacencyList &adl) const
std::list< Adjacent >::iterator iterator
uint64_t getFaceId(const ndn::FaceUri &faceUri)
bool isAdjLsaBuildable(const uint32_t interestRetryNo) const
Determines whether this list can be used to build an adj. LSA.
bool insert(const Adjacent &adjacent)
std::list< Adjacent > & getAdjList()
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor) const
bool isNeighbor(const ndn::Name &adjName) const
Adjacent getAdjacent(const ndn::Name &adjName) const
void setStatusOfNeighbor(const ndn::Name &neighbor, Adjacent::Status status)
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
A neighbor reachable over a Face.
Definition adjacent.hpp:47
bool compareFaceUri(const ndn::FaceUri &faceUri) const
Definition adjacent.hpp:170
bool compare(const ndn::Name &adjacencyName) const
Definition adjacent.hpp:158
const ndn::Name & getName() const
Definition adjacent.hpp:72
bool compareFaceId(const uint64_t faceId) const
Definition adjacent.hpp:164
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition logger.hpp:38
#define INIT_LOGGER(name)
Definition logger.hpp:35
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.