blob: aece25643fe7e4e0fd7aecd650f4fc8884dcdcb9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080020 */
21
22#ifndef NDN_META_INFO_HPP
23#define NDN_META_INFO_HPP
24
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025#include "common.hpp"
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070026#include "encoding/block.hpp"
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080027#include "encoding/encoding-buffer.hpp"
Alexander Afanasyev15f67312014-07-22 15:11:09 -070028#include "util/time.hpp"
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070029#include "name-component.hpp"
Shock Jiangca7ea702014-10-02 11:23:25 -070030#include <list>
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080031
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032namespace ndn {
33
34/**
35 * An MetaInfo holds the meta info which is signed inside the data packet.
Shock Jiangca7ea702014-10-02 11:23:25 -070036 *
37 * The class allows experimentation with application-defined meta information blocks,
38 * which slightly violates NDN-TLV specification. When using the application-defined
39 * meta information blocks be aware that this may result in packet drop (NFD and
40 * previous versions of ndn-cxx will gracefully accept such packet).
41 *
42 * The following definition of MetaInfo block is assumed in this implementation (compared
43 * to the NDN-TLV spec, definition extended to allow optional AppMetaInfo TLV blocks):
44 *
45 * MetaInfo ::= META-INFO-TYPE TLV-LENGTH
46 * ContentType?
47 * FreshnessPeriod?
48 * FinalBlockId?
49 * AppMetaInfo*
50 *
51 * AppMetaInfo ::= any TLV block with type in the restricted application range [128, 252]
52 *
53 * Note that AppMetaInfo blocks are application-defined and must have TLV type from
54 * the restricted application range [128, 252].
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070056class MetaInfo
57{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080058public:
Shock Jiangca7ea702014-10-02 11:23:25 -070059 class Error : public tlv::Error
60 {
61 public:
62 explicit
63 Error(const std::string& what)
64 : tlv::Error(what)
65 {
66 }
67 };
68
Junxiao Shicad32e32014-11-03 20:32:36 -070069 /** \brief ContentType codes
70 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080071 enum {
Junxiao Shicad32e32014-11-03 20:32:36 -070072 /** @brief indicates content is the actual data bits
73 */
74 TYPE_BLOB = 0,
75
76 /** @brief indicates content is another name which identifies actual data content
77 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080078 TYPE_LINK = 1,
Junxiao Shicad32e32014-11-03 20:32:36 -070079
80 /** @brief indicates content is a public key
81 */
82 TYPE_KEY = 2,
83
84 /** @brief indicates a producer generated NACK
85 * @warning Experimental. Not defined in NDN-TLV spec.
86 */
87 TYPE_NACK = 3,
88
89 TYPE_DEFAULT = TYPE_BLOB
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080090 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070092 MetaInfo();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080093
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070094 /**
95 * @brief Create from wire encoding
96 */
Alexander Afanasyev01065fb2014-10-02 13:01:46 -070097 explicit
98 MetaInfo(const Block& block);
Alexander Afanasyevc348f832014-02-17 16:35:17 -080099
100 template<bool T>
101 size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700102 wireEncode(EncodingImpl<T>& block) const;
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800103
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700104 const Block&
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800105 wireEncode() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800107 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700108 wireDecode(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700109
Alexander Afanasyevc348f832014-02-17 16:35:17 -0800110 ///////////////////////////////////////////////////////////////////////////////
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800111 // Getters/setters
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112
113 uint32_t
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700114 getType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800116 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700117 setType(uint32_t type);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118
119 const time::milliseconds&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700120 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800122 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700123 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800124
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800125 const name::Component&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700126 getFinalBlockId() const;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800127
128 MetaInfo&
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700129 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130
Shock Jiangca7ea702014-10-02 11:23:25 -0700131 /**
132 * @brief Get all app-defined MetaInfo items
133 *
134 * @note Warning: Experimental API, which may change or disappear in the future
135 *
136 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
137 * is called before *AppMetaInfo, all app-defined blocks will be lost
138 */
139 const std::list<Block>&
140 getAppMetaInfo() const;
141
142 /**
143 * @brief Set app-defined MetaInfo items
144 *
145 * This method will replace all existing app-defined MetaInfo items, if they existed.
146 *
147 * @throw Error if some block in @p info has type not in the application range
148 * (http://named-data.net/doc/ndn-tlv/types.html)
149 *
150 * @note Warning: Experimental API, which may change or disappear in the future
151 *
152 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
153 * is called before *AppMetaInfo, all app-defined blocks will be lost
154 */
155 MetaInfo&
156 setAppMetaInfo(const std::list<Block>& info);
157
158 /**
159 * @brief Add an app-defined MetaInfo item
160 *
161 * @throw Error if @p block has type not in the application range
162 * (http://named-data.net/doc/ndn-tlv/types.html)
163 *
164 * @note Warning: Experimental API, which may change or disappear in the future
165 *
166 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
167 * is called before *AppMetaInfo, all app-defined blocks will be lost
168 */
169 MetaInfo&
170 addAppMetaInfo(const Block& block);
171
172 /**
173 * @brief Remove a first app-defined MetaInfo item with type @p tlvType
174 *
175 * @return true if an item was deleted
176 *
177 * @note Warning: Experimental API, which may change or disappear in the future
178 *
179 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
180 * is called before *AppMetaInfo, all app-defined blocks will be lost
181 */
182 bool
183 removeAppMetaInfo(uint32_t tlvType);
184
185 /**
186 * @brief Find a first app-defined MetaInfo item of type @p tlvType
187 *
188 * @return NULL if an item is not found, otherwise const pointer to the item
189 *
190 * @throw Error if @p tlvType is not in the application range
191 * (http://named-data.net/doc/ndn-tlv/types.html)
192 *
193 * @note Warning: Experimental API, which may change or disappear in the future
194 *
195 * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId
196 * is called before *AppMetaInfo, all app-defined blocks will be lost
197 */
198 const Block*
199 findAppMetaInfo(uint32_t tlvType) const;
200
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700201public: // EqualityComparable concept
202 bool
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700203 operator==(const MetaInfo& other) const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700204
205 bool
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700206 operator!=(const MetaInfo& other) const;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700207
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800208private:
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800209 uint32_t m_type;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700210 time::milliseconds m_freshnessPeriod;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800211 name::Component m_finalBlockId;
Shock Jiangca7ea702014-10-02 11:23:25 -0700212 std::list<Block> m_appMetaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800213
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800214 mutable Block m_wire;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800215};
216
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700217std::ostream&
218operator<<(std::ostream& os, const MetaInfo& info);
219
220/////////////////////////////////////////////////////////////////////////
221
222inline uint32_t
223MetaInfo::getType() const
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800224{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700225 return m_type;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800226}
227
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700228inline const time::milliseconds&
229MetaInfo::getFreshnessPeriod() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800230{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700231 return m_freshnessPeriod;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800232}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700233
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700234inline const name::Component&
235MetaInfo::getFinalBlockId() const
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800236{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700237 return m_finalBlockId;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800238}
239
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700240inline bool
241MetaInfo::operator==(const MetaInfo& other) const
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800242{
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700243 return wireEncode() == other.wireEncode();
244}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800245
Alexander Afanasyev01065fb2014-10-02 13:01:46 -0700246inline bool
247MetaInfo::operator!=(const MetaInfo& other) const
248{
249 return !(*this == other);
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800250}
251
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800252} // namespace ndn
253
254#endif // NDN_META_INFO_HPP