blob: 291c428233fc6e7cb8abd1a9e68f10bca5378d09 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
3 * Copyright (c) 2014-2018, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * 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/>.
Alexander Afanasyev67758b12018-03-06 18:36:44 -050020 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080021
22#ifndef NLSR_TLV_LSA_INFO_HPP
23#define NLSR_TLV_LSA_INFO_HPP
24
Nick Gordond0a7df32017-05-30 16:44:34 -050025#include "lsa.hpp"
26
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080027#include <ndn-cxx/util/time.hpp>
28#include <ndn-cxx/encoding/block.hpp>
29#include <ndn-cxx/encoding/encoding-buffer.hpp>
30#include <ndn-cxx/encoding/tlv.hpp>
31#include <ndn-cxx/name.hpp>
dmcoomes9f936662017-03-02 10:33:09 -060032#include <boost/throw_exception.hpp>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080033
34namespace nlsr {
Nick G97e34942016-07-11 14:46:27 -050035namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080036
Nick G97e34942016-07-11 14:46:27 -050037/*!
38 \brief Data abstraction for LsaInfo
39
40 LsaInfo := LSA-TYPE TLV-LENGTH
41 OriginRouter
42 SequenceNumber
43 ExpirationPeriod?
44
dmcoomes9f936662017-03-02 10:33:09 -060045 \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080046 */
47class LsaInfo
48{
49public:
50 class Error : public ndn::tlv::Error
51 {
52 public:
53 explicit
54 Error(const std::string& what)
55 : ndn::tlv::Error(what)
56 {
57 }
58 };
59
60 LsaInfo();
61
62 explicit
63 LsaInfo(const ndn::Block& block);
64
65 const ndn::Name&
66 getOriginRouter() const
67 {
68 return m_originRouter;
69 }
70
71 LsaInfo&
72 setOriginRouter(const ndn::Name& name)
73 {
74 m_originRouter = name;
75 m_wire.reset();
76 return *this;
77 }
78
79 uint64_t
80 getSequenceNumber() const
81 {
82 return m_sequenceNumber;
83 }
84
85 LsaInfo&
86 setSequenceNumber(uint64_t sequenceNumber)
87 {
88 m_sequenceNumber = sequenceNumber;
89 m_wire.reset();
90 return *this;
91 }
92
93 static const ndn::time::milliseconds INFINITE_EXPIRATION_PERIOD;
94
95 const ndn::time::milliseconds&
96 getExpirationPeriod() const
97 {
98 return m_expirationPeriod;
99 }
100
101 LsaInfo&
102 setExpirationPeriod(const ndn::time::milliseconds& expirationPeriod)
103 {
104 m_expirationPeriod = expirationPeriod;
105
106 m_hasInfiniteExpirationPeriod = (m_expirationPeriod == INFINITE_EXPIRATION_PERIOD);
107
108 m_wire.reset();
109 return *this;
110 }
111
112 bool
113 hasInfiniteExpirationPeriod() const
114 {
115 return m_hasInfiniteExpirationPeriod;
116 }
117
Nick Gordond0a7df32017-05-30 16:44:34 -0500118 /*! \brief Encodes LSA info using the method in TAG.
119 *
120 * This function will TLV-format LSA info using the implementation
121 * speciifed by TAG. Usually this is called with an estimator first
122 * to guess how long the buffer needs to be, then with an encoder to
123 * do the real work. This process is automated by the other
124 * wireEncode.
125 * \sa LsaInfo::wireEncode()
126 */
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800127 template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800128 size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800129 wireEncode(ndn::EncodingImpl<TAG>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800130
Nick Gordond0a7df32017-05-30 16:44:34 -0500131 /*! \brief Create a TLV encoding of this object.
132 *
133 * Create a block containing the TLV encoding of this object. That
134 * involves two steps: estimating the size that the information will
135 * take up, and then creating a buffer of that size and encoding the
136 * information into it. Both steps are accomplished by
137 * LsaInfo::wireEncode(ndn::EncodingImpl<TAG>&)
138 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800139 const ndn::Block&
140 wireEncode() const;
141
Nick Gordond0a7df32017-05-30 16:44:34 -0500142 /*! \brief Populate this object by decoding the one contained in the
143 * given block.
144 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800145 void
146 wireDecode(const ndn::Block& wire);
147
148private:
149 ndn::Name m_originRouter;
150 uint64_t m_sequenceNumber;
151 ndn::time::milliseconds m_expirationPeriod;
152 bool m_hasInfiniteExpirationPeriod;
153
154 mutable ndn::Block m_wire;
155};
156
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500157NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(LsaInfo);
158
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800159std::ostream&
160operator<<(std::ostream& os, const LsaInfo& lsaInfo);
161
Jiewen Tana0497d82015-02-02 21:59:18 -0800162std::shared_ptr<LsaInfo>
163makeLsaInfo(const Lsa& lsa);
164
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800165} // namespace tlv
166} // namespace nlsr
167
168#endif // NLSR_TLV_LSA_INFO_HPP