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-2019 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 {
28 namespace lp {
29 
30 class Packet
31 {
32 public:
33  class Error : public ndn::tlv::Error
34  {
35  public:
37  };
38 
39  Packet();
40 
41  explicit
42  Packet(const Block& wire);
43 
47  Block
48  wireEncode() const;
49 
54  void
55  wireDecode(const Block& wire);
56 
62  empty() const
63  {
64  return m_wire.elements_size() == 0;
65  }
66 
67 public: // field access
72  template<typename FIELD>
74  has() const
75  {
76  return count<FIELD>() > 0;
77  }
78 
82  template<typename FIELD>
83  NDN_CXX_NODISCARD size_t
84  count() const
85  {
86  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
87  [] (const Block& block) { return block.type() == FIELD::TlvType::value; });
88  }
89 
94  template<typename FIELD>
95  typename FIELD::ValueType
96  get(size_t index = 0) const
97  {
98  size_t count = 0;
99  for (const Block& element : m_wire.elements()) {
100  if (element.type() != FIELD::TlvType::value) {
101  continue;
102  }
103  if (count++ == index) {
104  return FIELD::decode(element);
105  }
106  }
107 
108  NDN_THROW(std::out_of_range("lp::Packet::get: index out of range"));
109  }
110 
114  template<typename FIELD>
115  NDN_CXX_NODISCARD std::vector<typename FIELD::ValueType>
116  list() const
117  {
118  std::vector<typename FIELD::ValueType> output;
119 
120  for (const Block& element : m_wire.elements()) {
121  if (element.type() != FIELD::TlvType::value) {
122  continue;
123  }
124  output.push_back(FIELD::decode(element));
125  }
126 
127  return output;
128  }
129 
134  template<typename FIELD>
135  Packet&
136  set(const typename FIELD::ValueType& value)
137  {
138  clear<FIELD>();
139  return add<FIELD>(value);
140  }
141 
146  template<typename FIELD>
147  Packet&
148  add(const typename FIELD::ValueType& value)
149  {
150  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
151  NDN_THROW(std::invalid_argument("lp::Packet::add: field cannot be repeated"));
152  }
153 
154  EncodingEstimator estimator;
155  size_t estimatedSize = FIELD::encode(estimator, value);
156  EncodingBuffer buffer(estimatedSize, 0);
157  FIELD::encode(buffer, value);
158  Block block = buffer.block();
159 
160  auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
161  FIELD::TlvType::value, comparePos);
162  m_wire.insert(pos, block);
163 
164  return *this;
165  }
166 
171  template<typename FIELD>
172  Packet&
173  remove(size_t index = 0)
174  {
175  size_t count = 0;
176  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
177  if (it->type() == FIELD::TlvType::value) {
178  if (count == index) {
179  m_wire.erase(it);
180  return *this;
181  }
182  count++;
183  }
184  }
185 
186  NDN_THROW(std::out_of_range("lp::Packet::remove: index out of range"));
187  }
188 
192  template<typename FIELD>
193  Packet&
195  {
196  m_wire.remove(FIELD::TlvType::value);
197  return *this;
198  }
199 
200 private:
201  static bool
202  comparePos(uint64_t first, const Block& second) noexcept;
203 
204 private:
205  mutable Block m_wire;
206 };
207 
208 } // namespace lp
209 } // namespace ndn
210 
211 #endif // NDN_CXX_LP_PACKET_HPP
Definition: data.cpp:26
std::vector< typename FIELD::ValueType > list() const
Definition: packet.hpp:116
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Definition: packet.hpp:173
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:148
size_t count() const
Definition: packet.hpp:84
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
bool has() const
Definition: packet.hpp:74
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:464
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:378
#define NDN_THROW(e)
Definition: exception.hpp:61
Packet & clear()
remove all occurrences of FIELD
Definition: packet.hpp:194
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:133
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:119
bool empty() const
Definition: packet.hpp:62
EncodingImpl< EncoderTag > EncodingBuffer
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
EncodingImpl< EstimatorTag > EncodingEstimator