adjacency-list.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "adjacency-list.hpp"
23 
24 #include "adjacent.hpp"
25 #include "common.hpp"
26 #include "logger.hpp"
27 
28 #include <algorithm>
29 
30 namespace nlsr {
31 
33 
34 bool
36 {
37  std::list<Adjacent>::iterator it = find(adjacent.getName());
38  if (it != m_adjList.end()) {
39  return false;
40  }
41  m_adjList.push_back(adjacent);
42  return true;
43 }
44 
46 AdjacencyList::getAdjacent(const ndn::Name& adjName)
47 {
48  Adjacent adj(adjName);
49  std::list<Adjacent>::iterator it = find(adjName);
50  if (it != m_adjList.end()) {
51  return (*it);
52  }
53  return adj;
54 }
55 
56 bool
58 {
59  auto theirList = adl.getAdjList();
60  if (m_adjList.size() != theirList.size()) {
61  return false;
62  }
63 
64  std::set<Adjacent> ourSet(m_adjList.cbegin(), m_adjList.cend());
65  std::set<Adjacent> theirSet(theirList.cbegin(), theirList.cend());
66 
67  return ourSet == theirSet;
68 }
69 
70 bool
71 AdjacencyList::isNeighbor(const ndn::Name& adjName) const
72 {
73  std::list<Adjacent>::const_iterator it = find(adjName);
74  if (it == m_adjList.end())
75  {
76  return false;
77  }
78  return true;
79 }
80 
81 void
83 {
84  std::list<Adjacent>::iterator it = find(neighbor);
85  if (it == m_adjList.end()) {
86  return ;
87  }
88  (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
89 }
90 
91 void
92 AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
93  uint32_t count)
94 {
95  std::list<Adjacent>::iterator it = find(neighbor);
96  if (it != m_adjList.end()) {
97  (*it).setInterestTimedOutNo(count);
98  }
99 }
100 
101 int32_t
102 AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
103 {
104  std::list<Adjacent>::const_iterator it = find(neighbor);
105  if (it == m_adjList.end()) {
106  return -1;
107  }
108  return (*it).getInterestTimedOutNo();
109 }
110 
112 AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
113 {
114  std::list<Adjacent>::const_iterator it = find(neighbor);
115 
116  if (it == m_adjList.end()) {
118  }
119  else {
120  return it->getStatus();
121  }
122 }
123 
124 void
125 AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
126 {
127  std::list<Adjacent>::iterator it = find(neighbor);
128  if (it != m_adjList.end()) {
129  it->setStatus(status);
130  }
131 }
132 
133 std::list<Adjacent>&
135 {
136  return m_adjList;
137 }
138 
139 const std::list<Adjacent>&
141 {
142  return m_adjList;
143 }
144 
145 bool
146 AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
147 {
148  uint32_t nTimedOutNeighbors = 0;
149 
150  for (const Adjacent& adjacency : m_adjList) {
151 
152  if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
153  return true;
154  }
155  else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
156  nTimedOutNeighbors++;
157  }
158  }
159 
160  if (nTimedOutNeighbors == m_adjList.size()) {
161  return true;
162  }
163  else {
164  return false;
165  }
166 }
167 
168 int32_t
170 {
171  int32_t actNbrCount = 0;
172  for (const auto& adjacent: m_adjList) {
173  if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
174  actNbrCount++;
175  }
176  }
177  return actNbrCount;
178 }
179 
180 std::list<Adjacent>::iterator
181 AdjacencyList::find(const ndn::Name& adjName)
182 {
183  return std::find_if(m_adjList.begin(),
184  m_adjList.end(),
185  std::bind(&Adjacent::compare, _1, std::cref(adjName)));
186 }
187 
188 std::list<Adjacent>::const_iterator
189 AdjacencyList::find(const ndn::Name& adjName) const
190 {
191  return std::find_if(m_adjList.cbegin(),
192  m_adjList.cend(),
193  std::bind(&Adjacent::compare, _1, std::cref(adjName)));
194 }
195 
197 AdjacencyList::findAdjacent(const ndn::Name& adjName)
198 {
199  return std::find_if(m_adjList.begin(),
200  m_adjList.end(),
201  std::bind(&Adjacent::compare,
202  _1, std::cref(adjName)));
203 }
204 
207 {
208  return std::find_if(m_adjList.begin(),
209  m_adjList.end(),
210  std::bind(&Adjacent::compareFaceId,
211  _1, faceId));
212 }
213 
215 AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
216 {
217  return std::find_if(m_adjList.begin(),
218  m_adjList.end(),
219  std::bind(&Adjacent::compareFaceUri,
220  _1, faceUri));
221 }
222 
223 uint64_t
224 AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
225 {
226  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
227  m_adjList.end(),
228  std::bind(&Adjacent::compareFaceUri,
229  _1, faceUri));
230 
231  return it != m_adjList.end() ? it->getFaceId() : 0;
232 }
233 
234 void
236 {
237  NLSR_LOG_DEBUG("-------Adjacency List--------");
238  for (const auto& adjacent : m_adjList) {
239  NLSR_LOG_DEBUG(adjacent);
240  }
241 }
242 
243 } // 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:76
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
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
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
bool compare(const ndn::Name &adjacencyName) const
Definition: adjacent.hpp:162
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor) const
bool compareFaceUri(const ndn::FaceUri &faceUri) const
Definition: adjacent.hpp:174
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 insert(Adjacent &adjacent)
bool compareFaceId(const uint64_t faceId) const
Definition: adjacent.hpp:168
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)