blob: 28ddc10ce081cf9bb60ddea9e3a1acdaa52c0761 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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
28namespace nlsr {
29namespace tlv {
30
31BOOST_CONCEPT_ASSERT((ndn::WireEncodable<LsaInfo>));
32BOOST_CONCEPT_ASSERT((ndn::WireDecodable<LsaInfo>));
33static_assert(std::is_base_of<ndn::tlv::Error, LsaInfo::Error>::value,
34 "LsaInfo::Error must inherit from tlv::Error");
35
36const ndn::time::milliseconds LsaInfo::INFINITE_EXPIRATION_PERIOD(ndn::time::milliseconds::max());
37
38LsaInfo::LsaInfo()
39 : m_sequenceNumber(0)
40 , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD)
41 , m_hasInfiniteExpirationPeriod(true)
42{
43}
44
45LsaInfo::LsaInfo(const ndn::Block& block)
46{
47 wireDecode(block);
48}
49
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080050template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080051size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080052LsaInfo::wireEncode(ndn::EncodingImpl<TAG>& encoder) const
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080053{
54 size_t totalLength = 0;
55
56 // Absence of an ExpirationPeriod signifies non-expiration
57 if (!m_hasInfiniteExpirationPeriod) {
Vince Lehmanf4772d42015-06-29 14:02:22 -050058 totalLength += prependNonNegativeIntegerBlock(encoder,
59 ndn::tlv::nlsr::ExpirationPeriod,
60 m_expirationPeriod.count());
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080061 }
62
Vince Lehmanf4772d42015-06-29 14:02:22 -050063 totalLength += prependNonNegativeIntegerBlock(encoder,
64 ndn::tlv::nlsr::SequenceNumber,
65 m_sequenceNumber);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080066
Vince Lehmanf4772d42015-06-29 14:02:22 -050067 totalLength += prependNestedBlock(encoder, ndn::tlv::nlsr::OriginRouter, m_originRouter);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080068
Vince Lehmanf4772d42015-06-29 14:02:22 -050069 totalLength += encoder.prependVarNumber(totalLength);
70 totalLength += encoder.prependVarNumber(ndn::tlv::nlsr::LsaInfo);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080071
72 return totalLength;
73}
74
75template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080076LsaInfo::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080077
78template size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080079LsaInfo::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080080
81const ndn::Block&
82LsaInfo::wireEncode() const
83{
84 if (m_wire.hasWire()) {
85 return m_wire;
86 }
87
88 ndn::EncodingEstimator estimator;
89 size_t estimatedSize = wireEncode(estimator);
90
91 ndn::EncodingBuffer buffer(estimatedSize, 0);
92 wireEncode(buffer);
93
94 m_wire = buffer.block();
95
96 return m_wire;
97}
98
99void
100LsaInfo::wireDecode(const ndn::Block& wire)
101{
102 m_originRouter.clear();
103 m_sequenceNumber = 0;
104 m_expirationPeriod = ndn::time::milliseconds::min();
105
106 m_wire = wire;
107
108 if (m_wire.type() != ndn::tlv::nlsr::LsaInfo) {
109 std::stringstream error;
110 error << "Expected LsaInfo Block, but Block is of a different type: #"
111 << m_wire.type();
112 throw Error(error.str());
113 }
114
115 m_wire.parse();
116
117 ndn::Block::element_const_iterator val = m_wire.elements_begin();
118
119 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::OriginRouter) {
120 val->parse();
121 ndn::Block::element_const_iterator it = val->elements_begin();
122
123 if (it != val->elements_end() && it->type() == ndn::tlv::Name) {
124 m_originRouter.wireDecode(*it);
125 }
126 else {
127 throw Error("OriginRouter: Missing required Name field");
128 }
129
130 ++val;
131 }
132 else {
133 throw Error("Missing required OriginRouter field");
134 }
135
136 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::SequenceNumber) {
137 m_sequenceNumber = ndn::readNonNegativeInteger(*val);
138 ++val;
139 }
140 else {
141 throw Error("Missing required SequenceNumber field");
142 }
143
144 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::ExpirationPeriod) {
145 m_expirationPeriod = ndn::time::milliseconds(ndn::readNonNegativeInteger(*val));
146 m_hasInfiniteExpirationPeriod = false;
147 }
148 else {
149 m_expirationPeriod = INFINITE_EXPIRATION_PERIOD;
150 m_hasInfiniteExpirationPeriod = true;
151 }
152}
153
154std::ostream&
155operator<<(std::ostream& os, const LsaInfo& lsaInfo)
156{
157 os << "LsaInfo("
158 << "OriginRouter: " << lsaInfo.getOriginRouter() << ", "
159 << "SequenceNumber: " << lsaInfo.getSequenceNumber() << ", ";
160
161 if (!lsaInfo.hasInfiniteExpirationPeriod()) {
162 os << "ExpirationPeriod: " << lsaInfo.getExpirationPeriod();
163 }
164 else {
165 os << "ExpirationPeriod: Infinity";
166 }
167
168 os << ")";
169
170 return os;
171}
172
Jiewen Tana0497d82015-02-02 21:59:18 -0800173std::shared_ptr<LsaInfo>
174makeLsaInfo(const Lsa& lsa)
175{
176 std::shared_ptr<LsaInfo> lsaInfo = std::make_shared<LsaInfo>();
177
178 lsaInfo->setOriginRouter(lsa.getOrigRouter());
179 lsaInfo->setSequenceNumber(lsa.getLsSeqNo());
180
181 ndn::time::system_clock::duration duration
182 = lsa.getExpirationTimePoint() - ndn::time::system_clock::now();
183
184 lsaInfo->setExpirationPeriod(ndn::time::duration_cast<ndn::time::milliseconds>(duration));
185
186 return lsaInfo;
187}
188
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800189} // namespace tlv
190} // namespace nlsr