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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef NDN_META_INFO_HPP |
| 14 | #define NDN_META_INFO_HPP |
| 15 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 16 | #include "encoding/encoding-buffer.hpp" |
| 17 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 18 | namespace ndn { |
| 19 | |
| 20 | /** |
| 21 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 22 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 23 | class MetaInfo |
| 24 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 25 | public: |
| 26 | enum { |
| 27 | TYPE_DEFAULT = 0, |
| 28 | TYPE_LINK = 1, |
| 29 | TYPE_KEY = 2 |
| 30 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | MetaInfo() |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 33 | : m_type(TYPE_DEFAULT) |
| 34 | , m_freshnessPeriod(-1) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 35 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @brief Create from wire encoding |
| 40 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 41 | MetaInfo(const Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 45 | |
| 46 | template<bool T> |
| 47 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 48 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 50 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 51 | wireEncode() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 52 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 53 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 54 | wireDecode(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 57 | // Getters/setters |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | |
| 59 | uint32_t |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 60 | getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 61 | { |
| 62 | return m_type; |
| 63 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 65 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 66 | setType(uint32_t type) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 67 | { |
| 68 | m_wire.reset(); |
| 69 | m_type = type; |
| 70 | return *this; |
| 71 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | |
| 73 | const time::milliseconds& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 74 | getFreshnessPeriod() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 75 | { |
| 76 | return m_freshnessPeriod; |
| 77 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 79 | MetaInfo& |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 80 | setFreshnessPeriod(const time::milliseconds& freshnessPeriod) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 81 | { |
| 82 | m_wire.reset(); |
| 83 | m_freshnessPeriod = freshnessPeriod; |
| 84 | return *this; |
| 85 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 86 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 87 | const name::Component& |
| 88 | getFinalBlockId() const |
| 89 | { |
| 90 | return m_finalBlockId; |
| 91 | } |
| 92 | |
| 93 | MetaInfo& |
| 94 | setFinalBlockId(const name::Component& finalBlockId) |
| 95 | { |
| 96 | m_wire.reset(); |
| 97 | m_finalBlockId = finalBlockId; |
| 98 | return *this; |
| 99 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 101 | public: // EqualityComparable concept |
| 102 | bool |
| 103 | operator==(const MetaInfo& other) const |
| 104 | { |
| 105 | return wireEncode() == other.wireEncode(); |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | operator!=(const MetaInfo& other) const |
| 110 | { |
| 111 | return !(*this == other); |
| 112 | } |
| 113 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 114 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 115 | uint32_t m_type; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 116 | time::milliseconds m_freshnessPeriod; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 117 | name::Component m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 119 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 122 | template<bool T> |
| 123 | inline size_t |
| 124 | MetaInfo::wireEncode(EncodingImpl<T>& blk) const |
| 125 | { |
| 126 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 127 | // ContentType? |
| 128 | // FreshnessPeriod? |
| 129 | // FinalBlockId? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 131 | size_t totalLength = 0; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 132 | |
| 133 | // FinalBlockId |
| 134 | if (!m_finalBlockId.empty()) |
| 135 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 136 | totalLength += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 137 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 139 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 140 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 142 | totalLength += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod, |
| 143 | m_freshnessPeriod.count()); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // ContentType |
| 147 | if (m_type != TYPE_DEFAULT) |
| 148 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | totalLength += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | totalLength += blk.prependVarNumber(totalLength); |
| 153 | totalLength += blk.prependVarNumber(Tlv::MetaInfo); |
| 154 | return totalLength; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 157 | inline const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 158 | MetaInfo::wireEncode() const |
| 159 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 160 | if (m_wire.hasWire()) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 161 | return m_wire; |
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 | EncodingEstimator estimator; |
| 164 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 166 | EncodingBuffer buffer(estimatedSize, 0); |
| 167 | wireEncode(buffer); |
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 = buffer.block(); |
| 170 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 171 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 173 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 174 | MetaInfo::wireDecode(const Block& wire) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 175 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 176 | m_wire = wire; |
| 177 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 179 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 180 | // ContentType? |
| 181 | // FreshnessPeriod? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 183 | // ContentType |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 184 | Block::element_const_iterator val = m_wire.find(Tlv::ContentType); |
| 185 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 186 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 187 | m_type = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 188 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 189 | else |
| 190 | m_type = TYPE_DEFAULT; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 191 | |
| 192 | // FreshnessPeriod |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 193 | val = m_wire.find(Tlv::FreshnessPeriod); |
| 194 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 195 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 196 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 197 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 198 | else |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 199 | m_freshnessPeriod = time::milliseconds::min(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 200 | |
| 201 | // FinalBlockId |
| 202 | val = m_wire.find(Tlv::FinalBlockId); |
| 203 | if (val != m_wire.elements().end()) |
| 204 | { |
| 205 | m_finalBlockId = val->blockFromValue(); |
| 206 | if (m_finalBlockId.type() != Tlv::NameComponent) |
| 207 | { |
| 208 | /// @todo May or may not throw exception later... |
| 209 | m_finalBlockId.reset(); |
| 210 | } |
| 211 | } |
| 212 | else |
| 213 | m_finalBlockId.reset(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 216 | inline std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 217 | operator<<(std::ostream& os, const MetaInfo& info) |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 218 | { |
| 219 | // ContentType |
| 220 | os << "ContentType: " << info.getType(); |
| 221 | |
| 222 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 223 | if (info.getFreshnessPeriod() >= time::milliseconds::zero()) { |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 224 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 225 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 226 | |
| 227 | if (!info.getFinalBlockId().empty()) { |
| 228 | os << ", FinalBlockId: "; |
| 229 | info.getFinalBlockId().toUri(os); |
| 230 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 231 | return os; |
| 232 | } |
| 233 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 234 | } // namespace ndn |
| 235 | |
| 236 | #endif // NDN_META_INFO_HPP |