blob: 88ffcc4ac54573dd71e0db0ad883c3b3a45d67d8 [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
Jeff Thompson20af0732013-09-12 17:01:45 -070042 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080043 Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044
Jeff Thompson20af0732013-09-12 17:01:45 -070045 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046 * @brief Create a new Data object with the given name
Jeff Thompson20af0732013-09-12 17:01:45 -070047 * @param name A reference to the name which is copied.
48 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080049 Data(const Name& name);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080050
51 /**
52 * @brief Create a new Data object from wire encoding
53 */
54 explicit
55 Data(const Block& wire)
56 {
57 wireDecode(wire);
58 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059
Jeff Thompson25bfdca2013-10-16 17:05:41 -070060 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080061 * @brief The destructor
Jeff Thompsonc69163b2013-10-12 13:49:50 -070062 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063 ~Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
Jeff Thompsonc69163b2013-10-12 13:49:50 -070065 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080066 * @brief Fast encoding or block size estimation
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070067 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080068 template<bool T>
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070069 size_t
70 wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070071
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080072 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080073 * @brief Encode to a wire format
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080074 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070075 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -080076 wireEncode() const;
77
78 /**
79 * @brief Decode from the wire format
80 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070081 void
82 wireDecode(const Block& wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080083
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080084 /**
85 * @brief Check if already has wire
86 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 bool
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080088 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089
90 ////////////////////////////////////////////////////////////////////
91
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070092 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080093 getName() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070094
Jeff Thompson5cae5e52013-07-10 19:41:20 -070095 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080096 * @brief Set name to a copy of the given Name.
97 *
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070098 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070099 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700100 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700101 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800102 setName(const Name& name);
103
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800104 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700106 const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107 getMetaInfo() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700109 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800110 * @brief Set metaInfo to a copy of the given MetaInfo.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700111 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700112 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700113 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700114 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800115 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700116
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800117 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800119 ///////////////////////////////////////////////////////////////
120 ///////////////////////////////////////////////////////////////
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800121 ///////////////////////////////////////////////////////////////
122 // MetaInfo proxy methods
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800123
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700124 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800125 getContentType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700126
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700127 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800128 setContentType(uint32_t type);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800129
130 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700131
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700132 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800133 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700135 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800137
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800138 //
139
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700140 const name::Component&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800141 getFinalBlockId() const;
142
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700143 void
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800144 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800145
146 //
147 ///////////////////////////////////////////////////////////////
148 ///////////////////////////////////////////////////////////////
149 ///////////////////////////////////////////////////////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700150
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151 /**
152 * @brief Get content Block
153 *
154 * To access content value, one can use value()/value_size() or
155 * value_begin()/value_end() methods of the Block class
156 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700157 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800158 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800159
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700160 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800161 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700162 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700163 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700164 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700165 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800166 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800167
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700168 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800169 setContent(const Block& content);
170
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700171 void
172 setContent(const ConstBufferPtr& contentValue);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800173
174 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700176 const Signature&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800177 getSignature() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700178
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800179 /**
180 * @brief Set the signature to a copy of the given signature.
181 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800182 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700183 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800184 setSignature(const Signature& signature);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700185
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700186 void
187 setSignatureValue(const Block& value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800188
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800189 ///////////////////////////////////////////////////////////////
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800190
191 nfd::LocalControlHeader&
192 getLocalControlHeader();
193
194 const nfd::LocalControlHeader&
195 getLocalControlHeader() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700196
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700197 uint64_t
Yingdi Yua4e57672014-02-06 11:16:17 -0800198 getIncomingFaceId() const;
199
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700200 void
Yingdi Yua4e57672014-02-06 11:16:17 -0800201 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700202
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700203public: // EqualityComparable concept
204 bool
205 operator==(const Data& other) const;
206
207 bool
208 operator!=(const Data& other) const;
209
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700210private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700211 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800212 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700213 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700214 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700215 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800216
217private:
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800218 Name m_name;
219 MetaInfo m_metaInfo;
220 mutable Block m_content;
221 Signature m_signature;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800222
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800223 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800224
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800225 nfd::LocalControlHeader m_localControlHeader;
226 friend class nfd::LocalControlHeader;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700227};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800228
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800229inline
230Data::Data()
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800231 : m_content(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800232{
233}
234
235inline
236Data::Data(const Name& name)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800237 : m_name(name)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800238{
239}
240
241inline
242Data::~Data()
243{
244}
245
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800246template<bool T>
247inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700248Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800249{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700250 size_t totalLength = 0;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800251
252 // Data ::= DATA-TLV TLV-LENGTH
253 // Name
254 // MetaInfo
255 // Content
256 // Signature
257
258 // (reverse encoding)
259
260 if (!unsignedPortion && !m_signature)
261 {
262 throw Error("Requested wire format, but data packet has not been signed yet");
263 }
264
265 if (!unsignedPortion)
266 {
267 // SignatureValue
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700268 totalLength += prependBlock(block, m_signature.getValue());
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800269 }
270
271 // SignatureInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700272 totalLength += prependBlock(block, m_signature.getInfo());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700273
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800274 // Content
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700275 totalLength += prependBlock(block, getContent());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700276
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800277 // MetaInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700278 totalLength += getMetaInfo().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800279
280 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700281 totalLength += getName().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800282
283 if (!unsignedPortion)
284 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700285 totalLength += block.prependVarNumber (totalLength);
286 totalLength += block.prependVarNumber (Tlv::Data);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800287 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700288 return totalLength;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800289}
290
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700291inline const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800292Data::wireEncode() const
293{
294 if (m_wire.hasWire())
295 return m_wire;
296
297 EncodingEstimator estimator;
298 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700299
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800300 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800301 wireEncode(buffer);
302
303 const_cast<Data*>(this)->wireDecode(buffer.block());
304 return m_wire;
305}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700306
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800307/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700308 * Decode the input using a particular wire format and update this Data.
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800309 * @param input The input byte array to be decoded.
310 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700311inline void
312Data::wireDecode(const Block& wire)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800313{
314 m_wire = wire;
315 m_wire.parse();
316
317 // Data ::= DATA-TLV TLV-LENGTH
318 // Name
319 // MetaInfo
320 // Content
321 // Signature
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700322
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800323 // Name
324 m_name.wireDecode(m_wire.get(Tlv::Name));
325
326 // MetaInfo
327 m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo));
328
329 // Content
330 m_content = m_wire.get(Tlv::Content);
331
332 ///////////////
333 // Signature //
334 ///////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700335
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800336 // SignatureInfo
337 m_signature.setInfo(m_wire.get(Tlv::SignatureInfo));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700338
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800339 // SignatureValue
340 Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue);
341 if (val != m_wire.elements_end())
342 m_signature.setValue(*val);
343}
344
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800345inline bool
346Data::hasWire() const
347{
348 return m_wire.hasWire();
349}
350
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700351inline const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800352Data::getName() const
353{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800354 return m_name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800355}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700356
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800357inline void
358Data::setName(const Name& name)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700359{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800360 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700361 m_name = name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800362}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700363
364inline const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800365Data::getMetaInfo() const
366{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800367 return m_metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800368}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700369
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800370inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700371Data::setMetaInfo(const MetaInfo& metaInfo)
372{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800373 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700374 m_metaInfo = metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800375}
376
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700377inline uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800378Data::getContentType() const
379{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800380 return m_metaInfo.getType();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800381}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700382
383inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800384Data::setContentType(uint32_t type)
385{
386 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800387 m_metaInfo.setType(type);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800388}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700389
390inline const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800391Data::getFreshnessPeriod() const
392{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800393 return m_metaInfo.getFreshnessPeriod();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800394}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700395
396inline void
397Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800398{
399 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800400 m_metaInfo.setFreshnessPeriod(freshnessPeriod);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800401}
402
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800403inline const name::Component&
404Data::getFinalBlockId() const
405{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800406 return m_metaInfo.getFinalBlockId();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800407}
408
409inline void
410Data::setFinalBlockId(const name::Component& finalBlockId)
411{
412 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800413 m_metaInfo.setFinalBlockId(finalBlockId);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800414}
415
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700416inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800417Data::getContent() const
418{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800419 if (m_content.empty())
420 m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800421
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800422 if (!m_content.hasWire())
423 m_content.encode();
424 return m_content;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800425}
426
427inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700428Data::setContent(const uint8_t* content, size_t contentLength)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800429{
430 onChanged();
431
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800432 m_content = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800433}
434
435inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700436Data::setContent(const ConstBufferPtr& contentValue)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800437{
438 onChanged();
439
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800440 m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800441}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700442
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800443inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700444Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800445{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800446 onChanged();
447
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800448 if (content.type() == Tlv::Content)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800449 m_content = content;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800450 else {
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800451 m_content = Block(Tlv::Content, content);
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800452 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800453}
454
455inline const Signature&
456Data::getSignature() const
457{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800458 return m_signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800459}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700460
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800461inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700462Data::setSignature(const Signature& signature)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800463{
464 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800465 m_signature = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800466}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800467
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800468inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700469Data::setSignatureValue(const Block& value)
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800470{
471 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800472 m_signature.setValue(value);
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800473}
474
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800475//
476
477inline nfd::LocalControlHeader&
478Data::getLocalControlHeader()
479{
480 return m_localControlHeader;
481}
482
483inline const nfd::LocalControlHeader&
484Data::getLocalControlHeader() const
485{
486 return m_localControlHeader;
487}
488
Yingdi Yua4e57672014-02-06 11:16:17 -0800489inline uint64_t
490Data::getIncomingFaceId() const
491{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800492 return getLocalControlHeader().getIncomingFaceId();
Yingdi Yua4e57672014-02-06 11:16:17 -0800493}
494
495inline void
496Data::setIncomingFaceId(uint64_t incomingFaceId)
497{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800498 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800499 // ! do not reset Data's wire !
Yingdi Yua4e57672014-02-06 11:16:17 -0800500}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800501
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700502inline void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800503Data::onChanged()
504{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800505 // The values have changed, so the wire format is invalidated
506
507 // !!!Note!!! Signature is not invalidated and it is responsibility of
508 // the application to do proper re-signing if necessary
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700509
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800510 m_wire.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700511}
512
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700513inline bool
514Data::operator==(const Data& other) const
515{
516 return getName() == other.getName() &&
517 getMetaInfo() == other.getMetaInfo() &&
518 getContent() == other.getContent() &&
519 getSignature() == other.getSignature();
520}
521
522inline bool
523Data::operator!=(const Data& other) const
524{
525 return !(*this == other);
526}
527
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800528inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700529operator<<(std::ostream& os, const Data& data)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800530{
531 os << "Name: " << data.getName() << "\n";
532 os << "MetaInfo: " << data.getMetaInfo() << "\n";
533 os << "Content: (size: " << data.getContent().value_size() << ")\n";
534 os << "Signature: (type: " << data.getSignature().getType() <<
535 ", value_length: "<< data.getSignature().getValue().value_size() << ")";
536 os << std::endl;
537
538 return os;
539}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800540
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800541} // namespace ndn
542
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700543#endif