NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
block-helpers.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "block-helpers.hpp"
23 
24 namespace ndn {
25 namespace encoding {
26 
27 template<Tag TAG>
28 size_t
29 prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value)
30 {
31  size_t valueLength = encoder.prependNonNegativeInteger(value);
32  size_t totalLength = valueLength;
33  totalLength += encoder.prependVarNumber(valueLength);
34  totalLength += encoder.prependVarNumber(type);
35 
36  return totalLength;
37 }
38 
39 template size_t
41  uint32_t type, uint64_t value);
42 
43 template size_t
45  uint32_t type, uint64_t value);
46 
47 
48 Block
49 makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
50 {
51  EncodingEstimator estimator;
52  size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
53 
54  EncodingBuffer encoder(totalLength, 0);
55  prependNonNegativeIntegerBlock(encoder, type, value);
56 
57  return encoder.block();
58 }
59 
60 uint64_t
62 {
63  Buffer::const_iterator begin = block.value_begin();
64  return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
65 }
66 
68 
69 template<Tag TAG>
70 size_t
71 prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type)
72 {
73  size_t totalLength = encoder.prependVarNumber(0);
74  totalLength += encoder.prependVarNumber(type);
75 
76  return totalLength;
77 }
78 
79 template size_t
81 
82 template size_t
84 
85 
86 Block
87 makeEmptyBlock(uint32_t type)
88 {
89  EncodingEstimator estimator;
90  size_t totalLength = prependEmptyBlock(estimator, type);
91 
92  EncodingBuffer encoder(totalLength, 0);
93  prependEmptyBlock(encoder, type);
94 
95  return encoder.block();
96 }
97 
99 
100 template<Tag TAG>
101 size_t
102 prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value)
103 {
104  size_t valueLength = encoder.prependByteArray(reinterpret_cast<const uint8_t*>(value.data()),
105  value.size());
106  size_t totalLength = valueLength;
107  totalLength += encoder.prependVarNumber(valueLength);
108  totalLength += encoder.prependVarNumber(type);
109 
110  return totalLength;
111 }
112 
113 template size_t
115  uint32_t type, const std::string& value);
116 
117 template size_t
119  uint32_t type, const std::string& value);
120 
121 
122 Block
123 makeStringBlock(uint32_t type, const std::string& value)
124 {
125  EncodingEstimator estimator;
126  size_t totalLength = prependStringBlock(estimator, type, value);
127 
128  EncodingBuffer encoder(totalLength, 0);
129  prependStringBlock(encoder, type, value);
130 
131  return encoder.block();
132 }
133 
134 std::string
135 readString(const Block& block)
136 {
137  return std::string(reinterpret_cast<const char*>(block.value()), block.value_size());
138 }
139 
141 
142 Block
143 makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length)
144 {
145  EncodingEstimator estimator;
146  size_t totalLength = estimator.prependByteArrayBlock(type, value, length);
147 
148  EncodingBuffer encoder(totalLength, 0);
149  encoder.prependByteArrayBlock(type, value, length);
150 
151  return encoder.block();
152 }
153 
154 Block
155 makeBinaryBlock(uint32_t type, const char* value, size_t length)
156 {
157  return makeBinaryBlock(type, reinterpret_cast<const uint8_t*>(value), length);
158 }
159 
160 } // namespace encoding
161 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
EncodingImpl specialization for real TLV encoding.
template size_t prependStringBlock< EncoderTag >(EncodingImpl< EncoderTag > &encoder, uint32_t type, const std::string &value)
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
Block makeEmptyBlock(uint32_t type)
Create a TLV block type type containing no value (i.e., a boolean block)
size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: estimator.cpp:105
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Helper to prepend TLV block type type with value from a string value.
std::string readString(const Block &block)
Helper to read a string value from a block.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block type type containing non-negative integer value.
Buffer::const_iterator value_begin() const
Definition: block.hpp:352
Buffer::const_iterator value_end() const
Definition: block.hpp:358
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
EncodingImpl specialization TLV size estimation.
template size_t prependStringBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &encoder, uint32_t type, const std::string &value)
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Helper to prepend TLV block type type containing no value (i.e., a boolean block) ...
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
template size_t prependEmptyBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &encoder, uint32_t type)
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block type type with value from a string .
template size_t prependNonNegativeIntegerBlock< EstimatorTag >(EncodingImpl< EstimatorTag > &encoder, uint32_t type, uint64_t value)
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:60
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Definition: tlv.hpp:433
size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: encoder.cpp:241
size_t value_size() const
Definition: block.cpp:529
template size_t prependNonNegativeIntegerBlock< EncoderTag >(EncodingImpl< EncoderTag > &encoder, uint32_t type, uint64_t value)
template size_t prependEmptyBlock< EncoderTag >(EncodingImpl< EncoderTag > &encoder, uint32_t type)
const uint8_t * value() const
Definition: block.cpp:520