blob: d6aba80b6fbd505c7a51432d891360619805b61b [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);
95
96 /**
97 * @brief Get content Block
98 *
99 * To access content value, one can use value()/value_size() or
100 * value_begin()/value_end() methods of the Block class
101 */
102 inline const Block&
103 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800104
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700105 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800106 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700107 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700108 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700109 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800110 inline void
111 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800112
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800113 inline void
114 setContent(const Block& content);
115
116 inline void
117 setContent(const ConstBufferPtr &contentValue);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800118
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800119 inline const Signature&
120 getSignature() const;
121
122 /**
123 * @brief Set the signature to a copy of the given signature.
124 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800125 */
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800126 inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800127 setSignature(const Signature& signature);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700128
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800129 inline void
130 setSignatureValue(const Block &value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800131
132 inline uint64_t
133 getIncomingFaceId() const;
134
135 inline void
136 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800137
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700138private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700139 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800140 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700141 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800142 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700143 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800144
145private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700147 MetaInfo metaInfo_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800148 mutable Block content_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800149 Signature signature_;
150
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151 mutable Block wire_;
Yingdi Yua4e57672014-02-06 11:16:17 -0800152
153 uint64_t m_incomingFaceId;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700154};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800155
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800156inline
157Data::Data()
Alexander Afanasyevec3cbad2013-12-30 17:06:45 -0800158 : content_(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800159{
160}
161
162inline
163Data::Data(const Name& name)
164 : name_(name)
165{
166}
167
168inline
169Data::~Data()
170{
171}
172
173inline const Name&
174Data::getName() const
175{
176 return name_;
177}
178
179inline void
180Data::setName(const Name& name)
181{
182 onChanged();
183 name_ = name;
184}
185
186inline const MetaInfo&
187Data::getMetaInfo() const
188{
189 return metaInfo_;
190}
191
192inline void
193Data::setMetaInfo(const MetaInfo& metaInfo)
194{
195 onChanged();
196 metaInfo_ = metaInfo;
197}
198
199inline uint32_t
200Data::getContentType() const
201{
202 return metaInfo_.getType();
203}
204
205inline void
206Data::setContentType(uint32_t type)
207{
208 onChanged();
209 metaInfo_.setType(type);
210}
211
212inline Milliseconds
213Data::getFreshnessPeriod() const
214{
215 return metaInfo_.getFreshnessPeriod();
216}
217
218inline void
219Data::setFreshnessPeriod(Milliseconds freshnessPeriod)
220{
221 onChanged();
222 metaInfo_.setFreshnessPeriod(freshnessPeriod);
223}
224
225inline const Block&
226Data::getContent() const
227{
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800228 if (content_.empty())
229 content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
230
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800231 if (!content_.hasWire())
232 content_.encode();
233 return content_;
234}
235
236inline void
237Data::setContent(const uint8_t* content, size_t contentLength)
238{
239 onChanged();
240
Alexander Afanasyev277f4692014-01-03 15:29:55 -0800241 content_ = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800242}
243
244inline void
245Data::setContent(const ConstBufferPtr &contentValue)
246{
247 onChanged();
248
249 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
250}
251
252inline void
253Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800254{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800255 onChanged();
256
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800257 if (content.type() == Tlv::Content)
258 content_ = content;
259 else {
260 content_ = Block(Tlv::Content, content);
261 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800262}
263
264inline const Signature&
265Data::getSignature() const
266{
267 return signature_;
268}
269
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800270inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800271Data::setSignature(const Signature& signature)
272{
273 onChanged();
274 signature_ = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800275}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800276
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800277inline void
278Data::setSignatureValue(const Block &value)
279{
280 onChanged();
281 signature_.setValue(value);
282}
283
Yingdi Yua4e57672014-02-06 11:16:17 -0800284inline uint64_t
285Data::getIncomingFaceId() const
286{
287 return m_incomingFaceId;
288}
289
290inline void
291Data::setIncomingFaceId(uint64_t incomingFaceId)
292{
293 m_incomingFaceId = incomingFaceId;
294}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800295
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800296inline void
297Data::onChanged()
298{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800299 // The values have changed, so the wire format is invalidated
300
301 // !!!Note!!! Signature is not invalidated and it is responsibility of
302 // the application to do proper re-signing if necessary
303
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800304 wire_.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700305}
306
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800307std::ostream&
308operator << (std::ostream &os, const Data &data);
309
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800310} // namespace ndn
311
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700312#endif