adjacency-lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "adjacency-lsa.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<AdjacencyLsa>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<AdjacencyLsa>));
33 static_assert(std::is_base_of<ndn::tlv::Error, AdjacencyLsa::Error>::value,
34  "AdjacencyLsa::Error must inherit from tlv::Error");
35 
37  : m_hasAdjacencies(false)
38 {
39 }
40 
41 AdjacencyLsa::AdjacencyLsa(const ndn::Block& block)
42 {
43  wireDecode(block);
44 }
45 
46 template<ndn::encoding::Tag TAG>
47 size_t
48 AdjacencyLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
49 {
50  size_t totalLength = 0;
51 
52  for (std::list<Adjacency>::const_reverse_iterator it = m_adjacencies.rbegin();
53  it != m_adjacencies.rend(); ++it) {
54  totalLength += it->wireEncode(block);
55  }
56 
57  totalLength += m_lsaInfo.wireEncode(block);
58 
59  totalLength += block.prependVarNumber(totalLength);
60  totalLength += block.prependVarNumber(ndn::tlv::nlsr::AdjacencyLsa);
61 
62  return totalLength;
63 }
64 
65 template size_t
66 AdjacencyLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& encoder) const;
67 
68 template size_t
69 AdjacencyLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& encoder) const;
70 
71 const ndn::Block&
73 {
74  if (m_wire.hasWire()) {
75  return m_wire;
76  }
77 
78  ndn::EncodingEstimator estimator;
79  size_t estimatedSize = wireEncode(estimator);
80 
81  ndn::EncodingBuffer buffer(estimatedSize, 0);
82  wireEncode(buffer);
83 
84  m_wire = buffer.block();
85 
86  return m_wire;
87 }
88 
89 void
90 AdjacencyLsa::wireDecode(const ndn::Block& wire)
91 {
92  m_hasAdjacencies = false;
93  m_adjacencies.clear();
94 
95  m_wire = wire;
96 
97  if (m_wire.type() != ndn::tlv::nlsr::AdjacencyLsa) {
98  std::stringstream error;
99  error << "Expected AdjacencyLsa Block, but Block is of a different type: #"
100  << m_wire.type();
101  throw Error(error.str());
102  }
103 
104  m_wire.parse();
105 
106  ndn::Block::element_const_iterator val = m_wire.elements_begin();
107 
108  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::LsaInfo) {
109  m_lsaInfo.wireDecode(*val);
110  ++val;
111  }
112  else {
113  throw Error("Missing required LsaInfo field");
114  }
115 
116  for (; val != m_wire.elements_end(); ++val) {
117  if (val->type() == ndn::tlv::nlsr::Adjacency) {
118  m_adjacencies.push_back(Adjacency(*val));
119  m_hasAdjacencies = true;
120  }
121  else {
122  std::stringstream error;
123  error << "Expected Adjacency Block, but Block is of a different type: #"
124  << m_wire.type();
125  throw Error(error.str());
126  }
127  }
128 }
129 
130 std::ostream&
131 operator<<(std::ostream& os, const AdjacencyLsa& adjacencyLsa)
132 {
133  os << "AdjacencyLsa("
134  << adjacencyLsa.getLsaInfo();
135 
136  for (const auto& adjacency : adjacencyLsa) {
137  os << ", " << adjacency;
138  }
139 
140  os << ")";
141 
142  return os;
143 }
144 
145 } // namespace tlv
146 } // namespace nlsr
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes LSA info using the method in TAG.
Definition: lsa-info.cpp:52
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
const LsaInfo & getLsaInfo() const
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Data abstraction for AdjacencyLsa.
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
Definition: lsa-info.cpp:100