blob: 66796c313f0366987dbe56051774a4fda6a000aa [file] [log] [blame]
Alexander Afanasyev834f35c2011-08-16 17:13:50 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
Ilya Moiseenko332add02011-12-24 17:21:25 -080018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070019 */
20
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#ifndef _NDN_ENCODING_HELPER_H_
22#define _NDN_ENCODING_HELPER_H_
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070023
24#include <sys/types.h>
25
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070026#include "ccnb-parser/common.h"
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070027#include "ns3/ptr.h"
28#include "ns3/nstime.h"
29#include "ns3/buffer.h"
30
31namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070032namespace ndn {
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070033
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070034class NameComponents;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070035
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036class InterestHeader;
37class ContentObjectHeader;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070038
39/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040 * \brief Helper to encode/decode ccnb formatted Ndn message
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070041 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070042class EncodingHelper
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070043{
44public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080045 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046 * \brief Serialize NdnInterestHeader to Buffer::Iterator
47 * @param start Buffer to store serialized NdnInterestHeader
48 * @param interest Pointer to NdnInterestHeader to be serialized
49 * @return length of serialized NdnInterestHeader
Ilya Moiseenko332add02011-12-24 17:21:25 -080050 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070051 static size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 Serialize (Buffer::Iterator start, const InterestHeader &interest);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070053
Ilya Moiseenko332add02011-12-24 17:21:25 -080054 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070055 * \brief Compute the size of serialized NdnInterestHeader
56 * @param interest Pointer to NdnInterestHeader
Ilya Moiseenko332add02011-12-24 17:21:25 -080057 * @return length
58 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070059 static size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 GetSerializedSize (const InterestHeader &interest);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070061
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070062public:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070063 /**
64 * @brief Append CCNB block header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070065 * @param start Buffer to store serialized NdnInterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070066 * @param value dictionary id of the block header
67 * @param block_type Type of CCNB block
68 *
69 * @returns written length
70 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070071 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070072 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070073
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070074 /**
75 * @brief Estimate size of the CCNB block header
76 * @param value dictionary id of the block header
77 * @returns estimated length
78 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070079 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070080 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070081
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070082 /**
83 * @brief Add number in CCNB encoding
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070084 * @param start Buffer to store serialized NdnInterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070085 * @param number Number to be written
86 *
87 * @returns written length
88 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070089 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070090 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070091
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070092 /**
93 * @brief Estimate size of the number in CCNB encoding
94 * @param number Number to be written
95 * @returns estimated length
96 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070097 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070098 EstimateNumber (uint32_t number);
99
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700100 /**
101 * @brief Append CCNB closer tag (estimated size is 1)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102 * @param start Buffer to store serialized InterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700103 *
104 * @returns written length
105 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700106 static size_t
107 AppendCloser (Buffer::Iterator &start);
108
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700109 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700110 * @brief Append NameComponents in CCNB encoding
111 * @param start Buffer to store serialized InterestHeader
112 * @param name constant reference to NameComponents object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700113 *
114 * @returns written length
115 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700116 static size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117 AppendNameComponents (Buffer::Iterator &start, const NameComponents &name);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700118
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700119 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120 * @brief Estimate size of NameComponents in CCNB encoding
121 * @param name constant reference to NameComponents object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700122 * @returns estimated length
123 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700124 static size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700125 EstimateNameComponents (const NameComponents &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700126
127 /**
128 * Append a binary timestamp as a BLOB using the ccn binary
129 * Timestamp representation (12-bit fraction).
130 *
131 * @param start start iterator of the buffer to append to.
132 * @param time - Time object
133 *
134 * @returns written length
135 */
136 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700137 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
138
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700139 /**
140 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
141 * @param time - Time object
142 * @returns estimated length
143 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700144 static size_t
145 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700146
147 /**
148 * Append a tagged BLOB
149 *
150 * This is a ccnb-encoded element with containing the BLOB as content
151 *
152 * @param start start iterator of the buffer to append to.
153 * @param dtag is the element's dtab
154 * @param data points to the binary data
155 * @param size is the size of the data, in bytes
156 *
157 * @returns written length
158 */
159 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700160 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700161 const uint8_t *data, size_t size);
162
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700163 /**
164 * @brief Estimate size of a tagged BLOB in CCNB enconding
165 * @param dtag is the element's dtab
166 * @param size is the size of the data, in bytes
167 * @returns estimated length
168 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700169 static size_t
170 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700171
172 /**
173 * Append value as a tagged BLOB (templated version)
174 *
175 * This is a ccnb-encoded element with containing the BLOB as content
176 *
177 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
178 *
179 * @param start start iterator of the buffer to append to.
180 * @param dtag is the element's dtab
181 * @param data a value to add
182 *
183 * @returns written length
184 */
185 template<class T>
186 static inline size_t
187 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data);
188
189 /**
190 * Append a tagged string (should be a valid UTF-8 coded string)
191 *
192 * This is a ccnb-encoded element with containing UDATA as content
193 *
194 * @param start start iterator of the buffer to append to.
195 * @param dtag is the element's dtab
196 * @param string UTF-8 string to be written
197 *
198 * @returns written length
199 */
200 static size_t
201 AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
202 const std::string &string);
203
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700204 /**
205 * @brief Estimate size of the string in CCNB encoding
206 * @param dtag is the element's dtab
207 * @param string UTF-8 string to be written
208 * @returns estimated length
209 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700210 static size_t
211 EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700212};
213
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700214
215template<class T>
216size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700217EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700218{
219 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
220}
221
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222} // namespace ndn
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700223} // namespace ns3
224
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700225#endif // _NDN_ENCODING_HELPER_H_
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700226