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 | /** |
| 124 | * @brief Estimate size of a tagged BLOB in CCNB enconding |
| 125 | * @param dtag is the element's dtab |
| 126 | * @param size is the size of the data, in bytes |
| 127 | * @returns estimated length |
| 128 | */ |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 129 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 130 | EstimateTaggedBlob (uint32_t dtag, size_t size); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Append value as a tagged BLOB (templated version) |
| 134 | * |
| 135 | * This is a ccnb-encoded element with containing the BLOB as content |
| 136 | * |
| 137 | * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof |
| 138 | * |
| 139 | * @param start start iterator of the buffer to append to. |
| 140 | * @param dtag is the element's dtab |
| 141 | * @param data a value to add |
| 142 | * |
| 143 | * @returns written length |
| 144 | */ |
| 145 | template<class T> |
| 146 | static inline size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 147 | AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Append a tagged string (should be a valid UTF-8 coded string) |
| 151 | * |
| 152 | * This is a ccnb-encoded element with containing UDATA as content |
| 153 | * |
| 154 | * @param start start iterator of the buffer to append to. |
| 155 | * @param dtag is the element's dtab |
| 156 | * @param string UTF-8 string to be written |
| 157 | * |
| 158 | * @returns written length |
| 159 | */ |
| 160 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 161 | AppendString (Buffer::Iterator &start, uint32_t dtag, |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 162 | const std::string &string); |
| 163 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 164 | /** |
| 165 | * @brief Estimate size of the string in CCNB encoding |
| 166 | * @param dtag is the element's dtab |
| 167 | * @param string UTF-8 string to be written |
| 168 | * @returns estimated length |
| 169 | */ |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 170 | static size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 171 | EstimateString (uint32_t dtag, const std::string &string); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 172 | |
| 173 | //////////////////////////////// |
| 174 | // General use wire formatters |
| 175 | //////////////////////////////// |
| 176 | |
| 177 | /** |
| 178 | * @brief Append Name in CCNB encoding |
| 179 | * @param start Buffer to store serialized Interest |
| 180 | * @param name constant reference to Name object |
| 181 | * |
| 182 | * @returns written length |
| 183 | */ |
| 184 | static size_t |
| 185 | SerializeName (Buffer::Iterator &start, const Name &name); |
| 186 | |
| 187 | /** |
| 188 | * @brief Estimate size of Name in CCNB encoding |
| 189 | * @param name constant reference to Name object |
| 190 | * @returns estimated length |
| 191 | */ |
| 192 | static size_t |
| 193 | SerializedSizeName (const Name &name); |
| 194 | |
| 195 | /** |
| 196 | * @brief Deserialize Name from CCNB encodeing |
| 197 | * @param start Buffer that stores serialized Interest |
| 198 | * @param name Name object |
| 199 | */ |
| 200 | static Ptr<Name> |
| 201 | DeserializeName (Buffer::Iterator &start); |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 202 | }; // Ccnb |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 203 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 204 | |
| 205 | template<class T> |
| 206 | size_t |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 207 | Ccnb::AppendTaggedBlob (Buffer::Iterator &start, uint32_t dtag, const T &data) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 208 | { |
| 209 | return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 210 | } |
| 211 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 212 | } // wire |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 213 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 214 | NDN_NAMESPACE_END |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 215 | |
Alexander Afanasyev | 1043c70 | 2013-07-15 16:21:09 -0700 | [diff] [blame] | 216 | #endif // NDN_WIRE_CCNB_SYNTAX_H |