lsa-info.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "lsa-info.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<LsaInfo>));
32 BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsaInfo>));
33 static_assert(std::is_base_of<ndn::tlv::Error, LsaInfo::Error>::value,
34  "LsaInfo::Error must inherit from tlv::Error");
35 
36 const ndn::time::milliseconds LsaInfo::INFINITE_EXPIRATION_PERIOD(ndn::time::milliseconds::max());
37 
39  : m_sequenceNumber(0)
40  , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD)
41  , m_hasInfiniteExpirationPeriod(true)
42 {
43 }
44 
45 LsaInfo::LsaInfo(const ndn::Block& block)
46 {
47  wireDecode(block);
48 }
49 
50 template<ndn::encoding::Tag TAG>
51 size_t
52 LsaInfo::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
53 {
54  size_t totalLength = 0;
55 
56  // Absence of an ExpirationPeriod signifies non-expiration
57  if (!m_hasInfiniteExpirationPeriod) {
58  totalLength += prependNonNegativeIntegerBlock(encoder,
60  m_expirationPeriod.count());
61  }
62 
63  totalLength += prependNonNegativeIntegerBlock(encoder,
65  m_sequenceNumber);
66 
67  totalLength += prependNestedBlock(encoder, ndn::tlv::nlsr::OriginRouter, m_originRouter);
68 
69  totalLength += encoder.prependVarNumber(totalLength);
70  totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::LsaInfo);
71 
72  return totalLength;
73 }
74 
76 
77 const ndn::Block&
79 {
80  if (m_wire.hasWire()) {
81  return m_wire;
82  }
83 
84  ndn::EncodingEstimator estimator;
85  size_t estimatedSize = wireEncode(estimator);
86 
87  ndn::EncodingBuffer buffer(estimatedSize, 0);
88  wireEncode(buffer);
89 
90  m_wire = buffer.block();
91 
92  return m_wire;
93 }
94 
95 void
96 LsaInfo::wireDecode(const ndn::Block& wire)
97 {
98  m_originRouter.clear();
99  m_sequenceNumber = 0;
100  m_expirationPeriod = ndn::time::milliseconds::min();
101 
102  m_wire = wire;
103 
104  if (m_wire.type() != ndn::tlv::nlsr::LsaInfo) {
105  std::stringstream error;
106  error << "Expected LsaInfo Block, but Block is of a different type: #"
107  << m_wire.type();
108  BOOST_THROW_EXCEPTION(Error(error.str()));
109  }
110 
111  m_wire.parse();
112 
113  ndn::Block::element_const_iterator val = m_wire.elements_begin();
114 
115  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::OriginRouter) {
116  val->parse();
117  ndn::Block::element_const_iterator it = val->elements_begin();
118 
119  if (it != val->elements_end() && it->type() == ndn::tlv::Name) {
120  m_originRouter.wireDecode(*it);
121  }
122  else {
123  BOOST_THROW_EXCEPTION(Error("OriginRouter: Missing required Name field"));
124  }
125 
126  ++val;
127  }
128  else {
129  BOOST_THROW_EXCEPTION(Error("Missing required OriginRouter field"));
130  }
131 
132  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) {
133  m_sequenceNumber = ndn::readNonNegativeInteger(*val);
134  ++val;
135  }
136  else {
137  BOOST_THROW_EXCEPTION(Error("Missing required SequenceNumber field"));
138  }
139 
140  if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationPeriod) {
141  m_expirationPeriod = ndn::time::milliseconds(ndn::readNonNegativeInteger(*val));
142  m_hasInfiniteExpirationPeriod = false;
143  }
144  else {
145  m_expirationPeriod = INFINITE_EXPIRATION_PERIOD;
146  m_hasInfiniteExpirationPeriod = true;
147  }
148 }
149 
150 std::ostream&
151 operator<<(std::ostream& os, const LsaInfo& lsaInfo)
152 {
153  os << "LsaInfo("
154  << "OriginRouter: " << lsaInfo.getOriginRouter() << ", "
155  << "SequenceNumber: " << lsaInfo.getSequenceNumber() << ", ";
156 
157  if (!lsaInfo.hasInfiniteExpirationPeriod()) {
158  os << "ExpirationPeriod: " << lsaInfo.getExpirationPeriod();
159  }
160  else {
161  os << "ExpirationPeriod: Infinity";
162  }
163 
164  os << ")";
165 
166  return os;
167 }
168 
169 std::shared_ptr<LsaInfo>
170 makeLsaInfo(const Lsa& lsa)
171 {
172  std::shared_ptr<LsaInfo> lsaInfo = std::make_shared<LsaInfo>();
173 
174  lsaInfo->setOriginRouter(lsa.getOrigRouter());
175  lsaInfo->setSequenceNumber(lsa.getLsSeqNo());
176 
177  ndn::time::system_clock::duration duration
178  = lsa.getExpirationTimePoint() - ndn::time::system_clock::now();
179 
180  lsaInfo->setExpirationPeriod(ndn::time::duration_cast<ndn::time::milliseconds>(duration));
181 
182  return lsaInfo;
183 }
184 
185 } // namespace tlv
186 } // namespace nlsr
const ndn::time::system_clock::TimePoint & getExpirationTimePoint() const
Definition: lsa.hpp:89
const ndn::Name & getOrigRouter() const
Definition: lsa.hpp:77
const ndn::time::milliseconds & getExpirationPeriod() const
Definition: lsa-info.hpp:96
uint32_t getLsSeqNo() const
Definition: lsa.hpp:71
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa)
std::shared_ptr< LsaInfo > makeLsaInfo(const Lsa &lsa)
Definition: lsa-info.cpp:170
const ndn::Block & wireEncode() const
Create a TLV encoding of this object.
Definition: lsa-info.cpp:78
const ndn::Name & getOriginRouter() const
Definition: lsa-info.hpp:66
Data abstraction for LsaInfo.
Definition: lsa-info.hpp:47
uint64_t getSequenceNumber() const
Definition: lsa-info.hpp:80
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
bool hasInfiniteExpirationPeriod() const
Definition: lsa-info.hpp:113
std::ostream & operator<<(std::ostream &os, const AdjacencyLsa &adjacencyLsa)
static const ndn::time::milliseconds INFINITE_EXPIRATION_PERIOD
Definition: lsa-info.hpp:93
void wireDecode(const ndn::Block &wire)
Populate this object by decoding the one contained in the given block.
Definition: lsa-info.cpp:96