blob: 20c775d86a5293fde128d048d45ce97b42759008 [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_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
36namespace nlsr {
Nick G97e34942016-07-11 14:46:27 -050037namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080038
Nick G97e34942016-07-11 14:46:27 -050039/*!
40 \brief Data abstraction for AdjacencyLsa
41
42 AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH
43 LsaInfo
44 Adjacency*
45
Nick Gordond0a7df32017-05-30 16:44:34 -050046 \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080047 */
48class AdjacencyLsa
49{
50public:
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 Gordond0a7df32017-05-30 16:44:34 -0500112 /*! \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 Afanasyevf9f39102015-12-01 17:43:40 -0800121 template<ndn::encoding::Tag TAG>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800122 size_t
Alexander Afanasyevf9f39102015-12-01 17:43:40 -0800123 wireEncode(ndn::EncodingImpl<TAG>& block) const;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800124
Nick Gordond0a7df32017-05-30 16:44:34 -0500125 /*! \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 Tan7a56d1c2015-01-26 23:26:51 -0800133 const ndn::Block&
134 wireEncode() const;
135
Nick Gordond0a7df32017-05-30 16:44:34 -0500136 /*! \brief Populate this object by decoding the one contained in the
137 * given block.
138 */
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800139 void
140 wireDecode(const ndn::Block& wire);
141
142 iterator
143 begin() const;
144
145 iterator
146 end() const;
147
148private:
149 LsaInfo m_lsaInfo;
150 bool m_hasAdjacencies;
151 AdjacencyList m_adjacencies;
152
153 mutable ndn::Block m_wire;
154};
155
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500156NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(AdjacencyLsa);
157
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800158inline AdjacencyLsa::iterator
159AdjacencyLsa::begin() const
160{
161 return m_adjacencies.begin();
162}
163
164inline AdjacencyLsa::iterator
165AdjacencyLsa::end() const
166{
167 return m_adjacencies.end();
168}
169
170std::ostream&
171operator<<(std::ostream& os, const AdjacencyLsa& adjacencyLsa);
172
173} // namespace tlv
174} // namespace nlsr
175
176#endif // NLSR_TLV_ADJACENCY_LSA_HPP