blob: 0c4852c01477b3d8f0ff8808cc9d6d972632b7f3 [file] [log] [blame]
Alexander Afanasyev01065fb2014-10-02 13:01:46 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi51742322017-08-13 17:04:52 +00002/*
Junxiao Shiebfe4a22018-04-01 23:53:40 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev01065fb2014-10-02 13:01:46 -07004 *
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 Afanasyev01065fb2014-10-02 13:01:46 -070025
26namespace ndn {
27
Shock Jiangca7ea702014-10-02 11:23:25 -070028BOOST_CONCEPT_ASSERT((boost::EqualityComparable<MetaInfo>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070029BOOST_CONCEPT_ASSERT((WireEncodable<MetaInfo>));
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070030BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<MetaInfo>));
Junxiao Shic2b8d242014-11-04 08:35:29 -070031BOOST_CONCEPT_ASSERT((WireDecodable<MetaInfo>));
32static_assert(std::is_base_of<tlv::Error, MetaInfo::Error>::value,
33 "MetaInfo::Error must inherit from tlv::Error");
Shock Jiangca7ea702014-10-02 11:23:25 -070034
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070035MetaInfo::MetaInfo()
Junxiao Shia464b922014-11-12 21:13:06 -070036 : m_type(tlv::ContentType_Blob)
Eric Newberryb555b002017-05-17 00:30:44 -070037 , m_freshnessPeriod(DEFAULT_FRESHNESS_PERIOD)
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070038{
39}
40
41MetaInfo::MetaInfo(const Block& block)
42{
43 wireDecode(block);
44}
45
46MetaInfo&
47MetaInfo::setType(uint32_t type)
48{
49 m_wire.reset();
50 m_type = type;
51 return *this;
52}
53
54MetaInfo&
Eric Newberryb555b002017-05-17 00:30:44 -070055MetaInfo::setFreshnessPeriod(time::milliseconds freshnessPeriod)
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070056{
Eric Newberryb555b002017-05-17 00:30:44 -070057 if (freshnessPeriod < time::milliseconds::zero()) {
58 BOOST_THROW_EXCEPTION(std::invalid_argument("FreshnessPeriod must be >= 0"));
59 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070060 m_wire.reset();
61 m_freshnessPeriod = freshnessPeriod;
62 return *this;
63}
64
65MetaInfo&
Junxiao Shiebfe4a22018-04-01 23:53:40 +000066MetaInfo::setFinalBlock(optional<name::Component> finalBlockId)
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070067{
68 m_wire.reset();
Junxiao Shiebfe4a22018-04-01 23:53:40 +000069 m_finalBlockId = std::move(finalBlockId);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070070 return *this;
71}
72
Shock Jiangca7ea702014-10-02 11:23:25 -070073const std::list<Block>&
74MetaInfo::getAppMetaInfo() const
75{
76 return m_appMetaInfo;
77}
78
79MetaInfo&
80MetaInfo::setAppMetaInfo(const std::list<Block>& info)
81{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040082 for (const auto& block : info) {
83 if (block.type() < 128 || block.type() > 252)
84 BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range [128, 252]"));
Shock Jiangca7ea702014-10-02 11:23:25 -070085 }
86
87 m_wire.reset();
88 m_appMetaInfo = info;
89 return *this;
90}
91
92MetaInfo&
93MetaInfo::addAppMetaInfo(const Block& block)
94{
95 if (!(128 <= block.type() && block.type() <= 252))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070096 BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
97 "[128, 252]"));
Shock Jiangca7ea702014-10-02 11:23:25 -070098
99 m_wire.reset();
100 m_appMetaInfo.push_back(block);
101 return *this;
102}
103
104bool
105MetaInfo::removeAppMetaInfo(uint32_t tlvType)
106{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400107 for (auto it = m_appMetaInfo.begin(); it != m_appMetaInfo.end(); ++it) {
108 if (it->type() == tlvType) {
Shock Jiangca7ea702014-10-02 11:23:25 -0700109 m_wire.reset();
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400110 m_appMetaInfo.erase(it);
Shock Jiangca7ea702014-10-02 11:23:25 -0700111 return true;
112 }
113 }
114 return false;
115}
116
117const Block*
118MetaInfo::findAppMetaInfo(uint32_t tlvType) const
119{
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400120 auto it = std::find_if(m_appMetaInfo.begin(), m_appMetaInfo.end(),
121 [=] (const Block& b) { return b.type() == tlvType; });
122 return it != m_appMetaInfo.end() ? &*it : nullptr;
Shock Jiangca7ea702014-10-02 11:23:25 -0700123}
124
Alexander Afanasyev74633892015-02-08 18:08:46 -0800125template<encoding::Tag TAG>
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700126size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800127MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700128{
129 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
130 // ContentType?
131 // FreshnessPeriod?
132 // FinalBlockId?
Shock Jiangca7ea702014-10-02 11:23:25 -0700133 // AppMetaInfo*
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700134
135 size_t totalLength = 0;
136
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400137 for (auto it = m_appMetaInfo.rbegin(); it != m_appMetaInfo.rend(); ++it) {
138 totalLength += encoder.prependBlock(*it);
Shock Jiangca7ea702014-10-02 11:23:25 -0700139 }
140
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700141 // FinalBlockId
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000142 if (m_finalBlockId) {
143 totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
Eric Newberryb555b002017-05-17 00:30:44 -0700144 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700145
146 // FreshnessPeriod
Eric Newberryb555b002017-05-17 00:30:44 -0700147 if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400148 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod,
149 static_cast<uint64_t>(m_freshnessPeriod.count()));
Eric Newberryb555b002017-05-17 00:30:44 -0700150 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700151
152 // ContentType
Eric Newberryb555b002017-05-17 00:30:44 -0700153 if (m_type != tlv::ContentType_Blob) {
154 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
155 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700156
Alexander Afanasyev74633892015-02-08 18:08:46 -0800157 totalLength += encoder.prependVarNumber(totalLength);
158 totalLength += encoder.prependVarNumber(tlv::MetaInfo);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700159 return totalLength;
160}
161
Davide Pesavento88a0d812017-08-19 21:31:42 -0400162NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700163
164const Block&
165MetaInfo::wireEncode() const
166{
167 if (m_wire.hasWire())
168 return m_wire;
169
170 EncodingEstimator estimator;
171 size_t estimatedSize = wireEncode(estimator);
172
173 EncodingBuffer buffer(estimatedSize, 0);
174 wireEncode(buffer);
175
176 m_wire = buffer.block();
177 return m_wire;
178}
179
180void
181MetaInfo::wireDecode(const Block& wire)
182{
183 m_wire = wire;
184 m_wire.parse();
185
186 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
187 // ContentType?
188 // FreshnessPeriod?
Shock Jiangca7ea702014-10-02 11:23:25 -0700189 // FinalBlockId?
190 // AppMetaInfo*
191
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400192 auto val = m_wire.elements_begin();
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700193
194 // ContentType
Shock Jiangca7ea702014-10-02 11:23:25 -0700195 if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
Junxiao Shi51742322017-08-13 17:04:52 +0000196 m_type = readNonNegativeIntegerAs<uint32_t>(*val);
Shock Jiangca7ea702014-10-02 11:23:25 -0700197 ++val;
198 }
199 else {
Junxiao Shia464b922014-11-12 21:13:06 -0700200 m_type = tlv::ContentType_Blob;
Shock Jiangca7ea702014-10-02 11:23:25 -0700201 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700202
203 // FreshnessPeriod
Shock Jiangca7ea702014-10-02 11:23:25 -0700204 if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
205 m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
206 ++val;
207 }
208 else {
Eric Newberryb555b002017-05-17 00:30:44 -0700209 m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
Shock Jiangca7ea702014-10-02 11:23:25 -0700210 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700211
212 // FinalBlockId
Shock Jiangca7ea702014-10-02 11:23:25 -0700213 if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000214 m_finalBlockId.emplace(val->blockFromValue());
Shock Jiangca7ea702014-10-02 11:23:25 -0700215 ++val;
216 }
217 else {
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000218 m_finalBlockId = nullopt;
Shock Jiangca7ea702014-10-02 11:23:25 -0700219 }
220
221 // AppMetaInfo (if any)
222 for (; val != m_wire.elements().end(); ++val) {
223 m_appMetaInfo.push_back(*val);
224 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700225}
226
227std::ostream&
228operator<<(std::ostream& os, const MetaInfo& info)
229{
230 // ContentType
231 os << "ContentType: " << info.getType();
232
233 // FreshnessPeriod
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400234 if (info.getFreshnessPeriod() > 0_ms) {
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700235 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
236 }
237
Shock Jiangca7ea702014-10-02 11:23:25 -0700238 // FinalBlockId
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000239 if (info.getFinalBlock()) {
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700240 os << ", FinalBlockId: ";
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000241 info.getFinalBlock()->toUri(os);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700242 }
Shock Jiangca7ea702014-10-02 11:23:25 -0700243
244 // App-defined MetaInfo items
Davide Pesaventodb4da5e2018-06-15 11:37:52 -0400245 for (const auto& block : info.getAppMetaInfo()) {
246 os << ", AppMetaInfoTlvType: " << block.type();
Shock Jiangca7ea702014-10-02 11:23:25 -0700247 }
248
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700249 return os;
250}
251
252} // namespace ndn