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 | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 34 | class Name; |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 35 | typedef Name NameComponents; |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 37 | class Interest; |
| 38 | class ContentObject; |
Alexander Afanasyev | 73f06f6 | 2013-03-15 15:41:38 -0700 | [diff] [blame] | 39 | |
| 40 | typedef Interest InterestHeader; |
| 41 | typedef ContentObject ContentObjectHeader; |
| 42 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 43 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 44 | * \brief Helper to encode/decode ccnb formatted Ndn message |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 45 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 46 | class EncodingHelper |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 47 | { |
| 48 | public: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 49 | /** |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 50 | * \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 Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 54 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 55 | static size_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 56 | Serialize (Buffer::Iterator start, const Interest &interest); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 57 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 58 | /** |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 59 | * \brief Compute the size of serialized NdnInterest |
| 60 | * @param interest Pointer to NdnInterest |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 61 | * @return length |
| 62 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 63 | static size_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 64 | GetSerializedSize (const Interest &interest); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 66 | public: |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @brief Append CCNB block header |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 69 | * @param start Buffer to store serialized NdnInterest |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 70 | * @param value dictionary id of the block header |
| 71 | * @param block_type Type of CCNB block |
| 72 | * |
| 73 | * @returns written 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 | AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @brief Estimate size of the CCNB block header |
| 80 | * @param value dictionary id of the block header |
| 81 | * @returns estimated length |
| 82 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 83 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 84 | EstimateBlockHeader (size_t value); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @brief Add number in CCNB encoding |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 88 | * @param start Buffer to store serialized NdnInterest |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 89 | * @param number Number to be written |
| 90 | * |
| 91 | * @returns written length |
| 92 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 93 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 94 | AppendNumber (Buffer::Iterator &start, uint32_t number); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @brief Estimate size of the number in CCNB encoding |
| 98 | * @param number Number to be written |
| 99 | * @returns estimated length |
| 100 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 101 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 102 | EstimateNumber (uint32_t number); |
| 103 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @brief Append CCNB closer tag (estimated size is 1) |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 106 | * @param start Buffer to store serialized Interest |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 107 | * |
| 108 | * @returns written length |
| 109 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 110 | static size_t |
| 111 | AppendCloser (Buffer::Iterator &start); |
| 112 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 113 | /** |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 114 | * @brief Append Name in CCNB encoding |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 115 | * @param start Buffer to store serialized Interest |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 116 | * @param name constant reference to Name object |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 117 | * |
| 118 | * @returns written length |
| 119 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 120 | static size_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 121 | AppendName (Buffer::Iterator &start, const Name &name); |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 123 | /** |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 124 | * @brief Estimate size of Name in CCNB encoding |
| 125 | * @param name constant reference to Name object |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 126 | * @returns estimated length |
| 127 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 128 | static size_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 129 | EstimateName (const Name &name); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 130 | |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 141 | AppendTimestampBlob (Buffer::Iterator &start, const Time &time); |
| 142 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 143 | /** |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 148 | static size_t |
| 149 | EstimateTimestampBlob (const Time &time); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 150 | |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 164 | AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 165 | const uint8_t *data, size_t size); |
| 166 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 167 | /** |
| 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 Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 173 | static size_t |
| 174 | EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 175 | |
| 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 Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 208 | /** |
| 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 Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 214 | static size_t |
| 215 | EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 218 | |
| 219 | template<class T> |
| 220 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 221 | EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 222 | { |
| 223 | return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 224 | } |
| 225 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 226 | } // namespace ndn |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 227 | } // namespace ns3 |
| 228 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 229 | #endif // _NDN_ENCODING_HELPER_H_ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 230 | |