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