Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2020, 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 | #ifndef NLSR_LSA_LSA_HPP |
| 23 | #define NLSR_LSA_LSA_HPP |
| 24 | |
| 25 | #include "name-prefix-list.hpp" |
| 26 | #include "adjacent.hpp" |
| 27 | #include "adjacency-list.hpp" |
| 28 | #include "test-access-control.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/util/scheduler.hpp> |
| 31 | #include <ndn-cxx/util/time.hpp> |
| 32 | |
| 33 | namespace nlsr { |
| 34 | |
| 35 | /*! |
| 36 | \brief Data abstraction for Lsa |
| 37 | Lsa := LSA-TYPE TLV-LENGTH |
| 38 | Name |
| 39 | SequenceNumber |
| 40 | ExpirationTimePoint |
| 41 | */ |
| 42 | class Lsa |
| 43 | { |
| 44 | public: |
| 45 | class Error : public ndn::tlv::Error |
| 46 | { |
| 47 | public: |
| 48 | explicit |
| 49 | Error(const std::string& what) |
| 50 | : ndn::tlv::Error(what) |
| 51 | { |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | enum class Type { |
| 56 | ADJACENCY, |
| 57 | COORDINATE, |
| 58 | NAME, |
| 59 | BASE |
| 60 | }; |
| 61 | |
| 62 | protected: |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 63 | Lsa(const ndn::Name& originRouter, uint64_t seqNo, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 64 | ndn::time::system_clock::TimePoint expirationTimePoint); |
| 65 | |
| 66 | Lsa() = default; |
| 67 | |
| 68 | public: |
| 69 | virtual |
| 70 | ~Lsa() = default; |
| 71 | |
| 72 | virtual Type |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 73 | getType() const = 0; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 74 | |
| 75 | void |
| 76 | setSeqNo(uint64_t seqNo) |
| 77 | { |
| 78 | m_seqNo = seqNo; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 79 | m_wire.reset(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | uint64_t |
| 83 | getSeqNo() const |
| 84 | { |
| 85 | return m_seqNo; |
| 86 | } |
| 87 | |
| 88 | const ndn::Name& |
| 89 | getOriginRouter() const |
| 90 | { |
| 91 | return m_originRouter; |
| 92 | } |
| 93 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 94 | ndn::Name |
| 95 | getOriginRouterCopy() const |
| 96 | { |
| 97 | return m_originRouter; |
| 98 | } |
| 99 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 100 | const ndn::time::system_clock::TimePoint& |
| 101 | getExpirationTimePoint() const |
| 102 | { |
| 103 | return m_expirationTimePoint; |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | setExpirationTimePoint(const ndn::time::system_clock::TimePoint& lt) |
| 108 | { |
| 109 | m_expirationTimePoint = lt; |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 110 | m_wire.reset(); |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
| 114 | setExpiringEventId(ndn::scheduler::EventId eid) |
| 115 | { |
| 116 | m_expiringEventId = std::move(eid); |
| 117 | } |
| 118 | |
| 119 | ndn::scheduler::EventId |
| 120 | getExpiringEventId() const |
| 121 | { |
| 122 | return m_expiringEventId; |
| 123 | } |
| 124 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 125 | /*! Get data common to all LSA types. |
| 126 | */ |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 127 | virtual std::string |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 128 | toString() const; |
| 129 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 130 | virtual const ndn::Block& |
| 131 | wireEncode() const = 0; |
| 132 | |
| 133 | protected: |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 134 | template<ndn::encoding::Tag TAG> |
| 135 | size_t |
| 136 | wireEncode(ndn::EncodingImpl<TAG>& block) const; |
| 137 | |
| 138 | void |
| 139 | wireDecode(const ndn::Block& wire); |
| 140 | |
| 141 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
| 142 | ndn::Name m_originRouter; |
| 143 | uint64_t m_seqNo = 0; |
| 144 | ndn::time::system_clock::TimePoint m_expirationTimePoint; |
| 145 | ndn::scheduler::EventId m_expiringEventId; |
| 146 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame^] | 147 | mutable ndn::Block m_wire; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Lsa); |
| 151 | |
| 152 | std::ostream& |
| 153 | operator<<(std::ostream& os, const Lsa::Type& type); |
| 154 | |
| 155 | std::istream& |
| 156 | operator>>(std::istream& is, Lsa::Type& type); |
| 157 | |
| 158 | } // namespace nlsr |
| 159 | |
| 160 | #endif // NLSR_LSA_LSA_HPP |