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