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-2017 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 
22 #include "fib-entry.hpp"
25 #include "encoding/tlv-nfd.hpp"
26 #include "util/concepts.hpp"
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  BOOST_THROW_EXCEPTION(Error("expecting NextHopRecord, but Block has type " + to_string(block.type())));
100  }
101  m_wire = block;
102  m_wire.parse();
103  Block::element_const_iterator val = m_wire.elements_begin();
104 
105  if (val == m_wire.elements_end()) {
106  BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord"));
107  }
108  else if (val->type() != tlv::nfd::FaceId) {
109  BOOST_THROW_EXCEPTION(Error("expecting FaceId, but Block has type " + to_string(val->type())));
110  }
111  m_faceId = readNonNegativeInteger(*val);
112  ++val;
113 
114  if (val == m_wire.elements_end()) {
115  BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord"));
116  }
117  else if (val->type() != tlv::nfd::Cost) {
118  BOOST_THROW_EXCEPTION(Error("expecting Cost, but Block has type " + to_string(val->type())));
119  }
120  m_cost = readNonNegativeInteger(*val);
121  ++val;
122 }
123 
124 bool
126 {
127  return a.getFaceId() == b.getFaceId() &&
128  a.getCost() == b.getCost();
129 }
130 
131 std::ostream&
132 operator<<(std::ostream& os, const NextHopRecord& nh)
133 {
134  return os << "NextHopRecord("
135  << "FaceId: " << nh.getFaceId() << ", "
136  << "Cost: " << nh.getCost()
137  << ")";
138 }
139 
141 
142 FibEntry::FibEntry() = default;
143 
145 {
146  this->wireDecode(block);
147 }
148 
149 FibEntry&
150 FibEntry::setPrefix(const Name& prefix)
151 {
152  m_prefix = prefix;
153  m_wire.reset();
154  return *this;
155 }
156 
157 FibEntry&
159 {
160  m_nextHopRecords.push_back(nh);
161  m_wire.reset();
162  return *this;
163 }
164 
165 FibEntry&
167 {
168  m_nextHopRecords.clear();
169  m_wire.reset();
170  return *this;
171 }
172 
173 template<encoding::Tag TAG>
174 size_t
175 FibEntry::wireEncode(EncodingImpl<TAG>& block) const
176 {
177  size_t totalLength = 0;
178 
179  for (const auto& nh : m_nextHopRecords | boost::adaptors::reversed) {
180  totalLength += nh.wireEncode(block);
181  }
182  totalLength += m_prefix.wireEncode(block);
183 
184  totalLength += block.prependVarNumber(totalLength);
185  totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
186  return totalLength;
187 }
188 
190 
191 const Block&
193 {
194  if (m_wire.hasWire())
195  return m_wire;
196 
197  EncodingEstimator estimator;
198  size_t estimatedSize = wireEncode(estimator);
199 
200  EncodingBuffer buffer(estimatedSize, 0);
201  wireEncode(buffer);
202 
203  m_wire = buffer.block();
204  return m_wire;
205 }
206 
207 void
209 {
210  if (block.type() != tlv::nfd::FibEntry) {
211  BOOST_THROW_EXCEPTION(Error("expecting FibEntry, but Block has type " + to_string(block.type())));
212  }
213  m_wire = block;
214  m_wire.parse();
215  Block::element_const_iterator val = m_wire.elements_begin();
216 
217  if (val == m_wire.elements_end()) {
218  BOOST_THROW_EXCEPTION(Error("unexpected end of FibEntry"));
219  }
220  else if (val->type() != tlv::Name) {
221  BOOST_THROW_EXCEPTION(Error("expecting Name, but Block has type " + to_string(val->type())));
222  }
223  m_prefix.wireDecode(*val);
224  ++val;
225 
226  m_nextHopRecords.clear();
227  for (; val != m_wire.elements_end(); ++val) {
228  if (val->type() != tlv::nfd::NextHopRecord) {
229  BOOST_THROW_EXCEPTION(Error("expecting NextHopRecord, but Block has type " + to_string(val->type())));
230  }
231  m_nextHopRecords.emplace_back(*val);
232  }
233 }
234 
235 bool
236 operator==(const FibEntry& a, const FibEntry& b)
237 {
238  const auto& aNextHops = a.getNextHopRecords();
239  const auto& bNextHops = b.getNextHopRecords();
240 
241  if (a.getPrefix() != b.getPrefix() ||
242  aNextHops.size() != bNextHops.size())
243  return false;
244 
245  std::vector<bool> matched(bNextHops.size(), false);
246  return std::all_of(aNextHops.begin(), aNextHops.end(),
247  [&] (const NextHopRecord& nh) {
248  for (size_t i = 0; i < bNextHops.size(); ++i) {
249  if (!matched[i] && bNextHops[i] == nh) {
250  matched[i] = true;
251  return true;
252  }
253  }
254  return false;
255  });
256 }
257 
258 std::ostream&
259 operator<<(std::ostream& os, const FibEntry& entry)
260 {
261  os << "FibEntry(Prefix: " << entry.getPrefix() << ",\n"
262  << " NextHops: [";
263 
264  std::copy(entry.getNextHopRecords().begin(), entry.getNextHopRecords().end(),
265  make_ostream_joiner(os, ",\n "));
266 
267  os << "]\n";
268 
269  return os << " )";
270 }
271 
272 } // namespace nfd
273 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
void wireDecode(const Block &block)
Definition: fib-entry.cpp:208
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:192
FibEntry & setPrefix(const Name &prefix)
Definition: fib-entry.cpp:150
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
ostream_joiner< typename std::decay< DelimT >::type, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
void wireDecode(const Block &block)
Definition: fib-entry.cpp:96
const Block & wireEncode() const
Definition: fib-entry.cpp:80
BOOST_CONCEPT_ASSERT((StatusDatasetItem< ChannelStatus >))
const Name & getPrefix() const
Definition: fib-entry.hpp:124
uint64_t getCost() const
Definition: fib-entry.hpp:62
FibEntry & clearNextHopRecords()
Definition: fib-entry.cpp:166
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
size_t size() const
Get number of components.
Definition: name.hpp:154
NextHopRecord & setCost(uint64_t cost)
Definition: fib-entry.cpp:56
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:256
Represents an absolute name.
Definition: name.hpp:42
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:334
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
const std::vector< NextHopRecord > & getNextHopRecords() const
Definition: fib-entry.hpp:133
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:250
std::string to_string(const V &v)
Definition: backports.hpp:84
uint64_t getFaceId() const
Definition: fib-entry.hpp:53
EncodingImpl< EncoderTag > EncodingBuffer
const uint64_t INVALID_FACE_ID
EncodingImpl< EstimatorTag > EncodingEstimator
FibEntry & addNextHopRecord(const NextHopRecord &nh)
Definition: fib-entry.cpp:158