routing-table-status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "routing-table-status.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<RoutingTableStatus>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<RoutingTableStatus>));
33 static_assert(std::is_base_of<ndn::tlv::Error, RoutingTableStatus::Error>::value,
34  "RoutingTableStatus::Error must inherit from tlv::Error");
35 
37  : m_hasRoutingtable(false)
38 {
39 }
40 
42 {
43  wireDecode(block);
44 }
45 
48 {
49  m_routingtables.push_back(routetable);
50  m_wire.reset();
51  m_hasRoutingtable = true;
52  return *this;
53 }
54 
57 {
58  m_routingtables.clear();
59  m_hasRoutingtable = false;
60  return *this;
61 }
62 
63 bool
65 {
66  return m_hasRoutingtable;
67 }
68 
69 template<ndn::encoding::Tag TAG>
70 size_t
71 RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
72 {
73  size_t totalLength = 0;
74 
75  for (std::list<RoutingTable>::const_reverse_iterator it = m_routingtables.rbegin();
76  it != m_routingtables.rend(); ++it) {
77  totalLength += it->wireEncode(block);
78  }
79 
80  totalLength += block.prependVarNumber(totalLength);
81  totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable);
82 
83  return totalLength;
84 }
85 
87 
88 const ndn::Block&
90 {
91  if (m_wire.hasWire()) {
92  return m_wire;
93  }
94 
95  ndn::EncodingEstimator estimator;
96  size_t estimatedSize = wireEncode(estimator);
97 
98  ndn::EncodingBuffer buffer(estimatedSize, 0);
99  wireEncode(buffer);
100 
101  m_wire = buffer.block();
102 
103  return m_wire;
104 }
105 
106 void
107 RoutingTableStatus::wireDecode(const ndn::Block& wire)
108 {
109  m_routingtables.clear();
110 
111  m_hasRoutingtable = false;
112 
113  m_wire = wire;
114 
115  if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) {
116  std::stringstream error;
117  error << "Expected RoutingTableStatus Block, but Block is of a different type: #"
118  << m_wire.type();
119  BOOST_THROW_EXCEPTION(Error(error.str()));
120  }
121 
122  m_wire.parse();
123 
124  ndn::Block::element_const_iterator val = m_wire.elements_begin();
125 
126  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RouteTableEntry; ++val) {
127  m_routingtables.push_back(RoutingTable(*val));
128  m_hasRoutingtable = true;
129  }
130 
131  if (val != m_wire.elements_end()) {
132  std::stringstream error;
133  error << "Expected the end of elements, but Block is of a different type: #"
134  << val->type();
135  BOOST_THROW_EXCEPTION(Error(error.str()));
136  }
137 }
138 
139 std::ostream&
140 operator<<(std::ostream& os, const RoutingTableStatus& rtStatus)
141 {
142  os << "Routing Table Status: " << std::endl;
143 
144  bool isFirst = true;
145 
146  for (const auto& routingtable : rtStatus.getRoutingtable()) {
147  if (isFirst) {
148  isFirst = false;
149  }
150  else {
151  os << ", ";
152  }
153 
154  os << routingtable;
155  }
156 
157  return os;
158 }
159 
160 } // namespace tlv
161 } // namespace nlsr
RoutingTableStatus & addRoutingTable(const RoutingTable &routeTable)
const ndn::Block & wireEncode() const
Data abstraction for routing table status.
Data abstraction for RouteTableInfo.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa)
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
RoutingTableStatus & clearRoutingTable()
const std::list< RoutingTable > & getRoutingtable() const
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
void wireDecode(const ndn::Block &wire)