blob: ff7157017e1358f8e66d7de5003a980a662ee7aa [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 Afanasyevc348f832014-02-17 16:35:17 -080036
37 template<bool T>
38 size_t
39 wireEncode(EncodingImpl<T> &block) const;
40
41 const Block&
42 wireEncode() const;
43
44 void
45 wireDecode(const Block &wire);
46
47 ///////////////////////////////////////////////////////////////////////////////
48 // Gettest/setters
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080049
50 uint32_t
51 getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080052 {
53 return m_type;
54 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080056 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080057 setType(uint32_t type)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080058 {
59 m_wire.reset();
60 m_type = type;
61 return *this;
62 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063
64 Milliseconds
65 getFreshnessPeriod() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080066 {
67 return m_freshnessPeriod;
68 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080070 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080071 setFreshnessPeriod(Milliseconds freshnessPeriod)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080072 {
73 m_wire.reset();
74 m_freshnessPeriod = freshnessPeriod;
75 return *this;
76 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080077
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080078 const name::Component&
79 getFinalBlockId() const
80 {
81 return m_finalBlockId;
82 }
83
84 MetaInfo&
85 setFinalBlockId(const name::Component& finalBlockId)
86 {
87 m_wire.reset();
88 m_finalBlockId = finalBlockId;
89 return *this;
90 }
91
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080092private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080093 uint32_t m_type;
94 Milliseconds m_freshnessPeriod;
95 name::Component m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080096
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080097 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080098};
99
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800100template<bool T>
101inline size_t
102MetaInfo::wireEncode(EncodingImpl<T>& blk) const
103{
104 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
105 // ContentType?
106 // FreshnessPeriod?
107 // FinalBlockId?
108
109 size_t total_len = 0;
110
111 // FinalBlockId
112 if (!m_finalBlockId.empty())
113 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800114 total_len += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800115 }
116
117 // FreshnessPeriod
118 if (m_freshnessPeriod >= 0)
119 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800120 total_len += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod, m_freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800121 }
122
123 // ContentType
124 if (m_type != TYPE_DEFAULT)
125 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800126 total_len += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800127 }
128
129 total_len += blk.prependVarNumber (total_len);
130 total_len += blk.prependVarNumber (Tlv::MetaInfo);
131 return total_len;
132}
133
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800134inline const Block&
135MetaInfo::wireEncode() const
136{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800137 if (m_wire.hasWire ())
138 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800139
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800140 EncodingEstimator estimator;
141 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800142
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800143 EncodingBuffer buffer(estimatedSize, 0);
144 wireEncode(buffer);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800145
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800146 m_wire = buffer.block();
147 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800148}
149
150inline void
151MetaInfo::wireDecode(const Block &wire)
152{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800153 m_wire = wire;
154 m_wire.parse();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800155
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800156 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
157 // ContentType?
158 // FreshnessPeriod?
159
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800160 // ContentType
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800161 Block::element_const_iterator val = m_wire.find(Tlv::ContentType);
162 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800163 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800164 m_type = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800165 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800166 else
167 m_type = TYPE_DEFAULT;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800168
169 // FreshnessPeriod
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800170 val = m_wire.find(Tlv::FreshnessPeriod);
171 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800172 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800173 m_freshnessPeriod = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800174 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800175 else
176 m_freshnessPeriod = -1;
177
178 // FinalBlockId
179 val = m_wire.find(Tlv::FinalBlockId);
180 if (val != m_wire.elements().end())
181 {
182 m_finalBlockId = val->blockFromValue();
183 if (m_finalBlockId.type() != Tlv::NameComponent)
184 {
185 /// @todo May or may not throw exception later...
186 m_finalBlockId.reset();
187 }
188 }
189 else
190 m_finalBlockId.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800191}
192
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800193inline std::ostream&
194operator << (std::ostream &os, const MetaInfo &info)
195{
196 // ContentType
197 os << "ContentType: " << info.getType();
198
199 // FreshnessPeriod
200 if (info.getFreshnessPeriod() >= 0) {
201 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
202 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800203
204 if (!info.getFinalBlockId().empty()) {
205 os << ", FinalBlockId: ";
206 info.getFinalBlockId().toUri(os);
207 }
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800208 return os;
209}
210
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800211} // namespace ndn
212
213#endif // NDN_META_INFO_HPP