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