blob: dc6d9f016dd44ac7de15161df24a8f15a04b7a41 [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
Jeff Thompson56ec9e22013-08-02 11:34:07 -070021class Data {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070022public:
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080023 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
24
Jeff Thompson20af0732013-09-12 17:01:45 -070025 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080026 * @brief Create an empty Data object
Jeff Thompson20af0732013-09-12 17:01:45 -070027 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080028 inline
29 Data();
30
Jeff Thompson20af0732013-09-12 17:01:45 -070031 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032 * @brief Create a new Data object with the given name
Jeff Thompson20af0732013-09-12 17:01:45 -070033 * @param name A reference to the name which is copied.
34 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080035 inline
36 Data(const Name& name);
Jeff Thompson25bfdca2013-10-16 17:05:41 -070037
38 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080039 * @brief The virtual destructor.
Jeff Thompsonc69163b2013-10-12 13:49:50 -070040 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080041 inline virtual
42 ~Data();
Jeff Thompsonc69163b2013-10-12 13:49:50 -070043
44 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080045 * @brief Encode this Data for a wire format.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -070046 * @return The encoded byte array.
47 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080048 const Block&
49 wireEncode() const;
50
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080051 /**
52 * @brief Decode the input using a particular wire format and update this Data.
53 * @param input The input byte array to be decoded.
54 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080055 void
56 wireDecode(const Block &wire);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080057
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080058 inline const Name&
59 getName() const;
Jeff Thompson5cae5e52013-07-10 19:41:20 -070060
61 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080062 * @brief Set name to a copy of the given Name.
63 *
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070064 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070065 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070066 */
Alexander Afanasyeva61757b2014-01-03 15:09:29 -080067 inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080068 setName(const Name& name);
69
70 inline const MetaInfo&
71 getMetaInfo() const;
Jeff Thompson46bd45f2013-08-08 16:46:41 -070072
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070073 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080074 * @brief Set metaInfo to a copy of the given MetaInfo.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070075 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070076 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070077 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080078 inline void
79 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070080
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080081 ///////////////////////////////////////////////////////////////
82 // MetaInfo proxy methods
83 inline uint32_t
84 getContentType() const;
85
86 inline void
87 setContentType(uint32_t type);
88
89 inline Milliseconds
90 getFreshnessPeriod() const;
91
92 inline void
93 setFreshnessPeriod(Milliseconds freshnessPeriod);
94
95 /**
96 * @brief Get content Block
97 *
98 * To access content value, one can use value()/value_size() or
99 * value_begin()/value_end() methods of the Block class
100 */
101 inline const Block&
102 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800103
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700104 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800105 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700106 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700107 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700108 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800109 inline void
110 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800111
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800112 inline void
113 setContent(const Block& content);
114
115 inline void
116 setContent(const ConstBufferPtr &contentValue);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800117
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800118 inline const Signature&
119 getSignature() const;
120
121 /**
122 * @brief Set the signature to a copy of the given signature.
123 * @param signature The signature object which is cloned.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800124 */
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800125 inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800126 setSignature(const Signature& signature);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700127
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800128 inline void
129 setSignatureValue(const Block &value);
Yingdi Yua4e57672014-02-06 11:16:17 -0800130
131 inline uint64_t
132 getIncomingFaceId() const;
133
134 inline void
135 setIncomingFaceId(uint64_t incomingFaceId);
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800136
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700137private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700138 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800139 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700140 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800141 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700142 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800143
144private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700145 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700146 MetaInfo metaInfo_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800147 mutable Block content_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800148 Signature signature_;
149
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800150 mutable Block wire_;
Yingdi Yua4e57672014-02-06 11:16:17 -0800151
152 uint64_t m_incomingFaceId;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700153};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800154
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800155inline
156Data::Data()
Alexander Afanasyevec3cbad2013-12-30 17:06:45 -0800157 : content_(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800158{
159}
160
161inline
162Data::Data(const Name& name)
163 : name_(name)
164{
165}
166
167inline
168Data::~Data()
169{
170}
171
172inline const Name&
173Data::getName() const
174{
175 return name_;
176}
177
178inline void
179Data::setName(const Name& name)
180{
181 onChanged();
182 name_ = name;
183}
184
185inline const MetaInfo&
186Data::getMetaInfo() const
187{
188 return metaInfo_;
189}
190
191inline void
192Data::setMetaInfo(const MetaInfo& metaInfo)
193{
194 onChanged();
195 metaInfo_ = metaInfo;
196}
197
198inline uint32_t
199Data::getContentType() const
200{
201 return metaInfo_.getType();
202}
203
204inline void
205Data::setContentType(uint32_t type)
206{
207 onChanged();
208 metaInfo_.setType(type);
209}
210
211inline Milliseconds
212Data::getFreshnessPeriod() const
213{
214 return metaInfo_.getFreshnessPeriod();
215}
216
217inline void
218Data::setFreshnessPeriod(Milliseconds freshnessPeriod)
219{
220 onChanged();
221 metaInfo_.setFreshnessPeriod(freshnessPeriod);
222}
223
224inline const Block&
225Data::getContent() const
226{
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800227 if (content_.empty())
228 content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
229
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800230 if (!content_.hasWire())
231 content_.encode();
232 return content_;
233}
234
235inline void
236Data::setContent(const uint8_t* content, size_t contentLength)
237{
238 onChanged();
239
Alexander Afanasyev277f4692014-01-03 15:29:55 -0800240 content_ = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800241}
242
243inline void
244Data::setContent(const ConstBufferPtr &contentValue)
245{
246 onChanged();
247
248 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
249}
250
251inline void
252Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800253{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800254 onChanged();
255
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800256 if (content.type() == Tlv::Content)
257 content_ = content;
258 else {
259 content_ = Block(Tlv::Content, content);
260 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800261}
262
263inline const Signature&
264Data::getSignature() const
265{
266 return signature_;
267}
268
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800269inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800270Data::setSignature(const Signature& signature)
271{
272 onChanged();
273 signature_ = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800274}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800275
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800276inline void
277Data::setSignatureValue(const Block &value)
278{
279 onChanged();
280 signature_.setValue(value);
281}
282
Yingdi Yua4e57672014-02-06 11:16:17 -0800283inline uint64_t
284Data::getIncomingFaceId() const
285{
286 return m_incomingFaceId;
287}
288
289inline void
290Data::setIncomingFaceId(uint64_t incomingFaceId)
291{
292 m_incomingFaceId = incomingFaceId;
293}
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800294
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800295inline void
296Data::onChanged()
297{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800298 // The values have changed, so the wire format is invalidated
299
300 // !!!Note!!! Signature is not invalidated and it is responsibility of
301 // the application to do proper re-signing if necessary
302
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800303 wire_.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700304}
305
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800306std::ostream&
307operator << (std::ostream &os, const Data &data);
308
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800309} // namespace ndn
310
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700311#endif