Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "meta-info.hpp" |
| 23 | #include "encoding/block-helpers.hpp" |
| 24 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<MetaInfo>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 29 | BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<MetaInfo>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireDecodable<MetaInfo>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, MetaInfo::Error>::value, |
| 33 | "MetaInfo::Error must inherit from tlv::Error"); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 35 | MetaInfo::MetaInfo() |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 36 | : m_type(tlv::ContentType_Blob) |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 37 | , m_freshnessPeriod(DEFAULT_FRESHNESS_PERIOD) |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | MetaInfo::MetaInfo(const Block& block) |
| 42 | { |
| 43 | wireDecode(block); |
| 44 | } |
| 45 | |
| 46 | MetaInfo& |
| 47 | MetaInfo::setType(uint32_t type) |
| 48 | { |
| 49 | m_wire.reset(); |
| 50 | m_type = type; |
| 51 | return *this; |
| 52 | } |
| 53 | |
| 54 | MetaInfo& |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 55 | MetaInfo::setFreshnessPeriod(time::milliseconds freshnessPeriod) |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 56 | { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 57 | if (freshnessPeriod < time::milliseconds::zero()) { |
| 58 | BOOST_THROW_EXCEPTION(std::invalid_argument("FreshnessPeriod must be >= 0")); |
| 59 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 60 | m_wire.reset(); |
| 61 | m_freshnessPeriod = freshnessPeriod; |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | MetaInfo& |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 66 | MetaInfo::setFinalBlock(optional<name::Component> finalBlockId) |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 67 | { |
| 68 | m_wire.reset(); |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 69 | m_finalBlockId = std::move(finalBlockId); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 70 | return *this; |
| 71 | } |
| 72 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 73 | MetaInfo& |
| 74 | MetaInfo::setFinalBlockId(const name::Component& finalBlockId) |
| 75 | { |
| 76 | if (finalBlockId.isGeneric() && finalBlockId.empty()) { |
| 77 | return setFinalBlock(nullopt); |
| 78 | } |
| 79 | return setFinalBlock(finalBlockId); |
| 80 | } |
| 81 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 82 | const std::list<Block>& |
| 83 | MetaInfo::getAppMetaInfo() const |
| 84 | { |
| 85 | return m_appMetaInfo; |
| 86 | } |
| 87 | |
| 88 | MetaInfo& |
| 89 | MetaInfo::setAppMetaInfo(const std::list<Block>& info) |
| 90 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 91 | for (const auto& block : info) { |
| 92 | if (block.type() < 128 || block.type() > 252) |
| 93 | BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range [128, 252]")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | m_wire.reset(); |
| 97 | m_appMetaInfo = info; |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | MetaInfo& |
| 102 | MetaInfo::addAppMetaInfo(const Block& block) |
| 103 | { |
| 104 | if (!(128 <= block.type() && block.type() <= 252)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 105 | BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range " |
| 106 | "[128, 252]")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 107 | |
| 108 | m_wire.reset(); |
| 109 | m_appMetaInfo.push_back(block); |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | bool |
| 114 | MetaInfo::removeAppMetaInfo(uint32_t tlvType) |
| 115 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 116 | for (auto it = m_appMetaInfo.begin(); it != m_appMetaInfo.end(); ++it) { |
| 117 | if (it->type() == tlvType) { |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 118 | m_wire.reset(); |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 119 | m_appMetaInfo.erase(it); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | const Block* |
| 127 | MetaInfo::findAppMetaInfo(uint32_t tlvType) const |
| 128 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 129 | auto it = std::find_if(m_appMetaInfo.begin(), m_appMetaInfo.end(), |
| 130 | [=] (const Block& b) { return b.type() == tlvType; }); |
| 131 | return it != m_appMetaInfo.end() ? &*it : nullptr; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 134 | template<encoding::Tag TAG> |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 135 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 136 | MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 137 | { |
| 138 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 139 | // ContentType? |
| 140 | // FreshnessPeriod? |
| 141 | // FinalBlockId? |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 142 | // AppMetaInfo* |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 143 | |
| 144 | size_t totalLength = 0; |
| 145 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 146 | for (auto it = m_appMetaInfo.rbegin(); it != m_appMetaInfo.rend(); ++it) { |
| 147 | totalLength += encoder.prependBlock(*it); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 150 | // FinalBlockId |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 151 | if (m_finalBlockId) { |
| 152 | totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 153 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 154 | |
| 155 | // FreshnessPeriod |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 156 | if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 157 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, |
| 158 | static_cast<uint64_t>(m_freshnessPeriod.count())); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 159 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 160 | |
| 161 | // ContentType |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 162 | if (m_type != tlv::ContentType_Blob) { |
| 163 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type); |
| 164 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 166 | totalLength += encoder.prependVarNumber(totalLength); |
| 167 | totalLength += encoder.prependVarNumber(tlv::MetaInfo); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 168 | return totalLength; |
| 169 | } |
| 170 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 171 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 172 | |
| 173 | const Block& |
| 174 | MetaInfo::wireEncode() const |
| 175 | { |
| 176 | if (m_wire.hasWire()) |
| 177 | return m_wire; |
| 178 | |
| 179 | EncodingEstimator estimator; |
| 180 | size_t estimatedSize = wireEncode(estimator); |
| 181 | |
| 182 | EncodingBuffer buffer(estimatedSize, 0); |
| 183 | wireEncode(buffer); |
| 184 | |
| 185 | m_wire = buffer.block(); |
| 186 | return m_wire; |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | MetaInfo::wireDecode(const Block& wire) |
| 191 | { |
| 192 | m_wire = wire; |
| 193 | m_wire.parse(); |
| 194 | |
| 195 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 196 | // ContentType? |
| 197 | // FreshnessPeriod? |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 198 | // FinalBlockId? |
| 199 | // AppMetaInfo* |
| 200 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 201 | auto val = m_wire.elements_begin(); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 202 | |
| 203 | // ContentType |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 204 | if (val != m_wire.elements_end() && val->type() == tlv::ContentType) { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 205 | m_type = readNonNegativeIntegerAs<uint32_t>(*val); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 206 | ++val; |
| 207 | } |
| 208 | else { |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 209 | m_type = tlv::ContentType_Blob; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 210 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 211 | |
| 212 | // FreshnessPeriod |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 213 | if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) { |
| 214 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 215 | ++val; |
| 216 | } |
| 217 | else { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 218 | m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 219 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 220 | |
| 221 | // FinalBlockId |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 222 | if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 223 | m_finalBlockId.emplace(val->blockFromValue()); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 224 | ++val; |
| 225 | } |
| 226 | else { |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 227 | m_finalBlockId = nullopt; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // AppMetaInfo (if any) |
| 231 | for (; val != m_wire.elements().end(); ++val) { |
| 232 | m_appMetaInfo.push_back(*val); |
| 233 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | std::ostream& |
| 237 | operator<<(std::ostream& os, const MetaInfo& info) |
| 238 | { |
| 239 | // ContentType |
| 240 | os << "ContentType: " << info.getType(); |
| 241 | |
| 242 | // FreshnessPeriod |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 243 | if (info.getFreshnessPeriod() > 0_ms) { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 244 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 245 | } |
| 246 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 247 | // FinalBlockId |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 248 | if (info.getFinalBlock()) { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 249 | os << ", FinalBlockId: "; |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 250 | info.getFinalBlock()->toUri(os); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 251 | } |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 252 | |
| 253 | // App-defined MetaInfo items |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 254 | for (const auto& block : info.getAppMetaInfo()) { |
| 255 | os << ", AppMetaInfoTlvType: " << block.type(); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 258 | return os; |
| 259 | } |
| 260 | |
| 261 | } // namespace ndn |