blob: b82533a7e29a4d78fb9566a0de185c4a03860d46 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
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.
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
Jeff Thompson46bd45f2013-08-08 16:46:41 -070025#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070026#include "name.hpp"
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080027#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070028
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080029#include "signature.hpp"
30#include "meta-info.hpp"
31#include "key-locator.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080032#include "management/nfd-local-control-header.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070033
34namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080036class Data : public enable_shared_from_this<Data>
37{
Jeff Thompson5cae5e52013-07-10 19:41:20 -070038public:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070039 class Error : public std::runtime_error
40 {
41 public:
42 explicit
43 Error(const std::string& what)
44 : std::runtime_error(what)
45 {
46 }
47 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
Jeff Thompson20af0732013-09-12 17:01:45 -070049 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080050 * @brief Create an empty Data object
Alexander Afanasyev770827c2014-05-13 17:42:55 -070051 *
52 * Note that in certain contexts that use Data::shared_from_this(), Data must be
53 * created using `make_shared`:
54 *
55 * shared_ptr<Data> data = make_shared<Data>();
56 *
57 * Otherwise, Data::shared_from_this() will throw an exception.
Jeff Thompson20af0732013-09-12 17:01:45 -070058 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
Jeff Thompson20af0732013-09-12 17:01:45 -070061 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080062 * @brief Create a new Data object with the given name
Alexander Afanasyev770827c2014-05-13 17:42:55 -070063 *
64 * @param name A reference to the name
65 *
66 * Note that in certain contexts that use Data::shared_from_this(), Data must be
67 * created using `make_shared`:
68 *
69 * shared_ptr<Data> data = make_shared<Data>(name);
70 *
71 * Otherwise, Data::shared_from_this() will throw an exception.
Jeff Thompson20af0732013-09-12 17:01:45 -070072 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080073 Data(const Name& name);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080074
75 /**
76 * @brief Create a new Data object from wire encoding
Alexander Afanasyev770827c2014-05-13 17:42:55 -070077 *
78 * Note that in certain contexts that use Data::shared_from_this(), Data must be
79 * created using `make_shared`:
80 *
81 * shared_ptr<Data> data = make_shared<Data>(wire);
82 *
83 * Otherwise, Data::shared_from_this() will throw an exception.
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080084 */
85 explicit
86 Data(const Block& wire)
87 {
88 wireDecode(wire);
89 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070090
Jeff Thompson25bfdca2013-10-16 17:05:41 -070091 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080092 * @brief Fast encoding or block size estimation
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070093 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080094 template<bool T>
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070095 size_t
96 wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080098 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080099 * @brief Encode to a wire format
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800100 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700101 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800102 wireEncode() const;
103
104 /**
105 * @brief Decode from the wire format
106 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700107 void
108 wireDecode(const Block& wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800109
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800110 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700111 * @brief Check if Data is already has wire encoding
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800112 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700113 bool
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800114 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115
116 ////////////////////////////////////////////////////////////////////
117
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700118 /**
119 * @brief Get name of the Data packet
120 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700121 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800122 getName() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700124 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700125 * @brief Set name to a copy of the given Name
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800126 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700127 * @return This Data so that you can chain calls to update values
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700128 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700129 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800130 setName(const Name& name);
131
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800132 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700133
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700134 /**
135 * @brief Get MetaInfo block from Data packet
136 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700137 const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800138 getMetaInfo() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700140 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700141 * @brief Set metaInfo to a copy of the given MetaInfo
142 *
Jeff Thompson6d591972013-10-17 11:16:32 -0700143 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700144 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700145 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800146 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700147
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800148 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700149
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800150 ///////////////////////////////////////////////////////////////
151 ///////////////////////////////////////////////////////////////
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800152 ///////////////////////////////////////////////////////////////
153 // MetaInfo proxy methods
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800154
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700155 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800156 getContentType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700157
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700158 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800159 setContentType(uint32_t type);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800160
161 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700162
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700163 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800164 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700165
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700166 Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700167 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800168
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800169 //
170
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700171 const name::Component&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800172 getFinalBlockId() const;
173
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700174 Data&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800175 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800176
177 //
178 ///////////////////////////////////////////////////////////////
179 ///////////////////////////////////////////////////////////////
180 ///////////////////////////////////////////////////////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700181
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800182 /**
183 * @brief Get content Block
184 *
185 * To access content value, one can use value()/value_size() or
186 * value_begin()/value_end() methods of the Block class
187 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700188 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800189 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800190
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700191 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700192 * @brief Set the content from the buffer (buffer will be copied)
193 *
194 * @param buffer Pointer to first byte of the buffer
195 * @param bufferSize Size of the buffer
196 *
Jeff Thompson6d591972013-10-17 11:16:32 -0700197 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700198 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700199 Data&
200 setContent(const uint8_t* buffer, size_t bufferSize);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800201
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700202 /**
203 * @brief Set the content from the block
204 *
205 * Depending on type of the supplied block, there are two cases:
206 *
207 * - if block.type() == Tlv::Content, then block will be used directly as Data packet's
208 * content (no extra copying)
209 *
210 * - if block.type() != Tlv::Content, then this method will create a new Block with type
211 * Tlv::Content and put block as a nested element in the content Block.
212 *
213 * @param block The Block containing the content to assign
214 *
215 * @return This Data so that you can chain calls to update values.
216 */
217 Data&
218 setContent(const Block& block);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800219
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700220 /**
221 * @brief Set the content from the pointer to immutable buffer
222 *
223 * This method will create a Block with Tlv::Content and set contentValue as a payload
224 * for this block. Note that this method is very different from setContent(const
225 * Block&), since it does not require that payload should be a valid TLV element.
226 *
227 * @param contentValue The pointer to immutable buffer containing the content to assign
228 *
229 * @return This Data so that you can chain calls to update values.
230 */
231 Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700232 setContent(const ConstBufferPtr& contentValue);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800233
234 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700235
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700236 const Signature&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800237 getSignature() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700238
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800239 /**
240 * @brief Set the signature to a copy of the given signature.
241 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800242 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700243 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800244 setSignature(const Signature& signature);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700245
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700246 Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700247 setSignatureValue(const Block& value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800248
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800249 ///////////////////////////////////////////////////////////////
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800250
251 nfd::LocalControlHeader&
252 getLocalControlHeader();
253
254 const nfd::LocalControlHeader&
255 getLocalControlHeader() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700256
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700257 uint64_t
Yingdi Yua4e57672014-02-06 11:16:17 -0800258 getIncomingFaceId() const;
259
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700260 Data&
Yingdi Yua4e57672014-02-06 11:16:17 -0800261 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700262
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700263public: // EqualityComparable concept
264 bool
265 operator==(const Data& other) const;
266
267 bool
268 operator!=(const Data& other) const;
269
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700270private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700271 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800272 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700273 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700274 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700275 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800276
277private:
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800278 Name m_name;
279 MetaInfo m_metaInfo;
280 mutable Block m_content;
281 Signature m_signature;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800282
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800283 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800284
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800285 nfd::LocalControlHeader m_localControlHeader;
286 friend class nfd::LocalControlHeader;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700287};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800288
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800289inline
290Data::Data()
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800291 : m_content(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800292{
293}
294
295inline
296Data::Data(const Name& name)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800297 : m_name(name)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800298{
299}
300
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800301template<bool T>
302inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700303Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800304{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700305 size_t totalLength = 0;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800306
307 // Data ::= DATA-TLV TLV-LENGTH
308 // Name
309 // MetaInfo
310 // Content
311 // Signature
312
313 // (reverse encoding)
314
315 if (!unsignedPortion && !m_signature)
316 {
317 throw Error("Requested wire format, but data packet has not been signed yet");
318 }
319
320 if (!unsignedPortion)
321 {
322 // SignatureValue
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700323 totalLength += prependBlock(block, m_signature.getValue());
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800324 }
325
326 // SignatureInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700327 totalLength += prependBlock(block, m_signature.getInfo());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700328
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800329 // Content
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700330 totalLength += prependBlock(block, getContent());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700331
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800332 // MetaInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700333 totalLength += getMetaInfo().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800334
335 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700336 totalLength += getName().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800337
338 if (!unsignedPortion)
339 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700340 totalLength += block.prependVarNumber (totalLength);
341 totalLength += block.prependVarNumber (Tlv::Data);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800342 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700343 return totalLength;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800344}
345
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700346inline const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800347Data::wireEncode() const
348{
349 if (m_wire.hasWire())
350 return m_wire;
351
352 EncodingEstimator estimator;
353 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700354
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800355 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800356 wireEncode(buffer);
357
358 const_cast<Data*>(this)->wireDecode(buffer.block());
359 return m_wire;
360}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700361
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700362inline void
363Data::wireDecode(const Block& wire)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800364{
365 m_wire = wire;
366 m_wire.parse();
367
368 // Data ::= DATA-TLV TLV-LENGTH
369 // Name
370 // MetaInfo
371 // Content
372 // Signature
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700373
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800374 // Name
375 m_name.wireDecode(m_wire.get(Tlv::Name));
376
377 // MetaInfo
378 m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo));
379
380 // Content
381 m_content = m_wire.get(Tlv::Content);
382
383 ///////////////
384 // Signature //
385 ///////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700386
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800387 // SignatureInfo
388 m_signature.setInfo(m_wire.get(Tlv::SignatureInfo));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700389
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800390 // SignatureValue
391 Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue);
392 if (val != m_wire.elements_end())
393 m_signature.setValue(*val);
394}
395
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800396inline bool
397Data::hasWire() const
398{
399 return m_wire.hasWire();
400}
401
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700402inline const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800403Data::getName() const
404{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800405 return m_name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800406}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700407
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700408inline Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800409Data::setName(const Name& name)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700410{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800411 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700412 m_name = name;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700413
414 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800415}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700416
417inline const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800418Data::getMetaInfo() const
419{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800420 return m_metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800421}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700422
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700423inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700424Data::setMetaInfo(const MetaInfo& metaInfo)
425{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800426 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700427 m_metaInfo = metaInfo;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700428
429 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800430}
431
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700432inline uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800433Data::getContentType() const
434{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800435 return m_metaInfo.getType();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800436}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700437
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700438inline Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800439Data::setContentType(uint32_t type)
440{
441 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800442 m_metaInfo.setType(type);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700443
444 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800445}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700446
447inline const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800448Data::getFreshnessPeriod() const
449{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800450 return m_metaInfo.getFreshnessPeriod();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800451}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700452
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700453inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700454Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800455{
456 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800457 m_metaInfo.setFreshnessPeriod(freshnessPeriod);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700458
459 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800460}
461
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800462inline const name::Component&
463Data::getFinalBlockId() const
464{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800465 return m_metaInfo.getFinalBlockId();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800466}
467
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700468inline Data&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800469Data::setFinalBlockId(const name::Component& finalBlockId)
470{
471 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800472 m_metaInfo.setFinalBlockId(finalBlockId);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700473
474 return *this;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800475}
476
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700477inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800478Data::getContent() const
479{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800480 if (m_content.empty())
481 m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800482
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800483 if (!m_content.hasWire())
484 m_content.encode();
485 return m_content;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800486}
487
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700488inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700489Data::setContent(const uint8_t* content, size_t contentLength)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800490{
491 onChanged();
492
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800493 m_content = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700494
495 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800496}
497
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700498inline Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700499Data::setContent(const ConstBufferPtr& contentValue)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800500{
501 onChanged();
502
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800503 m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700504
505 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800506}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700507
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700508inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700509Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800510{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800511 onChanged();
512
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800513 if (content.type() == Tlv::Content)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800514 m_content = content;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800515 else {
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800516 m_content = Block(Tlv::Content, content);
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800517 }
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700518
519 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800520}
521
522inline const Signature&
523Data::getSignature() const
524{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800525 return m_signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800526}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700527
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700528inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700529Data::setSignature(const Signature& signature)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800530{
531 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800532 m_signature = signature;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700533
534 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800535}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800536
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700537inline Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700538Data::setSignatureValue(const Block& value)
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800539{
540 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800541 m_signature.setValue(value);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700542
543 return *this;
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800544}
545
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800546//
547
548inline nfd::LocalControlHeader&
549Data::getLocalControlHeader()
550{
551 return m_localControlHeader;
552}
553
554inline const nfd::LocalControlHeader&
555Data::getLocalControlHeader() const
556{
557 return m_localControlHeader;
558}
559
Yingdi Yua4e57672014-02-06 11:16:17 -0800560inline uint64_t
561Data::getIncomingFaceId() const
562{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800563 return getLocalControlHeader().getIncomingFaceId();
Yingdi Yua4e57672014-02-06 11:16:17 -0800564}
565
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700566inline Data&
Yingdi Yua4e57672014-02-06 11:16:17 -0800567Data::setIncomingFaceId(uint64_t incomingFaceId)
568{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800569 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800570 // ! do not reset Data's wire !
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700571
572 return *this;
Yingdi Yua4e57672014-02-06 11:16:17 -0800573}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800574
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700575inline void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800576Data::onChanged()
577{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800578 // The values have changed, so the wire format is invalidated
579
580 // !!!Note!!! Signature is not invalidated and it is responsibility of
581 // the application to do proper re-signing if necessary
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700582
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800583 m_wire.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700584}
585
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700586inline bool
587Data::operator==(const Data& other) const
588{
589 return getName() == other.getName() &&
590 getMetaInfo() == other.getMetaInfo() &&
591 getContent() == other.getContent() &&
592 getSignature() == other.getSignature();
593}
594
595inline bool
596Data::operator!=(const Data& other) const
597{
598 return !(*this == other);
599}
600
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800601inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700602operator<<(std::ostream& os, const Data& data)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800603{
604 os << "Name: " << data.getName() << "\n";
605 os << "MetaInfo: " << data.getMetaInfo() << "\n";
606 os << "Content: (size: " << data.getContent().value_size() << ")\n";
607 os << "Signature: (type: " << data.getSignature().getType() <<
608 ", value_length: "<< data.getSignature().getValue().value_size() << ")";
609 os << std::endl;
610
611 return os;
612}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800613
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800614} // namespace ndn
615
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700616#endif