blob: 66b28490d8ad75444a8c5a118cac4bfa31de783d [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070011 */
12
Jeff Thompson56ec9e22013-08-02 11:34:07 -070013#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -070014#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070015
Jeff Thompson46bd45f2013-08-08 16:46:41 -070016#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070017#include "name.hpp"
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080018#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070019
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080020#include "signature.hpp"
21#include "meta-info.hpp"
22#include "key-locator.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080023#include "management/nfd-local-control-header.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070024
25namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070026
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080027class Data : public enable_shared_from_this<Data>
28{
Jeff Thompson5cae5e52013-07-10 19:41:20 -070029public:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070030 class Error : public std::runtime_error
31 {
32 public:
33 explicit
34 Error(const std::string& what)
35 : std::runtime_error(what)
36 {
37 }
38 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039
Jeff Thompson20af0732013-09-12 17:01:45 -070040 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080041 * @brief Create an empty Data object
Alexander Afanasyev770827c2014-05-13 17:42:55 -070042 *
43 * Note that in certain contexts that use Data::shared_from_this(), Data must be
44 * created using `make_shared`:
45 *
46 * shared_ptr<Data> data = make_shared<Data>();
47 *
48 * Otherwise, Data::shared_from_this() will throw an exception.
Jeff Thompson20af0732013-09-12 17:01:45 -070049 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080050 Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051
Jeff Thompson20af0732013-09-12 17:01:45 -070052 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053 * @brief Create a new Data object with the given name
Alexander Afanasyev770827c2014-05-13 17:42:55 -070054 *
55 * @param name A reference to the name
56 *
57 * Note that in certain contexts that use Data::shared_from_this(), Data must be
58 * created using `make_shared`:
59 *
60 * shared_ptr<Data> data = make_shared<Data>(name);
61 *
62 * Otherwise, Data::shared_from_this() will throw an exception.
Jeff Thompson20af0732013-09-12 17:01:45 -070063 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080064 Data(const Name& name);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080065
66 /**
67 * @brief Create a new Data object from wire encoding
Alexander Afanasyev770827c2014-05-13 17:42:55 -070068 *
69 * Note that in certain contexts that use Data::shared_from_this(), Data must be
70 * created using `make_shared`:
71 *
72 * shared_ptr<Data> data = make_shared<Data>(wire);
73 *
74 * Otherwise, Data::shared_from_this() will throw an exception.
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080075 */
76 explicit
77 Data(const Block& wire)
78 {
79 wireDecode(wire);
80 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081
Jeff Thompson25bfdca2013-10-16 17:05:41 -070082 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080083 * @brief Fast encoding or block size estimation
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070084 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080085 template<bool T>
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070086 size_t
87 wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080089 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080090 * @brief Encode to a wire format
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080091 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070092 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -080093 wireEncode() const;
94
95 /**
96 * @brief Decode from the wire format
97 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070098 void
99 wireDecode(const Block& wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800100
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800101 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700102 * @brief Check if Data is already has wire encoding
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800103 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700104 bool
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800105 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700106
107 ////////////////////////////////////////////////////////////////////
108
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700109 /**
110 * @brief Get name of the Data packet
111 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700112 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800113 getName() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700115 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700116 * @brief Set name to a copy of the given Name
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800117 *
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700118 * @return This Data so that you can chain calls to update values
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700119 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700120 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800121 setName(const Name& name);
122
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800123 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700124
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700125 /**
126 * @brief Get MetaInfo block from Data packet
127 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700128 const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800129 getMetaInfo() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700131 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700132 * @brief Set metaInfo to a copy of the given MetaInfo
133 *
Jeff Thompson6d591972013-10-17 11:16:32 -0700134 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700135 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700136 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800137 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700138
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800139 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700140
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800141 ///////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800143 ///////////////////////////////////////////////////////////////
144 // MetaInfo proxy methods
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800145
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700146 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800147 getContentType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700149 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800150 setContentType(uint32_t type);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800151
152 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700154 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800155 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700156
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700157 Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700158 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800159
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800160 //
161
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700162 const name::Component&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800163 getFinalBlockId() const;
164
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700165 Data&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800166 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800167
168 //
169 ///////////////////////////////////////////////////////////////
170 ///////////////////////////////////////////////////////////////
171 ///////////////////////////////////////////////////////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700172
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800173 /**
174 * @brief Get content Block
175 *
176 * To access content value, one can use value()/value_size() or
177 * value_begin()/value_end() methods of the Block class
178 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700179 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800180 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800181
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700182 /**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700183 * @brief Set the content from the buffer (buffer will be copied)
184 *
185 * @param buffer Pointer to first byte of the buffer
186 * @param bufferSize Size of the buffer
187 *
Jeff Thompson6d591972013-10-17 11:16:32 -0700188 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700189 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700190 Data&
191 setContent(const uint8_t* buffer, size_t bufferSize);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800192
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700193 /**
194 * @brief Set the content from the block
195 *
196 * Depending on type of the supplied block, there are two cases:
197 *
198 * - if block.type() == Tlv::Content, then block will be used directly as Data packet's
199 * content (no extra copying)
200 *
201 * - if block.type() != Tlv::Content, then this method will create a new Block with type
202 * Tlv::Content and put block as a nested element in the content Block.
203 *
204 * @param block The Block containing the content to assign
205 *
206 * @return This Data so that you can chain calls to update values.
207 */
208 Data&
209 setContent(const Block& block);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800210
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700211 /**
212 * @brief Set the content from the pointer to immutable buffer
213 *
214 * This method will create a Block with Tlv::Content and set contentValue as a payload
215 * for this block. Note that this method is very different from setContent(const
216 * Block&), since it does not require that payload should be a valid TLV element.
217 *
218 * @param contentValue The pointer to immutable buffer containing the content to assign
219 *
220 * @return This Data so that you can chain calls to update values.
221 */
222 Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700223 setContent(const ConstBufferPtr& contentValue);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800224
225 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700226
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700227 const Signature&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800228 getSignature() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700229
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800230 /**
231 * @brief Set the signature to a copy of the given signature.
232 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800233 */
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700234 Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800235 setSignature(const Signature& signature);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700236
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700237 Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700238 setSignatureValue(const Block& value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800239
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800240 ///////////////////////////////////////////////////////////////
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800241
242 nfd::LocalControlHeader&
243 getLocalControlHeader();
244
245 const nfd::LocalControlHeader&
246 getLocalControlHeader() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700247
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700248 uint64_t
Yingdi Yua4e57672014-02-06 11:16:17 -0800249 getIncomingFaceId() const;
250
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700251 Data&
Yingdi Yua4e57672014-02-06 11:16:17 -0800252 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700253
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700254public: // EqualityComparable concept
255 bool
256 operator==(const Data& other) const;
257
258 bool
259 operator!=(const Data& other) const;
260
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700261private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700262 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800263 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700264 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700265 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700266 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800267
268private:
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800269 Name m_name;
270 MetaInfo m_metaInfo;
271 mutable Block m_content;
272 Signature m_signature;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800273
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800274 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800275
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800276 nfd::LocalControlHeader m_localControlHeader;
277 friend class nfd::LocalControlHeader;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700278};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800279
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800280inline
281Data::Data()
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800282 : m_content(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800283{
284}
285
286inline
287Data::Data(const Name& name)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800288 : m_name(name)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800289{
290}
291
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800292template<bool T>
293inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700294Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800295{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700296 size_t totalLength = 0;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800297
298 // Data ::= DATA-TLV TLV-LENGTH
299 // Name
300 // MetaInfo
301 // Content
302 // Signature
303
304 // (reverse encoding)
305
306 if (!unsignedPortion && !m_signature)
307 {
308 throw Error("Requested wire format, but data packet has not been signed yet");
309 }
310
311 if (!unsignedPortion)
312 {
313 // SignatureValue
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700314 totalLength += prependBlock(block, m_signature.getValue());
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800315 }
316
317 // SignatureInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700318 totalLength += prependBlock(block, m_signature.getInfo());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700319
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800320 // Content
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700321 totalLength += prependBlock(block, getContent());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700322
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800323 // MetaInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700324 totalLength += getMetaInfo().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800325
326 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700327 totalLength += getName().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800328
329 if (!unsignedPortion)
330 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700331 totalLength += block.prependVarNumber (totalLength);
332 totalLength += block.prependVarNumber (Tlv::Data);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800333 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700334 return totalLength;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800335}
336
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700337inline const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800338Data::wireEncode() const
339{
340 if (m_wire.hasWire())
341 return m_wire;
342
343 EncodingEstimator estimator;
344 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700345
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800346 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800347 wireEncode(buffer);
348
349 const_cast<Data*>(this)->wireDecode(buffer.block());
350 return m_wire;
351}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700352
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700353inline void
354Data::wireDecode(const Block& wire)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800355{
356 m_wire = wire;
357 m_wire.parse();
358
359 // Data ::= DATA-TLV TLV-LENGTH
360 // Name
361 // MetaInfo
362 // Content
363 // Signature
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700364
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800365 // Name
366 m_name.wireDecode(m_wire.get(Tlv::Name));
367
368 // MetaInfo
369 m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo));
370
371 // Content
372 m_content = m_wire.get(Tlv::Content);
373
374 ///////////////
375 // Signature //
376 ///////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700377
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800378 // SignatureInfo
379 m_signature.setInfo(m_wire.get(Tlv::SignatureInfo));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700380
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800381 // SignatureValue
382 Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue);
383 if (val != m_wire.elements_end())
384 m_signature.setValue(*val);
385}
386
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800387inline bool
388Data::hasWire() const
389{
390 return m_wire.hasWire();
391}
392
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700393inline const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800394Data::getName() const
395{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800396 return m_name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800397}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700398
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700399inline Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800400Data::setName(const Name& name)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700401{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800402 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700403 m_name = name;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700404
405 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800406}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700407
408inline const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800409Data::getMetaInfo() const
410{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800411 return m_metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800412}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700413
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700414inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700415Data::setMetaInfo(const MetaInfo& metaInfo)
416{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800417 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700418 m_metaInfo = metaInfo;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700419
420 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800421}
422
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700423inline uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800424Data::getContentType() const
425{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800426 return m_metaInfo.getType();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800427}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700428
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700429inline Data&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800430Data::setContentType(uint32_t type)
431{
432 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800433 m_metaInfo.setType(type);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700434
435 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800436}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700437
438inline const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800439Data::getFreshnessPeriod() const
440{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800441 return m_metaInfo.getFreshnessPeriod();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800442}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700443
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700444inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700445Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800446{
447 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800448 m_metaInfo.setFreshnessPeriod(freshnessPeriod);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700449
450 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800451}
452
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800453inline const name::Component&
454Data::getFinalBlockId() const
455{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800456 return m_metaInfo.getFinalBlockId();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800457}
458
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700459inline Data&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800460Data::setFinalBlockId(const name::Component& finalBlockId)
461{
462 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800463 m_metaInfo.setFinalBlockId(finalBlockId);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700464
465 return *this;
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800466}
467
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700468inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800469Data::getContent() const
470{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800471 if (m_content.empty())
472 m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800473
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800474 if (!m_content.hasWire())
475 m_content.encode();
476 return m_content;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800477}
478
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700479inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700480Data::setContent(const uint8_t* content, size_t contentLength)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800481{
482 onChanged();
483
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800484 m_content = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700485
486 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800487}
488
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700489inline Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700490Data::setContent(const ConstBufferPtr& contentValue)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800491{
492 onChanged();
493
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800494 m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700495
496 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800497}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700498
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700499inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700500Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800501{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800502 onChanged();
503
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800504 if (content.type() == Tlv::Content)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800505 m_content = content;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800506 else {
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800507 m_content = Block(Tlv::Content, content);
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800508 }
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700509
510 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800511}
512
513inline const Signature&
514Data::getSignature() const
515{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800516 return m_signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800517}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700518
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700519inline Data&
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700520Data::setSignature(const Signature& signature)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800521{
522 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800523 m_signature = signature;
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700524
525 return *this;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800526}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800527
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700528inline Data&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700529Data::setSignatureValue(const Block& value)
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800530{
531 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800532 m_signature.setValue(value);
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700533
534 return *this;
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800535}
536
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800537//
538
539inline nfd::LocalControlHeader&
540Data::getLocalControlHeader()
541{
542 return m_localControlHeader;
543}
544
545inline const nfd::LocalControlHeader&
546Data::getLocalControlHeader() const
547{
548 return m_localControlHeader;
549}
550
Yingdi Yua4e57672014-02-06 11:16:17 -0800551inline uint64_t
552Data::getIncomingFaceId() const
553{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800554 return getLocalControlHeader().getIncomingFaceId();
Yingdi Yua4e57672014-02-06 11:16:17 -0800555}
556
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700557inline Data&
Yingdi Yua4e57672014-02-06 11:16:17 -0800558Data::setIncomingFaceId(uint64_t incomingFaceId)
559{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800560 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800561 // ! do not reset Data's wire !
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700562
563 return *this;
Yingdi Yua4e57672014-02-06 11:16:17 -0800564}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800565
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700566inline void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800567Data::onChanged()
568{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800569 // The values have changed, so the wire format is invalidated
570
571 // !!!Note!!! Signature is not invalidated and it is responsibility of
572 // the application to do proper re-signing if necessary
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700573
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800574 m_wire.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700575}
576
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700577inline bool
578Data::operator==(const Data& other) const
579{
580 return getName() == other.getName() &&
581 getMetaInfo() == other.getMetaInfo() &&
582 getContent() == other.getContent() &&
583 getSignature() == other.getSignature();
584}
585
586inline bool
587Data::operator!=(const Data& other) const
588{
589 return !(*this == other);
590}
591
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800592inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700593operator<<(std::ostream& os, const Data& data)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800594{
595 os << "Name: " << data.getName() << "\n";
596 os << "MetaInfo: " << data.getMetaInfo() << "\n";
597 os << "Content: (size: " << data.getContent().value_size() << ")\n";
598 os << "Signature: (type: " << data.getSignature().getType() <<
599 ", value_length: "<< data.getSignature().getValue().value_size() << ")";
600 os << std::endl;
601
602 return os;
603}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800604
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800605} // namespace ndn
606
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700607#endif