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