name-lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "name-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<NameLsa>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<NameLsa>));
33 static_assert(std::is_base_of<ndn::tlv::Error, NameLsa::Error>::value,
34  "NameLsa::Error must inherit from tlv::Error");
35 
37  : m_hasNames(false)
38 {
39 }
40 
41 NameLsa::NameLsa(const ndn::Block& block)
42 {
43  wireDecode(block);
44 }
45 
46 template<ndn::encoding::Tag TAG>
47 size_t
48 NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
49 {
50  size_t totalLength = 0;
51 
52  for (std::list<ndn::Name>::const_reverse_iterator it = m_names.rbegin();
53  it != m_names.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::NameLsa);
61 
62  return totalLength;
63 }
64 
65 template size_t
66 NameLsa::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
67 
68 template size_t
69 NameLsa::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) 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 NameLsa::wireDecode(const ndn::Block& wire)
91 {
92  m_hasNames = false;
93  m_names.clear();
94 
95  m_wire = wire;
96 
97  if (m_wire.type() != ndn::tlv::nlsr::NameLsa) {
98  std::stringstream error;
99  error << "Expected NameLsa 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::Name) {
118  m_names.push_back(ndn::Name(*val));
119  m_hasNames = true;
120  }
121  else {
122  std::stringstream error;
123  error << "Expected Name 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 NameLsa& nameLsa)
132 {
133  os << "NameLsa("
134  << nameLsa.getLsaInfo();
135 
136  for (const auto& name : nameLsa) {
137  os << ", Name: " << name;
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.
Definition: name-lsa.cpp:90
Data abstraction for NameLsa.
Definition: name-lsa.hpp:47
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const LsaInfo & getLsaInfo() const
Definition: name-lsa.hpp:69
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
Definition: lsa-info.cpp:100
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
Definition: name-lsa.cpp:72