blob: 4bda816616c67f3e1846a479d9aa16151a45b123 [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 *
64 * This is virtual so that a subclass can override to validate the name.
65 *
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070066 * @param name The Name which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070067 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070068 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080069 inline virtual void
70 setName(const Name& name);
71
72 inline const MetaInfo&
73 getMetaInfo() const;
Jeff Thompson46bd45f2013-08-08 16:46:41 -070074
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070075 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080076 * @brief Set metaInfo to a copy of the given MetaInfo.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070077 * @param metaInfo The MetaInfo which is copied.
Jeff Thompson6d591972013-10-17 11:16:32 -070078 * @return This Data so that you can chain calls to update values.
Jeff Thompson0cd8c4a2013-09-13 17:46:40 -070079 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080 inline void
81 setMetaInfo(const MetaInfo& metaInfo);
Jeff Thompson46bd45f2013-08-08 16:46:41 -070082
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080083 ///////////////////////////////////////////////////////////////
84 // MetaInfo proxy methods
85 inline uint32_t
86 getContentType() const;
87
88 inline void
89 setContentType(uint32_t type);
90
91 inline Milliseconds
92 getFreshnessPeriod() const;
93
94 inline void
95 setFreshnessPeriod(Milliseconds freshnessPeriod);
96
97 /**
98 * @brief Get content Block
99 *
100 * To access content value, one can use value()/value_size() or
101 * value_begin()/value_end() methods of the Block class
102 */
103 inline const Block&
104 getContent() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800105
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700106 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800107 * @brief Set the content to a copy of the data in the vector.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700108 * @param content A vector whose contents are copied.
Jeff Thompson6d591972013-10-17 11:16:32 -0700109 * @return This Data so that you can chain calls to update values.
Jeff Thompson0899c0f2013-09-12 12:15:31 -0700110 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800111 inline void
112 setContent(const uint8_t* content, size_t contentLength);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800113
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800114 inline void
115 setContent(const Block& content);
116
117 inline void
118 setContent(const ConstBufferPtr &contentValue);
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800119
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800120 inline const Signature&
121 getSignature() const;
122
123 /**
124 * @brief Set the signature to a copy of the given signature.
125 * @param signature The signature object which is cloned.
126 * @return This Data so that you can chain calls to update values.
127 */
128 inline Data&
129 setSignature(const Signature& signature);
Jeff Thompsonc2b7b142013-09-12 15:29:04 -0700130
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700131private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700132 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800133 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700134 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800135 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700136 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800137
138private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700139 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700140 MetaInfo metaInfo_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800141 mutable Block content_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800142 Signature signature_;
143
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800144 mutable Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700145};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800146
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800147inline
148Data::Data()
149{
150}
151
152inline
153Data::Data(const Name& name)
154 : name_(name)
155{
156}
157
158inline
159Data::~Data()
160{
161}
162
163inline const Name&
164Data::getName() const
165{
166 return name_;
167}
168
169inline void
170Data::setName(const Name& name)
171{
172 onChanged();
173 name_ = name;
174}
175
176inline const MetaInfo&
177Data::getMetaInfo() const
178{
179 return metaInfo_;
180}
181
182inline void
183Data::setMetaInfo(const MetaInfo& metaInfo)
184{
185 onChanged();
186 metaInfo_ = metaInfo;
187}
188
189inline uint32_t
190Data::getContentType() const
191{
192 return metaInfo_.getType();
193}
194
195inline void
196Data::setContentType(uint32_t type)
197{
198 onChanged();
199 metaInfo_.setType(type);
200}
201
202inline Milliseconds
203Data::getFreshnessPeriod() const
204{
205 return metaInfo_.getFreshnessPeriod();
206}
207
208inline void
209Data::setFreshnessPeriod(Milliseconds freshnessPeriod)
210{
211 onChanged();
212 metaInfo_.setFreshnessPeriod(freshnessPeriod);
213}
214
215inline const Block&
216Data::getContent() const
217{
218 if (!content_.hasWire())
219 content_.encode();
220 return content_;
221}
222
223inline void
224Data::setContent(const uint8_t* content, size_t contentLength)
225{
226 onChanged();
227
228 OBufferStream os;
229 Tlv::writeVarNumber(os, Tlv::Content);
230 Tlv::writeVarNumber(os, contentLength);
231 os.write(reinterpret_cast<const char *>(content), contentLength);
232
233 content_ = Block(os.buf());
234}
235
236inline void
237Data::setContent(const ConstBufferPtr &contentValue)
238{
239 onChanged();
240
241 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
242}
243
244inline void
245Data::setContent(const Block& content)
246{
247 onChanged();
248
249 content_ = content;
250}
251
252inline const Signature&
253Data::getSignature() const
254{
255 return signature_;
256}
257
258inline Data&
259Data::setSignature(const Signature& signature)
260{
261 onChanged();
262 signature_ = signature;
263
264 return *this;
265}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800266
267inline void
268Data::onChanged()
269{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800270 // The values have changed, so the wire format is invalidated
271
272 // !!!Note!!! Signature is not invalidated and it is responsibility of
273 // the application to do proper re-signing if necessary
274
275 wire_ = Block();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700276}
277
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800278} // namespace ndn
279
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700280#endif