blob: 4153911f5f960cae58aabd23de5cf7f1cf83cecf [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 */
17class MetaInfo {
18public:
19 enum {
20 TYPE_DEFAULT = 0,
21 TYPE_LINK = 1,
22 TYPE_KEY = 2
23 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070024
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080025 MetaInfo()
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080026 : m_type(TYPE_DEFAULT)
27 , m_freshnessPeriod(-1)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070028 {
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080029 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080030
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070031 /**
32 * @brief Create from wire encoding
33 */
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080034 MetaInfo(const Block& block)
35 {
36 wireDecode(block);
37 }
Alexander Afanasyevc348f832014-02-17 16:35:17 -080038
39 template<bool T>
40 size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070041 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -080042
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -080044 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045
Alexander Afanasyevc348f832014-02-17 16:35:17 -080046 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070047 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
Alexander Afanasyevc348f832014-02-17 16:35:17 -080049 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080050 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051
52 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053 getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080054 {
55 return m_type;
56 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070057
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080058 MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 setType(uint32_t type)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080060 {
61 m_wire.reset();
62 m_type = type;
63 return *this;
64 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065
66 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080067 getFreshnessPeriod() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080068 {
69 return m_freshnessPeriod;
70 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080072 MetaInfo&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080074 {
75 m_wire.reset();
76 m_freshnessPeriod = freshnessPeriod;
77 return *this;
78 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080079
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080080 const name::Component&
81 getFinalBlockId() const
82 {
83 return m_finalBlockId;
84 }
85
86 MetaInfo&
87 setFinalBlockId(const name::Component& finalBlockId)
88 {
89 m_wire.reset();
90 m_finalBlockId = finalBlockId;
91 return *this;
92 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070094public: // EqualityComparable concept
95 bool
96 operator==(const MetaInfo& other) const
97 {
98 return wireEncode() == other.wireEncode();
99 }
100
101 bool
102 operator!=(const MetaInfo& other) const
103 {
104 return !(*this == other);
105 }
106
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800108 uint32_t m_type;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700109 time::milliseconds m_freshnessPeriod;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800110 name::Component m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800111
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800112 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800113};
114
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800115template<bool T>
116inline size_t
117MetaInfo::wireEncode(EncodingImpl<T>& blk) const
118{
119 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
120 // ContentType?
121 // FreshnessPeriod?
122 // FinalBlockId?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124 size_t totalLength = 0;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800125
126 // FinalBlockId
127 if (!m_finalBlockId.empty())
128 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700129 totalLength += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800130 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700131
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800132 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133 if (m_freshnessPeriod >= time::milliseconds::zero())
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800134 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700135 totalLength += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod,
136 m_freshnessPeriod.count());
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800137 }
138
139 // ContentType
140 if (m_type != TYPE_DEFAULT)
141 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142 totalLength += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800143 }
144
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700145 totalLength += blk.prependVarNumber(totalLength);
146 totalLength += blk.prependVarNumber(Tlv::MetaInfo);
147 return totalLength;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800148}
149
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151MetaInfo::wireEncode() const
152{
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700153 if (m_wire.hasWire())
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800154 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800155
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800156 EncodingEstimator estimator;
157 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700158
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800159 EncodingBuffer buffer(estimatedSize, 0);
160 wireEncode(buffer);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800161
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800162 m_wire = buffer.block();
163 return m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800164}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800166inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700167MetaInfo::wireDecode(const Block& wire)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800168{
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800169 m_wire = wire;
170 m_wire.parse();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800171
Alexander Afanasyev213ea6d2014-01-03 15:31:03 -0800172 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
173 // ContentType?
174 // FreshnessPeriod?
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800176 // ContentType
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800177 Block::element_const_iterator val = m_wire.find(Tlv::ContentType);
178 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800179 {
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800180 m_type = readNonNegativeInteger(*val);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800181 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800182 else
183 m_type = TYPE_DEFAULT;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800184
185 // FreshnessPeriod
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800186 val = m_wire.find(Tlv::FreshnessPeriod);
187 if (val != m_wire.elements().end())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800188 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700189 m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800190 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800191 else
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700192 m_freshnessPeriod = time::milliseconds::min();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800193
194 // FinalBlockId
195 val = m_wire.find(Tlv::FinalBlockId);
196 if (val != m_wire.elements().end())
197 {
198 m_finalBlockId = val->blockFromValue();
199 if (m_finalBlockId.type() != Tlv::NameComponent)
200 {
201 /// @todo May or may not throw exception later...
202 m_finalBlockId.reset();
203 }
204 }
205 else
206 m_finalBlockId.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800207}
208
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800209inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700210operator<<(std::ostream& os, const MetaInfo& info)
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800211{
212 // ContentType
213 os << "ContentType: " << info.getType();
214
215 // FreshnessPeriod
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700216 if (info.getFreshnessPeriod() >= time::milliseconds::zero()) {
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800217 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
218 }
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800219
220 if (!info.getFinalBlockId().empty()) {
221 os << ", FinalBlockId: ";
222 info.getFinalBlockId().toUri(os);
223 }
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800224 return os;
225}
226
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800227} // namespace ndn
228
229#endif // NDN_META_INFO_HPP