blob: c6a43a452665bf94e865284533b7348948375ad0 [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
Junxiao Shiebfe4a22018-04-01 23:53:40 +000073MetaInfo&
74MetaInfo::setFinalBlockId(const name::Component& finalBlockId)
75{
76 if (finalBlockId.isGeneric() && finalBlockId.empty()) {
77 return setFinalBlock(nullopt);
78 }
79 return setFinalBlock(finalBlockId);
80}
81
Shock Jiangca7ea702014-10-02 11:23:25 -070082const std::list<Block>&
83MetaInfo::getAppMetaInfo() const
84{
85 return m_appMetaInfo;
86}
87
88MetaInfo&
89MetaInfo::setAppMetaInfo(const std::list<Block>& info)
90{
91 for (std::list<Block>::const_iterator i = info.begin(); i != info.end(); ++i) {
92 if (!(128 <= i->type() && i->type() <= 252))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070093 BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
94 "[128, 252]"));
Shock Jiangca7ea702014-10-02 11:23:25 -070095 }
96
97 m_wire.reset();
98 m_appMetaInfo = info;
99 return *this;
100}
101
102MetaInfo&
103MetaInfo::addAppMetaInfo(const Block& block)
104{
105 if (!(128 <= block.type() && block.type() <= 252))
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700106 BOOST_THROW_EXCEPTION(Error("AppMetaInfo block has type outside the application range "
107 "[128, 252]"));
Shock Jiangca7ea702014-10-02 11:23:25 -0700108
109 m_wire.reset();
110 m_appMetaInfo.push_back(block);
111 return *this;
112}
113
114bool
115MetaInfo::removeAppMetaInfo(uint32_t tlvType)
116{
117 for (std::list<Block>::iterator iter = m_appMetaInfo.begin();
118 iter != m_appMetaInfo.end(); ++iter) {
119 if (iter->type() == tlvType) {
120 m_wire.reset();
121 m_appMetaInfo.erase(iter);
122 return true;
123 }
124 }
125 return false;
126}
127
128const Block*
129MetaInfo::findAppMetaInfo(uint32_t tlvType) const
130{
131 for (std::list<Block>::const_iterator iter = m_appMetaInfo.begin();
132 iter != m_appMetaInfo.end(); ++iter) {
133 if (iter->type() == tlvType) {
134 return &*iter;
135 }
136 }
137 return 0;
138}
139
Alexander Afanasyev74633892015-02-08 18:08:46 -0800140template<encoding::Tag TAG>
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700141size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800142MetaInfo::wireEncode(EncodingImpl<TAG>& encoder) const
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700143{
144 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
145 // ContentType?
146 // FreshnessPeriod?
147 // FinalBlockId?
Shock Jiangca7ea702014-10-02 11:23:25 -0700148 // AppMetaInfo*
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700149
150 size_t totalLength = 0;
151
Shock Jiangca7ea702014-10-02 11:23:25 -0700152 for (std::list<Block>::const_reverse_iterator appMetaInfoItem = m_appMetaInfo.rbegin();
153 appMetaInfoItem != m_appMetaInfo.rend(); ++appMetaInfoItem) {
Alexander Afanasyev74633892015-02-08 18:08:46 -0800154 totalLength += encoder.prependBlock(*appMetaInfoItem);
Shock Jiangca7ea702014-10-02 11:23:25 -0700155 }
156
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700157 // FinalBlockId
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000158 if (m_finalBlockId) {
159 totalLength += prependNestedBlock(encoder, tlv::FinalBlockId, *m_finalBlockId);
Eric Newberryb555b002017-05-17 00:30:44 -0700160 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700161
162 // FreshnessPeriod
Eric Newberryb555b002017-05-17 00:30:44 -0700163 if (m_freshnessPeriod != DEFAULT_FRESHNESS_PERIOD) {
Davide Pesavento88a0d812017-08-19 21:31:42 -0400164 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::FreshnessPeriod, m_freshnessPeriod.count());
Eric Newberryb555b002017-05-17 00:30:44 -0700165 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700166
167 // ContentType
Eric Newberryb555b002017-05-17 00:30:44 -0700168 if (m_type != tlv::ContentType_Blob) {
169 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::ContentType, m_type);
170 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700171
Alexander Afanasyev74633892015-02-08 18:08:46 -0800172 totalLength += encoder.prependVarNumber(totalLength);
173 totalLength += encoder.prependVarNumber(tlv::MetaInfo);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700174 return totalLength;
175}
176
Davide Pesavento88a0d812017-08-19 21:31:42 -0400177NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700178
179const Block&
180MetaInfo::wireEncode() const
181{
182 if (m_wire.hasWire())
183 return m_wire;
184
185 EncodingEstimator estimator;
186 size_t estimatedSize = wireEncode(estimator);
187
188 EncodingBuffer buffer(estimatedSize, 0);
189 wireEncode(buffer);
190
191 m_wire = buffer.block();
192 return m_wire;
193}
194
195void
196MetaInfo::wireDecode(const Block& wire)
197{
198 m_wire = wire;
199 m_wire.parse();
200
201 // MetaInfo ::= META-INFO-TYPE TLV-LENGTH
202 // ContentType?
203 // FreshnessPeriod?
Shock Jiangca7ea702014-10-02 11:23:25 -0700204 // FinalBlockId?
205 // AppMetaInfo*
206
207
208 Block::element_const_iterator val = m_wire.elements_begin();
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700209
210 // ContentType
Shock Jiangca7ea702014-10-02 11:23:25 -0700211 if (val != m_wire.elements_end() && val->type() == tlv::ContentType) {
Junxiao Shi51742322017-08-13 17:04:52 +0000212 m_type = readNonNegativeIntegerAs<uint32_t>(*val);
Shock Jiangca7ea702014-10-02 11:23:25 -0700213 ++val;
214 }
215 else {
Junxiao Shia464b922014-11-12 21:13:06 -0700216 m_type = tlv::ContentType_Blob;
Shock Jiangca7ea702014-10-02 11:23:25 -0700217 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700218
219 // FreshnessPeriod
Shock Jiangca7ea702014-10-02 11:23:25 -0700220 if (val != m_wire.elements_end() && val->type() == tlv::FreshnessPeriod) {
221 m_freshnessPeriod = time::milliseconds(readNonNegativeInteger(*val));
222 ++val;
223 }
224 else {
Eric Newberryb555b002017-05-17 00:30:44 -0700225 m_freshnessPeriod = DEFAULT_FRESHNESS_PERIOD;
Shock Jiangca7ea702014-10-02 11:23:25 -0700226 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700227
228 // FinalBlockId
Shock Jiangca7ea702014-10-02 11:23:25 -0700229 if (val != m_wire.elements_end() && val->type() == tlv::FinalBlockId) {
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000230 m_finalBlockId.emplace(val->blockFromValue());
Shock Jiangca7ea702014-10-02 11:23:25 -0700231 ++val;
232 }
233 else {
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000234 m_finalBlockId = nullopt;
Shock Jiangca7ea702014-10-02 11:23:25 -0700235 }
236
237 // AppMetaInfo (if any)
238 for (; val != m_wire.elements().end(); ++val) {
239 m_appMetaInfo.push_back(*val);
240 }
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700241}
242
243std::ostream&
244operator<<(std::ostream& os, const MetaInfo& info)
245{
246 // ContentType
247 os << "ContentType: " << info.getType();
248
249 // FreshnessPeriod
Eric Newberryb555b002017-05-17 00:30:44 -0700250 if (info.getFreshnessPeriod() > time::milliseconds::zero()) {
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700251 os << ", FreshnessPeriod: " << info.getFreshnessPeriod();
252 }
253
Shock Jiangca7ea702014-10-02 11:23:25 -0700254 // FinalBlockId
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000255 if (info.getFinalBlock()) {
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700256 os << ", FinalBlockId: ";
Junxiao Shiebfe4a22018-04-01 23:53:40 +0000257 info.getFinalBlock()->toUri(os);
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700258 }
Shock Jiangca7ea702014-10-02 11:23:25 -0700259
260 // App-defined MetaInfo items
261 for (std::list<Block>::const_iterator iter = info.getAppMetaInfo().begin();
262 iter != info.getAppMetaInfo().end(); ++iter) {
263 os << ", AppMetaInfoTlvType: " << iter->type();
264 }
265
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700266 return os;
267}
268
269} // namespace ndn