NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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  Block
59  wireEncode() const;
60 
65  void
66  wireDecode(const Block& wire);
67 
68 public: // field access
73  template<typename FIELD>
74  bool
75  has() const
76  {
77  return count<FIELD>() > 0;
78  }
79 
83  template<typename FIELD>
84  size_t
85  count() const
86  {
87  m_wire.parse();
88 
89  return std::count_if(m_wire.elements_begin(), m_wire.elements_end(),
90  [] (const Block& block) {
91  return block.type() == FIELD::TlvType::value; });
92  }
93 
98  template<typename FIELD>
99  typename FIELD::ValueType
100  get(size_t index = 0) const
101  {
102  m_wire.parse();
103 
104  size_t count = 0;
105  for (const Block& element : m_wire.elements()) {
106  if (element.type() != FIELD::TlvType::value) {
107  continue;
108  }
109  if (count++ == index) {
110  return FIELD::decode(element);
111  }
112  }
113 
114  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
115  }
116 
120  template<typename FIELD>
121  std::vector<typename FIELD::ValueType>
122  list() const
123  {
124  std::vector<typename FIELD::ValueType> output;
125 
126  m_wire.parse();
127 
128  for (const Block& element : m_wire.elements()) {
129  if (element.type() != FIELD::TlvType::value) {
130  continue;
131  }
132  output.push_back(FIELD::decode(element));
133  }
134 
135  return output;
136  }
137 
142  template<typename FIELD>
143  Packet&
144  set(const typename FIELD::ValueType& value)
145  {
146  clear<FIELD>();
147  return add<FIELD>(value);
148  }
149 
154  template<typename FIELD>
155  Packet&
156  add(const typename FIELD::ValueType& value)
157  {
158  if (!FIELD::IsRepeatable::value && has<FIELD>()) {
159  BOOST_THROW_EXCEPTION(std::length_error("Field cannot be repeated"));
160  }
161 
162  EncodingEstimator estimator;
163  size_t estimatedSize = FIELD::encode(estimator, value);
164  EncodingBuffer buffer(estimatedSize, 0);
165  FIELD::encode(buffer, value);
166  Block block = buffer.block();
167 
168  Block::element_const_iterator pos = std::lower_bound(m_wire.elements_begin(),
169  m_wire.elements_end(),
170  FIELD::TlvType::value,
171  comparePos);
172  m_wire.insert(pos, block);
173 
174  return *this;
175  }
176 
181  template<typename FIELD>
182  Packet&
183  remove(size_t index = 0)
184  {
185  m_wire.parse();
186 
187  size_t count = 0;
188  for (Block::element_const_iterator it = m_wire.elements_begin(); it != m_wire.elements_end();
189  ++it) {
190  if (it->type() == FIELD::TlvType::value) {
191  if (count == index) {
192  m_wire.erase(it);
193  return *this;
194  }
195  count++;
196  }
197  }
198 
199  BOOST_THROW_EXCEPTION(std::out_of_range("Index out of range"));
200  }
201 
205  template<typename FIELD>
206  Packet&
208  {
209  m_wire.parse();
210  m_wire.remove(FIELD::TlvType::value);
211  return *this;
212  }
213 
214 private:
215  static bool
216  comparePos(const Block& first, const uint64_t second);
217 
218 private:
219  mutable Block m_wire;
220 };
221 
222 } // namespace lp
223 } // namespace ndn
224 
225 #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:122
Packet & remove(size_t index=0)
remove the index-th occurrence of FIELD
Definition: packet.hpp:183
EncodingImpl< EstimatorTag > EncodingEstimator
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
Definition: packet.hpp:156
size_t count() const
Definition: packet.hpp:85
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
bool has() const
Definition: packet.hpp:75
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:207
void wireDecode(const Block &wire)
decode packet from wire format
Definition: packet.cpp:92
EncodingImpl< EncoderTag > EncodingBuffer
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
Block wireEncode() const
encode packet into wire format
Definition: packet.cpp:67
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50