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-2021, 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 
27 namespace nlsr {
28 
30 
31 bool
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 
43 AdjacencyList::getAdjacent(const ndn::Name& adjName)
44 {
45  Adjacent adj(adjName);
46  std::list<Adjacent>::iterator it = find(adjName);
47  if (it != m_adjList.end()) {
48  return (*it);
49  }
50  return adj;
51 }
52 
53 bool
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 
64  return ourSet == theirSet;
65 }
66 
67 bool
68 AdjacencyList::isNeighbor(const ndn::Name& adjName) const
69 {
70  std::list<Adjacent>::const_iterator it = find(adjName);
71  if (it == m_adjList.end())
72  {
73  return false;
74  }
75  return true;
76 }
77 
78 void
80 {
81  std::list<Adjacent>::iterator it = find(neighbor);
82  if (it == m_adjList.end()) {
83  return ;
84  }
85  (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
86 }
87 
88 void
89 AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
90  uint32_t count)
91 {
92  std::list<Adjacent>::iterator it = find(neighbor);
93  if (it != m_adjList.end()) {
94  (*it).setInterestTimedOutNo(count);
95  }
96 }
97 
98 int32_t
99 AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
100 {
101  std::list<Adjacent>::const_iterator it = find(neighbor);
102  if (it == m_adjList.end()) {
103  return -1;
104  }
105  return (*it).getInterestTimedOutNo();
106 }
107 
109 AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
110 {
111  std::list<Adjacent>::const_iterator it = find(neighbor);
112 
113  if (it == m_adjList.end()) {
115  }
116  else {
117  return it->getStatus();
118  }
119 }
120 
121 void
122 AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
123 {
124  std::list<Adjacent>::iterator it = find(neighbor);
125  if (it != m_adjList.end()) {
126  it->setStatus(status);
127  }
128 }
129 
130 std::list<Adjacent>&
132 {
133  return m_adjList;
134 }
135 
136 const std::list<Adjacent>&
138 {
139  return m_adjList;
140 }
141 
142 bool
143 AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
144 {
145  uint32_t nTimedOutNeighbors = 0;
146 
147  for (const Adjacent& adjacency : m_adjList) {
148 
149  if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
150  return true;
151  }
152  else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
153  nTimedOutNeighbors++;
154  }
155  }
156 
157  if (nTimedOutNeighbors == m_adjList.size()) {
158  return true;
159  }
160  else {
161  return false;
162  }
163 }
164 
165 int32_t
167 {
168  int32_t actNbrCount = 0;
169  for (const auto& adjacent: m_adjList) {
170  if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
171  actNbrCount++;
172  }
173  }
174  return actNbrCount;
175 }
176 
177 std::list<Adjacent>::iterator
178 AdjacencyList::find(const ndn::Name& adjName)
179 {
180  return std::find_if(m_adjList.begin(),
181  m_adjList.end(),
182  std::bind(&Adjacent::compare, _1, std::cref(adjName)));
183 }
184 
185 std::list<Adjacent>::const_iterator
186 AdjacencyList::find(const ndn::Name& adjName) const
187 {
188  return std::find_if(m_adjList.cbegin(),
189  m_adjList.cend(),
190  std::bind(&Adjacent::compare, _1, std::cref(adjName)));
191 }
192 
194 AdjacencyList::findAdjacent(const ndn::Name& adjName)
195 {
196  return std::find_if(m_adjList.begin(),
197  m_adjList.end(),
198  std::bind(&Adjacent::compare,
199  _1, std::cref(adjName)));
200 }
201 
204 {
205  return std::find_if(m_adjList.begin(),
206  m_adjList.end(),
207  std::bind(&Adjacent::compareFaceId,
208  _1, faceId));
209 }
210 
212 AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
213 {
214  return std::find_if(m_adjList.begin(),
215  m_adjList.end(),
216  std::bind(&Adjacent::compareFaceUri,
217  _1, faceUri));
218 }
219 
220 uint64_t
221 AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
222 {
223  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
224  m_adjList.end(),
225  std::bind(&Adjacent::compareFaceUri,
226  _1, faceUri));
227 
228  return it != m_adjList.end() ? it->getFaceId() : 0;
229 }
230 
231 void
233 {
234  NLSR_LOG_DEBUG("-------Adjacency List--------");
235  for (const auto& adjacent : m_adjList) {
236  NLSR_LOG_DEBUG(adjacent);
237  }
238 }
239 
240 } // namespace nlsr
bool operator==(const AdjacencyList &adl) const
void setTimedOutInterestCount(const ndn::Name &neighbor, uint32_t count)
bool isNeighbor(const ndn::Name &adjName) const
const ndn::Name & getName() const
Definition: adjacent.hpp:72
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
bool insert(const Adjacent &adjacent)
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
int32_t getNumOfActiveNeighbor() const
Adjacent getAdjacent(const ndn::Name &adjName)
int32_t getTimedOutInterestCount(const ndn::Name &neighbor) const
#define INIT_LOGGER(name)
Definition: logger.hpp:35
bool compare(const ndn::Name &adjacencyName) const
Definition: adjacent.hpp:158
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor) const
bool compareFaceUri(const ndn::FaceUri &faceUri) const
Definition: adjacent.hpp:170
A neighbor reachable over a Face.
Definition: adjacent.hpp:46
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
bool isAdjLsaBuildable(const uint32_t interestRetryNo) const
Determines whether this list can be used to build an adj. LSA.
bool compareFaceId(const uint64_t faceId) const
Definition: adjacent.hpp:164
std::list< Adjacent >::iterator iterator
void incrementTimedOutInterestCount(const ndn::Name &neighbor)
uint64_t getFaceId(const ndn::FaceUri &faceUri)
std::list< Adjacent > & getAdjList()
void setStatusOfNeighbor(const ndn::Name &neighbor, Adjacent::Status status)