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