Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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" |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 25 | #include "util/concepts.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 29 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<MetaInfo>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>)); |
| 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)) |
| 81 | throw Error("AppMetaInfo block has type outside the application range [128, 252]"); |
| 82 | } |
| 83 | |
| 84 | m_wire.reset(); |
| 85 | m_appMetaInfo = info; |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | MetaInfo& |
| 90 | MetaInfo::addAppMetaInfo(const Block& block) |
| 91 | { |
| 92 | if (!(128 <= block.type() && block.type() <= 252)) |
| 93 | throw Error("AppMetaInfo block has type outside the application range [128, 252]"); |
| 94 | |
| 95 | m_wire.reset(); |
| 96 | m_appMetaInfo.push_back(block); |
| 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | bool |
| 101 | MetaInfo::removeAppMetaInfo(uint32_t tlvType) |
| 102 | { |
| 103 | for (std::list<Block>::iterator iter = m_appMetaInfo.begin(); |
| 104 | iter != m_appMetaInfo.end(); ++iter) { |
| 105 | if (iter->type() == tlvType) { |
| 106 | m_wire.reset(); |
| 107 | m_appMetaInfo.erase(iter); |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | const Block* |
| 115 | MetaInfo::findAppMetaInfo(uint32_t tlvType) const |
| 116 | { |
| 117 | for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin(); |
| 118 | iter != m_appMetaInfo.end(); ++iter) { |
| 119 | if (iter->type() == tlvType) { |
| 120 | return &*iter; |
| 121 | } |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 126 | template<bool T> |
| 127 | size_t |
| 128 | MetaInfo::wireEncode(EncodingImpl<T>& blk) const |
| 129 | { |
| 130 | // MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 131 | // ContentType? |
| 132 | // FreshnessPeriod? |
| 133 | // FinalBlockId? |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 134 | // AppMetaInfo* |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 135 | |
| 136 | size_t totalLength = 0; |
| 137 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 138 | for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin(); |
| 139 | appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) { |
| 140 | totalLength += prependBlock(blk, *appMetaInfoItem); |
| 141 | } |
| 142 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 143 | // FinalBlockId |
| 144 | if (!m_finalBlockId.empty()) |
| 145 | { |
| 146 | totalLength += prependNestedBlock(blk, tlv::FinalBlockId, m_finalBlockId); |
| 147 | } |
| 148 | |
| 149 | // FreshnessPeriod |
| 150 | if (m_freshnessPeriod >= time::milliseconds::zero()) |
| 151 | { |
| 152 | totalLength += prependNonNegativeIntegerBlock(blk, tlv::FreshnessPeriod, |
| 153 | m_freshnessPeriod.count()); |
| 154 | } |
| 155 | |
| 156 | // ContentType |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 157 | if (m_type != tlv::ContentType_Blob) |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 158 | { |
| 159 | totalLength += prependNonNegativeIntegerBlock(blk, tlv::ContentType, m_type); |
| 160 | } |
| 161 | |
| 162 | totalLength += blk.prependVarNumber(totalLength); |
| 163 | totalLength += blk.prependVarNumber(tlv::MetaInfo); |
| 164 | return totalLength; |
| 165 | } |
| 166 | |
| 167 | template size_t |
| 168 | MetaInfo::wireEncode<true>(EncodingImpl<true>& block) const; |
| 169 | |
| 170 | template size_t |
| 171 | MetaInfo::wireEncode<false>(EncodingImpl<false>& block) const; |
| 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 | |
| 201 | |
| 202 | Block::element_const_iterator val = m_wire.elements_begin(); |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 203 | |
| 204 | // ContentType |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 205 | if (val != m_wire.elements_end() && val->type() == tlv::ContentType) { |
| 206 | m_type = readNonNegativeInteger(*val); |
| 207 | ++val; |
| 208 | } |
| 209 | else { |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 210 | m_type = tlv::ContentType_Blob; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 211 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 212 | |
| 213 | // FreshnessPeriod |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 214 | if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) { |
| 215 | m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 216 | ++val; |
| 217 | } |
| 218 | else { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 219 | m_freshnessPeriod = time::milliseconds::min(); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 220 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 221 | |
| 222 | // FinalBlockId |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 223 | if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) { |
| 224 | m_finalBlockId = val->blockFromValue(); |
| 225 | if (m_finalBlockId.type() != tlv::NameComponent) |
| 226 | { |
| 227 | /// @todo May or may not throw exception later... |
| 228 | m_finalBlockId.reset(); |
| 229 | } |
| 230 | ++val; |
| 231 | } |
| 232 | else { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 233 | m_finalBlockId.reset(); |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // AppMetaInfo (if any) |
| 237 | for (; val != m_wire.elements().end(); ++val) { |
| 238 | m_appMetaInfo.push_back(*val); |
| 239 | } |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | std::ostream& |
| 243 | operator<<(std::ostream& os, const MetaInfo& info) |
| 244 | { |
| 245 | // ContentType |
| 246 | os << "ContentType: " << info.getType(); |
| 247 | |
| 248 | // FreshnessPeriod |
| 249 | if (info.getFreshnessPeriod() >= time::milliseconds::zero()) { |
| 250 | os << ", FreshnessPeriod: " << info.getFreshnessPeriod(); |
| 251 | } |
| 252 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 253 | // FinalBlockId |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 254 | if (!info.getFinalBlockId().empty()) { |
| 255 | os << ", FinalBlockId: "; |
| 256 | info.getFinalBlockId().toUri(os); |
| 257 | } |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 258 | |
| 259 | // App-defined MetaInfo items |
| 260 | for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin(); |
| 261 | iter != info.getAppMetaInfo().end(); ++iter) { |
| 262 | os << ", AppMetaInfoTlvType: " << iter->type(); |
| 263 | } |
| 264 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 265 | return os; |
| 266 | } |
| 267 | |
| 268 | } // namespace ndn |