All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
meta-info.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_META_INFO_HPP
8 #define NDN_META_INFO_HPP
9 
11 
12 namespace ndn {
13 
17 class MetaInfo
18 {
19 public:
20  enum {
22  TYPE_LINK = 1,
24  };
25 
27  : m_type(TYPE_DEFAULT)
28  , m_freshnessPeriod(-1)
29  {
30  }
31 
35  MetaInfo(const Block& block)
36  {
37  wireDecode(block);
38  }
39 
40  template<bool T>
41  size_t
42  wireEncode(EncodingImpl<T>& block) const;
43 
44  const Block&
45  wireEncode() const;
46 
47  void
48  wireDecode(const Block& wire);
49 
51  // Getters/setters
52 
53  uint32_t
54  getType() const
55  {
56  return m_type;
57  }
58 
59  MetaInfo&
60  setType(uint32_t type)
61  {
62  m_wire.reset();
63  m_type = type;
64  return *this;
65  }
66 
67  const time::milliseconds&
69  {
70  return m_freshnessPeriod;
71  }
72 
73  MetaInfo&
74  setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
75  {
76  m_wire.reset();
77  m_freshnessPeriod = freshnessPeriod;
78  return *this;
79  }
80 
81  const name::Component&
83  {
84  return m_finalBlockId;
85  }
86 
87  MetaInfo&
88  setFinalBlockId(const name::Component& finalBlockId)
89  {
90  m_wire.reset();
91  m_finalBlockId = finalBlockId;
92  return *this;
93  }
94 
95 public: // EqualityComparable concept
96  bool
97  operator==(const MetaInfo& other) const
98  {
99  return wireEncode() == other.wireEncode();
100  }
101 
102  bool
103  operator!=(const MetaInfo& other) const
104  {
105  return !(*this == other);
106  }
107 
108 private:
109  uint32_t m_type;
110  time::milliseconds m_freshnessPeriod;
111  name::Component m_finalBlockId;
112 
113  mutable Block m_wire;
114 };
115 
116 template<bool T>
117 inline size_t
119 {
120  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
121  // ContentType?
122  // FreshnessPeriod?
123  // FinalBlockId?
124 
125  size_t totalLength = 0;
126 
127  // FinalBlockId
128  if (!m_finalBlockId.empty())
129  {
130  totalLength += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId);
131  }
132 
133  // FreshnessPeriod
134  if (m_freshnessPeriod >= time::milliseconds::zero())
135  {
137  m_freshnessPeriod.count());
138  }
139 
140  // ContentType
141  if (m_type != TYPE_DEFAULT)
142  {
143  totalLength += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type);
144  }
145 
146  totalLength += blk.prependVarNumber(totalLength);
147  totalLength += blk.prependVarNumber(Tlv::MetaInfo);
148  return totalLength;
149 }
150 
151 inline const Block&
153 {
154  if (m_wire.hasWire())
155  return m_wire;
156 
157  EncodingEstimator estimator;
158  size_t estimatedSize = wireEncode(estimator);
159 
160  EncodingBuffer buffer(estimatedSize, 0);
161  wireEncode(buffer);
162 
163  m_wire = buffer.block();
164  return m_wire;
165 }
166 
167 inline void
169 {
170  m_wire = wire;
171  m_wire.parse();
172 
173  // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
174  // ContentType?
175  // FreshnessPeriod?
176 
177  // ContentType
179  if (val != m_wire.elements().end())
180  {
181  m_type = readNonNegativeInteger(*val);
182  }
183  else
184  m_type = TYPE_DEFAULT;
185 
186  // FreshnessPeriod
187  val = m_wire.find(Tlv::FreshnessPeriod);
188  if (val != m_wire.elements().end())
189  {
190  m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
191  }
192  else
193  m_freshnessPeriod = time::milliseconds::min();
194 
195  // FinalBlockId
196  val = m_wire.find(Tlv::FinalBlockId);
197  if (val != m_wire.elements().end())
198  {
199  m_finalBlockId = val->blockFromValue();
200  if (m_finalBlockId.type() != Tlv::NameComponent)
201  {
203  m_finalBlockId.reset();
204  }
205  }
206  else
207  m_finalBlockId.reset();
208 }
209 
210 inline std::ostream&
211 operator<<(std::ostream& os, const MetaInfo& info)
212 {
213  // ContentType
214  os << "ContentType: " << info.getType();
215 
216  // FreshnessPeriod
217  if (info.getFreshnessPeriod() >= time::milliseconds::zero()) {
218  os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
219  }
220 
221  if (!info.getFinalBlockId().empty()) {
222  os << ", FinalBlockId: ";
223  info.getFinalBlockId().toUri(os);
224  }
225  return os;
226 }
227 
228 } // namespace ndn
229 
230 #endif // NDN_META_INFO_HPP
size_t prependNestedBlock(EncodingImpl< P > &encoder, uint32_t type, const U &nestedBlock)
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
const element_container & elements() const
Get all subelements.
Definition: block.hpp:464
Class representing wire element of the NDN packet.
Definition: block.hpp:26
const time::milliseconds & getFreshnessPeriod() const
Definition: meta-info.hpp:68
uint64_t readNonNegativeInteger(const Block &block)
const Block & wireEncode() const
Definition: meta-info.hpp:152
const name::Component & getFinalBlockId() const
Definition: meta-info.hpp:82
MetaInfo & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: meta-info.hpp:74
Block blockFromValue() const
Definition: block.cpp:352
size_t wireEncode(EncodingImpl< T > &block) const
Definition: meta-info.hpp:118
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.hpp:88
MetaInfo(const Block &block)
Create from wire encoding.
Definition: meta-info.hpp:35
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:17
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
void toUri(std::ostream &result) const
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
bool operator!=(const MetaInfo &other) const
Definition: meta-info.hpp:103
Component holds a read-only name component value.
bool operator==(const MetaInfo &other) const
Definition: meta-info.hpp:97
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
MetaInfo & setType(uint32_t type)
Definition: meta-info.hpp:60
size_t prependNonNegativeIntegerBlock(EncodingImpl< P > &encoder, uint32_t type, uint64_t number)
helper methods
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.hpp:523
uint32_t getType() const
Definition: meta-info.hpp:54
Class representing wire element of the NDN packet.
void wireDecode(const Block &wire)
Definition: meta-info.hpp:168