adjacent.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2021, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "adjacent.hpp"
22 #include "logger.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 namespace nlsr {
26 
27 INIT_LOGGER(Adjacent);
28 
29 const double Adjacent::DEFAULT_LINK_COST = 10.0;
30 const double Adjacent::NON_ADJACENT_COST = -12345;
31 
33  : m_name()
34  , m_faceUri()
35  , m_linkCost(DEFAULT_LINK_COST)
36  , m_status(STATUS_INACTIVE)
37  , m_interestTimedOutNo(0)
38  , m_faceId(0)
39 {
40 }
41 
42 Adjacent::Adjacent(const ndn::Block& block)
43 {
44  wireDecode(block);
45 }
46 
47 Adjacent::Adjacent(const ndn::Name& an)
48  : m_name(an)
49  , m_faceUri()
50  , m_linkCost(DEFAULT_LINK_COST)
51  , m_status(STATUS_INACTIVE)
52  , m_interestTimedOutNo(0)
53  , m_faceId(0)
54 {
55 }
56 
57 Adjacent::Adjacent(const ndn::Name& an, const ndn::FaceUri& faceUri, double lc,
58  Status s, uint32_t iton, uint64_t faceId)
59  : m_name(an)
60  , m_faceUri(faceUri)
61  , m_status(s)
62  , m_interestTimedOutNo(iton)
63  , m_faceId(faceId)
64 {
65  this->setLinkCost(lc);
66 }
67 
68 void
70 {
71  // NON_ADJACENT_COST is a negative value and is used for nodes that aren't direct neighbors.
72  // But for direct/active neighbors, the cost cannot be negative.
73  if (lc < 0 && lc != NON_ADJACENT_COST) {
74  NLSR_LOG_ERROR(" Neighbor's link-cost cannot be negative");
75  NDN_THROW(ndn::tlv::Error("Neighbor's link-cost cannot be negative"));
76  }
77 
78  m_linkCost = lc;
79 }
80 
82 
83 template<ndn::encoding::Tag TAG>
84 size_t
85 Adjacent::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
86 {
87  size_t totalLength = 0;
88 
89  totalLength += prependDoubleBlock(encoder, ndn::tlv::nlsr::Cost, m_linkCost);
90 
91  totalLength += prependStringBlock(encoder, ndn::tlv::nlsr::Uri, m_faceUri.toString());
92 
93  totalLength += m_name.wireEncode(encoder);
94 
95  totalLength += encoder.prependVarNumber(totalLength);
96  totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::Adjacency);
97 
98  return totalLength;
99 }
100 
101 const ndn::Block&
103 {
104  if (m_wire.hasWire()) {
105  return m_wire;
106  }
107 
108  ndn::EncodingEstimator estimator;
109  size_t estimatedSize = wireEncode(estimator);
110 
111  ndn::EncodingBuffer buffer(estimatedSize, 0);
112  wireEncode(buffer);
113 
114  m_wire = buffer.block();
115 
116  return m_wire;
117 }
118 
119 void
120 Adjacent::wireDecode(const ndn::Block& wire)
121 {
122  m_name.clear();
123  m_faceUri = ndn::FaceUri();
124  m_linkCost = 0;
125 
126  m_wire = wire;
127 
128  if (m_wire.type() != ndn::tlv::nlsr::Adjacency) {
129  NDN_THROW(Error("Adjacency", m_wire.type()));
130  }
131 
132  m_wire.parse();
133 
134  auto val = m_wire.elements_begin();
135 
136  if (val != m_wire.elements_end() && val->type() == ndn::tlv::Name) {
137  m_name.wireDecode(*val);
138  ++val;
139  }
140  else {
141  NDN_THROW(Error("Missing required Name field"));
142  }
143 
144  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Uri) {
145  m_faceUri = ndn::FaceUri(readString(*val));
146  ++val;
147  }
148  else {
149  NDN_THROW(Error("Missing required Uri field"));
150  }
151 
152  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Cost) {
153  m_linkCost = ndn::encoding::readDouble(*val);
154  ++val;
155  }
156  else {
157  NDN_THROW(Error("Missing required Cost field"));
158  }
159 }
160 
161 bool
162 Adjacent::operator==(const Adjacent& adjacent) const
163 {
164  return (m_name == adjacent.getName()) &&
165  (m_faceUri == adjacent.getFaceUri()) &&
166  (std::abs(m_linkCost - adjacent.getLinkCost()) <
167  std::numeric_limits<double>::epsilon());
168 }
169 
170 bool
171 Adjacent::operator<(const Adjacent& adjacent) const
172 {
173  auto linkCost = adjacent.getLinkCost();
174  return std::tie(m_name, m_linkCost) <
175  std::tie(adjacent.getName(), linkCost);
176 }
177 
178 std::ostream&
179 operator<<(std::ostream& os, const Adjacent& adjacent)
180 {
181  os << "Adjacent: " << adjacent.m_name
182  << "\n\t\tConnecting FaceUri: " << adjacent.m_faceUri
183  << "\n\t\tLink cost: " << adjacent.m_linkCost
184  << "\n\t\tStatus: " << adjacent.m_status
185  << "\n\t\tInterest Timed Out: " << adjacent.m_interestTimedOutNo << std::endl;
186  return os;
187 }
188 
189 } // namespace nlsr
A neighbor reachable over a Face.
Definition: adjacent.hpp:47
const ndn::FaceUri & getFaceUri() const
Definition: adjacent.hpp:85
void wireDecode(const ndn::Block &wire)
Definition: adjacent.cpp:120
static const double DEFAULT_LINK_COST
Definition: adjacent.hpp:186
bool operator<(const Adjacent &adjacent) const
Definition: adjacent.cpp:171
void setLinkCost(double lc)
Definition: adjacent.cpp:69
const ndn::Block & wireEncode() const
Definition: adjacent.cpp:102
const ndn::Name & getName() const
Definition: adjacent.hpp:72
bool operator==(const Adjacent &adjacent) const
Equality is when name, Face URI, and link cost are all equal.
Definition: adjacent.cpp:162
static const double NON_ADJACENT_COST
Definition: adjacent.hpp:187
double getLinkCost() const
Definition: adjacent.hpp:98
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:179
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)