blob: 8f55ca200ea3076ffbe4e5fa9553b20234530fbb [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi81206d52017-07-23 12:43:22 +00002/*
3 * Copyright (c) 2013-2017 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.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020 */
21
Jeff Thompson56ec9e22013-08-02 11:34:07 -070022#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -070023#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070024
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080025#include "meta-info.hpp"
Junxiao Shi81206d52017-07-23 12:43:22 +000026#include "name.hpp"
27#include "signature.hpp"
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080028#include "tag-host.hpp"
Junxiao Shi81206d52017-07-23 12:43:22 +000029#include "encoding/block.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070030
31namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070032
Junxiao Shi81206d52017-07-23 12:43:22 +000033/** @brief Represents a Data packet
Junxiao Shic2b8d242014-11-04 08:35:29 -070034 */
Alexander Afanasyeva3887ae2014-12-29 16:11:57 -080035class Data : public TagHost, public enable_shared_from_this<Data>
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080036{
Jeff Thompson5cae5e52013-07-10 19:41:20 -070037public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070038 class Error : public tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070039 {
40 public:
41 explicit
42 Error(const std::string& what)
Junxiao Shic2b8d242014-11-04 08:35:29 -070043 : tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070044 {
45 }
46 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070047
Junxiao Shi81206d52017-07-23 12:43:22 +000048 /** @brief Create a new Data with the given name and empty Content
49 * @warning In certain contexts that use Data::shared_from_this(), Data must be created
50 * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
Jeff Thompson20af0732013-09-12 17:01:45 -070051 */
Junxiao Shi81206d52017-07-23 12:43:22 +000052 Data(const Name& name = Name());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053
Junxiao Shi81206d52017-07-23 12:43:22 +000054 /** @brief Create from wire encoding
55 * @warning In certain contexts that use Data::shared_from_this(), Data must be created
56 * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior.
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080057 */
58 explicit
Alexander Afanasyev197e5652014-06-13 16:56:31 -070059 Data(const Block& wire);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
Junxiao Shi81206d52017-07-23 12:43:22 +000061 /** @brief Fast encoding or block size estimation
62 * @param encoder EncodingEstimator or EncodingBuffer instance
63 * @param wantUnsignedPortionOnly If true, only prepends Name, MetaInfo, Content, and
64 * SignatureInfo to @p encoder, but omit SignatureValue and outmost Type-Length of Data
65 * element. This is intended to be used with wireEncode(encoder, signatureValue).
66 * @throw Error SignatureBits are not provided and wantUnsignedPortionOnly is false.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070067 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080068 template<encoding::Tag TAG>
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070069 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070070 wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071
Junxiao Shi81206d52017-07-23 12:43:22 +000072 /** @brief Finalize Data packet encoding with the specified SignatureValue
73 * @param encoder EncodingBuffer containing Name, MetaInfo, Content, and SignatureInfo, but
74 * without SignatureValue or outmost Type-Length of Data element
75 * @param signatureValue SignatureValue element
Alexander Afanasyev197e5652014-06-13 16:56:31 -070076 *
Junxiao Shi81206d52017-07-23 12:43:22 +000077 * This method is intended to be used in concert with Data::wireEncode(encoder, true)
78 * @code
Alexander Afanasyev197e5652014-06-13 16:56:31 -070079 * Data data;
80 * ...
81 * EncodingBuffer encoder;
82 * data.wireEncode(encoder, true);
83 * ...
84 * Block signatureValue = <sign_over_unsigned_portion>(encoder.buf(), encoder.size());
85 * data.wireEncode(encoder, signatureValue)
Junxiao Shi81206d52017-07-23 12:43:22 +000086 * @endcode
Alexander Afanasyev197e5652014-06-13 16:56:31 -070087 */
88 const Block&
89 wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const;
90
Junxiao Shi81206d52017-07-23 12:43:22 +000091 /** @brief Encode to a wire format
92 */
93 const Block&
94 wireEncode() const;
95
96 /** @brief Decode from the wire format
Alexander Afanasyev809805d2014-02-17 17:20:33 -080097 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070098 void
99 wireDecode(const Block& wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800100
Junxiao Shi81206d52017-07-23 12:43:22 +0000101 /** @brief Check if already has wire
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800102 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700103 bool
Junxiao Shi81206d52017-07-23 12:43:22 +0000104 hasWire() const
105 {
106 return m_wire.hasWire();
107 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108
Junxiao Shi81206d52017-07-23 12:43:22 +0000109 /** @brief Get full name including implicit digest
110 * @pre hasWire() == true; i.e. wireEncode() must have been called
111 * @throw Error Data has no wire encoding
Alexander Afanasyev3b703102014-06-13 17:01:14 -0700112 */
113 const Name&
114 getFullName() const;
115
Junxiao Shi81206d52017-07-23 12:43:22 +0000116public: // Data fields
117 /** @brief Get name
118 */
119 const Name&
120 getName() const
121 {
122 return m_name;
123 }
124
125 /** @brief Set name
126 * @return a reference to this Data, to allow chaining
127 */
128 Data&
129 setName(const Name& name);
130
131 /** @brief Get MetaInfo
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700132 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700133 const MetaInfo&
Junxiao Shi81206d52017-07-23 12:43:22 +0000134 getMetaInfo() const
135 {
136 return m_metaInfo;
137 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700138
Junxiao Shi81206d52017-07-23 12:43:22 +0000139 /** @brief Set MetaInfo
140 * @return a reference to this Data, to allow chaining
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700141 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700142 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800143 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700144
Junxiao Shi81206d52017-07-23 12:43:22 +0000145 /** @brief Get Content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800146 *
Junxiao Shi81206d52017-07-23 12:43:22 +0000147 * The Content value is accessible through value()/value_size() or value_begin()/value_end()
148 * methods of the Block class.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800149 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700150 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800152
Junxiao Shi81206d52017-07-23 12:43:22 +0000153 /** @brief Set Content from a block
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700154 *
Junxiao Shi81206d52017-07-23 12:43:22 +0000155 * If block's TLV-TYPE is Content, it will be used directly as Data's Content element.
156 * If block's TLV-TYPE is not Content, it will be nested into a Content element.
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700157 *
Junxiao Shi81206d52017-07-23 12:43:22 +0000158 * @return a reference to this Data, to allow chaining
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700159 */
160 Data&
161 setContent(const Block& block);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800162
Junxiao Shi81206d52017-07-23 12:43:22 +0000163 /** @brief Copy Content value from raw buffer
164 * @param value pointer to the first octet of the value
165 * @param valueSize size of the raw buffer
166 * @return a reference to this Data, to allow chaining
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700167 */
168 Data&
Junxiao Shi81206d52017-07-23 12:43:22 +0000169 setContent(const uint8_t* value, size_t valueSize);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800170
Junxiao Shi81206d52017-07-23 12:43:22 +0000171 /** @brief Set Content from wire buffer
172 * @param value Content value, which does not need to be a TLV element
173 * @return a reference to this Data, to allow chaining
174 */
175 Data&
176 setContent(const ConstBufferPtr& value);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700177
Junxiao Shi81206d52017-07-23 12:43:22 +0000178 /** @brief Get Signature
179 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700180 const Signature&
Junxiao Shi81206d52017-07-23 12:43:22 +0000181 getSignature() const
182 {
183 return m_signature;
184 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700185
Junxiao Shi81206d52017-07-23 12:43:22 +0000186 /** @brief Set Signature
187 * @return a reference to this Data, to allow chaining
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800188 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700189 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800190 setSignature(const Signature& signature);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700191
Junxiao Shi81206d52017-07-23 12:43:22 +0000192 /** @brief Set SignatureValue
193 * @return a reference to this Data, to allow chaining
194 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700195 Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700196 setSignatureValue(const Block& value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800197
Junxiao Shi81206d52017-07-23 12:43:22 +0000198public: // MetaInfo fields
199 uint32_t
200 getContentType() const
201 {
202 return m_metaInfo.getType();
203 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700204
Junxiao Shi81206d52017-07-23 12:43:22 +0000205 Data&
206 setContentType(uint32_t type);
207
208 const time::milliseconds&
209 getFreshnessPeriod() const
210 {
211 return m_metaInfo.getFreshnessPeriod();
212 }
213
214 Data&
215 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
216
217 const name::Component&
218 getFinalBlockId() const
219 {
220 return m_metaInfo.getFinalBlockId();
221 }
222
223 Data&
224 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700225
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700226protected:
Junxiao Shi81206d52017-07-23 12:43:22 +0000227 /** @brief Clear wire encoding and cached FullName
228 * @note This does not clear the SignatureValue.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700229 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700230 void
Junxiao Shi81206d52017-07-23 12:43:22 +0000231 resetWire();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800232
233private:
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800234 Name m_name;
235 MetaInfo m_metaInfo;
Junxiao Shi81206d52017-07-23 12:43:22 +0000236 Block m_content;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800237 Signature m_signature;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800238
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800239 mutable Block m_wire;
Junxiao Shi81206d52017-07-23 12:43:22 +0000240 mutable Name m_fullName; ///< cached FullName computed from m_wire
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700241};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800242
Alexander Afanasyeva0c5f832014-06-19 13:27:56 -0700243std::ostream&
244operator<<(std::ostream& os, const Data& data);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800245
Junxiao Shi81206d52017-07-23 12:43:22 +0000246bool
247operator==(const Data& lhs, const Data& rhs);
248
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800249inline bool
Junxiao Shi81206d52017-07-23 12:43:22 +0000250operator!=(const Data& lhs, const Data& rhs)
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800251{
Junxiao Shi81206d52017-07-23 12:43:22 +0000252 return !(lhs == rhs);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800253}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700254
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800255} // namespace ndn
256
Junxiao Shi81206d52017-07-23 12:43:22 +0000257#endif // NDN_DATA_HPP