blob: 752f24c9ae211fd812b8a9727ba5cfba8d6fbf7a [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
9 */
10
11#ifndef NDN_CONTENT_H
12#define NDN_CONTENT_H
13
14#include "ndn-cpp/common.h"
15#include "ndn-cpp/fields/blob.h"
16#include "ndn-cpp/fields/name-component.h"
17
18namespace ndn {
19
20/**
21 * @brief Class providing an interface to work with content NDN data packets
22 *
23 * Content of data packets consists of two parts: information about content
24 * (timestamp, freshness, type, etc.) and raw content itself
25 *
26 * @code
27 * Content ::= ContentInfo
28 * ContentData
29 *
30 * ContentInfo ::= Timestamp
31 * Type?
32 * Freshness?
33 * FinalBlockID?
34 * @endcode
35 */
36class Content
37{
38public:
39 /**
40 * @brief Enum of content types
41 */
42 enum Type
43 {
44 DATA = 0, ///< @brief No semantics is defined for the content
45 ENCR, ///< @brief Indicate that the content is encrypted
46 GONE, ///< @brief ?
47 KEY, ///< @brief Content is a key object
48 LINK, ///< @brief Content contains a LINK object
49 NACK ///< @brief Negative acknowledgment by the content producer, indicating that there is no data with the requested name
50 };
51
52 /**
53 * @brief Create an empty content
54 */
55 Content ();
56
57 /**
58 * @brief Create a content from a memory buffer
59 * @param buffer pointer to first byte of the memory buffer
60 * @param size size of the memory buffer
61 * @param timestamp content generation timestamp
62 * @param type type of content (default is Content::DATA)
63 * @param freshness amount of time the content is considered "fresh" (default is 2147 seconds, maximum possible value for CCNx)
64 * @param finalBlock name of the final DATA
65 *
66 * Use the other version of the constructor, if timestamp needs to be automatically generated
67 */
68 Content (const void *buffer, size_t size,
69 const Time &timestamp,
70 Type type = DATA,
71 const TimeInterval &freshness = maxFreshness,
72 const name::Component &finalBlock = noFinalBlock);
73
74 /**
75 * @brief Create a content from a memory buffer
76 * @param buffer pointer to first byte of the memory buffer
77 * @param size size of the memory buffer
78 * @param type type of content (default is Content::DATA)
79 * @param freshness amount of time the content is considered "fresh" (default is 2147 seconds, maximum possible value for CCNx)
80 * @param finalBlock name of the final DATA
81 *
82 * This method automatically sets timestamp of the created content to the current time (UTC clock)
83 */
84 Content (const void *buffer, size_t size,
85 Type type = DATA,
86 const TimeInterval &freshness = maxFreshness,
87 const name::Component &finalBlock = noFinalBlock);
88
89 /**
90 * @brief Get content timestamp (const reference)
91 */
92 inline const Time &
93 getTimestamp () const;
94
95 /**
96 * @brief Get content timestamp (reference)
97 */
98 inline Time &
99 getTimestamp ();
100
101 /**
102 * @brief Set content timestamp
103 * @param timestamp content timestamp (default is empty ptime object)
104 *
105 * If parameter is omitted, then the current time (UTC clock) is used
106 */
107 inline void
108 setTimeStamp (const Time &timestamp = Time ());
109
110 /**
111 * @brief Get type of content
112 */
113 inline Type
114 getType () const;
115
116 /**
117 * @brief Set type of content
118 * @param type content type @see Content::Type
119 */
120 inline void
121 setType (Type type);
122
123 /**
124 * @brief Get content freshness (const reference)
125 */
126 inline const TimeInterval &
127 getFreshness () const;
128
129 /**
130 * @brief Get content freshness (reference)
131 */
132 inline TimeInterval &
133 getFreshness ();
134
135 /**
136 * @brief Set content freshness
137 * @param freshness content freshness (default value is Content::maxFreshness = 2147 seconds)
138 */
139 inline void
140 setFreshness (const TimeInterval &freshness = maxFreshness);
141
142 /**
143 * @brief Get final block ID of the content (const reference)
144 */
145 inline const name::Component &
146 getFinalBlockId () const;
147
148 /**
149 * @brief Get final block ID of the content (reference)
150 */
151 inline name::Component &
152 getFinalBlockId ();
153
154 /**
155 * @brief Set final block ID of the content
156 * @param finalBlock component name of the final block
157 */
158 inline void
159 setFinalBlockId (const name::Component &finalBlock);
160
161 /**
162 * @brief Get const reference to content bits
163 */
164 inline const Blob &
165 getContent () const;
166
167 /**
168 * @brief Get reference to content bits
169 */
170 inline Blob &
171 getContent ();
172
173 /**
174 * @brief Set content bits from blob
175 * @param content blob that holds content bits
176 *
177 * In certain cases, getContent ().swap (content); is more appropriate,
178 * since it would avoid object copying
179 */
180 inline void
181 setContent (const Blob &content);
182
183 /**
184 * @brief Set content bits from memory buffer
185 * @param buf pointer to first byte of memory buffer
186 * @param length size of memory buffer
187 */
188 inline void
189 setContent (const void *buf, size_t length);
190
191public:
192 static const name::Component noFinalBlock; ///< @brief Not a final block == name::Component ()
193 static const TimeInterval noFreshness; ///< @brief Minimum freshness == seconds (0)
194 static const TimeInterval maxFreshness; ///< @brief Maximum freshnes == seconds (2147)
195
196private:
197 // ContentInfo
198 Time m_timestamp;
199 Type m_type;
200 TimeInterval m_freshness;
201 name::Component m_finalBlockId;
202
203 // ContentData
204 Blob m_content;
205};
206
207inline const Time &
208Content::getTimestamp () const
209{
210 return m_timestamp;
211}
212
213inline Time &
214Content::getTimestamp ()
215{
216 return m_timestamp;
217}
218
219inline void
220Content::setTimeStamp (const Time &timestamp/* = Time ()*/)
221{
222 if (timestamp != Time ())
223 {
224 m_timestamp = timestamp;
225 }
226 else
227 {
228 m_timestamp = time::Now ();
229 }
230}
231
232inline Content::Type
233Content::getType () const
234{
235 return m_type;
236}
237
238inline void
239Content::setType (Content::Type type)
240{
241 m_type = type;
242}
243
244inline const TimeInterval &
245Content::getFreshness () const
246{
247 return m_freshness;
248}
249
250inline TimeInterval &
251Content::getFreshness ()
252{
253 return m_freshness;
254}
255
256inline void
257Content::setFreshness (const TimeInterval &freshness/* = Content::maxFreshness*/)
258{
259 m_freshness = freshness;
260}
261
262inline const name::Component &
263Content::getFinalBlockId () const
264{
265 return m_finalBlockId;
266}
267
268inline name::Component &
269Content::getFinalBlockId ()
270{
271 return m_finalBlockId;
272}
273
274inline void
275Content::setFinalBlockId (const name::Component &finalBlock)
276{
277 m_finalBlockId = finalBlock;
278}
279
280inline const Blob &
281Content::getContent () const
282{
283 return m_content;
284}
285
286inline Blob &
287Content::getContent ()
288{
289 return m_content;
290}
291
292inline void
293Content::setContent (const Blob &content)
294{
295 m_content = content;
296}
297
298inline void
299Content::setContent (const void *buf, size_t length)
300{
301 Blob (buf, length).swap (m_content);
302}
303
304} // ndn
305
306#endif // NDN_SIGNATURE_H