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 | /* |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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& |
| 66 | MetaInfo::setFinalBlockId(const name::Component& finalBlockId) |
| 67 | { |
| 68 | m_wire.reset(); |
| 69 | m_finalBlockId = finalBlockId; |
| 70 | return *this; |
| 71 | } |
| 72 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 73 | const std::list<Block>& |
| 74 | MetaInfo::getAppMetaInfo() const |
| 75 | { |
| 76 | return m_appMetaInfo; |
| 77 | } |
| 78 | |
| 79 | MetaInfo& |
| 80 | MetaInfo::setAppMetaInfo(const std::list<Block>& info) |
| 81 | { |
| 82 | for (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) { |
| 83 | if (!(128 <= i->type() && i->type() <= 252)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 84 | BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range " |
| 85 | "[128, 252]")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | m_wire.reset(); |
| 89 | m_appMetaInfo = info; |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | MetaInfo& |
| 94 | MetaInfo::addAppMetaInfo(const Block& block) |
| 95 | { |
| 96 | if (!(128 <= block.type() && block.type() <= 252)) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range " |
| 98 | "[128, 252]")); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 99 | |
| 100 | m_wire.reset(); |
| 101 | m_appMetaInfo.push_back(block); |
| 102 | return *this; |
| 103 | } |
| 104 | |
| 105 | bool |
| 106 | MetaInfo::removeAppMetaInfo(uint32_t tlvType) |
| 107 | { |
| 108 | for (std::list<Block>::iterator iter = m_appMetaInfo.begin(); |
| 109 | iter != m_appMetaInfo.end(); ++iter) { |
| 110 | if (iter->type() == tlvType) { |
| 111 | m_wire.reset(); |
| 112 | m_appMetaInfo.erase(iter); |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | const Block* |
| 120 | MetaInfo::findAppMetaInfo(uint32_t tlvType) const |
| 121 | { |
| 122 | for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin(); |
| 123 | iter != m_appMetaInfo.end(); ++iter) { |
| 124 | if (iter->type() == tlvType) { |
| 125 | return &*iter; |
| 126 | } |
| 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 131 | template<encoding::Tag TAG> |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 132 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 133 | MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 134 | { |
| 135 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 136 | // ContentType? |
| 137 | // FreshnessPeriod? |
| 138 | // FinalBlockId? |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 139 | // AppMetaInfo* |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 140 | |
| 141 | size_t totalLength = 0; |
| 142 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 143 | for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin(); |
| 144 | appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) { |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 145 | totalLength += encoder.prependBlock(*appMetaInfoItem); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 148 | // FinalBlockId |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 149 | if (!m_finalBlockId.empty()) { |
| 150 | totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, m_finalBlockId); |
| 151 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 152 | |
| 153 | // FreshnessPeriod |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 154 | if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) { |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 155 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod.count()); |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 156 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 157 | |
| 158 | // ContentType |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 159 | if (m_type != tlv::ContentType_Blob) { |
| 160 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type); |
| 161 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 163 | totalLength += encoder.prependVarNumber(totalLength); |
| 164 | totalLength += encoder.prependVarNumber(tlv::MetaInfo); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 165 | return totalLength; |
| 166 | } |
| 167 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 168 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 169 | |
| 170 | const Block& |
| 171 | MetaInfo::wireEncode() const |
| 172 | { |
| 173 | if (m_wire.hasWire()) |
| 174 | return m_wire; |
| 175 | |
| 176 | EncodingEstimator estimator; |
| 177 | size_t estimatedSize = wireEncode(estimator); |
| 178 | |
| 179 | EncodingBuffer buffer(estimatedSize, 0); |
| 180 | wireEncode(buffer); |
| 181 | |
| 182 | m_wire = buffer.block(); |
| 183 | return m_wire; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | MetaInfo::wireDecode(const Block& wire) |
| 188 | { |
| 189 | m_wire = wire; |
| 190 | m_wire.parse(); |
| 191 | |
| 192 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 193 | // ContentType? |
| 194 | // FreshnessPeriod? |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 195 | // FinalBlockId? |
| 196 | // AppMetaInfo* |
| 197 | |
| 198 | |
| 199 | Block::element_const_iterator val = m_wire.elements_begin(); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 200 | |
| 201 | // ContentType |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 202 | if (val != m_wire.elements_end() && val->type() == tlv::ContentType) { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 203 | m_type = readNonNegativeIntegerAs<uint32_t>(*val); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 204 | ++val; |
| 205 | } |
| 206 | else { |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 207 | m_type = tlv::ContentType_Blob; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 208 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 209 | |
| 210 | // FreshnessPeriod |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 211 | if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) { |
| 212 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 213 | ++val; |
| 214 | } |
| 215 | else { |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 216 | m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 217 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 218 | |
| 219 | // FinalBlockId |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 220 | if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) { |
| 221 | m_finalBlockId = val->blockFromValue(); |
| 222 | if (m_finalBlockId.type() != tlv::NameComponent) |
| 223 | { |
| 224 | /// @todo May or may not throw exception later... |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 225 | m_finalBlockId = name::Component(); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 226 | } |
| 227 | ++val; |
| 228 | } |
| 229 | else { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 230 | m_finalBlockId = name::Component(); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // AppMetaInfo (if any) |
| 234 | for (; val != m_wire.elements().end(); ++val) { |
| 235 | m_appMetaInfo.push_back(*val); |
| 236 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | std::ostream& |
| 240 | operator<<(std::ostream& os, const MetaInfo& info) |
| 241 | { |
| 242 | // ContentType |
| 243 | os << "ContentType: " << info.getType(); |
| 244 | |
| 245 | // FreshnessPeriod |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 246 | if (info.getFreshnessPeriod() > time::milliseconds::zero()) { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 247 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 248 | } |
| 249 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 250 | // FinalBlockId |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 251 | if (!info.getFinalBlockId().empty()) { |
| 252 | os << ", FinalBlockId: "; |
| 253 | info.getFinalBlockId().toUri(os); |
| 254 | } |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 255 | |
| 256 | // App-defined MetaInfo items |
| 257 | for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin(); |
| 258 | iter != info.getAppMetaInfo().end(); ++iter) { |
| 259 | os << ", AppMetaInfoTlvType: " << iter->type(); |
| 260 | } |
| 261 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 262 | return os; |
| 263 | } |
| 264 | |
| 265 | } // namespace ndn |