NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
block.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  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22  */
23 
24 #ifndef NDN_ENCODING_BLOCK_HPP
25 #define NDN_ENCODING_BLOCK_HPP
26 
27 #include "buffer.hpp"
28 #include "encoding-buffer-fwd.hpp"
29 #include "tlv.hpp"
30 
31 namespace boost {
32 namespace asio {
33 class const_buffer;
34 } // namespace asio
35 } // namespace boost
36 
37 namespace ndn {
38 
42 class Block
43 {
44 public:
45  using element_container = std::vector<Block>;
47  using element_const_iterator = element_container::const_iterator;
48 
49  class Error : public tlv::Error
50  {
51  public:
52  explicit
53  Error(const std::string& what)
54  : tlv::Error(what)
55  {
56  }
57  };
58 
59 public: // constructor, creation, assignment
62  Block();
63 
68  explicit
69  Block(const EncodingBuffer& buffer);
70 
76  explicit
77  Block(const ConstBufferPtr& buffer);
78 
88  Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
89  bool verifyLength = true);
90 
99  Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
100  bool verifyLength = true);
101 
110  Block(ConstBufferPtr buffer, uint32_t type,
111  Buffer::const_iterator begin, Buffer::const_iterator end,
112  Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
113 
120  Block(const uint8_t* buf, size_t bufSize);
121 
125  explicit
126  Block(uint32_t type);
127 
132  Block(uint32_t type, ConstBufferPtr value);
133 
138  Block(uint32_t type, const Block& value);
139 
144  static Block
145  fromStream(std::istream& is);
146 
153  static std::tuple<bool, Block>
154  fromBuffer(ConstBufferPtr buffer, size_t offset);
155 
163  static std::tuple<bool, Block>
164  fromBuffer(const uint8_t* buf, size_t bufSize);
165 
166 public: // wire format
172  bool
173  empty() const
174  {
175  return m_type == std::numeric_limits<uint32_t>::max();
176  }
177 
183  bool
184  hasWire() const;
185 
189  void
190  reset();
191 
196  void
197  resetWire();
198 
202  Buffer::const_iterator
203  begin() const;
204 
208  Buffer::const_iterator
209  end() const;
210 
214  const uint8_t*
215  wire() const;
216 
220  size_t
221  size() const;
222 
225  shared_ptr<const Buffer>
226  getBuffer() const
227  {
228  return m_buffer;
229  }
230 
231 public: // type and value
234  uint32_t
235  type() const
236  {
237  return m_type;
238  }
239 
245  bool
246  hasValue() const
247  {
248  return m_buffer != nullptr;
249  }
250 
254  Buffer::const_iterator
255  value_begin() const
256  {
257  return m_valueBegin;
258  }
259 
263  Buffer::const_iterator
264  value_end() const
265  {
266  return m_valueEnd;
267  }
268 
271  const uint8_t*
272  value() const;
273 
276  size_t
277  value_size() const;
278 
279  Block
280  blockFromValue() const;
281 
282 public: // sub elements
290  void
291  parse() const;
292 
296  void
297  encode();
298 
303  const Block&
304  get(uint32_t type) const;
305 
311  find(uint32_t type) const;
312 
317  void
318  remove(uint32_t type);
319 
323  erase(element_const_iterator position);
324 
329 
332  void
333  push_back(const Block& element);
334 
341  insert(element_const_iterator pos, const Block& element);
342 
346  const element_container&
347  elements() const
348  {
349  return m_elements;
350  }
351 
356  {
357  return m_elements.begin();
358  }
359 
363  elements_end() const
364  {
365  return m_elements.end();
366  }
367 
370  size_t
372  {
373  return m_elements.size();
374  }
375 
376 public: // misc
379  operator boost::asio::const_buffer() const;
380 
381 private:
384  size_t
385  encode(EncodingEstimator& estimator) const;
386 
391  size_t
392  encode(EncodingBuffer& encoder);
393 
394 protected:
403  shared_ptr<const Buffer> m_buffer;
404  Buffer::const_iterator m_begin;
405  Buffer::const_iterator m_end;
406 
407  Buffer::const_iterator m_valueBegin;
408  Buffer::const_iterator m_valueEnd;
409 
410  uint32_t m_type;
411 
416  uint32_t m_size;
417 
423 };
424 
427 bool
428 operator==(const Block& lhs, const Block& rhs);
429 
430 inline bool
431 operator!=(const Block& lhs, const Block& rhs)
432 {
433  return !(lhs == rhs);
434 }
435 
436 } // namespace ndn
437 
438 #endif // NDN_ENCODING_BLOCK_HPP
shared_ptr< const Buffer > m_buffer
underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields
Definition: block.hpp:403
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:251
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Definition: block.cpp:164
Copyright (c) 2011-2015 Regents of the University of California.
Error(const std::string &what)
Definition: block.hpp:53
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
Definition: block.cpp:196
Buffer::const_iterator m_valueBegin
Definition: block.hpp:407
Buffer::const_iterator m_begin
Definition: block.hpp:404
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:318
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:291
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:335
element_container m_elements
sub elements
Definition: block.hpp:422
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
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
const uint8_t * value() const
Get pointer to TLV-VALUE.
Definition: block.cpp:312
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:259
element_iterator erase(element_const_iterator position)
Erase a sub element.
Definition: block.cpp:446
void resetWire()
Reset wire buffer but keep TLV-TYPE and sub elements (if any)
Definition: block.cpp:266
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
uint32_t m_size
total size including Type-Length-Value
Definition: block.hpp:416
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:255
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Buffer::const_iterator m_valueEnd
Definition: block.hpp:408
shared_ptr< const Buffer > getBuffer() const
Get underlying buffer.
Definition: block.hpp:226
bool empty() const
Check if the Block is empty.
Definition: block.hpp:173
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:300
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Definition: block.cpp:429
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:264
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
Definition: block.cpp:273
element_container::iterator element_iterator
Definition: block.hpp:46
void reset()
Reset wire buffer of the element.
Definition: block.cpp:257
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:476
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Definition: block.cpp:282
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:371
void encode()
Encode sub elements into TLV-VALUE.
Definition: block.cpp:363
Block blockFromValue() const
Definition: block.cpp:324
Buffer::const_iterator m_end
Definition: block.hpp:405
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:265
uint32_t m_type
TLV-TYPE.
Definition: block.hpp:410
bool hasValue() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:246
Block()
Create an empty Block.
Definition: block.cpp:50
std::vector< Block > element_container
Definition: block.hpp:45
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:89