packet.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2023 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_LP_PACKET_HPP
23 #define NDN_CXX_LP_PACKET_HPP
24 
27 #include "ndn-cxx/lp/tlv.hpp"
28 
32 namespace ndn::lp {
33 
34 class Packet
35 {
36 public:
37  class Error : public ndn::tlv::Error
38  {
39  public:
41  };
42 
43  Packet();
44 
45  explicit
46  Packet(const Block& wire);
47 
51  Block
52  wireEncode() const;
53 
58  void
59  wireDecode(const Block& wire);
60 
65  [[nodiscard]] bool
66  empty() const
67  {
68  return m_wire.elements_size() == 0;
69  }
70 
71 public: // field access
76  template<typename FIELD>
77  [[nodiscard]] bool
78  has() const
79  {
80  return count<FIELD>() > 0;
81  }
82 
86  template<typename FIELD>
87  [[nodiscard]] size_t
88  count() const
89  {
90  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
91  [] (const Block& block) { return block.type() == FIELD::TlvType::value; });
92  }
93 
98  template<typename FIELD>
99  typename FIELD::ValueType
100  get(size_t index = 0) const
101  {
102  size_t count = 0;
103  for (const Block& element : m_wire.elements()) {
104  if (element.type() != FIELD::TlvType::value) {
105  continue;
106  }
107  if (count++ == index) {
108  return FIELD::decode(element);
109  }
110  }
111 
112  NDN_THROW(std::out_of_range("lp::Packet::get: index out of range"));
113  }
114 
118  template<typename FIELD>
119  [[nodiscard]] std::vector<typename FIELD::ValueType>
120  list() const
121  {
122  std::vector<typename FIELD::ValueType> output;
123 
124  for (const Block& element : m_wire.elements()) {
125  if (element.type() != FIELD::TlvType::value) {
126  continue;
127  }
128  output.push_back(FIELD::decode(element));
129  }
130 
131  return output;
132  }
133 
138  template<typename FIELD>
139  Packet&
140  set(const typename FIELD::ValueType& value)
141  {
142  clear<FIELD>();
143  return add<FIELD>(value);
144  }
145 
150  template<typename FIELD>
151  Packet&
152  add(const typename FIELD::ValueType& value)
153  {
154  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
155  NDN_THROW(std::invalid_argument("lp::Packet::add: field cannot be repeated"));
156  }
157 
158  EncodingEstimator estimator;
159  size_t estimatedSize = FIELD::encode(estimator, value);
160  EncodingBuffer buffer(estimatedSize, 0);
161  FIELD::encode(buffer, value);
162  Block block = buffer.block();
163 
164  auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
165  FIELD::TlvType::value, comparePos);
166  m_wire.insert(pos, block);
167 
168  return *this;
169  }
170 
175  template<typename FIELD>
176  Packet&
177  remove(size_t index = 0)
178  {
179  size_t count = 0;
180  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
181  if (it->type() == FIELD::TlvType::value) {
182  if (count == index) {
183  m_wire.erase(it);
184  return *this;
185  }
186  count++;
187  }
188  }
189 
190  NDN_THROW(std::out_of_range("lp::Packet::remove: index out of range"));
191  }
192 
196  template<typename FIELD>
197  Packet&
199  {
200  m_wire.remove(FIELD::TlvType::value);
201  return *this;
202  }
203 
204 private:
205  static bool
206  comparePos(uint32_t first, const Block& second) noexcept;
207 
208 private:
209  mutable Block m_wire{tlv::LpPacket};
210 };
211 
212 } // namespace ndn::lp
213 
214 #endif // NDN_CXX_LP_PACKET_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition: block.hpp:433
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Definition: block.cpp:442
size_t elements_size() const noexcept
Equivalent to elements().size().
Definition: block.hpp:451
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:424
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
Definition: block.cpp:432
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:442
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:470
Packet & remove(size_t index=0)
Remove the index-th occurrence of FIELD.
Definition: packet.hpp:177
Packet & clear()
Remove all occurrences of FIELD.
Definition: packet.hpp:198
FIELD::ValueType get(size_t index=0) const
Returns the value of the index-th occurrence of FIELD.
Definition: packet.hpp:100
size_t count() const
Returns the number of occurrences of FIELD.
Definition: packet.hpp:88
Packet & set(const typename FIELD::ValueType &value)
Remove all occurrences of FIELD, and add a FIELD with value.
Definition: packet.hpp:140
std::vector< typename FIELD::ValueType > list() const
Returns the values of all occurrences of FIELD.
Definition: packet.hpp:120
Packet & add(const typename FIELD::ValueType &value)
Add a FIELD with value.
Definition: packet.hpp:152
bool empty() const
Definition: packet.hpp:66
Block wireEncode() const
Encode packet into wire format.
Definition: packet.cpp:91
bool has() const
Returns true if FIELD occurs one or more times.
Definition: packet.hpp:78
void wireDecode(const Block &wire)
Decode packet from wire format.
Definition: packet.cpp:105
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:28
#define NDN_THROW(e)
Definition: exception.hpp:56
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
@ LpPacket
Definition: tlv.hpp:37
Contains classes and functions related to NDNLPv2.