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