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 | }; |
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 | |
| 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 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 41 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 42 | wireEncode() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 43 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 44 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 45 | wireDecode(const Block &wire); |
| 46 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 48 | // Getters/setters |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 49 | |
| 50 | uint32_t |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 51 | getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 52 | { |
| 53 | return m_type; |
| 54 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [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 | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 63 | |
| 64 | const time::milliseconds& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 65 | getFreshnessPeriod() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 66 | { |
| 67 | return m_freshnessPeriod; |
| 68 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 69 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 70 | MetaInfo& |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 71 | setFreshnessPeriod(const time::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 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 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; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 94 | time::milliseconds m_freshnessPeriod; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 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? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 108 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 116 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 117 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 118 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 119 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 120 | total_len += prependNonNegativeIntegerBlock(blk, Tlv::FreshnessPeriod, |
| 121 | m_freshnessPeriod.count()); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // ContentType |
| 125 | if (m_type != TYPE_DEFAULT) |
| 126 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 127 | total_len += prependNonNegativeIntegerBlock(blk, Tlv::ContentType, m_type); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | total_len += blk.prependVarNumber (total_len); |
| 131 | total_len += blk.prependVarNumber (Tlv::MetaInfo); |
| 132 | return total_len; |
| 133 | } |
| 134 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 135 | inline const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 136 | MetaInfo::wireEncode() const |
| 137 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 138 | if (m_wire.hasWire ()) |
| 139 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 141 | EncodingEstimator estimator; |
| 142 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 143 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 144 | EncodingBuffer buffer(estimatedSize, 0); |
| 145 | wireEncode(buffer); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 147 | m_wire = buffer.block(); |
| 148 | return m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 149 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 150 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 151 | inline void |
| 152 | MetaInfo::wireDecode(const Block &wire) |
| 153 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 154 | m_wire = wire; |
| 155 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | 213ea6d | 2014-01-03 15:31:03 -0800 | [diff] [blame] | 157 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 158 | // ContentType? |
| 159 | // FreshnessPeriod? |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 160 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 161 | // ContentType |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 162 | Block::element_const_iterator val = m_wire.find(Tlv::ContentType); |
| 163 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 164 | { |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 165 | m_type = readNonNegativeInteger(*val); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 166 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 167 | else |
| 168 | m_type = TYPE_DEFAULT; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 169 | |
| 170 | // FreshnessPeriod |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 171 | val = m_wire.find(Tlv::FreshnessPeriod); |
| 172 | if (val != m_wire.elements().end()) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 173 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 174 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
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 | else |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 177 | m_freshnessPeriod = time::milliseconds::min(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 178 | |
| 179 | // FinalBlockId |
| 180 | val = m_wire.find(Tlv::FinalBlockId); |
| 181 | if (val != m_wire.elements().end()) |
| 182 | { |
| 183 | m_finalBlockId = val->blockFromValue(); |
| 184 | if (m_finalBlockId.type() != Tlv::NameComponent) |
| 185 | { |
| 186 | /// @todo May or may not throw exception later... |
| 187 | m_finalBlockId.reset(); |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | m_finalBlockId.reset(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 194 | inline std::ostream& |
| 195 | operator << (std::ostream &os, const MetaInfo &info) |
| 196 | { |
| 197 | // ContentType |
| 198 | os << "ContentType: " << info.getType(); |
| 199 | |
| 200 | // FreshnessPeriod |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 201 | if (info.getFreshnessPeriod() >= time::milliseconds::zero()) { |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 202 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 203 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 204 | |
| 205 | if (!info.getFinalBlockId().empty()) { |
| 206 | os << ", FinalBlockId: "; |
| 207 | info.getFinalBlockId().toUri(os); |
| 208 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 209 | return os; |
| 210 | } |
| 211 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 212 | } // namespace ndn |
| 213 | |
| 214 | #endif // NDN_META_INFO_HPP |