Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011-2013 University of California, Los Angeles |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 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 | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 21 | #ifndef NDN_WIRE_CCNB_SYNTAX_H |
| 22 | #define NDN_WIRE_CCNB_SYNTAX_H |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 24 | #include "ns3/ptr.h" |
| 25 | #include "ns3/nstime.h" |
| 26 | #include "ns3/buffer.h" |
| 27 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 28 | #include "ns3/ndn-common.h" |
| 29 | #include "ns3/ndn-name.h" |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 31 | NDN_NAMESPACE_BEGIN |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 33 | namespace wire { |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 35 | /** |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 36 | * \brief Helper to encode CCNb blocks |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 37 | */ |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 38 | class Ccnb |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 41 | /** |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 42 | * @brief Append CCNB block header |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 43 | * @param start Buffer to store serialized |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 44 | * @param value dictionary id of the block header |
| 45 | * @param block_type Type of CCNB block |
| 46 | * |
| 47 | * @returns written length |
| 48 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 49 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 50 | AppendBlockHeader (Buffer::Iterator &start, size_t value, uint32_t block_type); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 52 | /** |
| 53 | * @brief Estimate size of the CCNB block header |
| 54 | * @param value dictionary id of the block header |
| 55 | * @returns estimated length |
| 56 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 57 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 58 | EstimateBlockHeader (size_t value); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 60 | /** |
| 61 | * @brief Add number in CCNB encoding |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 62 | * @param start Buffer to store serialized NdnInterest |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 63 | * @param number Number to be written |
| 64 | * |
| 65 | * @returns written length |
| 66 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 67 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 68 | AppendNumber (Buffer::Iterator &start, uint32_t number); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 70 | /** |
| 71 | * @brief Estimate size of the number in CCNB encoding |
| 72 | * @param number Number to be written |
| 73 | * @returns estimated length |
| 74 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 75 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 76 | EstimateNumber (uint32_t number); |
| 77 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @brief Append CCNB closer tag (estimated size is 1) |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 80 | * @param start Buffer to store serialized Interest |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 81 | * |
| 82 | * @returns written length |
| 83 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 84 | static size_t |
| 85 | AppendCloser (Buffer::Iterator &start); |
| 86 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 87 | /** |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 88 | * Append a binary timestamp as a BLOB using the ccn binary |
| 89 | * Timestamp representation (12-bit fraction). |
| 90 | * |
| 91 | * @param start start iterator of the buffer to append to. |
| 92 | * @param time - Time object |
| 93 | * |
| 94 | * @returns written length |
| 95 | */ |
| 96 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 97 | AppendTimestampBlob (Buffer::Iterator &start, const Time &time); |
| 98 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 99 | /** |
| 100 | * @brief Estimate size of a binary timestamp as a BLOB using CCNB enconding |
| 101 | * @param time - Time object |
| 102 | * @returns estimated length |
| 103 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 104 | static size_t |
| 105 | EstimateTimestampBlob (const Time &time); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * Append a tagged BLOB |
| 109 | * |
| 110 | * This is a ccnb-encoded element with containing the BLOB as content |
| 111 | * |
| 112 | * @param start start iterator of the buffer to append to. |
| 113 | * @param dtag is the element's dtab |
| 114 | * @param data points to the binary data |
| 115 | * @param size is the size of the data, in bytes |
| 116 | * |
| 117 | * @returns written length |
| 118 | */ |
| 119 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 120 | AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 121 | const uint8_t *data, size_t size); |
| 122 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 123 | /** |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 124 | * Append a tagged BLOB, adding 0-byte padding if necessary |
| 125 | * |
| 126 | * This is a ccnb-encoded element with containing the BLOB as content |
| 127 | * |
| 128 | * @param start start iterator of the buffer to append to. |
| 129 | * @param dtag is the element's dtab |
| 130 | * @param length minimum required length of the added field (padding added if necessary) |
| 131 | * @param data points to the binary data |
| 132 | * @param size is the size of the data, in bytes |
| 133 | * |
| 134 | * @returns written length |
| 135 | */ |
| 136 | static size_t |
| 137 | AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, |
| 138 | uint32_t length, |
| 139 | const uint8_t *data, size_t size); |
| 140 | |
| 141 | /** |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 142 | * @brief Estimate size of a tagged BLOB in CCNB enconding |
| 143 | * @param dtag is the element's dtab |
| 144 | * @param size is the size of the data, in bytes |
| 145 | * @returns estimated length |
| 146 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 147 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 148 | EstimateTaggedBlob (uint32_t dtag, size_t size); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Append value as a tagged BLOB (templated version) |
| 152 | * |
| 153 | * This is a ccnb-encoded element with containing the BLOB as content |
| 154 | * |
| 155 | * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof |
| 156 | * |
| 157 | * @param start start iterator of the buffer to append to. |
| 158 | * @param dtag is the element's dtab |
| 159 | * @param data a value to add |
| 160 | * |
| 161 | * @returns written length |
| 162 | */ |
| 163 | template<class T> |
| 164 | static inline size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 165 | AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 166 | |
| 167 | /** |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 168 | * Append value as a tagged BLOB (templated version), add 0-padding if necessary |
| 169 | * |
| 170 | * This is a ccnb-encoded element with containing the BLOB as content |
| 171 | * |
| 172 | * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof |
| 173 | * |
| 174 | * @param start start iterator of the buffer to append to. |
| 175 | * @param dtag is the element's dtab |
| 176 | * @param length minimum required length of the field |
| 177 | * @param data a value to add |
| 178 | * |
| 179 | * @returns written length |
| 180 | */ |
| 181 | template<class T> |
| 182 | static inline size_t |
| 183 | AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, uint32_t length, const T &data); |
| 184 | |
| 185 | /** |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 186 | * Append a tagged string (should be a valid UTF-8 coded string) |
| 187 | * |
| 188 | * This is a ccnb-encoded element with containing UDATA as content |
| 189 | * |
| 190 | * @param start start iterator of the buffer to append to. |
| 191 | * @param dtag is the element's dtab |
| 192 | * @param string UTF-8 string to be written |
| 193 | * |
| 194 | * @returns written length |
| 195 | */ |
| 196 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 197 | AppendString (Buffer::Iterator &start, uint32_t dtag, |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 198 | const std::string &string); |
| 199 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 200 | /** |
| 201 | * @brief Estimate size of the string in CCNB encoding |
| 202 | * @param dtag is the element's dtab |
| 203 | * @param string UTF-8 string to be written |
| 204 | * @returns estimated length |
| 205 | */ |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 206 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 207 | EstimateString (uint32_t dtag, const std::string &string); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 208 | |
| 209 | //////////////////////////////// |
| 210 | // General use wire formatters |
| 211 | //////////////////////////////// |
| 212 | |
| 213 | /** |
| 214 | * @brief Append Name in CCNB encoding |
| 215 | * @param start Buffer to store serialized Interest |
| 216 | * @param name constant reference to Name object |
| 217 | * |
| 218 | * @returns written length |
| 219 | */ |
| 220 | static size_t |
| 221 | SerializeName (Buffer::Iterator &start, const Name &name); |
| 222 | |
| 223 | /** |
| 224 | * @brief Estimate size of Name in CCNB encoding |
| 225 | * @param name constant reference to Name object |
| 226 | * @returns estimated length |
| 227 | */ |
| 228 | static size_t |
| 229 | SerializedSizeName (const Name &name); |
| 230 | |
| 231 | /** |
| 232 | * @brief Deserialize Name from CCNB encodeing |
| 233 | * @param start Buffer that stores serialized Interest |
| 234 | * @param name Name object |
| 235 | */ |
| 236 | static Ptr<Name> |
| 237 | DeserializeName (Buffer::Iterator &start); |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 238 | }; // Ccnb |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 239 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 240 | |
| 241 | template<class T> |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 242 | inline size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 243 | Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 244 | { |
| 245 | return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 246 | } |
| 247 | |
Alexander Afanasyev | 0aa47bf | 2013-07-30 10:55:36 -0700 | [diff] [blame] | 248 | template<class T> |
| 249 | inline size_t |
| 250 | Ccnb::AppendTaggedBlobWithPadding (Buffer::Iterator &start, uint32_t dtag, uint32_t length, const T &data) |
| 251 | { |
| 252 | return AppendTaggedBlobWithPadding (start, dtag, length, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 253 | } |
| 254 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 255 | } // wire |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 256 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 257 | NDN_NAMESPACE_END |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 259 | #endif // NDN_WIRE_CCNB_SYNTAX_H |