blob: f1b64a3f9628c2313179ff9b596cbc761f04bb4f [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.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson5cae5e52013-07-10 19:41:20 -07005 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson56ec9e22013-08-02 11:34:07 -07008#ifndef NDN_DATA_HPP
Jeff Thompsona0d18c92013-08-06 13:55:32 -07009#define NDN_DATA_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070010
Jeff Thompson46bd45f2013-08-08 16:46:41 -070011#include "common.hpp"
Jeff Thompson53412192013-08-06 13:35:50 -070012#include "name.hpp"
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080013#include "encoding/block.hpp"
Jeff Thompson25b4e612013-10-10 16:03:24 -070014
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080015#include "signature.hpp"
16#include "meta-info.hpp"
17#include "key-locator.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070018
19namespace ndn {
Jeff Thompson5cae5e52013-07-10 19:41:20 -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 Afanasyev2ba8f662014-01-05 22:53:18 -080024 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
25
Jeff Thompson20af0732013-09-12 17:01:45 -070026 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080027 * @brief Create an empty Data object
Jeff Thompson20af0732013-09-12 17:01:45 -070028 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080029 inline
30 Data();
31
Jeff Thompson20af0732013-09-12 17:01:45 -070032 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080033 * @brief Create a new Data object with the given name
Jeff Thompson20af0732013-09-12 17:01:45 -070034 * @param name A reference to the name which is copied.
35 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080036 inline
37 Data(const Name& name);
Jeff Thompson25bfdca2013-10-16 17:05:41 -070038
39 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080040 * @brief The virtual destructor.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070041 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080042 inline virtual
43 ~Data();
Jeff Thompsonc69163b2013-10-12 13:49:50 -070044
45 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046 * @brief Encode this Data for a wire format.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070047 * @return The encoded byte array.
48 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080049 const Block&
50 wireEncode() const;
51
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080052 /**
53 * @brief Decode the input using a particular wire format and update this Data.
54 * @param input The input byte array to be decoded.
55 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080056 void
57 wireDecode(const Block &wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080058
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 inline const Name&
60 getName() const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070061
62 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063 * @brief Set name to a copy of the given Name.
64 *
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070065 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070066 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070067 */
Alexander Afanasyeva61757b2014-01-03 15:09:29 -080068 inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069 setName(const Name& name);
70
71 inline const MetaInfo&
72 getMetaInfo() const;
Jeff Thompson46bd45f2013-08-08 16:46:41 -070073
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070074 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080075 * @brief Set metaInfo to a copy of the given MetaInfo.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070076 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070077 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070078 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080079 inline void
80 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070081
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080082 ///////////////////////////////////////////////////////////////
83 // MetaInfo proxy methods
84 inline uint32_t
85 getContentType() const;
86
87 inline void
88 setContentType(uint32_t type);
89
90 inline Milliseconds
91 getFreshnessPeriod() const;
92
93 inline void
94 setFreshnessPeriod(Milliseconds freshnessPeriod);
Alexander Afanasyev95b0e342014-02-12 21:34:44 -080095
96 inline const name::Component&
97 getFinalBlockId() const;
98
99 inline void
100 setFinalBlockId(const name::Component& finalBlockId);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800101
102 /**
103 * @brief Get content Block
104 *
105 * To access content value, one can use value()/value_size() or
106 * value_begin()/value_end() methods of the Block class
107 */
108 inline const Block&
109 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800110
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700111 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800112 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700113 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700114 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700115 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800116 inline void
117 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800118
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800119 inline void
120 setContent(const Block& content);
121
122 inline void
123 setContent(const ConstBufferPtr &contentValue);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800124
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800125 inline const Signature&
126 getSignature() const;
127
128 /**
129 * @brief Set the signature to a copy of the given signature.
130 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800131 */
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800132 inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800133 setSignature(const Signature& signature);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700134
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800135 inline void
136 setSignatureValue(const Block &value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800137
138 inline uint64_t
139 getIncomingFaceId() const;
140
141 inline void
142 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800143
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700144private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700145 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800146 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700147 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800148 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700149 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800150
151private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700152 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700153 MetaInfo metaInfo_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800154 mutable Block content_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800155 Signature signature_;
156
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800157 mutable Block wire_;
Yingdi Yua4e57672014-02-06 11:16:17 -0800158
159 uint64_t m_incomingFaceId;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700160};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800161
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800162inline
163Data::Data()
Alexander Afanasyevec3cbad2013-12-30 17:06:45 -0800164 : content_(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800165{
166}
167
168inline
169Data::Data(const Name& name)
170 : name_(name)
171{
172}
173
174inline
175Data::~Data()
176{
177}
178
179inline const Name&
180Data::getName() const
181{
182 return name_;
183}
184
185inline void
186Data::setName(const Name& name)
187{
188 onChanged();
189 name_ = name;
190}
191
192inline const MetaInfo&
193Data::getMetaInfo() const
194{
195 return metaInfo_;
196}
197
198inline void
199Data::setMetaInfo(const MetaInfo& metaInfo)
200{
201 onChanged();
202 metaInfo_ = metaInfo;
203}
204
205inline uint32_t
206Data::getContentType() const
207{
208 return metaInfo_.getType();
209}
210
211inline void
212Data::setContentType(uint32_t type)
213{
214 onChanged();
215 metaInfo_.setType(type);
216}
217
218inline Milliseconds
219Data::getFreshnessPeriod() const
220{
221 return metaInfo_.getFreshnessPeriod();
222}
223
224inline void
225Data::setFreshnessPeriod(Milliseconds freshnessPeriod)
226{
227 onChanged();
228 metaInfo_.setFreshnessPeriod(freshnessPeriod);
229}
230
Alexander Afanasyev95b0e342014-02-12 21:34:44 -0800231inline const name::Component&
232Data::getFinalBlockId() const
233{
234 return metaInfo_.getFinalBlockId();
235}
236
237inline void
238Data::setFinalBlockId(const name::Component& finalBlockId)
239{
240 onChanged();
241 metaInfo_.setFinalBlockId(finalBlockId);
242}
243
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800244inline const Block&
245Data::getContent() const
246{
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800247 if (content_.empty())
248 content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
249
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800250 if (!content_.hasWire())
251 content_.encode();
252 return content_;
253}
254
255inline void
256Data::setContent(const uint8_t* content, size_t contentLength)
257{
258 onChanged();
259
Alexander Afanasyev277f4692014-01-03 15:29:55 -0800260 content_ = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800261}
262
263inline void
264Data::setContent(const ConstBufferPtr &contentValue)
265{
266 onChanged();
267
268 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
269}
270
271inline void
272Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800273{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800274 onChanged();
275
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800276 if (content.type() == Tlv::Content)
277 content_ = content;
278 else {
279 content_ = Block(Tlv::Content, content);
280 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800281}
282
283inline const Signature&
284Data::getSignature() const
285{
286 return signature_;
287}
288
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800289inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800290Data::setSignature(const Signature& signature)
291{
292 onChanged();
293 signature_ = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800294}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800295
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800296inline void
297Data::setSignatureValue(const Block &value)
298{
299 onChanged();
300 signature_.setValue(value);
301}
302
Yingdi Yua4e57672014-02-06 11:16:17 -0800303inline uint64_t
304Data::getIncomingFaceId() const
305{
306 return m_incomingFaceId;
307}
308
309inline void
310Data::setIncomingFaceId(uint64_t incomingFaceId)
311{
312 m_incomingFaceId = incomingFaceId;
313}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800314
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800315inline void
316Data::onChanged()
317{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800318 // The values have changed, so the wire format is invalidated
319
320 // !!!Note!!! Signature is not invalidated and it is responsibility of
321 // the application to do proper re-signing if necessary
322
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800323 wire_.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700324}
325
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800326std::ostream&
327operator << (std::ostream &os, const Data &data);
328
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800329} // namespace ndn
330
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700331#endif