Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_META_INFO_HPP |
| 9 | #define NDN_META_INFO_HPP |
| 10 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 11 | #include "encoding/encoding-buffer.hpp" |
| 12 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 13 | namespace ndn { |
| 14 | |
| 15 | /** |
| 16 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 17 | */ |
| 18 | class MetaInfo { |
| 19 | public: |
| 20 | enum { |
| 21 | TYPE_DEFAULT = 0, |
| 22 | TYPE_LINK = 1, |
| 23 | TYPE_KEY = 2 |
| 24 | }; |
| 25 | |
| 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 | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 29 | { |
| 30 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 31 | |
| 32 | MetaInfo(const Block& block) |
| 33 | { |
| 34 | wireDecode(block); |
| 35 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | |
| 37 | uint32_t |
| 38 | getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 39 | { |
| 40 | return m_type; |
| 41 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 43 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 44 | setType(uint32_t type) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 45 | { |
| 46 | m_wire.reset(); |
| 47 | m_type = type; |
| 48 | return *this; |
| 49 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 50 | |
| 51 | Milliseconds |
| 52 | getFreshnessPeriod() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 53 | { |
| 54 | return m_freshnessPeriod; |
| 55 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 57 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 58 | setFreshnessPeriod(Milliseconds freshnessPeriod) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 59 | { |
| 60 | m_wire.reset(); |
| 61 | m_freshnessPeriod = freshnessPeriod; |
| 62 | return *this; |
| 63 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 65 | const name::Component& |
| 66 | getFinalBlockId() const |
| 67 | { |
| 68 | return m_finalBlockId; |
| 69 | } |
| 70 | |
| 71 | MetaInfo& |
| 72 | setFinalBlockId(const name::Component& finalBlockId) |
| 73 | { |
| 74 | m_wire.reset(); |
| 75 | m_finalBlockId = finalBlockId; |
| 76 | return *this; |
| 77 | } |
| 78 | |
| 79 | template<bool T> |
| 80 | size_t |
| 81 | wireEncode(EncodingImpl<T> &block) const; |
| 82 | |
| 83 | const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 84 | wireEncode() const; |
| 85 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 86 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 87 | wireDecode(const Block &wire); |
| 88 | |
| 89 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 90 | uint32_t m_type; |
| 91 | Milliseconds m_freshnessPeriod; |
| 92 | name::Component m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 93 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 94 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 97 | template<bool T> |
| 98 | inline size_t |
| 99 | MetaInfo::wireEncode(EncodingImpl<T>& blk) const |
| 100 | { |
| 101 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 102 | // ContentType? |
| 103 | // FreshnessPeriod? |
| 104 | // FinalBlockId? |
| 105 | |
| 106 | size_t total_len = 0; |
| 107 | |
| 108 | // FinalBlockId |
| 109 | if (!m_finalBlockId.empty()) |
| 110 | { |
| 111 | size_t var_len = m_finalBlockId.wireEncode (blk); |
| 112 | total_len += var_len; |
| 113 | total_len += blk.prependVarNumber (var_len); |
| 114 | total_len += blk.prependVarNumber (Tlv::FinalBlockId); |
| 115 | } |
| 116 | |
| 117 | // FreshnessPeriod |
| 118 | if (m_freshnessPeriod >= 0) |
| 119 | { |
| 120 | size_t var_len = blk.prependNonNegativeInteger (m_freshnessPeriod); |
| 121 | total_len += var_len; |
| 122 | total_len += blk.prependVarNumber (var_len); |
| 123 | total_len += blk.prependVarNumber (Tlv::FreshnessPeriod); |
| 124 | } |
| 125 | |
| 126 | // ContentType |
| 127 | if (m_type != TYPE_DEFAULT) |
| 128 | { |
| 129 | size_t var_len = blk.prependNonNegativeInteger (m_type); |
| 130 | total_len += var_len; |
| 131 | total_len += blk.prependVarNumber (var_len); |
| 132 | total_len += blk.prependVarNumber (Tlv::ContentType); |
| 133 | } |
| 134 | |
| 135 | total_len += blk.prependVarNumber (total_len); |
| 136 | total_len += blk.prependVarNumber (Tlv::MetaInfo); |
| 137 | return total_len; |
| 138 | } |
| 139 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 140 | inline const Block& |
| 141 | MetaInfo::wireEncode() const |
| 142 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 143 | if (m_wire.hasWire ()) |
| 144 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 146 | EncodingEstimator estimator; |
| 147 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 148 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 149 | EncodingBuffer buffer(estimatedSize, 0); |
| 150 | wireEncode(buffer); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 152 | m_wire = buffer.block(); |
| 153 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | inline void |
| 157 | MetaInfo::wireDecode(const Block &wire) |
| 158 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 159 | m_wire = wire; |
| 160 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 162 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 163 | // ContentType? |
| 164 | // FreshnessPeriod? |
| 165 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 166 | // ContentType |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 167 | Block::element_const_iterator val = m_wire.find(Tlv::ContentType); |
| 168 | if (val != m_wire.elements().end()) |
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_type = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 171 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 172 | else |
| 173 | m_type = TYPE_DEFAULT; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 174 | |
| 175 | // FreshnessPeriod |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 176 | val = m_wire.find(Tlv::FreshnessPeriod); |
| 177 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 178 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 179 | m_freshnessPeriod = readNonNegativeInteger(*val); |
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 | else |
| 182 | m_freshnessPeriod = -1; |
| 183 | |
| 184 | // FinalBlockId |
| 185 | val = m_wire.find(Tlv::FinalBlockId); |
| 186 | if (val != m_wire.elements().end()) |
| 187 | { |
| 188 | m_finalBlockId = val->blockFromValue(); |
| 189 | if (m_finalBlockId.type() != Tlv::NameComponent) |
| 190 | { |
| 191 | /// @todo May or may not throw exception later... |
| 192 | m_finalBlockId.reset(); |
| 193 | } |
| 194 | } |
| 195 | else |
| 196 | m_finalBlockId.reset(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 199 | inline std::ostream& |
| 200 | operator << (std::ostream &os, const MetaInfo &info) |
| 201 | { |
| 202 | // ContentType |
| 203 | os << "ContentType: " << info.getType(); |
| 204 | |
| 205 | // FreshnessPeriod |
| 206 | if (info.getFreshnessPeriod() >= 0) { |
| 207 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 208 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 209 | |
| 210 | if (!info.getFinalBlockId().empty()) { |
| 211 | os << ", FinalBlockId: "; |
| 212 | info.getFinalBlockId().toUri(os); |
| 213 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 214 | return os; |
| 215 | } |
| 216 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 217 | } // namespace ndn |
| 218 | |
| 219 | #endif // NDN_META_INFO_HPP |