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 
32 INIT_LOGGER("AdjacencyList");
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 
62 bool
64 {
65  std::list<Adjacent>::iterator it = find(adjName);
66 
67  if (it == m_adjList.end()) {
68  return false;
69  }
70  else {
71  it->setStatus(s);
72  return true;
73  }
74 }
75 
77 AdjacencyList::getAdjacent(const ndn::Name& adjName)
78 {
79  Adjacent adj(adjName);
80  std::list<Adjacent>::iterator it = find(adjName);
81  if (it != m_adjList.end()) {
82  return (*it);
83  }
84  return adj;
85 }
86 
87 bool
89 {
90  if (size() != adl.size()) {
91  return false;
92  }
93 
94  auto comparator =
95  [] (const Adjacent* lhs, const Adjacent* rhs) {
96  return *lhs < *rhs;
97  };
98 
99  std::vector<const Adjacent*> ourList;
100  std::transform(m_adjList.begin(), m_adjList.end(),
101  std::back_inserter(ourList), std::pointer_traits<const Adjacent*>::pointer_to);
102 
103  std::vector<const Adjacent*> theirList;
104  std::transform(adl.getAdjList().begin(), adl.getAdjList().end(),
105  std::back_inserter(theirList), std::pointer_traits<const Adjacent*>::pointer_to);
106 
107  std::sort(ourList.begin(), ourList.end(), std::bind(comparator, _1, _2));
108  std::sort(theirList.begin(), theirList.end(), std::bind(comparator, _1, _2));
109 
110  for (size_t i = 0; i < ourList.size(); i++) {
111  if (*(ourList[i]) != *(theirList[i])) {
112  return false;
113  }
114  }
115  return true;
116 }
117 
118 int32_t
119 AdjacencyList::updateAdjacentLinkCost(const ndn::Name& adjName, double lc)
120 {
121  std::list<Adjacent>::iterator it = find(adjName);
122  if (it == m_adjList.end()) {
123  return -1;
124  }
125  (*it).setLinkCost(lc);
126  return 0;
127 }
128 
129 bool
130 AdjacencyList::isNeighbor(const ndn::Name& adjName)
131 {
132  std::list<Adjacent>::iterator it = find(adjName);
133  if (it == m_adjList.end())
134  {
135  return false;
136  }
137  return true;
138 }
139 
140 void
142 {
143  std::list<Adjacent>::iterator it = find(neighbor);
144  if (it == m_adjList.end()) {
145  return ;
146  }
147  (*it).setInterestTimedOutNo((*it).getInterestTimedOutNo() + 1);
148 }
149 
150 void
151 AdjacencyList::setTimedOutInterestCount(const ndn::Name& neighbor,
152  uint32_t count)
153 {
154  std::list<Adjacent>::iterator it = find(neighbor);
155  if (it != m_adjList.end()) {
156  (*it).setInterestTimedOutNo(count);
157  }
158 }
159 
160 int32_t
161 AdjacencyList::getTimedOutInterestCount(const ndn::Name& neighbor)
162 {
163  std::list<Adjacent>::iterator it = find(neighbor);
164  if (it == m_adjList.end()) {
165  return -1;
166  }
167  return (*it).getInterestTimedOutNo();
168 }
169 
171 AdjacencyList::getStatusOfNeighbor(const ndn::Name& neighbor)
172 {
173  std::list<Adjacent>::iterator it = find(neighbor);
174 
175  if (it == m_adjList.end()) {
177  }
178  else {
179  return it->getStatus();
180  }
181 }
182 
183 void
184 AdjacencyList::setStatusOfNeighbor(const ndn::Name& neighbor, Adjacent::Status status)
185 {
186  std::list<Adjacent>::iterator it = find(neighbor);
187  if (it != m_adjList.end()) {
188  it->setStatus(status);
189  }
190 }
191 
192 std::list<Adjacent>&
194 {
195  return m_adjList;
196 }
197 
198 const std::list<Adjacent>&
200 {
201  return m_adjList;
202 }
203 
204 bool
205 AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
206 {
207  uint32_t nTimedOutNeighbors = 0;
208 
209  for (const Adjacent& adjacency : m_adjList) {
210 
211  if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
212  return true;
213  }
214  else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
215  nTimedOutNeighbors++;
216  }
217  }
218 
219  if (nTimedOutNeighbors == m_adjList.size()) {
220  return true;
221  }
222  else {
223  return false;
224  }
225 }
226 
227 int32_t
229 {
230  int32_t actNbrCount = 0;
231  for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end(); it++) {
232 
233  if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
234  actNbrCount++;
235  }
236  }
237  return actNbrCount;
238 }
239 
240 std::list<Adjacent>::iterator
241 AdjacencyList::find(const ndn::Name& adjName)
242 {
243  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
244  m_adjList.end(),
245  std::bind(&Adjacent::compare,
246  _1, std::cref(adjName)));
247  return it;
248 }
249 
251 AdjacencyList::findAdjacent(const ndn::Name& adjName)
252 {
253  return std::find_if(m_adjList.begin(),
254  m_adjList.end(),
255  std::bind(&Adjacent::compare,
256  _1, std::cref(adjName)));
257 }
258 
261 {
262  return std::find_if(m_adjList.begin(),
263  m_adjList.end(),
264  std::bind(&Adjacent::compareFaceId,
265  _1, faceId));
266 }
267 
269 AdjacencyList::findAdjacent(const ndn::FaceUri& faceUri)
270 {
271  return std::find_if(m_adjList.begin(),
272  m_adjList.end(),
273  std::bind(&Adjacent::compareFaceUri,
274  _1, faceUri));
275 }
276 
277 uint64_t
278 AdjacencyList::getFaceId(const ndn::FaceUri& faceUri)
279 {
280  std::list<Adjacent>::iterator it = std::find_if(m_adjList.begin(),
281  m_adjList.end(),
282  std::bind(&Adjacent::compareFaceUri,
283  _1, faceUri));
284  if (it != m_adjList.end()) {
285  return it->getFaceId();
286  }
287 
288  return 0;
289 }
290 
291 void
293 {
294  NLSR_LOG_DEBUG("-------Adjacency List--------");
295  for (std::list<Adjacent>::iterator it = m_adjList.begin();
296  it != m_adjList.end(); it++) {
297  (*it).writeLog();
298  }
299 }
300 
301 } // namespace nlsr
void setTimedOutInterestCount(const ndn::Name &neighbor, uint32_t count)
bool updateAdjacentStatus(const ndn::Name &adjName, Adjacent::Status s)
Sets the status of an adjacency.
Adjacent::Status getStatusOfNeighbor(const ndn::Name &neighbor)
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
Copyright (c) 2014-2017, 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)
int32_t updateAdjacentLinkCost(const ndn::Name &adjName, double lc)
bool compare(const ndn::Name &adjacencyName)
Definition: adjacent.hpp:143
#define INIT_LOGGER(name)
Definition: logger.hpp:35
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
const ndn::Name & getName() const
Definition: adjacent.hpp:57
bool isNeighbor(const ndn::Name &adjName)
int32_t getNumOfActiveNeighbor()
bool compareFaceUri(const ndn::FaceUri &faceUri)
Definition: adjacent.hpp:155
int32_t insert(Adjacent &adjacent)
Inserts an adjacency into the list.
A neighbor reachable over a Face.
Definition: adjacent.hpp:38
int32_t getTimedOutInterestCount(const ndn::Name &neighbor)
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
bool compareFaceId(const uint64_t faceId)
Definition: adjacent.hpp:149
void addAdjacents(AdjacencyList &adl)
Copies the adjacencies in a list to this one.
std::list< Adjacent >::iterator iterator
size_t size() const
void incrementTimedOutInterestCount(const ndn::Name &neighbor)
uint64_t getFaceId(const ndn::FaceUri &faceUri)
std::list< Adjacent > & getAdjList()
bool operator==(AdjacencyList &adl) const
void setStatusOfNeighbor(const ndn::Name &neighbor, Adjacent::Status status)