blob: ee8461538635f7722a7addb0efaf11d1f0982f27 [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/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson5cae5e52013-07-10 19:41:20 -07004 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson56ec9e22013-08-02 11:34:07 -07007#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07008#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -07009
Jeff Thompson46bd45f2013-08-08 16:46:41 -070010#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070011#include "name.hpp"
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080012#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070013
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080014#include "signature.hpp"
15#include "meta-info.hpp"
16#include "key-locator.hpp"
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080017#include "management/nfd-local-control-header.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070018
19namespace ndn {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070020
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080021class Data : public enable_shared_from_this<Data>
22{
Jeff Thompson5cae5e52013-07-10 19:41:20 -070023public:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070024 class Error : public std::runtime_error
25 {
26 public:
27 explicit
28 Error(const std::string& what)
29 : std::runtime_error(what)
30 {
31 }
32 };
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033
Jeff Thompson20af0732013-09-12 17:01:45 -070034 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080035 * @brief Create an empty Data object
Jeff Thompson20af0732013-09-12 17:01:45 -070036 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080037 Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038
Jeff Thompson20af0732013-09-12 17:01:45 -070039 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080040 * @brief Create a new Data object with the given name
Jeff Thompson20af0732013-09-12 17:01:45 -070041 * @param name A reference to the name which is copied.
42 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080043 Data(const Name& name);
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080044
45 /**
46 * @brief Create a new Data object from wire encoding
47 */
48 explicit
49 Data(const Block& wire)
50 {
51 wireDecode(wire);
52 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053
Jeff Thompson25bfdca2013-10-16 17:05:41 -070054 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080055 * @brief The destructor
Jeff Thompsonc69163b2013-10-12 13:49:50 -070056 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080057 ~Data();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070058
Jeff Thompsonc69163b2013-10-12 13:49:50 -070059 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080060 * @brief Fast encoding or block size estimation
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070061 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080062 template<bool T>
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070063 size_t
64 wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080066 /**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080067 * @brief Encode to a wire format
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080068 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070069 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -080070 wireEncode() const;
71
72 /**
73 * @brief Decode from the wire format
74 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070075 void
76 wireDecode(const Block& wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080077
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080078 /**
79 * @brief Check if already has wire
80 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070081 bool
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -080082 hasWire() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083
84 ////////////////////////////////////////////////////////////////////
85
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070086 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080087 getName() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088
Jeff Thompson5cae5e52013-07-10 19:41:20 -070089 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080090 * @brief Set name to a copy of the given Name.
91 *
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070092 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070093 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070094 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070095 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080096 setName(const Name& name);
97
Alexander Afanasyev809805d2014-02-17 17:20:33 -080098 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070099
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700100 const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800101 getMetaInfo() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700103 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800104 * @brief Set metaInfo to a copy of the given MetaInfo.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700105 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700106 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -0700107 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700108 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800109 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -0700110
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800111 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800113 ///////////////////////////////////////////////////////////////
114 ///////////////////////////////////////////////////////////////
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800115 ///////////////////////////////////////////////////////////////
116 // MetaInfo proxy methods
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800117
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700118 uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800119 getContentType() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700120
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700121 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800122 setContentType(uint32_t type);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800123
124 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700125
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700126 const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800127 getFreshnessPeriod() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700129 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800131
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800132 //
133
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700134 const name::Component&
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800135 getFinalBlockId() const;
136
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700137 void
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800138 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800139
140 //
141 ///////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////
143 ///////////////////////////////////////////////////////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800145 /**
146 * @brief Get content Block
147 *
148 * To access content value, one can use value()/value_size() or
149 * value_begin()/value_end() methods of the Block class
150 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700151 const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800152 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800153
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700154 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800155 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700156 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700157 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700158 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700159 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800160 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800161
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700162 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800163 setContent(const Block& content);
164
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700165 void
166 setContent(const ConstBufferPtr& contentValue);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800167
168 //
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700169
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700170 const Signature&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800171 getSignature() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700172
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800173 /**
174 * @brief Set the signature to a copy of the given signature.
175 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800176 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700177 void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800178 setSignature(const Signature& signature);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700179
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700180 void
181 setSignatureValue(const Block& value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800182
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800183 ///////////////////////////////////////////////////////////////
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800184
185 nfd::LocalControlHeader&
186 getLocalControlHeader();
187
188 const nfd::LocalControlHeader&
189 getLocalControlHeader() const;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700190
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700191 uint64_t
Yingdi Yua4e57672014-02-06 11:16:17 -0800192 getIncomingFaceId() const;
193
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700194 void
Yingdi Yua4e57672014-02-06 11:16:17 -0800195 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700196
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700197public: // EqualityComparable concept
198 bool
199 operator==(const Data& other) const;
200
201 bool
202 operator!=(const Data& other) const;
203
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700204private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700205 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800206 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700207 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700208 void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700209 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800210
211private:
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800212 Name m_name;
213 MetaInfo m_metaInfo;
214 mutable Block m_content;
215 Signature m_signature;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800216
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800217 mutable Block m_wire;
Yingdi Yua4e57672014-02-06 11:16:17 -0800218
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800219 nfd::LocalControlHeader m_localControlHeader;
220 friend class nfd::LocalControlHeader;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700221};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800222
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800223inline
224Data::Data()
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800225 : m_content(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800226{
227}
228
229inline
230Data::Data(const Name& name)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800231 : m_name(name)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800232{
233}
234
235inline
236Data::~Data()
237{
238}
239
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800240template<bool T>
241inline size_t
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700242Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800243{
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700244 size_t totalLength = 0;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800245
246 // Data ::= DATA-TLV TLV-LENGTH
247 // Name
248 // MetaInfo
249 // Content
250 // Signature
251
252 // (reverse encoding)
253
254 if (!unsignedPortion && !m_signature)
255 {
256 throw Error("Requested wire format, but data packet has not been signed yet");
257 }
258
259 if (!unsignedPortion)
260 {
261 // SignatureValue
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700262 totalLength += prependBlock(block, m_signature.getValue());
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800263 }
264
265 // SignatureInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700266 totalLength += prependBlock(block, m_signature.getInfo());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700267
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800268 // Content
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700269 totalLength += prependBlock(block, getContent());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700270
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800271 // MetaInfo
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700272 totalLength += getMetaInfo().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800273
274 // Name
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700275 totalLength += getName().wireEncode(block);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800276
277 if (!unsignedPortion)
278 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700279 totalLength += block.prependVarNumber (totalLength);
280 totalLength += block.prependVarNumber (Tlv::Data);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800281 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700282 return totalLength;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800283}
284
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700285inline const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800286Data::wireEncode() const
287{
288 if (m_wire.hasWire())
289 return m_wire;
290
291 EncodingEstimator estimator;
292 size_t estimatedSize = wireEncode(estimator);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700293
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800294 EncodingBuffer buffer(estimatedSize, 0);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800295 wireEncode(buffer);
296
297 const_cast<Data*>(this)->wireDecode(buffer.block());
298 return m_wire;
299}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700300
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800301/**
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700302 * Decode the input using a particular wire format and update this Data.
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800303 * @param input The input byte array to be decoded.
304 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700305inline void
306Data::wireDecode(const Block& wire)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800307{
308 m_wire = wire;
309 m_wire.parse();
310
311 // Data ::= DATA-TLV TLV-LENGTH
312 // Name
313 // MetaInfo
314 // Content
315 // Signature
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700316
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800317 // Name
318 m_name.wireDecode(m_wire.get(Tlv::Name));
319
320 // MetaInfo
321 m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo));
322
323 // Content
324 m_content = m_wire.get(Tlv::Content);
325
326 ///////////////
327 // Signature //
328 ///////////////
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700329
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800330 // SignatureInfo
331 m_signature.setInfo(m_wire.get(Tlv::SignatureInfo));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700332
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800333 // SignatureValue
334 Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue);
335 if (val != m_wire.elements_end())
336 m_signature.setValue(*val);
337}
338
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800339inline bool
340Data::hasWire() const
341{
342 return m_wire.hasWire();
343}
344
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700345inline const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800346Data::getName() const
347{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800348 return m_name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800349}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700350
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800351inline void
352Data::setName(const Name& name)
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700353{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800354 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700355 m_name = name;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800356}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700357
358inline const MetaInfo&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800359Data::getMetaInfo() const
360{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800361 return m_metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800362}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700363
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800364inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700365Data::setMetaInfo(const MetaInfo& metaInfo)
366{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800367 onChanged();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700368 m_metaInfo = metaInfo;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800369}
370
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700371inline uint32_t
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800372Data::getContentType() const
373{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800374 return m_metaInfo.getType();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800375}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700376
377inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800378Data::setContentType(uint32_t type)
379{
380 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800381 m_metaInfo.setType(type);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800382}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700383
384inline const time::milliseconds&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800385Data::getFreshnessPeriod() const
386{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800387 return m_metaInfo.getFreshnessPeriod();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800388}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700389
390inline void
391Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800392{
393 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800394 m_metaInfo.setFreshnessPeriod(freshnessPeriod);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800395}
396
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800397inline const name::Component&
398Data::getFinalBlockId() const
399{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800400 return m_metaInfo.getFinalBlockId();
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800401}
402
403inline void
404Data::setFinalBlockId(const name::Component& finalBlockId)
405{
406 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800407 m_metaInfo.setFinalBlockId(finalBlockId);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800408}
409
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700410inline const Block&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800411Data::getContent() const
412{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800413 if (m_content.empty())
414 m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800415
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800416 if (!m_content.hasWire())
417 m_content.encode();
418 return m_content;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800419}
420
421inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700422Data::setContent(const uint8_t* content, size_t contentLength)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800423{
424 onChanged();
425
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800426 m_content = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800427}
428
429inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700430Data::setContent(const ConstBufferPtr& contentValue)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800431{
432 onChanged();
433
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800434 m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800435}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700436
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800437inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700438Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800439{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800440 onChanged();
441
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800442 if (content.type() == Tlv::Content)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800443 m_content = content;
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800444 else {
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800445 m_content = Block(Tlv::Content, content);
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800446 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800447}
448
449inline const Signature&
450Data::getSignature() const
451{
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800452 return m_signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800453}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700454
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800455inline void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700456Data::setSignature(const Signature& signature)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800457{
458 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800459 m_signature = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800460}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800461
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800462inline void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700463Data::setSignatureValue(const Block& value)
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800464{
465 onChanged();
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800466 m_signature.setValue(value);
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800467}
468
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800469//
470
471inline nfd::LocalControlHeader&
472Data::getLocalControlHeader()
473{
474 return m_localControlHeader;
475}
476
477inline const nfd::LocalControlHeader&
478Data::getLocalControlHeader() const
479{
480 return m_localControlHeader;
481}
482
Yingdi Yua4e57672014-02-06 11:16:17 -0800483inline uint64_t
484Data::getIncomingFaceId() const
485{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800486 return getLocalControlHeader().getIncomingFaceId();
Yingdi Yua4e57672014-02-06 11:16:17 -0800487}
488
489inline void
490Data::setIncomingFaceId(uint64_t incomingFaceId)
491{
Alexander Afanasyev6d48bc12014-02-18 00:10:51 -0800492 getLocalControlHeader().setIncomingFaceId(incomingFaceId);
Alexander Afanasyev5964fb72014-02-18 12:42:45 -0800493 // ! do not reset Data's wire !
Yingdi Yua4e57672014-02-06 11:16:17 -0800494}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800495
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700496inline void
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800497Data::onChanged()
498{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800499 // The values have changed, so the wire format is invalidated
500
501 // !!!Note!!! Signature is not invalidated and it is responsibility of
502 // the application to do proper re-signing if necessary
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700503
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800504 m_wire.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700505}
506
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700507inline bool
508Data::operator==(const Data& other) const
509{
510 return getName() == other.getName() &&
511 getMetaInfo() == other.getMetaInfo() &&
512 getContent() == other.getContent() &&
513 getSignature() == other.getSignature();
514}
515
516inline bool
517Data::operator!=(const Data& other) const
518{
519 return !(*this == other);
520}
521
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800522inline std::ostream&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700523operator<<(std::ostream& os, const Data& data)
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800524{
525 os << "Name: " << data.getName() << "\n";
526 os << "MetaInfo: " << data.getMetaInfo() << "\n";
527 os << "Content: (size: " << data.getContent().value_size() << ")\n";
528 os << "Signature: (type: " << data.getSignature().getType() <<
529 ", value_length: "<< data.getSignature().getValue().value_size() << ")";
530 os << std::endl;
531
532 return os;
533}
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800534
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800535} // namespace ndn
536
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700537#endif