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 | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 36 | |
| 37 | template<bool T> |
| 38 | size_t |
| 39 | wireEncode(EncodingImpl<T> &block) const; |
| 40 | |
| 41 | const Block& |
| 42 | wireEncode() const; |
| 43 | |
| 44 | void |
| 45 | wireDecode(const Block &wire); |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | // Gettest/setters |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 49 | |
| 50 | uint32_t |
| 51 | getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 52 | { |
| 53 | return m_type; |
| 54 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 56 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | setType(uint32_t type) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 58 | { |
| 59 | m_wire.reset(); |
| 60 | m_type = type; |
| 61 | return *this; |
| 62 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 63 | |
| 64 | Milliseconds |
| 65 | getFreshnessPeriod() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 66 | { |
| 67 | return m_freshnessPeriod; |
| 68 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 70 | MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 71 | setFreshnessPeriod(Milliseconds freshnessPeriod) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 72 | { |
| 73 | m_wire.reset(); |
| 74 | m_freshnessPeriod = freshnessPeriod; |
| 75 | return *this; |
| 76 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 78 | const name::Component& |
| 79 | getFinalBlockId() const |
| 80 | { |
| 81 | return m_finalBlockId; |
| 82 | } |
| 83 | |
| 84 | MetaInfo& |
| 85 | setFinalBlockId(const name::Component& finalBlockId) |
| 86 | { |
| 87 | m_wire.reset(); |
| 88 | m_finalBlockId = finalBlockId; |
| 89 | return *this; |
| 90 | } |
| 91 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 92 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 93 | uint32_t m_type; |
| 94 | Milliseconds m_freshnessPeriod; |
| 95 | name::Component m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 97 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 100 | template<bool T> |
| 101 | inline size_t |
| 102 | MetaInfo::wireEncode(EncodingImpl<T>& blk) const |
| 103 | { |
| 104 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 105 | // ContentType? |
| 106 | // FreshnessPeriod? |
| 107 | // FinalBlockId? |
| 108 | |
| 109 | size_t total_len = 0; |
| 110 | |
| 111 | // FinalBlockId |
| 112 | if (!m_finalBlockId.empty()) |
| 113 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 114 | total_len += prependNestedBlock(blk, Tlv::FinalBlockId, m_finalBlockId); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // FreshnessPeriod |
| 118 | if (m_freshnessPeriod >= 0) |
| 119 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 120 | total_len += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod, m_freshnessPeriod); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // ContentType |
| 124 | if (m_type != TYPE_DEFAULT) |
| 125 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 126 | total_len += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | total_len += blk.prependVarNumber (total_len); |
| 130 | total_len += blk.prependVarNumber (Tlv::MetaInfo); |
| 131 | return total_len; |
| 132 | } |
| 133 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 134 | inline const Block& |
| 135 | MetaInfo::wireEncode() const |
| 136 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 137 | if (m_wire.hasWire ()) |
| 138 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 139 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 140 | EncodingEstimator estimator; |
| 141 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 143 | EncodingBuffer buffer(estimatedSize, 0); |
| 144 | wireEncode(buffer); |
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 | m_wire = buffer.block(); |
| 147 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | inline void |
| 151 | MetaInfo::wireDecode(const Block &wire) |
| 152 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 153 | m_wire = wire; |
| 154 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 155 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 156 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 157 | // ContentType? |
| 158 | // FreshnessPeriod? |
| 159 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 160 | // ContentType |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 161 | Block::element_const_iterator val = m_wire.find(Tlv::ContentType); |
| 162 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 163 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 164 | m_type = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 165 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 166 | else |
| 167 | m_type = TYPE_DEFAULT; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 168 | |
| 169 | // FreshnessPeriod |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 170 | val = m_wire.find(Tlv::FreshnessPeriod); |
| 171 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 172 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 173 | m_freshnessPeriod = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 174 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 175 | else |
| 176 | m_freshnessPeriod = -1; |
| 177 | |
| 178 | // FinalBlockId |
| 179 | val = m_wire.find(Tlv::FinalBlockId); |
| 180 | if (val != m_wire.elements().end()) |
| 181 | { |
| 182 | m_finalBlockId = val->blockFromValue(); |
| 183 | if (m_finalBlockId.type() != Tlv::NameComponent) |
| 184 | { |
| 185 | /// @todo May or may not throw exception later... |
| 186 | m_finalBlockId.reset(); |
| 187 | } |
| 188 | } |
| 189 | else |
| 190 | m_finalBlockId.reset(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 193 | inline std::ostream& |
| 194 | operator << (std::ostream &os, const MetaInfo &info) |
| 195 | { |
| 196 | // ContentType |
| 197 | os << "ContentType: " << info.getType(); |
| 198 | |
| 199 | // FreshnessPeriod |
| 200 | if (info.getFreshnessPeriod() >= 0) { |
| 201 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 202 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 203 | |
| 204 | if (!info.getFinalBlockId().empty()) { |
| 205 | os << ", FinalBlockId: "; |
| 206 | info.getFinalBlockId().toUri(os); |
| 207 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 208 | return os; |
| 209 | } |
| 210 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 211 | } // namespace ndn |
| 212 | |
| 213 | #endif // NDN_META_INFO_HPP |