blob: 42875d8607aaf7075d83009f95efc400ffe3011f [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 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070025
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080026 MetaInfo()
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080027 : m_type(TYPE_DEFAULT)
28 , m_freshnessPeriod(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 {
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080030 }
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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043
Alexander Afanasyevc348f832014-02-17 16:35:17 -080044 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045 wireDecode(const Block &wire);
46
Alexander Afanasyevc348f832014-02-17 16:35:17 -080047 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080048 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049
50 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080051 getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080052 {
53 return m_type;
54 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070055
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 Afanasyevaa0e7da2014-03-17 14:37:33 -070063
64 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080065 getFreshnessPeriod() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080066 {
67 return m_freshnessPeriod;
68 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080070 MetaInfo&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071 setFreshnessPeriod(const time::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 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080092private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080093 uint32_t m_type;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094 time::milliseconds m_freshnessPeriod;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080095 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?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800109 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 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800117 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118 if (m_freshnessPeriod >= time::milliseconds::zero())
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800119 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700120 total_len += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod,
121 m_freshnessPeriod.count());
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800122 }
123
124 // ContentType
125 if (m_type != TYPE_DEFAULT)
126 {
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800127 total_len += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800128 }
129
130 total_len += blk.prependVarNumber (total_len);
131 total_len += blk.prependVarNumber (Tlv::MetaInfo);
132 return total_len;
133}
134
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800136MetaInfo::wireEncode() const
137{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800138 if (m_wire.hasWire ())
139 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800140
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800141 EncodingEstimator estimator;
142 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700143
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800144 EncodingBuffer buffer(estimatedSize, 0);
145 wireEncode(buffer);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800146
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800147 m_wire = buffer.block();
148 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800149}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151inline void
152MetaInfo::wireDecode(const Block &wire)
153{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800154 m_wire = wire;
155 m_wire.parse();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800156
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800157 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
158 // ContentType?
159 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700160
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800161 // ContentType
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800162 Block::element_const_iterator val = m_wire.find(Tlv::ContentType);
163 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800164 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800165 m_type = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800166 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800167 else
168 m_type = TYPE_DEFAULT;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800169
170 // FreshnessPeriod
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800171 val = m_wire.find(Tlv::FreshnessPeriod);
172 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800173 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700174 m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800175 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800176 else
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700177 m_freshnessPeriod = time::milliseconds::min();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800178
179 // FinalBlockId
180 val = m_wire.find(Tlv::FinalBlockId);
181 if (val != m_wire.elements().end())
182 {
183 m_finalBlockId = val->blockFromValue();
184 if (m_finalBlockId.type() != Tlv::NameComponent)
185 {
186 /// @todo May or may not throw exception later...
187 m_finalBlockId.reset();
188 }
189 }
190 else
191 m_finalBlockId.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800192}
193
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800194inline std::ostream&
195operator << (std::ostream &os, const MetaInfo &info)
196{
197 // ContentType
198 os << "ContentType: " << info.getType();
199
200 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700201 if (info.getFreshnessPeriod() >= time::milliseconds::zero()) {
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800202 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
203 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800204
205 if (!info.getFinalBlockId().empty()) {
206 os << ", FinalBlockId: ";
207 info.getFinalBlockId().toUri(os);
208 }
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800209 return os;
210}
211
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800212} // namespace ndn
213
214#endif // NDN_META_INFO_HPP