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 | */ |
| 42 | class CcnxEncodingHelper |
| 43 | { |
| 44 | public: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 45 | /** |
| 46 | * \brief Serialize CcnxInterestHeader to Buffer::Iterator |
| 47 | * @param start Buffer to store serialized CcnxInterestHeader |
| 48 | * @param interest Pointer to CcnxInterestHeader to be serialized |
| 49 | * @return length of serialized CcnxInterestHeader |
| 50 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 51 | static size_t |
| 52 | Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest); |
| 53 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 54 | /** |
| 55 | * \brief Compute the size of serialized CcnxInterestHeader |
| 56 | * @param interest Pointer to CcnxInterestHeader |
| 57 | * @return length |
| 58 | */ |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 59 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 60 | GetSerializedSize (const CcnxInterestHeader &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 | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 63 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 64 | AppendBlockHeader (Buffer::Iterator &start, size_t value, CcnbParser::ccn_tt block_type); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 65 | |
| 66 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 67 | EstimateBlockHeader (size_t value); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 68 | |
| 69 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 70 | AppendNumber (Buffer::Iterator &start, uint32_t number); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 71 | |
| 72 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 73 | EstimateNumber (uint32_t number); |
| 74 | |
| 75 | static size_t |
| 76 | AppendCloser (Buffer::Iterator &start); |
| 77 | |
| 78 | static size_t |
| 79 | AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name); |
| 80 | |
| 81 | static size_t |
| 82 | EstimateNameComponents (const CcnxNameComponents &name); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Append a binary timestamp as a BLOB using the ccn binary |
| 86 | * Timestamp representation (12-bit fraction). |
| 87 | * |
| 88 | * @param start start iterator of the buffer to append to. |
| 89 | * @param time - Time object |
| 90 | * |
| 91 | * @returns written length |
| 92 | */ |
| 93 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 94 | AppendTimestampBlob (Buffer::Iterator &start, const Time &time); |
| 95 | |
| 96 | static size_t |
| 97 | EstimateTimestampBlob (const Time &time); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Append a tagged BLOB |
| 101 | * |
| 102 | * This is a ccnb-encoded element with containing the BLOB as content |
| 103 | * |
| 104 | * @param start start iterator of the buffer to append to. |
| 105 | * @param dtag is the element's dtab |
| 106 | * @param data points to the binary data |
| 107 | * @param size is the size of the data, in bytes |
| 108 | * |
| 109 | * @returns written length |
| 110 | */ |
| 111 | static size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 112 | AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 113 | const uint8_t *data, size_t size); |
| 114 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 115 | static size_t |
| 116 | EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Append value as a tagged BLOB (templated version) |
| 120 | * |
| 121 | * This is a ccnb-encoded element with containing the BLOB as content |
| 122 | * |
| 123 | * Data will be reinterpret_cast<const uint8_t*> and size will be obtained using sizeof |
| 124 | * |
| 125 | * @param start start iterator of the buffer to append to. |
| 126 | * @param dtag is the element's dtab |
| 127 | * @param data a value to add |
| 128 | * |
| 129 | * @returns written length |
| 130 | */ |
| 131 | template<class T> |
| 132 | static inline size_t |
| 133 | AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data); |
| 134 | |
| 135 | /** |
| 136 | * Append a tagged string (should be a valid UTF-8 coded string) |
| 137 | * |
| 138 | * This is a ccnb-encoded element with containing UDATA as content |
| 139 | * |
| 140 | * @param start start iterator of the buffer to append to. |
| 141 | * @param dtag is the element's dtab |
| 142 | * @param string UTF-8 string to be written |
| 143 | * |
| 144 | * @returns written length |
| 145 | */ |
| 146 | static size_t |
| 147 | AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
| 148 | const std::string &string); |
| 149 | |
| 150 | static size_t |
| 151 | EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string); |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 154 | |
| 155 | template<class T> |
| 156 | size_t |
| 157 | CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, const T &data) |
| 158 | { |
| 159 | return AppendTaggedBlob (start, dtag, reinterpret_cast<const uint8_t*> (&data), sizeof (data)); |
| 160 | } |
| 161 | |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 162 | } // namespace ns3 |
| 163 | |
| 164 | #endif // _CCNX_ENCODING_HELPER_H_ |
| 165 | |