blob: 6f0f3974df4bb6f578d6d87e7424fa4a6c621b10 [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"
Alexander Afanasyev7b4a7fc2014-01-10 10:11:07 -080018#include "security/signature/signature-sha256-with-rsa.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070019
20namespace ndn {
Jeff Thompson5cae5e52013-07-10 19:41:20 -070021
Jeff Thompson56ec9e22013-08-02 11:34:07 -070022class Data {
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);
131
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700132private:
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700133 /**
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800134 * @brief Clear the wire encoding.
Jeff Thompsonb7aefa002013-09-16 18:22:00 -0700135 */
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800136 inline void
Jeff Thompson0050abe2013-09-17 12:50:25 -0700137 onChanged();
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800138
139private:
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700140 Name name_;
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700141 MetaInfo metaInfo_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800142 mutable Block content_;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800143 Signature signature_;
144
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800145 mutable Block wire_;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700146};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800147
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800148inline
149Data::Data()
Alexander Afanasyevec3cbad2013-12-30 17:06:45 -0800150 : content_(Tlv::Content) // empty content
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800151{
152}
153
154inline
155Data::Data(const Name& name)
156 : name_(name)
157{
158}
159
160inline
161Data::~Data()
162{
163}
164
165inline const Name&
166Data::getName() const
167{
168 return name_;
169}
170
171inline void
172Data::setName(const Name& name)
173{
174 onChanged();
175 name_ = name;
176}
177
178inline const MetaInfo&
179Data::getMetaInfo() const
180{
181 return metaInfo_;
182}
183
184inline void
185Data::setMetaInfo(const MetaInfo& metaInfo)
186{
187 onChanged();
188 metaInfo_ = metaInfo;
189}
190
191inline uint32_t
192Data::getContentType() const
193{
194 return metaInfo_.getType();
195}
196
197inline void
198Data::setContentType(uint32_t type)
199{
200 onChanged();
201 metaInfo_.setType(type);
202}
203
204inline Milliseconds
205Data::getFreshnessPeriod() const
206{
207 return metaInfo_.getFreshnessPeriod();
208}
209
210inline void
211Data::setFreshnessPeriod(Milliseconds freshnessPeriod)
212{
213 onChanged();
214 metaInfo_.setFreshnessPeriod(freshnessPeriod);
215}
216
217inline const Block&
218Data::getContent() const
219{
220 if (!content_.hasWire())
221 content_.encode();
222 return content_;
223}
224
225inline void
226Data::setContent(const uint8_t* content, size_t contentLength)
227{
228 onChanged();
229
Alexander Afanasyev277f4692014-01-03 15:29:55 -0800230 content_ = dataBlock(Tlv::Content, content, contentLength);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800231}
232
233inline void
234Data::setContent(const ConstBufferPtr &contentValue)
235{
236 onChanged();
237
238 content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet
239}
240
241inline void
242Data::setContent(const Block& content)
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800243{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800244 onChanged();
245
Alexander Afanasyeve0c02f52013-12-28 20:44:25 -0800246 if (content.type() == Tlv::Content)
247 content_ = content;
248 else {
249 content_ = Block(Tlv::Content, content);
250 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800251}
252
253inline const Signature&
254Data::getSignature() const
255{
256 return signature_;
257}
258
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800259inline void
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800260Data::setSignature(const Signature& signature)
261{
262 onChanged();
263 signature_ = signature;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800264}
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800265
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800266inline void
267Data::setSignatureValue(const Block &value)
268{
269 onChanged();
270 signature_.setValue(value);
271}
272
273
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800274inline void
275Data::onChanged()
276{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800277 // The values have changed, so the wire format is invalidated
278
279 // !!!Note!!! Signature is not invalidated and it is responsibility of
280 // the application to do proper re-signing if necessary
281
Alexander Afanasyevad39b182014-01-03 15:38:58 -0800282 wire_.reset();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700283}
284
Alexander Afanasyev4ff3c912014-01-03 15:25:02 -0800285std::ostream&
286operator << (std::ostream &os, const Data &data);
287
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800288} // namespace ndn
289
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700290#endif