blob: e482a92c030fe5e0881bdbcf28963730d20c7f41 [file] [log] [blame]
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi153fbc12024-01-09 23:37:23 +00003 * Copyright (c) 2014-2024, The University of Memphis,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -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/>.
20 */
21
22#ifndef NLSR_LSA_LSA_HPP
23#define NLSR_LSA_LSA_HPP
24
Junxiao Shi20377642023-08-24 23:55:40 +000025#include "common.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080026#include "test-access-control.hpp"
27
28#include <ndn-cxx/util/scheduler.hpp>
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080029
Junxiao Shi20377642023-08-24 23:55:40 +000030#include <list>
31
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080032namespace nlsr {
33
Junxiao Shi153fbc12024-01-09 23:37:23 +000034/**
35 * @brief Represents a Link State Announcement (LSA).
36 *
37 * The base level LSA is encoded as:
38 * @code{.abnf}
39 * Lsa = LSA-TYPE TLV-LENGTH
40 * Name ; origin router
41 * SequenceNumber
42 * ExpirationTime
43 * @endcode
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080044 */
45class Lsa
46{
47public:
48 class Error : public ndn::tlv::Error
49 {
50 public:
Davide Pesaventod90338d2021-01-07 17:50:05 -050051 using ndn::tlv::Error::Error;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080052 };
53
54 enum class Type {
55 ADJACENCY,
56 COORDINATE,
57 NAME,
58 BASE
59 };
60
61protected:
Junxiao Shi153fbc12024-01-09 23:37:23 +000062 Lsa() = default;
63
Ashlesh Gawande57a87172020-05-09 19:47:06 -070064 Lsa(const ndn::Name& originRouter, uint64_t seqNo,
Davide Pesavento658fd852023-05-10 22:15:03 -040065 ndn::time::system_clock::time_point expirationTimePoint);
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080066
Junxiao Shi153fbc12024-01-09 23:37:23 +000067 explicit
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070068 Lsa(const Lsa& lsa);
69
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080070public:
71 virtual
72 ~Lsa() = default;
73
74 virtual Type
Ashlesh Gawande57a87172020-05-09 19:47:06 -070075 getType() const = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080076
77 void
78 setSeqNo(uint64_t seqNo)
79 {
80 m_seqNo = seqNo;
Ashlesh Gawande57a87172020-05-09 19:47:06 -070081 m_wire.reset();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080082 }
83
84 uint64_t
85 getSeqNo() const
86 {
87 return m_seqNo;
88 }
89
90 const ndn::Name&
91 getOriginRouter() const
92 {
93 return m_originRouter;
94 }
95
Davide Pesavento658fd852023-05-10 22:15:03 -040096 const ndn::time::system_clock::time_point&
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080097 getExpirationTimePoint() const
98 {
99 return m_expirationTimePoint;
100 }
101
102 void
Davide Pesavento658fd852023-05-10 22:15:03 -0400103 setExpirationTimePoint(const ndn::time::system_clock::time_point& lt)
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800104 {
105 m_expirationTimePoint = lt;
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700106 m_wire.reset();
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800107 }
108
109 void
110 setExpiringEventId(ndn::scheduler::EventId eid)
111 {
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700112 m_expiringEventId = eid;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800113 }
114
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700115 virtual std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
116 update(const std::shared_ptr<Lsa>& lsa) = 0;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800117
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700118 virtual const ndn::Block&
119 wireEncode() const = 0;
120
121protected:
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800122 template<ndn::encoding::Tag TAG>
123 size_t
124 wireEncode(ndn::EncodingImpl<TAG>& block) const;
125
126 void
127 wireDecode(const ndn::Block& wire);
128
Junxiao Shi153fbc12024-01-09 23:37:23 +0000129private:
130 virtual void
131 print(std::ostream& os) const = 0;
132
133 friend std::ostream&
134 operator<<(std::ostream& os, const Lsa& lsa);
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700135
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800136PUBLIC_WITH_TESTS_ELSE_PROTECTED:
137 ndn::Name m_originRouter;
138 uint64_t m_seqNo = 0;
Davide Pesavento658fd852023-05-10 22:15:03 -0400139 ndn::time::system_clock::time_point m_expirationTimePoint;
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -0700140 ndn::scheduler::ScopedEventId m_expiringEventId;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800141
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700142 mutable ndn::Block m_wire;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800143};
144
145NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Lsa);
146
147std::ostream&
148operator<<(std::ostream& os, const Lsa::Type& type);
149
150std::istream&
151operator>>(std::istream& is, Lsa::Type& type);
152
153} // namespace nlsr
154
155#endif // NLSR_LSA_LSA_HPP