Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 1 | /* -*- 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 Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #ifndef _NDN_ENCODING_HELPER_H_ |
| 22 | #define _NDN_ENCODING_HELPER_H_ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 23 | |
| 24 | #include <sys/types.h> |
| 25 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 26 | #include "ccnb-parser/common.h" |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 27 | #include "ns3/ptr.h" |
| 28 | #include "ns3/nstime.h" |
| 29 | #include "ns3/buffer.h" |
| 30 | |
| 31 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 32 | namespace ndn { |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 34 | class NameComponents; |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | class InterestHeader; |
| 37 | class ContentObjectHeader; |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 40 | * \brief Helper to encode/decode ccnb formatted Ndn message |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 42 | class EncodingHelper |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 43 | { |
| 44 | public: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 45 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 46 | * \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 Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 50 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 51 | static size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 52 | Serialize (Buffer::Iterator start, const InterestHeader &interest); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 53 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 54 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 55 | * \brief Compute the size of serialized NdnInterestHeader |
| 56 | * @param interest Pointer to NdnInterestHeader |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 57 | * @return length |
| 58 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 59 | static size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 60 | GetSerializedSize (const InterestHeader &interest); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 62 | public: |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 63 | /** |
| 64 | * @brief Append CCNB block header |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 65 | * @param start Buffer to store serialized NdnInterestHeader |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 66 | * @param value dictionary id of the block header |
| 67 | * @param block_type Type of CCNB block |
| 68 | * |
| 69 | * @returns written length |
| 70 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 71 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 72 | AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 74 | /** |
| 75 | * @brief Estimate size of the CCNB block header |
| 76 | * @param value dictionary id of the block header |
| 77 | * @returns estimated length |
| 78 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 79 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 80 | EstimateBlockHeader (size_t value); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @brief Add number in CCNB encoding |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 84 | * @param start Buffer to store serialized NdnInterestHeader |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 85 | * @param number Number to be written |
| 86 | * |
| 87 | * @returns written length |
| 88 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 89 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 90 | AppendNumber (Buffer::Iterator &start, uint32_t number); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @brief Estimate size of the number in CCNB encoding |
| 94 | * @param number Number to be written |
| 95 | * @returns estimated length |
| 96 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 97 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 98 | EstimateNumber (uint32_t number); |
| 99 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 100 | /** |
| 101 | * @brief Append CCNB closer tag (estimated size is 1) |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 102 | * @param start Buffer to store serialized InterestHeader |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 103 | * |
| 104 | * @returns written length |
| 105 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 106 | static size_t |
| 107 | AppendCloser (Buffer::Iterator &start); |
| 108 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 109 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 110 | * @brief Append NameComponents in CCNB encoding |
| 111 | * @param start Buffer to store serialized InterestHeader |
| 112 | * @param name constant reference to NameComponents object |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 113 | * |
| 114 | * @returns written length |
| 115 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 116 | static size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 117 | AppendNameComponents (Buffer::Iterator &start, const NameComponents &name); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 119 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 120 | * @brief Estimate size of NameComponents in CCNB encoding |
| 121 | * @param name constant reference to NameComponents object |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 122 | * @returns estimated length |
| 123 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 124 | static size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 125 | EstimateNameComponents (const NameComponents &name); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 126 | |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 137 | AppendTimestampBlob (Buffer::Iterator &start, const Time &time); |
| 138 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 139 | /** |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 144 | static size_t |
| 145 | EstimateTimestampBlob (const Time &time); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 146 | |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 160 | AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 161 | const uint8_t *data, size_t size); |
| 162 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 163 | /** |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 169 | static size_t |
| 170 | EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 171 | |
| 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 Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 204 | /** |
| 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 Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 210 | static size_t |
| 211 | EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 214 | |
| 215 | template<class T> |
| 216 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 217 | EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 218 | { |
| 219 | return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 220 | } |
| 221 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 222 | } // namespace ndn |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 223 | } // namespace ns3 |
| 224 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 225 | #endif // _NDN_ENCODING_HELPER_H_ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 226 | |