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