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-2024 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 "ndn-cxx/meta-info.hpp"
24 
25 #include <algorithm>
26 #include <boost/range/adaptor/reversed.hpp>
27 
28 namespace ndn {
29 
30 MetaInfo::MetaInfo() = default;
31 
33 {
34  wireDecode(block);
35 }
36 
37 MetaInfo&
38 MetaInfo::setType(uint32_t type)
39 {
40  m_wire.reset();
41  m_type = type;
42  return *this;
43 }
44 
47 {
48  if (m_freshnessPeriod > static_cast<uint64_t>(time::milliseconds::max().count())) {
49  return time::milliseconds::max();
50  }
51  return time::milliseconds(m_freshnessPeriod);
52 }
53 
54 MetaInfo&
56 {
57  if (freshnessPeriod < 0_ms) {
58  NDN_THROW(std::invalid_argument("FreshnessPeriod must be >= 0"));
59  }
60  m_wire.reset();
61  m_freshnessPeriod = static_cast<uint64_t>(freshnessPeriod.count());
62  return *this;
63 }
64 
65 MetaInfo&
66 MetaInfo::setFinalBlock(std::optional<name::Component> finalBlockId)
67 {
68  m_wire.reset();
69  m_finalBlockId = std::move(finalBlockId);
70  return *this;
71 }
72 
73 const std::list<Block>&
75 {
76  return m_appMetaInfo;
77 }
78 
79 MetaInfo&
80 MetaInfo::setAppMetaInfo(const std::list<Block>& info)
81 {
82  for (const auto& block : info) {
83  if (block.type() < 128 || block.type() > 252)
84  NDN_THROW(Error("AppMetaInfo block has type outside the application range [128, 252]"));
85  }
86 
87  m_wire.reset();
88  m_appMetaInfo = info;
89  return *this;
90 }
91 
92 MetaInfo&
94 {
95  if (!(128 <= block.type() && block.type() <= 252))
96  NDN_THROW(Error("AppMetaInfo block has type outside the application range [128, 252]"));
97 
98  m_wire.reset();
99  m_appMetaInfo.push_back(block);
100  return *this;
101 }
102 
103 bool
105 {
106  for (auto it = m_appMetaInfo.begin(); it != m_appMetaInfo.end(); ++it) {
107  if (it->type() == tlvType) {
108  m_wire.reset();
109  m_appMetaInfo.erase(it);
110  return true;
111  }
112  }
113  return false;
114 }
115 
116 const Block*
118 {
119  auto it = std::find_if(m_appMetaInfo.begin(), m_appMetaInfo.end(),
120  [=] (const Block& b) { return b.type() == tlvType; });
121  return it != m_appMetaInfo.end() ? &*it : nullptr;
122 }
123 
124 template<encoding::Tag TAG>
125 size_t
127 {
128  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
129  // ContentType?
130  // FreshnessPeriod?
131  // FinalBlockId?
132  // AppMetaInfo*
133 
134  size_t totalLength = 0;
135 
136  // AppMetaInfo (in reverse order)
137  for (const auto& block : m_appMetaInfo | boost::adaptors::reversed) {
138  totalLength += prependBlock(encoder, block);
139  }
140 
141  // FinalBlockId
142  if (m_finalBlockId) {
143  totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
144  }
145 
146  // FreshnessPeriod
147  if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD.count()) {
148  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod);
149  }
150 
151  // ContentType
152  if (m_type != tlv::ContentType_Blob) {
153  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
154  }
155 
156  totalLength += encoder.prependVarNumber(totalLength);
157  totalLength += encoder.prependVarNumber(tlv::MetaInfo);
158  return totalLength;
159 }
160 
162 
163 const Block&
165 {
166  if (m_wire.hasWire())
167  return m_wire;
168 
169  EncodingEstimator estimator;
170  size_t estimatedSize = wireEncode(estimator);
171 
172  EncodingBuffer buffer(estimatedSize, 0);
173  wireEncode(buffer);
174 
175  m_wire = buffer.block();
176  return m_wire;
177 }
178 
179 void
181 {
182  m_wire = wire;
183  m_wire.parse();
184 
185  // MetaInfo = META-INFO-TYPE TLV-LENGTH
186  // [ContentType]
187  // [FreshnessPeriod]
188  // [FinalBlockId]
189  // *AppMetaInfo
190 
191  auto val = m_wire.elements_begin();
192 
193  // ContentType
194  if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
195  m_type = readNonNegativeIntegerAs<uint32_t>(*val);
196  ++val;
197  }
198  else {
199  m_type = tlv::ContentType_Blob;
200  }
201 
202  // FreshnessPeriod
203  if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
204  m_freshnessPeriod = readNonNegativeInteger(*val);
205  ++val;
206  }
207  else {
208  m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD.count();
209  }
210 
211  // FinalBlockId
212  if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
213  m_finalBlockId.emplace(val->blockFromValue());
214  ++val;
215  }
216  else {
217  m_finalBlockId = std::nullopt;
218  }
219 
220  // AppMetaInfo (if any)
221  for (; val != m_wire.elements().end(); ++val) {
222  m_appMetaInfo.push_back(*val);
223  }
224 }
225 
226 std::ostream&
227 operator<<(std::ostream& os, const MetaInfo& info)
228 {
229  // ContentType
230  os << "ContentType: " << info.getType();
231 
232  // FreshnessPeriod
233  if (info.getFreshnessPeriod() > 0_ms) {
234  os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
235  }
236 
237  // FinalBlockId
238  if (info.getFinalBlock()) {
239  os << ", FinalBlockId: ";
240  info.getFinalBlock()->toUri(os);
241  }
242 
243  // App-defined MetaInfo items
244  for (const auto& block : info.getAppMetaInfo()) {
245  os << ", AppMetaInfoTlvType: " << block.type();
246  }
247 
248  return os;
249 }
250 
251 } // namespace ndn
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition: block.hpp:433
const element_container & elements() const noexcept
Get container of sub-elements.
Definition: block.hpp:424
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:442
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:275
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:293
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:326
A MetaInfo holds the meta info which is signed inside the Data packet.
Definition: meta-info.hpp:62
void wireDecode(const Block &wire)
Definition: meta-info.cpp:180
MetaInfo & setFinalBlock(std::optional< name::Component > finalBlockId)
Set the FinalBlockId.
Definition: meta-info.cpp:66
MetaInfo & addAppMetaInfo(const Block &block)
Add an app-defined MetaInfo item.
Definition: meta-info.cpp:93
MetaInfo & setAppMetaInfo(const std::list< Block > &info)
Set app-defined MetaInfo items.
Definition: meta-info.cpp:80
MetaInfo & setType(uint32_t type)
Set the ContentType.
Definition: meta-info.cpp:38
const Block & wireEncode() const
Definition: meta-info.cpp:164
const Block * findAppMetaInfo(uint32_t tlvType) const
Find a first app-defined MetaInfo item of type tlvType.
Definition: meta-info.cpp:117
time::milliseconds getFreshnessPeriod() const noexcept
Return the value of FreshnessPeriod.
Definition: meta-info.cpp:46
MetaInfo & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Set the FreshnessPeriod.
Definition: meta-info.cpp:55
const std::list< Block > & getAppMetaInfo() const
Get all app-defined MetaInfo items.
Definition: meta-info.cpp:74
bool removeAppMetaInfo(uint32_t tlvType)
Remove a first app-defined MetaInfo item with type tlvType.
Definition: meta-info.cpp:104
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
EncodingImpl< EstimatorTag > EncodingEstimator
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV element containing a nested TLV element.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
EncodingImpl< EncoderTag > EncodingBuffer
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Prepend a TLV element.
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ FinalBlockId
Definition: tlv.hpp:98
@ ContentType
Definition: tlv.hpp:96
@ MetaInfo
Definition: tlv.hpp:92
@ FreshnessPeriod
Definition: tlv.hpp:97
@ ContentType_Blob
payload
Definition: tlv.hpp:145
Definition: data.cpp:25
constexpr time::milliseconds DEFAULT_FRESHNESS_PERIOD
Default value of FreshnessPeriod.
Definition: meta-info.hpp:38
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:377
uint32_t tlvType
TLV-TYPE of the field; 0 if field does not exist.
Definition: packet.cpp:49
SignatureInfo info