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>));
30 BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<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&
66 MetaInfo::setFinalBlock(optional<name::Component> finalBlockId)
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 (const auto& block : info) {
92  if (block.type() < 128 || block.type() > 252)
93  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range [128, 252]"));
94  }
95 
96  m_wire.reset();
97  m_appMetaInfo = info;
98  return *this;
99 }
100 
101 MetaInfo&
103 {
104  if (!(128 <= block.type() && block.type() <= 252))
105  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
106  "[128, 252]"));
107 
108  m_wire.reset();
109  m_appMetaInfo.push_back(block);
110  return *this;
111 }
112 
113 bool
115 {
116  for (auto it = m_appMetaInfo.begin(); it != m_appMetaInfo.end(); ++it) {
117  if (it->type() == tlvType) {
118  m_wire.reset();
119  m_appMetaInfo.erase(it);
120  return true;
121  }
122  }
123  return false;
124 }
125 
126 const Block*
128 {
129  auto it = std::find_if(m_appMetaInfo.begin(), m_appMetaInfo.end(),
130  [=] (const Block& b) { return b.type() == tlvType; });
131  return it != m_appMetaInfo.end() ? &*it : nullptr;
132 }
133 
134 template<encoding::Tag TAG>
135 size_t
136 MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
137 {
138  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
139  // ContentType?
140  // FreshnessPeriod?
141  // FinalBlockId?
142  // AppMetaInfo*
143 
144  size_t totalLength = 0;
145 
146  for (auto it = m_appMetaInfo.rbegin(); it != m_appMetaInfo.rend(); ++it) {
147  totalLength += encoder.prependBlock(*it);
148  }
149 
150  // FinalBlockId
151  if (m_finalBlockId) {
152  totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
153  }
154 
155  // FreshnessPeriod
156  if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
158  static_cast<uint64_t>(m_freshnessPeriod.count()));
159  }
160 
161  // ContentType
162  if (m_type != tlv::ContentType_Blob) {
163  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
164  }
165 
166  totalLength += encoder.prependVarNumber(totalLength);
167  totalLength += encoder.prependVarNumber(tlv::MetaInfo);
168  return totalLength;
169 }
170 
172 
173 const Block&
175 {
176  if (m_wire.hasWire())
177  return m_wire;
178 
179  EncodingEstimator estimator;
180  size_t estimatedSize = wireEncode(estimator);
181 
182  EncodingBuffer buffer(estimatedSize, 0);
183  wireEncode(buffer);
184 
185  m_wire = buffer.block();
186  return m_wire;
187 }
188 
189 void
191 {
192  m_wire = wire;
193  m_wire.parse();
194 
195  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
196  // ContentType?
197  // FreshnessPeriod?
198  // FinalBlockId?
199  // AppMetaInfo*
200 
201  auto val = m_wire.elements_begin();
202 
203  // ContentType
204  if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
205  m_type = readNonNegativeIntegerAs<uint32_t>(*val);
206  ++val;
207  }
208  else {
209  m_type = tlv::ContentType_Blob;
210  }
211 
212  // FreshnessPeriod
213  if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
214  m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
215  ++val;
216  }
217  else {
218  m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
219  }
220 
221  // FinalBlockId
222  if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
223  m_finalBlockId.emplace(val->blockFromValue());
224  ++val;
225  }
226  else {
227  m_finalBlockId = nullopt;
228  }
229 
230  // AppMetaInfo (if any)
231  for (; val != m_wire.elements().end(); ++val) {
232  m_appMetaInfo.push_back(*val);
233  }
234 }
235 
236 std::ostream&
237 operator<<(std::ostream& os, const MetaInfo& info)
238 {
239  // ContentType
240  os << "ContentType: " << info.getType();
241 
242  // FreshnessPeriod
243  if (info.getFreshnessPeriod() > 0_ms) {
244  os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
245  }
246 
247  // FinalBlockId
248  if (info.getFinalBlock()) {
249  os << ", FinalBlockId: ";
250  info.getFinalBlock()->toUri(os);
251  }
252 
253  // App-defined MetaInfo items
254  for (const auto& block : info.getAppMetaInfo()) {
255  os << ", AppMetaInfoTlvType: " << block.type();
256  }
257 
258  return os;
259 }
260 
261 } // namespace ndn
bool isGeneric() const
Check if the component is GenericComponent.
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
const optional< name::Component > & getFinalBlock() const
return FinalBlockId
Definition: meta-info.hpp:121
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
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
MetaInfo & setFreshnessPeriod(time::milliseconds freshnessPeriod)
set FreshnessPeriod
Definition: meta-info.cpp:55
const Block & wireEncode() const
Definition: meta-info.cpp:174
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
set FinalBlockId
Definition: meta-info.cpp:74
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
Definition: meta-info.hpp:34
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
time::milliseconds getFreshnessPeriod() const
return FreshnessPeriod
Definition: meta-info.hpp:107
bool removeAppMetaInfo(uint32_t tlvType)
Remove a first app-defined MetaInfo item with type tlvType.
Definition: meta-info.cpp:114
const std::list< Block > & getAppMetaInfo() const
Get all app-defined MetaInfo items.
Definition: meta-info.cpp:83
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:255
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:333
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
uint64_t tlvType
TLV-TYPE of the field; 0 if field does not exist.
Definition: packet.cpp:62
Represents a name component.
const Block * findAppMetaInfo(uint32_t tlvType) const
Find a first app-defined MetaInfo item of type tlvType.
Definition: meta-info.cpp:127
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:249
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:377
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:369
MetaInfo & setAppMetaInfo(const std::list< Block > &info)
Set app-defined MetaInfo items.
Definition: meta-info.cpp:89
uint32_t getType() const
return ContentType
Definition: meta-info.hpp:91
MetaInfo & addAppMetaInfo(const Block &block)
Add an app-defined MetaInfo item.
Definition: meta-info.cpp:102
EncodingImpl< EncoderTag > EncodingBuffer
const nullopt_t nullopt((nullopt_t::init()))
EncodingImpl< EstimatorTag > EncodingEstimator
void wireDecode(const Block &wire)
Definition: meta-info.cpp:190