tlv/routing-table-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "routing-table-entry.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 #include <ndn-cxx/util/concepts.hpp>
26 #include <ndn-cxx/encoding/block-helpers.hpp>
27 
28 namespace nlsr {
29 namespace tlv {
30 
31 BOOST_CONCEPT_ASSERT((ndn::WireEncodable<RoutingTable>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTable>));
33 static_assert(std::is_base_of<ndn::tlv::Error, RoutingTable::Error>::value,
34  "RoutingTable::Error must inherit from tlv::Error");
35 
37  : m_hasNexthops(false)
38 {
39 }
40 
41 RoutingTable::RoutingTable(const ndn::Block& block)
42 {
43  wireDecode(block);
44 }
45 
46 bool
48 {
49  return m_hasNexthops;
50 }
51 
54 {
55  m_nexthops.push_back(nexthop);
56  m_wire.reset();
57  m_hasNexthops = true;
58  return *this;
59 }
60 
63 {
64  m_nexthops.clear();
65  m_hasNexthops = false;
66  return *this;
67 }
68 
69 template<ndn::encoding::Tag TAG>
70 size_t
71 RoutingTable::wireEncode(ndn::EncodingImpl<TAG>& block) const
72 {
73  size_t totalLength = 0;
74 
75  for (std::list<NextHop>::const_reverse_iterator it = m_nexthops.rbegin();
76  it != m_nexthops.rend(); ++it) {
77  totalLength += it->wireEncode(block);
78  }
79 
80  totalLength += m_des.wireEncode(block);
81 
82  totalLength += block.prependVarNumber(totalLength);
83  totalLength += block.prependVarNumber(ndn::tlv::nlsr::RouteTableEntry);
84 
85  return totalLength;
86 }
87 
89 
90 const ndn::Block&
92 {
93  if (m_wire.hasWire()) {
94  return m_wire;
95  }
96 
97  ndn::EncodingEstimator estimator;
98  size_t estimatedSize = wireEncode(estimator);
99 
100  ndn::EncodingBuffer buffer(estimatedSize, 0);
101  wireEncode(buffer);
102 
103  m_wire = buffer.block();
104 
105  return m_wire;
106 }
107 
108 void
109 RoutingTable::wireDecode(const ndn::Block& wire)
110 {
111  m_hasNexthops = false;
112  m_nexthops.clear();
113 
114  m_wire = wire;
115 
116  if (m_wire.type() != ndn::tlv::nlsr::RouteTableEntry) {
117  std::stringstream error;
118  error << "Expected RoutingTable Block, but Block is of a different type: #"
119  << m_wire.type();
120  BOOST_THROW_EXCEPTION(Error(error.str()));
121  }
122 
123  m_wire.parse();
124 
125  ndn::Block::element_const_iterator val = m_wire.elements_begin();
126 
127  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Destination) {
128  m_des.wireDecode(*val);
129  ++val;
130  }
131  else {
132  BOOST_THROW_EXCEPTION(Error("Missing required destination field"));
133  }
134 
135  for (; val != m_wire.elements_end(); ++val) {
136  if (val->type() == ndn::tlv::nlsr::NextHop) {
137  m_nexthops.push_back(NextHop(*val));
138  m_hasNexthops = true;
139  }
140  else {
141  std::stringstream error;
142  error << "Expected NextHop Block, but Block is of a different type: #"
143  << m_wire.type();
144  BOOST_THROW_EXCEPTION(Error(error.str()));
145  }
146  }
147 }
148 
149 std::ostream&
150 operator<<(std::ostream& os, const RoutingTable& routingtable)
151 {
152  os << routingtable.getDestination() << std::endl;
153  os << "NexthopList(" << std::endl;
154 
155  for (const auto& rtentry : routingtable) {
156  os << rtentry;
157  }
158 
159  os << ")";
160  return os;
161 }
162 
163 } // namespace tlv
164 } // namespace nlsr
void wireDecode(const ndn::Block &wire)
Data abstraction for Nexthop.
Definition: tlv/nexthop.hpp:43
const Destination & getDestination() const
Data abstraction for RouteTableInfo.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa)
const ndn::Block & wireEncode() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void wireDecode(const ndn::Block &wire)
Definition: destination.cpp:78
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Definition: destination.cpp:45
RoutingTable & addNexthops(const NextHop &nexthop)