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-2018 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:
36  using ndn::tlv::Error::Error;
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 
61  bool
62  empty() const
63  {
64  return m_wire.elements_size() == 0;
65  }
66 
67 public: // field access
72  template<typename FIELD>
73  bool
74  has() const
75  {
76  return count<FIELD>() > 0;
77  }
78 
82  template<typename FIELD>
83  size_t
84  count() const
85  {
86  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
87  [] (const Block& block) {
88  return block.type() == FIELD::TlvType::value; });
89  }
90 
95  template<typename FIELD>
96  typename FIELD::ValueType
97  get(size_t index = 0) const
98  {
99  size_t count = 0;
100  for (const Block& element : m_wire.elements()) {
101  if (element.type() != FIELD::TlvType::value) {
102  continue;
103  }
104  if (count++ == index) {
105  return FIELD::decode(element);
106  }
107  }
108 
109  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
110  }
111 
115  template<typename FIELD>
116  std::vector<typename FIELD::ValueType>
117  list() const
118  {
119  std::vector<typename FIELD::ValueType> output;
120 
121  for (const Block& element : m_wire.elements()) {
122  if (element.type() != FIELD::TlvType::value) {
123  continue;
124  }
125  output.push_back(FIELD::decode(element));
126  }
127 
128  return output;
129  }
130 
135  template<typename FIELD>
136  Packet&
137  set(const typename FIELD::ValueType& value)
138  {
139  clear<FIELD>();
140  return add<FIELD>(value);
141  }
142 
147  template<typename FIELD>
148  Packet&
149  add(const typename FIELD::ValueType& value)
150  {
151  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
152  BOOST_THROW_EXCEPTION(std::length_error("Field cannot be repeated"));
153  }
154 
155  EncodingEstimator estimator;
156  size_t estimatedSize = FIELD::encode(estimator, value);
157  EncodingBuffer buffer(estimatedSize, 0);
158  FIELD::encode(buffer, value);
159  Block block = buffer.block();
160 
161  auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
162  FIELD::TlvType::value, comparePos);
163  m_wire.insert(pos, block);
164 
165  return *this;
166  }
167 
172  template<typename FIELD>
173  Packet&
174  remove(size_t index = 0)
175  {
176  size_t count = 0;
177  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
178  if (it->type() == FIELD::TlvType::value) {
179  if (count == index) {
180  m_wire.erase(it);
181  return *this;
182  }
183  count++;
184  }
185  }
186 
187  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
188  }
189 
193  template<typename FIELD>
194  Packet&
196  {
197  m_wire.remove(FIELD::TlvType::value);
198  return *this;
199  }
200 
201 private:
202  static bool
203  comparePos(uint64_t first, const Block& second) noexcept;
204 
205 private:
206  mutable Block m_wire;
207 };
208 
209 } // namespace lp
210 } // namespace ndn
211 
212 #endif // NDN_CXX_LP_PACKET_HPP
Copyright (c) 2011-2015 Regents of the University of California.
std::vector< typename FIELD::ValueType > list() const
Definition: packet.hpp:117
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Definition: packet.hpp:174
Packet & set(const typename FIELD::ValueType &value)
remove all occurrences of FIELD, and add a FIELD with value
Definition: packet.hpp:137
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:149
size_t count() const
Definition: packet.hpp:84
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool has() const
Definition: packet.hpp:74
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:369
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub element.
Definition: block.cpp:472
element_iterator erase(element_const_iterator position)
Erase a sub element.
Definition: block.cpp:451
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:377
FIELD::ValueType get(size_t index=0) const
Definition: packet.hpp:97
Packet & clear()
remove all occurrences of FIELD
Definition: packet.hpp:195
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:133
void remove(uint32_t type)
Remove all sub elements of specified TLV-TYPE.
Definition: block.cpp:441
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:385
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:119
bool empty() const
Definition: packet.hpp:62
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator