blob: d28425c26279e670873c0027049d297cccffe515 [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 Afanasyevcfdc14f2013-03-15 14:38:44 -070034class Name;
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070035typedef Name NameComponents;
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070036
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070037class Interest;
38class ContentObject;
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070039
40typedef Interest InterestHeader;
41typedef ContentObject ContentObjectHeader;
42
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070043/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 * \brief Helper to encode/decode ccnb formatted Ndn message
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070045 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046class EncodingHelper
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070047{
48public:
Ilya Moiseenko332add02011-12-24 17:21:25 -080049 /**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070050 * \brief Serialize NdnInterest to Buffer::Iterator
51 * @param start Buffer to store serialized NdnInterest
52 * @param interest Pointer to NdnInterest to be serialized
53 * @return length of serialized NdnInterest
Ilya Moiseenko332add02011-12-24 17:21:25 -080054 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070055 static size_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070056 Serialize (Buffer::Iterator start, const Interest &interest);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070057
Ilya Moiseenko332add02011-12-24 17:21:25 -080058 /**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070059 * \brief Compute the size of serialized NdnInterest
60 * @param interest Pointer to NdnInterest
Ilya Moiseenko332add02011-12-24 17:21:25 -080061 * @return length
62 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070063 static size_t
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070064 GetSerializedSize (const Interest &interest);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070065
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -070066public:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070067 /**
68 * @brief Append CCNB block header
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070069 * @param start Buffer to store serialized NdnInterest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070070 * @param value dictionary id of the block header
71 * @param block_type Type of CCNB block
72 *
73 * @returns written length
74 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070075 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070076 AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070077
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070078 /**
79 * @brief Estimate size of the CCNB block header
80 * @param value dictionary id of the block header
81 * @returns estimated length
82 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070083 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070084 EstimateBlockHeader (size_t value);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070085
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070086 /**
87 * @brief Add number in CCNB encoding
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070088 * @param start Buffer to store serialized NdnInterest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070089 * @param number Number to be written
90 *
91 * @returns written length
92 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070093 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -070094 AppendNumber (Buffer::Iterator &start, uint32_t number);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -070095
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070096 /**
97 * @brief Estimate size of the number in CCNB encoding
98 * @param number Number to be written
99 * @returns estimated length
100 */
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700101 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700102 EstimateNumber (uint32_t number);
103
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700104 /**
105 * @brief Append CCNB closer tag (estimated size is 1)
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700106 * @param start Buffer to store serialized Interest
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700107 *
108 * @returns written length
109 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700110 static size_t
111 AppendCloser (Buffer::Iterator &start);
112
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700113 /**
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700114 * @brief Append Name in CCNB encoding
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700115 * @param start Buffer to store serialized Interest
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700116 * @param name constant reference to Name object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700117 *
118 * @returns written length
119 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700120 static size_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700121 AppendName (Buffer::Iterator &start, const Name &name);
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700122
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700123 /**
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700124 * @brief Estimate size of Name in CCNB encoding
125 * @param name constant reference to Name object
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700126 * @returns estimated length
127 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700128 static size_t
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700129 EstimateName (const Name &name);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700130
131 /**
132 * Append a binary timestamp as a BLOB using the ccn binary
133 * Timestamp representation (12-bit fraction).
134 *
135 * @param start start iterator of the buffer to append to.
136 * @param time - Time object
137 *
138 * @returns written length
139 */
140 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700141 AppendTimestampBlob (Buffer::Iterator &start, const Time &time);
142
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700143 /**
144 * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding
145 * @param time - Time object
146 * @returns estimated length
147 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700148 static size_t
149 EstimateTimestampBlob (const Time &time);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700150
151 /**
152 * Append a tagged BLOB
153 *
154 * This is a ccnb-encoded element with containing the BLOB as content
155 *
156 * @param start start iterator of the buffer to append to.
157 * @param dtag is the element's dtab
158 * @param data points to the binary data
159 * @param size is the size of the data, in bytes
160 *
161 * @returns written length
162 */
163 static size_t
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700164 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700165 const uint8_t *data, size_t size);
166
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700167 /**
168 * @brief Estimate size of a tagged BLOB in CCNB enconding
169 * @param dtag is the element's dtab
170 * @param size is the size of the data, in bytes
171 * @returns estimated length
172 */
Alexander Afanasyev85a3bca2011-08-31 16:51:03 -0700173 static size_t
174 EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size);
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700175
176 /**
177 * Append value as a tagged BLOB (templated version)
178 *
179 * This is a ccnb-encoded element with containing the BLOB as content
180 *
181 * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof
182 *
183 * @param start start iterator of the buffer to append to.
184 * @param dtag is the element's dtab
185 * @param data a value to add
186 *
187 * @returns written length
188 */
189 template<class T>
190 static inline size_t
191 AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data);
192
193 /**
194 * Append a tagged string (should be a valid UTF-8 coded string)
195 *
196 * This is a ccnb-encoded element with containing UDATA as content
197 *
198 * @param start start iterator of the buffer to append to.
199 * @param dtag is the element's dtab
200 * @param string UTF-8 string to be written
201 *
202 * @returns written length
203 */
204 static size_t
205 AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag,
206 const std::string &string);
207
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700208 /**
209 * @brief Estimate size of the string in CCNB encoding
210 * @param dtag is the element's dtab
211 * @param string UTF-8 string to be written
212 * @returns estimated length
213 */
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700214 static size_t
215 EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string);
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700216};
217
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700218
219template<class T>
220size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700221EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data)
Alexander Afanasyev8c5046a2012-06-05 16:22:14 -0700222{
223 return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data));
224}
225
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700226} // namespace ndn
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700227} // namespace ns3
228
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700229#endif // _NDN_ENCODING_HELPER_H_
Alexander Afanasyev834f35c2011-08-16 17:13:50 -0700230