fib-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
27 
28 #include <boost/range/adaptor/reversed.hpp>
29 
30 namespace ndn {
31 namespace nfd {
32 
33 BOOST_CONCEPT_ASSERT((StatusDatasetItem<NextHopRecord>));
34 BOOST_CONCEPT_ASSERT((StatusDatasetItem<FibEntry>));
35 
37  : m_faceId(INVALID_FACE_ID)
38  , m_cost(0)
39 {
40 }
41 
43 {
44  this->wireDecode(block);
45 }
46 
48 NextHopRecord::setFaceId(uint64_t faceId)
49 {
50  m_faceId = faceId;
51  m_wire.reset();
52  return *this;
53 }
54 
56 NextHopRecord::setCost(uint64_t cost)
57 {
58  m_cost = cost;
59  m_wire.reset();
60  return *this;
61 }
62 
63 template<encoding::Tag TAG>
64 size_t
65 NextHopRecord::wireEncode(EncodingImpl<TAG>& block) const
66 {
67  size_t totalLength = 0;
68 
69  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
70  totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);
71 
72  totalLength += block.prependVarNumber(totalLength);
73  totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord);
74  return totalLength;
75 }
76 
78 
79 const Block&
81 {
82  if (m_wire.hasWire())
83  return m_wire;
84 
85  EncodingEstimator estimator;
86  size_t estimatedSize = wireEncode(estimator);
87 
88  EncodingBuffer buffer(estimatedSize, 0);
89  wireEncode(buffer);
90 
91  m_wire = buffer.block();
92  return m_wire;
93 }
94 
95 void
97 {
98  if (block.type() != tlv::nfd::NextHopRecord) {
99  NDN_THROW(Error("NextHopRecord", block.type()));
100  }
101 
102  m_wire = block;
103  m_wire.parse();
104  auto val = m_wire.elements_begin();
105 
106  if (val == m_wire.elements_end()) {
107  NDN_THROW(Error("unexpected end of NextHopRecord"));
108  }
109  else if (val->type() != tlv::nfd::FaceId) {
110  NDN_THROW(Error("FaceId", val->type()));
111  }
112  m_faceId = readNonNegativeInteger(*val);
113  ++val;
114 
115  if (val == m_wire.elements_end()) {
116  NDN_THROW(Error("unexpected end of NextHopRecord"));
117  }
118  else if (val->type() != tlv::nfd::Cost) {
119  NDN_THROW(Error("Cost", val->type()));
120  }
121  m_cost = readNonNegativeInteger(*val);
122  ++val;
123 }
124 
125 bool
127 {
128  return a.getFaceId() == b.getFaceId() &&
129  a.getCost() == b.getCost();
130 }
131 
132 std::ostream&
133 operator<<(std::ostream& os, const NextHopRecord& nh)
134 {
135  return os << "NextHopRecord("
136  << "FaceId: " << nh.getFaceId() << ", "
137  << "Cost: " << nh.getCost()
138  << ")";
139 }
140 
142 
143 FibEntry::FibEntry() = default;
144 
146 {
147  this->wireDecode(block);
148 }
149 
150 FibEntry&
151 FibEntry::setPrefix(const Name& prefix)
152 {
153  m_prefix = prefix;
154  m_wire.reset();
155  return *this;
156 }
157 
158 FibEntry&
160 {
161  m_nextHopRecords.push_back(nh);
162  m_wire.reset();
163  return *this;
164 }
165 
166 FibEntry&
168 {
169  m_nextHopRecords.clear();
170  m_wire.reset();
171  return *this;
172 }
173 
174 template<encoding::Tag TAG>
175 size_t
176 FibEntry::wireEncode(EncodingImpl<TAG>& block) const
177 {
178  size_t totalLength = 0;
179 
180  for (const auto& nh : m_nextHopRecords | boost::adaptors::reversed) {
181  totalLength += nh.wireEncode(block);
182  }
183  totalLength += m_prefix.wireEncode(block);
184 
185  totalLength += block.prependVarNumber(totalLength);
186  totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
187  return totalLength;
188 }
189 
191 
192 const Block&
194 {
195  if (m_wire.hasWire())
196  return m_wire;
197 
198  EncodingEstimator estimator;
199  size_t estimatedSize = wireEncode(estimator);
200 
201  EncodingBuffer buffer(estimatedSize, 0);
202  wireEncode(buffer);
203 
204  m_wire = buffer.block();
205  return m_wire;
206 }
207 
208 void
210 {
211  if (block.type() != tlv::nfd::FibEntry) {
212  NDN_THROW(Error("FibEntry", block.type()));
213  }
214 
215  m_wire = block;
216  m_wire.parse();
217  auto val = m_wire.elements_begin();
218 
219  if (val == m_wire.elements_end()) {
220  NDN_THROW(Error("unexpected end of FibEntry"));
221  }
222  else if (val->type() != tlv::Name) {
223  NDN_THROW(Error("Name", val->type()));
224  }
225  m_prefix.wireDecode(*val);
226  ++val;
227 
228  m_nextHopRecords.clear();
229  for (; val != m_wire.elements_end(); ++val) {
230  if (val->type() != tlv::nfd::NextHopRecord) {
231  NDN_THROW(Error("NextHopRecord", val->type()));
232  }
233  m_nextHopRecords.emplace_back(*val);
234  }
235 }
236 
237 bool
238 operator==(const FibEntry& a, const FibEntry& b)
239 {
240  const auto& aNextHops = a.getNextHopRecords();
241  const auto& bNextHops = b.getNextHopRecords();
242 
243  if (a.getPrefix() != b.getPrefix() ||
244  aNextHops.size() != bNextHops.size())
245  return false;
246 
247  std::vector<bool> matched(bNextHops.size(), false);
248  return std::all_of(aNextHops.begin(), aNextHops.end(),
249  [&] (const NextHopRecord& nh) {
250  for (size_t i = 0; i < bNextHops.size(); ++i) {
251  if (!matched[i] && bNextHops[i] == nh) {
252  matched[i] = true;
253  return true;
254  }
255  }
256  return false;
257  });
258 }
259 
260 std::ostream&
261 operator<<(std::ostream& os, const FibEntry& entry)
262 {
263  os << "FibEntry(Prefix: " << entry.getPrefix() << ",\n"
264  << " NextHops: [";
265 
266  std::copy(entry.getNextHopRecords().begin(), entry.getNextHopRecords().end(),
267  make_ostream_joiner(os, ",\n "));
268 
269  os << "]\n";
270 
271  return os << " )";
272 }
273 
274 } // namespace nfd
275 } // namespace ndn
Definition: data.cpp:26
void wireDecode(const Block &block)
Definition: fib-entry.cpp:209
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
NextHopRecord & setFaceId(uint64_t faceId)
Definition: fib-entry.cpp:48
const Block & wireEncode() const
Definition: fib-entry.cpp:193
FibEntry & setPrefix(const Name &prefix)
Definition: fib-entry.cpp:151
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
void wireDecode(const Block &block)
Definition: fib-entry.cpp:96
#define NDN_THROW(e)
Definition: exception.hpp:61
const Block & wireEncode() const
Definition: fib-entry.cpp:80
const Name & getPrefix() const
Definition: fib-entry.hpp:116
uint64_t getCost() const
Definition: fib-entry.hpp:58
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:249
FibEntry & clearNextHopRecords()
Definition: fib-entry.cpp:167
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
size_t size() const
Get number of components.
Definition: name.hpp:147
NextHopRecord & setCost(uint64_t cost)
Definition: fib-entry.cpp:56
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
Represents an absolute name.
Definition: name.hpp:43
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
const std::vector< NextHopRecord > & getNextHopRecords() const
Definition: fib-entry.hpp:125
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
uint64_t getFaceId() const
Definition: fib-entry.hpp:49
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
EncodingImpl< EncoderTag > EncodingBuffer
const uint64_t INVALID_FACE_ID
EncodingImpl< EstimatorTag > EncodingEstimator
FibEntry & addNextHopRecord(const NextHopRecord &nh)
Definition: fib-entry.cpp:159