NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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-2017 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>));
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&
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 = 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 (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) {
83  if (!(128 <= i->type() && i->type() <= 252))
84  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
85  "[128, 252]"));
86  }
87 
88  m_wire.reset();
89  m_appMetaInfo = info;
90  return *this;
91 }
92 
93 MetaInfo&
95 {
96  if (!(128 <= block.type() && block.type() <= 252))
97  BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
98  "[128, 252]"));
99 
100  m_wire.reset();
101  m_appMetaInfo.push_back(block);
102  return *this;
103 }
104 
105 bool
107 {
108  for (std::list<Block>::iterator iter = m_appMetaInfo.begin();
109  iter != m_appMetaInfo.end(); ++iter) {
110  if (iter->type() == tlvType) {
111  m_wire.reset();
112  m_appMetaInfo.erase(iter);
113  return true;
114  }
115  }
116  return false;
117 }
118 
119 const Block*
121 {
122  for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin();
123  iter != m_appMetaInfo.end(); ++iter) {
124  if (iter->type() == tlvType) {
125  return &*iter;
126  }
127  }
128  return 0;
129 }
130 
131 template<encoding::Tag TAG>
132 size_t
133 MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
134 {
135  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
136  // ContentType?
137  // FreshnessPeriod?
138  // FinalBlockId?
139  // AppMetaInfo*
140 
141  size_t totalLength = 0;
142 
143  for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
144  appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
145  totalLength += encoder.prependBlock(*appMetaInfoItem);
146  }
147 
148  // FinalBlockId
149  if (!m_finalBlockId.empty()) {
150  totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId);
151  }
152 
153  // FreshnessPeriod
154  if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
155  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod.count());
156  }
157 
158  // ContentType
159  if (m_type != tlv::ContentType_Blob) {
160  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
161  }
162 
163  totalLength += encoder.prependVarNumber(totalLength);
164  totalLength += encoder.prependVarNumber(tlv::MetaInfo);
165  return totalLength;
166 }
167 
169 
170 const Block&
172 {
173  if (m_wire.hasWire())
174  return m_wire;
175 
176  EncodingEstimator estimator;
177  size_t estimatedSize = wireEncode(estimator);
178 
179  EncodingBuffer buffer(estimatedSize, 0);
180  wireEncode(buffer);
181 
182  m_wire = buffer.block();
183  return m_wire;
184 }
185 
186 void
188 {
189  m_wire = wire;
190  m_wire.parse();
191 
192  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
193  // ContentType?
194  // FreshnessPeriod?
195  // FinalBlockId?
196  // AppMetaInfo*
197 
198 
200 
201  // ContentType
202  if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
203  m_type = readNonNegativeIntegerAs<uint32_t>(*val);
204  ++val;
205  }
206  else {
207  m_type = tlv::ContentType_Blob;
208  }
209 
210  // FreshnessPeriod
211  if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
212  m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
213  ++val;
214  }
215  else {
216  m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
217  }
218 
219  // FinalBlockId
220  if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
221  m_finalBlockId = val->blockFromValue();
222  if (m_finalBlockId.type() != tlv::NameComponent)
223  {
225  m_finalBlockId = name::Component();
226  }
227  ++val;
228  }
229  else {
230  m_finalBlockId = name::Component();
231  }
232 
233  // AppMetaInfo (if any)
234  for (; val != m_wire.elements().end(); ++val) {
235  m_appMetaInfo.push_back(*val);
236  }
237 }
238 
239 std::ostream&
240 operator<<(std::ostream& os, const MetaInfo& info)
241 {
242  // ContentType
243  os << "ContentType: " << info.getType();
244 
245  // FreshnessPeriod
246  if (info.getFreshnessPeriod() > time::milliseconds::zero()) {
247  os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
248  }
249 
250  // FinalBlockId
251  if (!info.getFinalBlockId().empty()) {
252  os << ", FinalBlockId: ";
253  info.getFinalBlockId().toUri(os);
254  }
255 
256  // App-defined MetaInfo items
257  for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin();
258  iter != info.getAppMetaInfo().end(); ++iter) {
259  os << ", AppMetaInfoTlvType: " << iter->type();
260  }
261 
262  return os;
263 }
264 
265 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:347
const time::milliseconds & getFreshnessPeriod() const
Definition: meta-info.hpp:213
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:274
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
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.
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:171
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.cpp:66
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
const time::milliseconds DEFAULT_FRESHNESS_PERIOD
Definition: meta-info.hpp:34
MetaInfo & setType(uint32_t type)
set ContentType
Definition: meta-info.cpp:47
Block blockFromValue() const
Definition: block.cpp:323
bool removeAppMetaInfo(uint32_t tlvType)
Remove a first app-defined MetaInfo item with type tlvType.
Definition: meta-info.cpp:106
const std::list< Block > & getAppMetaInfo() const
Get all app-defined MetaInfo items.
Definition: meta-info.cpp:74
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Exclude)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:256
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:334
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
indicates content is the actual data bits
Definition: tlv.hpp:128
uint64_t tlvType
TLV-TYPE of the field; 0 if field does not exist.
Definition: packet.cpp:61
Component holds a read-only name component value.
const Block * findAppMetaInfo(uint32_t tlvType) const
Find a first app-defined MetaInfo item of type tlvType.
Definition: meta-info.cpp:120
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:250
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
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:80
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
uint32_t getType() const
Definition: meta-info.hpp:207
MetaInfo & addAppMetaInfo(const Block &block)
Add an app-defined MetaInfo item.
Definition: meta-info.cpp:94
const name::Component & getFinalBlockId() const
Definition: meta-info.hpp:219
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator
void wireDecode(const Block &wire)
Definition: meta-info.cpp:187