Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_META_INFO_HPP |
| 8 | #define NDN_META_INFO_HPP |
| 9 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 10 | #include "encoding/encoding-buffer.hpp" |
| 11 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 12 | namespace ndn { |
| 13 | |
| 14 | /** |
| 15 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 16 | */ |
| 17 | class MetaInfo { |
| 18 | public: |
| 19 | enum { |
| 20 | TYPE_DEFAULT = 0, |
| 21 | TYPE_LINK = 1, |
| 22 | TYPE_KEY = 2 |
| 23 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 25 | MetaInfo() |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 26 | : m_type(TYPE_DEFAULT) |
| 27 | , m_freshnessPeriod(-1) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 28 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 29 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 31 | /** |
| 32 | * @brief Create from wire encoding |
| 33 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 34 | MetaInfo(const Block& block) |
| 35 | { |
| 36 | wireDecode(block); |
| 37 | } |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 38 | |
| 39 | template<bool T> |
| 40 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 41 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 43 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 44 | wireEncode() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 46 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 47 | wireDecode(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 50 | // Getters/setters |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 51 | |
| 52 | uint32_t |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 53 | getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 54 | { |
| 55 | return m_type; |
| 56 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 58 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 59 | setType(uint32_t type) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 60 | { |
| 61 | m_wire.reset(); |
| 62 | m_type = type; |
| 63 | return *this; |
| 64 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 65 | |
| 66 | const time::milliseconds& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 67 | getFreshnessPeriod() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 68 | { |
| 69 | return m_freshnessPeriod; |
| 70 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 72 | MetaInfo& |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 73 | setFreshnessPeriod(const time::milliseconds& freshnessPeriod) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 74 | { |
| 75 | m_wire.reset(); |
| 76 | m_freshnessPeriod = freshnessPeriod; |
| 77 | return *this; |
| 78 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 80 | 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 94 | public: // 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 Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 107 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 108 | uint32_t m_type; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 109 | time::milliseconds m_freshnessPeriod; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 110 | name::Component m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 112 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 115 | template<bool T> |
| 116 | inline size_t |
| 117 | MetaInfo::wireEncode(EncodingImpl<T>& blk) const |
| 118 | { |
| 119 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 120 | // ContentType? |
| 121 | // FreshnessPeriod? |
| 122 | // FinalBlockId? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 124 | size_t totalLength = 0; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 125 | |
| 126 | // FinalBlockId |
| 127 | if (!m_finalBlockId.empty()) |
| 128 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 129 | totalLength += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 130 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 132 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 133 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 134 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 135 | totalLength += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod, |
| 136 | m_freshnessPeriod.count()); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // ContentType |
| 140 | if (m_type != TYPE_DEFAULT) |
| 141 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 142 | totalLength += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame^] | 145 | totalLength += blk.prependVarNumber(totalLength); |
| 146 | totalLength += blk.prependVarNumber(Tlv::MetaInfo); |
| 147 | return totalLength; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 150 | inline const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 151 | MetaInfo::wireEncode() const |
| 152 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 153 | if (m_wire.hasWire()) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 154 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 156 | EncodingEstimator estimator; |
| 157 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 158 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 159 | EncodingBuffer buffer(estimatedSize, 0); |
| 160 | wireEncode(buffer); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 162 | m_wire = buffer.block(); |
| 163 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 164 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 166 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 167 | MetaInfo::wireDecode(const Block& wire) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 168 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 169 | m_wire = wire; |
| 170 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 172 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 173 | // ContentType? |
| 174 | // FreshnessPeriod? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 176 | // ContentType |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 177 | Block::element_const_iterator val = m_wire.find(Tlv::ContentType); |
| 178 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 179 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 180 | m_type = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 181 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 182 | else |
| 183 | m_type = TYPE_DEFAULT; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 184 | |
| 185 | // FreshnessPeriod |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 186 | val = m_wire.find(Tlv::FreshnessPeriod); |
| 187 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 188 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 189 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 190 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 191 | else |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 192 | m_freshnessPeriod = time::milliseconds::min(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 193 | |
| 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 Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 209 | inline std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 210 | operator<<(std::ostream& os, const MetaInfo& info) |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 211 | { |
| 212 | // ContentType |
| 213 | os << "ContentType: " << info.getType(); |
| 214 | |
| 215 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 216 | if (info.getFreshnessPeriod() >= time::milliseconds::zero()) { |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 217 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 218 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 219 | |
| 220 | if (!info.getFinalBlockId().empty()) { |
| 221 | os << ", FinalBlockId: "; |
| 222 | info.getFinalBlockId().toUri(os); |
| 223 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 224 | return os; |
| 225 | } |
| 226 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 227 | } // namespace ndn |
| 228 | |
| 229 | #endif // NDN_META_INFO_HPP |