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 (std::list<Adjacent>::const_iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
190 
191  if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
192  actNbrCount++;
193  }
194  }
195  return actNbrCount;
196 }
197 
198 std::list<Adjacent>::iterator
199 AdjacencyList::find(const ndn::Name& adjName)
200 {
201  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
202  m_adjList.end(),
203  std::bind(&Adjacent::compare,
204  _1, std::cref(adjName)));
205  return it;
206 }
207 
208 std::list<Adjacent>::const_iterator
209 AdjacencyList::find(const ndn::Name& adjName) const
210 {
211  std::list<Adjacent>::const_iterator it = std::find_if(m_adjList.cbegin(),
212  m_adjList.cend(),
213  std::bind(&Adjacent::compare,
214  _1, std::cref(adjName)));
215  return it;
216 }
217 
218 
220 AdjacencyList::findAdjacent(const ndn::Name& adjName)
221 {
222  return std::find_if(m_adjList.begin(),
223  m_adjList.end(),
224  std::bind(&Adjacent::compare,
225  _1, std::cref(adjName)));
226 }
227 
230 {
231  return std::find_if(m_adjList.begin(),
232  m_adjList.end(),
233  std::bind(&Adjacent::compareFaceId,
234  _1, faceId));
235 }
236 
238 AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
239 {
240  return std::find_if(m_adjList.begin(),
241  m_adjList.end(),
242  std::bind(&Adjacent::compareFaceUri,
243  _1, faceUri));
244 }
245 
246 uint64_t
247 AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
248 {
249  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
250  m_adjList.end(),
251  std::bind(&Adjacent::compareFaceUri,
252  _1, faceUri));
253  if (it != m_adjList.end()) {
254  return it->getFaceId();
255  }
256 
257  return 0;
258 }
259 
260 void
262 {
263  NLSR_LOG_DEBUG("-------Adjacency List--------");
264  for (std::list<Adjacent>::iterator it = m_adjList.begin();
265  it != m_adjList.end(); it++) {
266  (*it).writeLog();
267  }
268 }
269 
270 } // 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-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: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)