blob: 90b3f57038c10c19100a47e0a5ca118d1a6a30fa [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 Afanasyevf9f4eb02011-12-16 01:51:14 -080026#include "ccnb-parser/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 {
32
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070033class NdnNameComponents;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070034
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070035class NdnInterestHeader;
36class NdnContentObjectHeader;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070037
38/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070039 * \brief Helper to encode/decode ccnb formatted Ndn message
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070040 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041class NdnEncodingHelper
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070042{
43public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080044 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045 * \brief Serialize NdnInterestHeader to Buffer::Iterator
46 * @param start Buffer to store serialized NdnInterestHeader
47 * @param interest Pointer to NdnInterestHeader to be serialized
48 * @return length of serialized NdnInterestHeader
Ilya Moiseenko332add02011-12-24 17:21:25 -080049 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070050 static size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070051 Serialize (Buffer::Iterator start, const NdnInterestHeader &interest);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070052
Ilya Moiseenko332add02011-12-24 17:21:25 -080053 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070054 * \brief Compute the size of serialized NdnInterestHeader
55 * @param interest Pointer to NdnInterestHeader
Ilya Moiseenko332add02011-12-24 17:21:25 -080056 * @return length
57 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070058 static size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070059 GetSerializedSize (const NdnInterestHeader &interest);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070060
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070061public:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070062 /**
63 * @brief Append CCNB block header
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 * @param start Buffer to store serialized NdnInterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070065 * @param value dictionary id of the block header
66 * @param block_type Type of CCNB block
67 *
68 * @returns written length
69 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070070 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070071 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070072
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070073 /**
74 * @brief Estimate size of the CCNB block header
75 * @param value dictionary id of the block header
76 * @returns estimated length
77 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070078 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070079 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070080
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070081 /**
82 * @brief Add number in CCNB encoding
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070083 * @param start Buffer to store serialized NdnInterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070084 * @param number Number to be written
85 *
86 * @returns written length
87 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070088 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070089 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070090
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070091 /**
92 * @brief Estimate size of the number in CCNB encoding
93 * @param number Number to be written
94 * @returns estimated length
95 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070096 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070097 EstimateNumber (uint32_t number);
98
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070099 /**
100 * @brief Append CCNB closer tag (estimated size is 1)
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700101 * @param start Buffer to store serialized NdnInterestHeader
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700102 *
103 * @returns written length
104 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700105 static size_t
106 AppendCloser (Buffer::Iterator &start);
107
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700108 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700109 * @brief Append NdnNameComponents in CCNB encoding
110 * @param start Buffer to store serialized NdnInterestHeader
111 * @param name constant reference to NdnNameComponents object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700112 *
113 * @returns written length
114 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700115 static size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700116 AppendNameComponents (Buffer::Iterator &start, const NdnNameComponents &name);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700117
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700118 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700119 * @brief Estimate size of NdnNameComponents in CCNB encoding
120 * @param name constant reference to NdnNameComponents object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700121 * @returns estimated length
122 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700123 static size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700124 EstimateNameComponents (const NdnNameComponents &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700125
126 /**
127 * Append a binary timestamp as a BLOB using the ccn binary
128 * Timestamp representation (12-bit fraction).
129 *
130 * @param start start iterator of the buffer to append to.
131 * @param time - Time object
132 *
133 * @returns written length
134 */
135 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700136 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
137
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700138 /**
139 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
140 * @param time - Time object
141 * @returns estimated length
142 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700143 static size_t
144 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700145
146 /**
147 * Append a tagged BLOB
148 *
149 * This is a ccnb-encoded element with containing the BLOB as content
150 *
151 * @param start start iterator of the buffer to append to.
152 * @param dtag is the element's dtab
153 * @param data points to the binary data
154 * @param size is the size of the data, in bytes
155 *
156 * @returns written length
157 */
158 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700159 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700160 const uint8_t *data, size_t size);
161
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700162 /**
163 * @brief Estimate size of a tagged BLOB in CCNB enconding
164 * @param dtag is the element's dtab
165 * @param size is the size of the data, in bytes
166 * @returns estimated length
167 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700168 static size_t
169 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700170
171 /**
172 * Append value as a tagged BLOB (templated version)
173 *
174 * This is a ccnb-encoded element with containing the BLOB as content
175 *
176 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
177 *
178 * @param start start iterator of the buffer to append to.
179 * @param dtag is the element's dtab
180 * @param data a value to add
181 *
182 * @returns written length
183 */
184 template<class T>
185 static inline size_t
186 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data);
187
188 /**
189 * Append a tagged string (should be a valid UTF-8 coded string)
190 *
191 * This is a ccnb-encoded element with containing UDATA as content
192 *
193 * @param start start iterator of the buffer to append to.
194 * @param dtag is the element's dtab
195 * @param string UTF-8 string to be written
196 *
197 * @returns written length
198 */
199 static size_t
200 AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
201 const std::string &string);
202
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700203 /**
204 * @brief Estimate size of the string in CCNB encoding
205 * @param dtag is the element's dtab
206 * @param string UTF-8 string to be written
207 * @returns estimated length
208 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700209 static size_t
210 EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700211};
212
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700213
214template<class T>
215size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700216NdnEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700217{
218 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
219}
220
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700221} // namespace ns3
222
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700223#endif // _NDN_ENCODING_HELPER_H_
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700224