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-2022 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 
25 #include "ndn-cxx/lp/fields.hpp"
26 
27 namespace ndn {
31 namespace lp {
32 
33 class Packet
34 {
35 public:
36  class Error : public ndn::tlv::Error
37  {
38  public:
40  };
41 
42  Packet();
43 
44  explicit
45  Packet(const Block& wire);
46 
50  Block
51  wireEncode() const;
52 
57  void
58  wireDecode(const Block& wire);
59 
65  empty() const
66  {
67  return m_wire.elements_size() == 0;
68  }
69 
70 public: // field access
75  template<typename FIELD>
77  has() const
78  {
79  return count<FIELD>() > 0;
80  }
81 
85  template<typename FIELD>
86  NDN_CXX_NODISCARD size_t
87  count() const
88  {
89  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
90  [] (const Block& block) { return block.type() == FIELD::TlvType::value; });
91  }
92 
97  template<typename FIELD>
98  typename FIELD::ValueType
99  get(size_t index = 0) const
100  {
101  size_t count = 0;
102  for (const Block& element : m_wire.elements()) {
103  if (element.type() != FIELD::TlvType::value) {
104  continue;
105  }
106  if (count++ == index) {
107  return FIELD::decode(element);
108  }
109  }
110 
111  NDN_THROW(std::out_of_range("lp::Packet::get: index out of range"));
112  }
113 
117  template<typename FIELD>
118  NDN_CXX_NODISCARD std::vector<typename FIELD::ValueType>
119  list() const
120  {
121  std::vector<typename FIELD::ValueType> output;
122 
123  for (const Block& element : m_wire.elements()) {
124  if (element.type() != FIELD::TlvType::value) {
125  continue;
126  }
127  output.push_back(FIELD::decode(element));
128  }
129 
130  return output;
131  }
132 
137  template<typename FIELD>
138  Packet&
139  set(const typename FIELD::ValueType& value)
140  {
141  clear<FIELD>();
142  return add<FIELD>(value);
143  }
144 
149  template<typename FIELD>
150  Packet&
151  add(const typename FIELD::ValueType& value)
152  {
153  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
154  NDN_THROW(std::invalid_argument("lp::Packet::add: field cannot be repeated"));
155  }
156 
157  EncodingEstimator estimator;
158  size_t estimatedSize = FIELD::encode(estimator, value);
159  EncodingBuffer buffer(estimatedSize, 0);
160  FIELD::encode(buffer, value);
161  Block block = buffer.block();
162 
163  auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
164  FIELD::TlvType::value, comparePos);
165  m_wire.insert(pos, block);
166 
167  return *this;
168  }
169 
174  template<typename FIELD>
175  Packet&
176  remove(size_t index = 0)
177  {
178  size_t count = 0;
179  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
180  if (it->type() == FIELD::TlvType::value) {
181  if (count == index) {
182  m_wire.erase(it);
183  return *this;
184  }
185  count++;
186  }
187  }
188 
189  NDN_THROW(std::out_of_range("lp::Packet::remove: index out of range"));
190  }
191 
195  template<typename FIELD>
196  Packet&
198  {
199  m_wire.remove(FIELD::TlvType::value);
200  return *this;
201  }
202 
203 private:
204  static bool
205  comparePos(uint64_t first, const Block& second) noexcept;
206 
207 private:
208  mutable Block m_wire;
209 };
210 
211 } // namespace lp
212 } // namespace ndn
213 
214 #endif // NDN_CXX_LP_PACKET_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
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:435
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Definition: block.cpp:441
size_t elements_size() const noexcept
Equivalent to elements().size().
Definition: block.hpp:453
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:426
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
Definition: block.cpp:431
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:444
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:469
Packet & remove(size_t index=0)
Remove the index-th occurrence of FIELD.
Definition: packet.hpp:176
Packet & clear()
Remove all occurrences of FIELD.
Definition: packet.hpp:197
FIELD::ValueType get(size_t index=0) const
Returns the value of the index-th occurrence of FIELD.
Definition: packet.hpp:99
size_t count() const
Returns the number of occurrences of FIELD.
Definition: packet.hpp:87
Packet & set(const typename FIELD::ValueType &value)
Remove all occurrences of FIELD, and add a FIELD with value.
Definition: packet.hpp:139
std::vector< typename FIELD::ValueType > list() const
Returns the values of all occurrences of FIELD.
Definition: packet.hpp:119
Packet & add(const typename FIELD::ValueType &value)
Add a FIELD with value.
Definition: packet.hpp:151
bool empty() const
Definition: packet.hpp:65
Block wireEncode() const
Encode packet into wire format.
Definition: packet.cpp:119
bool has() const
Returns true if FIELD occurs one or more times.
Definition: packet.hpp:77
void wireDecode(const Block &wire)
Decode packet from wire format.
Definition: packet.cpp:133
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
#define NDN_THROW(e)
Definition: exception.hpp:61
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
Definition: data.cpp:25