Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, The University of Memphis, |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 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/>. |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 20 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 21 | |
| 22 | #ifndef NLSR_TLV_ADJACENCY_LSA_HPP |
| 23 | #define NLSR_TLV_ADJACENCY_LSA_HPP |
| 24 | |
| 25 | #include "lsa-info.hpp" |
| 26 | #include "adjacency.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/util/time.hpp> |
| 29 | #include <ndn-cxx/encoding/block.hpp> |
| 30 | #include <ndn-cxx/encoding/encoding-buffer.hpp> |
| 31 | #include <ndn-cxx/encoding/tlv.hpp> |
| 32 | #include <ndn-cxx/name.hpp> |
| 33 | |
| 34 | #include <list> |
| 35 | |
| 36 | namespace nlsr { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 37 | namespace tlv { |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 38 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 39 | /*! |
| 40 | \brief Data abstraction for AdjacencyLsa |
| 41 | |
| 42 | AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH |
| 43 | LsaInfo |
| 44 | Adjacency* |
| 45 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 46 | \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 47 | */ |
| 48 | class AdjacencyLsa |
| 49 | { |
| 50 | public: |
| 51 | class Error : public ndn::tlv::Error |
| 52 | { |
| 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : ndn::tlv::Error(what) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | typedef std::list<Adjacency> AdjacencyList; |
| 62 | typedef AdjacencyList::const_iterator iterator; |
| 63 | |
| 64 | AdjacencyLsa(); |
| 65 | |
| 66 | explicit |
| 67 | AdjacencyLsa(const ndn::Block& block); |
| 68 | |
| 69 | const LsaInfo& |
| 70 | getLsaInfo() const |
| 71 | { |
| 72 | return m_lsaInfo; |
| 73 | } |
| 74 | |
| 75 | AdjacencyLsa& |
| 76 | setLsaInfo(const LsaInfo& lsaInfo) |
| 77 | { |
| 78 | m_lsaInfo = lsaInfo; |
| 79 | m_wire.reset(); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | bool |
| 84 | hasAdjacencies() const |
| 85 | { |
| 86 | return m_hasAdjacencies; |
| 87 | } |
| 88 | |
| 89 | const std::list<Adjacency>& |
| 90 | getAdjacencies() const |
| 91 | { |
| 92 | return m_adjacencies; |
| 93 | } |
| 94 | |
| 95 | AdjacencyLsa& |
| 96 | addAdjacency(const Adjacency& adjacency) |
| 97 | { |
| 98 | m_adjacencies.push_back(adjacency); |
| 99 | m_wire.reset(); |
| 100 | m_hasAdjacencies = true; |
| 101 | return *this; |
| 102 | } |
| 103 | |
| 104 | AdjacencyLsa& |
| 105 | clearAdjacencies() |
| 106 | { |
| 107 | m_adjacencies.clear(); |
| 108 | m_hasAdjacencies = false; |
| 109 | return *this; |
| 110 | } |
| 111 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 112 | /*! \brief Encodes the Adjacent objects and some info using the method in TAG. |
| 113 | * |
| 114 | * This function will TLV-format the Adjacent objects and some LSA |
| 115 | * info using the implementation speciifed by TAG. Usually this is |
| 116 | * called with an estimator first to guess how long the buffer needs |
| 117 | * to be, then with an encoder to do the real work. This process is |
| 118 | * automated by the other wireEncode. |
| 119 | * \sa AdjacencyLsa::wireEncode() |
| 120 | */ |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 121 | template<ndn::encoding::Tag TAG> |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 122 | size_t |
Alexander Afanasyev | f9f3910 | 2015-12-01 17:43:40 -0800 | [diff] [blame] | 123 | wireEncode(ndn::EncodingImpl<TAG>& block) const; |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 124 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 125 | /*! \brief Create a TLV encoding of this object. |
| 126 | * |
| 127 | * Create a block containing the TLV encoding of this object. That |
| 128 | * involves two steps: estimating the size that the information will |
| 129 | * take up, and then creating a buffer of that size and encoding the |
| 130 | * information into it. Both steps are accomplished by |
| 131 | * AdjacencyLsa::wireEncode(ndn::EncodingImpl<TAG>&) |
| 132 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 133 | const ndn::Block& |
| 134 | wireEncode() const; |
| 135 | |
Nick Gordon | d0a7df3 | 2017-05-30 16:44:34 -0500 | [diff] [blame] | 136 | /*! \brief Populate this object by decoding the one contained in the |
| 137 | * given block. |
| 138 | */ |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 139 | void |
| 140 | wireDecode(const ndn::Block& wire); |
| 141 | |
| 142 | iterator |
| 143 | begin() const; |
| 144 | |
| 145 | iterator |
| 146 | end() const; |
| 147 | |
| 148 | private: |
| 149 | LsaInfo m_lsaInfo; |
| 150 | bool m_hasAdjacencies; |
| 151 | AdjacencyList m_adjacencies; |
| 152 | |
| 153 | mutable ndn::Block m_wire; |
| 154 | }; |
| 155 | |
Alexander Afanasyev | 67758b1 | 2018-03-06 18:36:44 -0500 | [diff] [blame] | 156 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa); |
| 157 | |
Jiewen Tan | 7a56d1c | 2015-01-26 23:26:51 -0800 | [diff] [blame] | 158 | inline AdjacencyLsa::iterator |
| 159 | AdjacencyLsa::begin() const |
| 160 | { |
| 161 | return m_adjacencies.begin(); |
| 162 | } |
| 163 | |
| 164 | inline AdjacencyLsa::iterator |
| 165 | AdjacencyLsa::end() const |
| 166 | { |
| 167 | return m_adjacencies.end(); |
| 168 | } |
| 169 | |
| 170 | std::ostream& |
| 171 | operator<<(std::ostream& os, const AdjacencyLsa& adjacencyLsa); |
| 172 | |
| 173 | } // namespace tlv |
| 174 | } // namespace nlsr |
| 175 | |
| 176 | #endif // NLSR_TLV_ADJACENCY_LSA_HPP |