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-2017 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 "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  explicit
37  Error(const std::string& what)
38  : ndn::tlv::Error(what)
39  {
40  }
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  bool
66  empty() const
67  {
68  return m_wire.elements_size() == 0;
69  }
70 
71 public: // field access
76  template<typename FIELD>
77  bool
78  has() const
79  {
80  return count<FIELD>() > 0;
81  }
82 
86  template<typename FIELD>
87  size_t
88  count() const
89  {
90  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
91  [] (const Block& block) {
92  return block.type() == FIELD::TlvType::value; });
93  }
94 
99  template<typename FIELD>
100  typename FIELD::ValueType
101  get(size_t index = 0) const
102  {
103  size_t count = 0;
104  for (const Block& element : m_wire.elements()) {
105  if (element.type() != FIELD::TlvType::value) {
106  continue;
107  }
108  if (count++ == index) {
109  return FIELD::decode(element);
110  }
111  }
112 
113  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
114  }
115 
119  template<typename FIELD>
120  std::vector<typename FIELD::ValueType>
121  list() const
122  {
123  std::vector<typename FIELD::ValueType> output;
124 
125  for (const Block& element : m_wire.elements()) {
126  if (element.type() != FIELD::TlvType::value) {
127  continue;
128  }
129  output.push_back(FIELD::decode(element));
130  }
131 
132  return output;
133  }
134 
139  template<typename FIELD>
140  Packet&
141  set(const typename FIELD::ValueType& value)
142  {
143  clear<FIELD>();
144  return add<FIELD>(value);
145  }
146 
151  template<typename FIELD>
152  Packet&
153  add(const typename FIELD::ValueType& value)
154  {
155  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
156  BOOST_THROW_EXCEPTION(std::length_error("Field cannot be repeated"));
157  }
158 
159  EncodingEstimator estimator;
160  size_t estimatedSize = FIELD::encode(estimator, value);
161  EncodingBuffer buffer(estimatedSize, 0);
162  FIELD::encode(buffer, value);
163  Block block = buffer.block();
164 
165  auto pos = std::upper_bound(m_wire.elements_begin(), m_wire.elements_end(),
166  FIELD::TlvType::value, comparePos);
167  m_wire.insert(pos, block);
168 
169  return *this;
170  }
171 
176  template<typename FIELD>
177  Packet&
178  remove(size_t index = 0)
179  {
180  size_t count = 0;
181  for (auto it = m_wire.elements_begin(); it != m_wire.elements_end(); ++it) {
182  if (it->type() == FIELD::TlvType::value) {
183  if (count == index) {
184  m_wire.erase(it);
185  return *this;
186  }
187  count++;
188  }
189  }
190 
191  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
192  }
193 
197  template<typename FIELD>
198  Packet&
200  {
201  m_wire.remove(FIELD::TlvType::value);
202  return *this;
203  }
204 
205 private:
206  static bool
207  comparePos(uint64_t first, const Block& second);
208 
209 private:
210  mutable Block m_wire;
211 };
212 
213 } // namespace lp
214 } // namespace ndn
215 
216 #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:121
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:153
size_t count() const
Definition: packet.hpp:88
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool has() const
Definition: packet.hpp:78
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub element.
Definition: block.cpp:483
element_iterator erase(element_const_iterator position)
Erase a sub element.
Definition: block.cpp:446
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
Error(const std::string &what)
Definition: packet.hpp:37
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
Packet & clear()
remove all occurrences of FIELD
Definition: packet.hpp:199
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:145
void remove(uint32_t type)
Remove all sub elements of specified TLV-TYPE.
Definition: block.cpp:436
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:371
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:131
bool empty() const
Definition: packet.hpp:66
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
EncodingImpl< EstimatorTag > EncodingEstimator