blob: fc5458e49925671de17ae19728188b27bcab5e16 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_META_INFO_HPP
9#define NDN_META_INFO_HPP
10
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080011#include "encoding/encoding-buffer.hpp"
12
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080013namespace ndn {
14
15/**
16 * An MetaInfo holds the meta info which is signed inside the data packet.
17 */
18class MetaInfo {
19public:
20 enum {
21 TYPE_DEFAULT = 0,
22 TYPE_LINK = 1,
23 TYPE_KEY = 2
24 };
25
26 MetaInfo()
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080027 : m_type(TYPE_DEFAULT)
28 , m_freshnessPeriod(-1)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080029 {
30 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080031
32 MetaInfo(const Block& block)
33 {
34 wireDecode(block);
35 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080036
37 uint32_t
38 getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080039 {
40 return m_type;
41 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080042
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080043 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080044 setType(uint32_t type)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080045 {
46 m_wire.reset();
47 m_type = type;
48 return *this;
49 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080050
51 Milliseconds
52 getFreshnessPeriod() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080053 {
54 return m_freshnessPeriod;
55 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080056
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080057 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080058 setFreshnessPeriod(Milliseconds freshnessPeriod)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080059 {
60 m_wire.reset();
61 m_freshnessPeriod = freshnessPeriod;
62 return *this;
63 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080065 const name::Component&
66 getFinalBlockId() const
67 {
68 return m_finalBlockId;
69 }
70
71 MetaInfo&
72 setFinalBlockId(const name::Component& finalBlockId)
73 {
74 m_wire.reset();
75 m_finalBlockId = finalBlockId;
76 return *this;
77 }
78
79 template<bool T>
80 size_t
81 wireEncode(EncodingImpl<T> &block) const;
82
83 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080084 wireEncode() const;
85
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080086 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080087 wireDecode(const Block &wire);
88
89private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080090 uint32_t m_type;
91 Milliseconds m_freshnessPeriod;
92 name::Component m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080093
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080094 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095};
96
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080097template<bool T>
98inline size_t
99MetaInfo::wireEncode(EncodingImpl<T>& blk) const
100{
101 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
102 // ContentType?
103 // FreshnessPeriod?
104 // FinalBlockId?
105
106 size_t total_len = 0;
107
108 // FinalBlockId
109 if (!m_finalBlockId.empty())
110 {
111 size_t var_len = m_finalBlockId.wireEncode (blk);
112 total_len += var_len;
113 total_len += blk.prependVarNumber (var_len);
114 total_len += blk.prependVarNumber (Tlv::FinalBlockId);
115 }
116
117 // FreshnessPeriod
118 if (m_freshnessPeriod >= 0)
119 {
120 size_t var_len = blk.prependNonNegativeInteger (m_freshnessPeriod);
121 total_len += var_len;
122 total_len += blk.prependVarNumber (var_len);
123 total_len += blk.prependVarNumber (Tlv::FreshnessPeriod);
124 }
125
126 // ContentType
127 if (m_type != TYPE_DEFAULT)
128 {
129 size_t var_len = blk.prependNonNegativeInteger (m_type);
130 total_len += var_len;
131 total_len += blk.prependVarNumber (var_len);
132 total_len += blk.prependVarNumber (Tlv::ContentType);
133 }
134
135 total_len += blk.prependVarNumber (total_len);
136 total_len += blk.prependVarNumber (Tlv::MetaInfo);
137 return total_len;
138}
139
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800140inline const Block&
141MetaInfo::wireEncode() const
142{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800143 if (m_wire.hasWire ())
144 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800145
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800146 EncodingEstimator estimator;
147 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800148
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800149 EncodingBuffer buffer(estimatedSize, 0);
150 wireEncode(buffer);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800152 m_wire = buffer.block();
153 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800154}
155
156inline void
157MetaInfo::wireDecode(const Block &wire)
158{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800159 m_wire = wire;
160 m_wire.parse();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800161
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800162 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
163 // ContentType?
164 // FreshnessPeriod?
165
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800166 // ContentType
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800167 Block::element_const_iterator val = m_wire.find(Tlv::ContentType);
168 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800169 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800170 m_type = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800171 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800172 else
173 m_type = TYPE_DEFAULT;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800174
175 // FreshnessPeriod
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800176 val = m_wire.find(Tlv::FreshnessPeriod);
177 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800178 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800179 m_freshnessPeriod = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800180 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800181 else
182 m_freshnessPeriod = -1;
183
184 // FinalBlockId
185 val = m_wire.find(Tlv::FinalBlockId);
186 if (val != m_wire.elements().end())
187 {
188 m_finalBlockId = val->blockFromValue();
189 if (m_finalBlockId.type() != Tlv::NameComponent)
190 {
191 /// @todo May or may not throw exception later...
192 m_finalBlockId.reset();
193 }
194 }
195 else
196 m_finalBlockId.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800197}
198
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800199inline std::ostream&
200operator << (std::ostream &os, const MetaInfo &info)
201{
202 // ContentType
203 os << "ContentType: " << info.getType();
204
205 // FreshnessPeriod
206 if (info.getFreshnessPeriod() >= 0) {
207 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
208 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800209
210 if (!info.getFinalBlockId().empty()) {
211 os << ", FinalBlockId: ";
212 info.getFinalBlockId().toUri(os);
213 }
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800214 return os;
215}
216
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800217} // namespace ndn
218
219#endif // NDN_META_INFO_HPP