NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
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  template<encoding::Tag TAG>
52  size_t
53  wireEncode(EncodingImpl<TAG>& encoder) const;
54 
58  const Block&
59  wireEncode() const;
60 
64  void
65  wireDecode(const Block& wire);
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  m_wire.parse();
87 
88  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
89  [] (const Block& block) {
90  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  m_wire.parse();
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  m_wire.parse();
126 
127  for (const Block& element : m_wire.elements()) {
128  if (element.type() != FIELD::TlvType::value) {
129  continue;
130  }
131  output.push_back(FIELD::decode(element));
132  }
133 
134  return output;
135  }
136 
141  template<typename FIELD>
142  Packet&
143  set(const typename FIELD::ValueType& value)
144  {
145  clear<FIELD>();
146  return add<FIELD>(value);
147  }
148 
153  template<typename FIELD>
154  Packet&
155  add(const typename FIELD::ValueType& value)
156  {
157  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
158  BOOST_THROW_EXCEPTION(std::length_error("Field cannot be repeated"));
159  }
160 
161  EncodingEstimator estimator;
162  size_t estimatedSize = FIELD::encode(estimator, value);
163  EncodingBuffer buffer(estimatedSize, 0);
164  FIELD::encode(buffer, value);
165  Block block = buffer.block();
166 
167  Block::element_const_iterator pos = std::lower_bound(m_wire.elements_begin(),
168  m_wire.elements_end(),
169  FIELD::TlvType::value,
170  comparePos);
171  m_wire.insert(pos, block);
172 
173  return *this;
174  }
175 
180  template<typename FIELD>
181  Packet&
182  remove(size_t index = 0)
183  {
184  m_wire.parse();
185 
186  size_t count = 0;
187  for (Block::element_const_iterator it = m_wire.elements_begin(); it != m_wire.elements_end();
188  ++it) {
189  if (it->type() == FIELD::TlvType::value) {
190  if (count == index) {
191  m_wire.erase(it);
192  return *this;
193  }
194  count++;
195  }
196  }
197 
198  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
199  }
200 
204  template<typename FIELD>
205  Packet&
207  {
208  m_wire.parse();
209  m_wire.remove(FIELD::TlvType::value);
210  return *this;
211  }
212 
213 private:
214  static bool
215  comparePos(const Block& first, const uint64_t second);
216 
217 private:
218  mutable Block m_wire;
219 };
220 
221 } // namespace lp
222 } // namespace ndn
223 
224 #endif // NDN_CXX_LP_PACKET_HPP
const Block & wireEncode() const
encode packet into wire format
Definition: packet.cpp:67
Copyright (c) 2011-2015 Regents of the University of California.
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Definition: packet.hpp:182
EncodingImpl< EstimatorTag > EncodingEstimator
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:155
const element_container & elements() const
Get all subelements.
Definition: block.hpp:364
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
bool has() const
Definition: packet.hpp:74
element_iterator insert(element_const_iterator pos, const Block &element)
insert Insert a new element in a specific position
Definition: block.cpp:575
Error(const std::string &what)
Definition: packet.hpp:37
Packet & clear()
remove all occurrences of FIELD
Definition: packet.hpp:206
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:84
EncodingImpl< EncoderTag > EncodingBuffer
size_t count() const
Definition: packet.hpp:84
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
std::vector< typename FIELD::ValueType > list() const
Definition: packet.hpp:121
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50