NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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
ndn::lp::Packet
Definition: packet.hpp:31
ndn::Block::elements
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
ndn::Block::insert
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:464
ndn::lp::Packet::add
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:148
ndn::lp::Packet::has
NDN_CXX_NODISCARD bool has() const
Definition: packet.hpp:74
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::lp::Packet::count
NDN_CXX_NODISCARD size_t count() const
Definition: packet.hpp:84
ndn::lp::Packet::Packet
Packet()
Definition: packet.cpp:108
ndn::lp::Packet::remove
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Definition: packet.hpp:173
ndn::lp::Packet::list
NDN_CXX_NODISCARD std::vector< typename FIELD::ValueType > list() const
Definition: packet.hpp:116
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::lp::Packet::empty
NDN_CXX_NODISCARD bool empty() const
Definition: packet.hpp:62
ndn::Block::remove
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
Definition: block.cpp:433
ndn::lp::Packet::set
Packet & set(const typename FIELD::ValueType &value)
remove all occurrences of FIELD, and add a FIELD with value
Definition: packet.hpp:136
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::Block::erase
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Definition: block.cpp:443
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::lp::Packet::wireDecode
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:133
fields.hpp
ndn::lp::Packet::Error
Definition: packet.hpp:34
ndn::lp::Packet::wireEncode
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:119
ndn::lp::Packet::clear
Packet & clear()
remove all occurrences of FIELD
Definition: packet.hpp:194
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_size
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::lp
Definition: cache-policy.cpp:28
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::tlv::Error::Error
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
ndn::lp::Packet::get
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:96
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38