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  if (m_adjList.size() != adl.getAdjList().size()) {
77  return false;
78  }
79 
80  std::set<Adjacent> ourList(m_adjList.cbegin(), m_adjList.cend());
81  std::set<Adjacent> theirList(adl.getAdjList().cbegin(), adl.getAdjList().cend());
82 
83  return ourList == theirList;
84 }
85 
86 bool
87 AdjacencyList::isNeighbor(const ndn::Name& adjName) const
88 {
89  std::list<Adjacent>::const_iterator it = find(adjName);
90  if (it == m_adjList.end())
91  {
92  return false;
93  }
94  return true;
95 }
96 
97 void
99 {
100  std::list<Adjacent>::iterator it = find(neighbor);
101  if (it == m_adjList.end()) {
102  return ;
103  }
104  (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
105 }
106 
107 void
108 AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
109  uint32_t count)
110 {
111  std::list<Adjacent>::iterator it = find(neighbor);
112  if (it != m_adjList.end()) {
113  (*it).setInterestTimedOutNo(count);
114  }
115 }
116 
117 int32_t
118 AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor) const
119 {
120  std::list<Adjacent>::const_iterator it = find(neighbor);
121  if (it == m_adjList.end()) {
122  return -1;
123  }
124  return (*it).getInterestTimedOutNo();
125 }
126 
128 AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor) const
129 {
130  std::list<Adjacent>::const_iterator it = find(neighbor);
131 
132  if (it == m_adjList.end()) {
134  }
135  else {
136  return it->getStatus();
137  }
138 }
139 
140 void
141 AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
142 {
143  std::list<Adjacent>::iterator it = find(neighbor);
144  if (it != m_adjList.end()) {
145  it->setStatus(status);
146  }
147 }
148 
149 std::list<Adjacent>&
151 {
152  return m_adjList;
153 }
154 
155 const std::list<Adjacent>&
157 {
158  return m_adjList;
159 }
160 
161 bool
162 AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
163 {
164  uint32_t nTimedOutNeighbors = 0;
165 
166  for (const Adjacent& adjacency : m_adjList) {
167 
168  if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
169  return true;
170  }
171  else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
172  nTimedOutNeighbors++;
173  }
174  }
175 
176  if (nTimedOutNeighbors == m_adjList.size()) {
177  return true;
178  }
179  else {
180  return false;
181  }
182 }
183 
184 int32_t
186 {
187  int32_t actNbrCount = 0;
188  for (std::list<Adjacent>::const_iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
189 
190  if (it->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  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
201  m_adjList.end(),
202  std::bind(&Adjacent::compare,
203  _1, std::cref(adjName)));
204  return it;
205 }
206 
207 std::list<Adjacent>::const_iterator
208 AdjacencyList::find(const ndn::Name& adjName) const
209 {
210  std::list<Adjacent>::const_iterator it = std::find_if(m_adjList.cbegin(),
211  m_adjList.cend(),
212  std::bind(&Adjacent::compare,
213  _1, std::cref(adjName)));
214  return it;
215 }
216 
217 
219 AdjacencyList::findAdjacent(const ndn::Name& adjName)
220 {
221  return std::find_if(m_adjList.begin(),
222  m_adjList.end(),
223  std::bind(&Adjacent::compare,
224  _1, std::cref(adjName)));
225 }
226 
229 {
230  return std::find_if(m_adjList.begin(),
231  m_adjList.end(),
232  std::bind(&Adjacent::compareFaceId,
233  _1, faceId));
234 }
235 
237 AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
238 {
239  return std::find_if(m_adjList.begin(),
240  m_adjList.end(),
241  std::bind(&Adjacent::compareFaceUri,
242  _1, faceUri));
243 }
244 
245 uint64_t
246 AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
247 {
248  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
249  m_adjList.end(),
250  std::bind(&Adjacent::compareFaceUri,
251  _1, faceUri));
252  if (it != m_adjList.end()) {
253  return it->getFaceId();
254  }
255 
256  return 0;
257 }
258 
259 void
261 {
262  NLSR_LOG_DEBUG("-------Adjacency List--------");
263  for (std::list<Adjacent>::iterator it = m_adjList.begin();
264  it != m_adjList.end(); it++) {
265  (*it).writeLog();
266  }
267 }
268 
269 } // 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:143
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-2017, The University of Memphis, Regents of the University of California.
bool compareFaceUri(const ndn::FaceUri &faceUri) const
Definition: adjacent.hpp:155
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-2018, 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:149
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)