NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
meta-info.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 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 #include "meta-info.hpp"
25 
26 namespace ndn {
27 
28 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<MetaInfo>));
29 BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>));
31 BOOST_CONCEPT_ASSERT((WireDecodable<MetaInfo>));
32 static_assert(std::is_base_of<tlv::Error, MetaInfo::Error>::value,
33  "MetaInfo::Error must inherit from tlv::Error");
34 
36  : m_type(tlv::ContentType_Blob)
37  , m_freshnessPeriod(DEFAULT_FRESHNESS_PERIOD)
38 {
39 }
40 
42 {
43  wireDecode(block);
44 }
45 
46 MetaInfo&
47 MetaInfo::setType(uint32_t type)
48 {
49  m_wire.reset();
50  m_type = type;
51  return *this;
52 }
53 
54 MetaInfo&
55 MetaInfo::setFreshnessPeriod(time::milliseconds freshnessPeriod)
56 {
57  if (freshnessPeriod < time::milliseconds::zero()) {
58  BOOST_THROW_EXCEPTION(std::invalid_argument("FreshnessPeriod must be >= 0"));
59  }
60  m_wire.reset();
61  m_freshnessPeriod = freshnessPeriod;
62  return *this;
63 }
64 
65 MetaInfo&
67 {
68  m_wire.reset();
69  m_finalBlockId = std::move(finalBlockId);
70  return *this;
71 }
72 
73 MetaInfo&
75 {
76  if (finalBlockId.isGeneric() && finalBlockId.empty()) {
77  return setFinalBlock(nullopt);
78  }
79  return setFinalBlock(finalBlockId);
80 }
81 
82 const std::list<Block>&
84 {
85  return m_appMetaInfo;
86 }
87 
88 MetaInfo&
89 MetaInfo::setAppMetaInfo(const std::list<Block>& info)
90 {
91  for (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) {
92  if (!(128 <= i->type() && i->type() <= 252))
93  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
94  "[128, 252]"));
95  }
96 
97  m_wire.reset();
98  m_appMetaInfo = info;
99  return *this;
100 }
101 
102 MetaInfo&
104 {
105  if (!(128 <= block.type() && block.type() <= 252))
106  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
107  "[128, 252]"));
108 
109  m_wire.reset();
110  m_appMetaInfo.push_back(block);
111  return *this;
112 }
113 
114 bool
116 {
117  for (std::list<Block>::iterator iter = m_appMetaInfo.begin();
118  iter != m_appMetaInfo.end(); ++iter) {
119  if (iter->type() == tlvType) {
120  m_wire.reset();
121  m_appMetaInfo.erase(iter);
122  return true;
123  }
124  }
125  return false;
126 }
127 
128 const Block*
130 {
131  for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin();
132  iter != m_appMetaInfo.end(); ++iter) {
133  if (iter->type() == tlvType) {
134  return &*iter;
135  }
136  }
137  return 0;
138 }
139 
140 template<encoding::Tag TAG>
141 size_t
143 {
144  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
145  // ContentType?
146  // FreshnessPeriod?
147  // FinalBlockId?
148  // AppMetaInfo*
149 
150  size_t totalLength = 0;
151 
152  for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
153  appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
154  totalLength += encoder.prependBlock(*appMetaInfoItem);
155  }
156 
157  // FinalBlockId
158  if (m_finalBlockId) {
159  totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
160  }
161 
162  // FreshnessPeriod
163  if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
164  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod.count());
165  }
166 
167  // ContentType
168  if (m_type != tlv::ContentType_Blob) {
169  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
170  }
171 
172  totalLength += encoder.prependVarNumber(totalLength);
173  totalLength += encoder.prependVarNumber(tlv::MetaInfo);
174  return totalLength;
175 }
176 
178 
179 const Block&
181 {
182  if (m_wire.hasWire())
183  return m_wire;
184 
185  EncodingEstimator estimator;
186  size_t estimatedSize = wireEncode(estimator);
187 
188  EncodingBuffer buffer(estimatedSize, 0);
189  wireEncode(buffer);
190 
191  m_wire = buffer.block();
192  return m_wire;
193 }
194 
195 void
197 {
198  m_wire = wire;
199  m_wire.parse();
200 
201  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
202  // ContentType?
203  // FreshnessPeriod?
204  // FinalBlockId?
205  // AppMetaInfo*
206 
207 
209 
210  // ContentType
211  if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
212  m_type = readNonNegativeIntegerAs<uint32_t>(*val);
213  ++val;
214  }
215  else {
216  m_type = tlv::ContentType_Blob;
217  }
218 
219  // FreshnessPeriod
220  if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
221  m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
222  ++val;
223  }
224  else {
225  m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
226  }
227 
228  // FinalBlockId
229  if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
230  m_finalBlockId.emplace(val->blockFromValue());
231  ++val;
232  }
233  else {
234  m_finalBlockId = nullopt;
235  }
236 
237  // AppMetaInfo (if any)
238  for (; val != m_wire.elements().end(); ++val) {
239  m_appMetaInfo.push_back(*val);
240  }
241 }
242 
243 std::ostream&
244 operator<<(std::ostream& os, const MetaInfo& info)
245 {
246  // ContentType
247  os << "ContentType: " << info.getType();
248 
249  // FreshnessPeriod
250  if (info.getFreshnessPeriod() > time::milliseconds::zero()) {
251  os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
252  }
253 
254  // FinalBlockId
255  if (info.getFinalBlock()) {
256  os << ", FinalBlockId: ";
257  info.getFinalBlock()->toUri(os);
258  }
259 
260  // App-defined MetaInfo items
261  for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin();
262  iter != info.getAppMetaInfo().end(); ++iter) {
263  os << ", AppMetaInfoTlvType: " << iter->type();
264  }
265 
266  return os;
267 }
268 
269 } // namespace ndn
constexpr nullopt_t nullopt
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:252
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
const Block * findAppMetaInfo(uint32_t tlvType) const
Find a first app-defined MetaInfo item of type tlvType.
Definition: meta-info.cpp:129
MetaInfo & setFinalBlock(optional< name::Component > finalBlockId)
set FinalBlockId
Definition: meta-info.cpp:66
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:336
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
MetaInfo & setFreshnessPeriod(time::milliseconds freshnessPeriod)
set FreshnessPeriod
Definition: meta-info.cpp:55
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
Table::const_iterator iterator
Definition: cs-internal.hpp:41
uint32_t getType() const
return ContentType
Definition: meta-info.hpp:95
NDN_CXX_DEPRECATED MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
set FinalBlockId
Definition: meta-info.cpp:74
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
Definition: meta-info.hpp:34
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
bool removeAppMetaInfo(uint32_t tlvType)
Remove a first app-defined MetaInfo item with type tlvType.
Definition: meta-info.cpp:115
const Block & wireEncode() const
Definition: meta-info.cpp:180
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
time::milliseconds getFreshnessPeriod() const
return FreshnessPeriod
Definition: meta-info.hpp:111
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:258
const optional< name::Component > & getFinalBlock() const
return FinalBlockId
Definition: meta-info.hpp:125
const std::list< Block > & getAppMetaInfo() const
Get all app-defined MetaInfo items.
Definition: meta-info.cpp:83
indicates content is the actual data bits
Definition: tlv.hpp:140
uint64_t tlvType
TLV-TYPE of the field; 0 if field does not exist.
Definition: packet.cpp:61
Represents a name component.
bool isGeneric() const
Check if the component is GenericComponent.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
MetaInfo & setAppMetaInfo(const std::list< Block > &info)
Set app-defined MetaInfo items.
Definition: meta-info.cpp:89
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
MetaInfo & addAppMetaInfo(const Block &block)
Add an app-defined MetaInfo item.
Definition: meta-info.cpp:103
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
EncodingImpl< EstimatorTag > EncodingEstimator
void wireDecode(const Block &wire)
Definition: meta-info.cpp:196