blob: 03600cf1b01fbc2363b3423b271c9bea63a60e15 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -07003 * Copyright (C) 2013-2014 Regents of the University of California.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_META_INFO_HPP
8#define NDN_META_INFO_HPP
9
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080010#include "encoding/encoding-buffer.hpp"
11
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080012namespace ndn {
13
14/**
15 * An MetaInfo holds the meta info which is signed inside the data packet.
16 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070017class MetaInfo
18{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080019public:
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
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032 /**
33 * @brief Create from wire encoding
34 */
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080035 MetaInfo(const Block& block)
36 {
37 wireDecode(block);
38 }
Alexander Afanasyevc348f832014-02-17 16:35:17 -080039
40 template<bool T>
41 size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070042 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080043
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080045 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070046
Alexander Afanasyevc348f832014-02-17 16:35:17 -080047 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070048 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049
Alexander Afanasyevc348f832014-02-17 16:35:17 -080050 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080051 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070052
53 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080054 getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080055 {
56 return m_type;
57 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080059 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080060 setType(uint32_t type)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080061 {
62 m_wire.reset();
63 m_type = type;
64 return *this;
65 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070066
67 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080068 getFreshnessPeriod() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080069 {
70 return m_freshnessPeriod;
71 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080073 MetaInfo&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080075 {
76 m_wire.reset();
77 m_freshnessPeriod = freshnessPeriod;
78 return *this;
79 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080081 const name::Component&
82 getFinalBlockId() const
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 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070095public: // 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
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800108private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800109 uint32_t m_type;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110 time::milliseconds m_freshnessPeriod;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800111 name::Component m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800112
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800113 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800114};
115
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800116template<bool T>
117inline size_t
118MetaInfo::wireEncode(EncodingImpl<T>& blk) const
119{
120 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
121 // ContentType?
122 // FreshnessPeriod?
123 // FinalBlockId?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700125 size_t totalLength = 0;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800126
127 // FinalBlockId
128 if (!m_finalBlockId.empty())
129 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700130 totalLength += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800131 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800133 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134 if (m_freshnessPeriod >= time::milliseconds::zero())
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800135 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700136 totalLength += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod,
137 m_freshnessPeriod.count());
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800138 }
139
140 // ContentType
141 if (m_type != TYPE_DEFAULT)
142 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700143 totalLength += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800144 }
145
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700146 totalLength += blk.prependVarNumber(totalLength);
147 totalLength += blk.prependVarNumber(Tlv::MetaInfo);
148 return totalLength;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800149}
150
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800152MetaInfo::wireEncode() const
153{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700154 if (m_wire.hasWire())
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800155 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800156
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800157 EncodingEstimator estimator;
158 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700159
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800160 EncodingBuffer buffer(estimatedSize, 0);
161 wireEncode(buffer);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800162
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800163 m_wire = buffer.block();
164 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800165}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700166
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800167inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700168MetaInfo::wireDecode(const Block& wire)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800169{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800170 m_wire = wire;
171 m_wire.parse();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800172
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800173 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
174 // ContentType?
175 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700176
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800177 // ContentType
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800178 Block::element_const_iterator val = m_wire.find(Tlv::ContentType);
179 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800180 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800181 m_type = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800182 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800183 else
184 m_type = TYPE_DEFAULT;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800185
186 // FreshnessPeriod
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800187 val = m_wire.find(Tlv::FreshnessPeriod);
188 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800189 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190 m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800191 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800192 else
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700193 m_freshnessPeriod = time::milliseconds::min();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800194
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 {
202 /// @todo May or may not throw exception later...
203 m_finalBlockId.reset();
204 }
205 }
206 else
207 m_finalBlockId.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800208}
209
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800210inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700211operator<<(std::ostream& os, const MetaInfo& info)
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800212{
213 // ContentType
214 os << "ContentType: " << info.getType();
215
216 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700217 if (info.getFreshnessPeriod() >= time::milliseconds::zero()) {
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800218 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
219 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800220
221 if (!info.getFinalBlockId().empty()) {
222 os << ", FinalBlockId: ";
223 info.getFinalBlockId().toUri(os);
224 }
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800225 return os;
226}
227
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800228} // namespace ndn
229
230#endif // NDN_META_INFO_HPP