Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -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 | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 21 | #include "ndn-encoding-helper.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 23 | #include "ns3/ndn-name.h" |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 24 | #include "ns3/ndn-interest-header.h" |
| 25 | #include "ns3/ndn-content-object-header.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 26 | |
| 27 | #include <sstream> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 33 | size_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 34 | EncodingHelper::Serialize (Buffer::Iterator start, const Interest &interest) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 35 | { |
| 36 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 37 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 39 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 40 | written += AppendName (start, interest.GetName()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 41 | written += AppendCloser (start); // </Name> |
| 42 | |
| 43 | if (interest.GetMinSuffixComponents() >= 0) |
| 44 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 45 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 46 | written += AppendNumber (start, interest.GetMinSuffixComponents ()); |
| 47 | written += AppendCloser (start); |
| 48 | } |
| 49 | if (interest.GetMaxSuffixComponents() >= 0) |
| 50 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 51 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 52 | written += AppendNumber (start, interest.GetMaxSuffixComponents ()); |
| 53 | written += AppendCloser (start); |
| 54 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 55 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 56 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 57 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude> |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 58 | written += AppendName (start, interest.GetExclude()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 59 | written += AppendCloser (start); // </Exclude> |
| 60 | } |
| 61 | if (interest.IsEnabledChildSelector()) |
| 62 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 63 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 64 | written += AppendNumber (start, 1); |
| 65 | written += AppendCloser (start); |
| 66 | } |
| 67 | if (interest.IsEnabledAnswerOriginKind()) |
| 68 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 69 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 70 | written += AppendNumber (start, 1); |
| 71 | written += AppendCloser (start); |
| 72 | } |
| 73 | if (interest.GetScope() >= 0) |
| 74 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 75 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 76 | written += AppendNumber (start, interest.GetScope ()); |
| 77 | written += AppendCloser (start); |
| 78 | } |
| 79 | if (!interest.GetInterestLifetime().IsZero()) |
| 80 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 81 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 82 | written += AppendTimestampBlob (start, interest.GetInterestLifetime ()); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 83 | written += AppendCloser (start); |
| 84 | } |
| 85 | if (interest.GetNonce()>0) |
| 86 | { |
| 87 | uint32_t nonce = interest.GetNonce(); |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 88 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, nonce); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 89 | } |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 91 | if (interest.GetNack ()>0) |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 92 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 93 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG); |
| 94 | written += AppendNumber (start, interest.GetNack ()); |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 95 | written += AppendCloser (start); |
| 96 | } |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 97 | written += AppendCloser (start); // </Interest> |
| 98 | |
| 99 | return written; |
| 100 | } |
| 101 | |
| 102 | size_t |
Alexander Afanasyev | eae83ee | 2013-03-15 15:01:10 -0700 | [diff] [blame] | 103 | EncodingHelper::GetSerializedSize (const Interest &interest) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 104 | { |
| 105 | size_t written = 0; |
| 106 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest> |
| 107 | |
| 108 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 109 | written += EstimateName (interest.GetName()); // <Component>...</Component>... |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 110 | written += 1; // </Name> |
| 111 | |
| 112 | if (interest.GetMinSuffixComponents() >= 0) |
| 113 | { |
| 114 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents); |
| 115 | written += EstimateNumber (interest.GetMinSuffixComponents ()); |
| 116 | written += 1; |
| 117 | } |
| 118 | if (interest.GetMaxSuffixComponents() >= 0) |
| 119 | { |
| 120 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents); |
| 121 | written += EstimateNumber (interest.GetMaxSuffixComponents ()); |
| 122 | written += 1; |
| 123 | } |
| 124 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
| 125 | { |
| 126 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude); |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 127 | written += EstimateName (interest.GetExclude()); // <Component>...</Component>... |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 128 | written += 1; // </Exclude> |
| 129 | } |
| 130 | if (interest.IsEnabledChildSelector()) |
| 131 | { |
| 132 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector); |
| 133 | written += EstimateNumber (1); |
| 134 | written += 1; |
| 135 | } |
| 136 | if (interest.IsEnabledAnswerOriginKind()) |
| 137 | { |
| 138 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind); |
| 139 | written += EstimateNumber (1); |
| 140 | written += 1; |
| 141 | } |
| 142 | if (interest.GetScope() >= 0) |
| 143 | { |
| 144 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope); |
| 145 | written += EstimateNumber (interest.GetScope ()); |
| 146 | written += 1; |
| 147 | } |
| 148 | if (!interest.GetInterestLifetime().IsZero()) |
| 149 | { |
| 150 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime); |
| 151 | written += EstimateTimestampBlob (interest.GetInterestLifetime()); |
| 152 | written += 1; |
| 153 | } |
| 154 | if (interest.GetNonce()>0) |
| 155 | { |
| 156 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t)); |
| 157 | } |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 158 | if (interest.GetNack ()>0) |
Ilya Moiseenko | aa17c8e | 2011-10-28 13:04:30 -0700 | [diff] [blame] | 159 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 160 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Nack); |
| 161 | written += EstimateNumber (interest.GetNack ()); |
Ilya Moiseenko | aa17c8e | 2011-10-28 13:04:30 -0700 | [diff] [blame] | 162 | written += 1; |
| 163 | } |
| 164 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 165 | written += 1; // </Interest> |
| 166 | |
| 167 | return written; |
| 168 | } |
| 169 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 170 | ////////////////////////////////////////////////////////////////////// |
| 171 | ////////////////////////////////////////////////////////////////////// |
| 172 | ////////////////////////////////////////////////////////////////////// |
| 173 | |
| 174 | #define CCN_TT_BITS 3 |
| 175 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 176 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 177 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 178 | |
| 179 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 180 | EncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 181 | { |
| 182 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 183 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 184 | size_t n = 1; |
| 185 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 186 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 187 | (CCN_TT_MASK & tt); |
| 188 | val >>= (7-CCN_TT_BITS); |
| 189 | while (val != 0) { |
| 190 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 191 | n++; |
| 192 | val >>= 7; |
| 193 | } |
| 194 | start.Write (p,n); |
| 195 | return n; |
| 196 | } |
| 197 | |
| 198 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 199 | EncodingHelper::EstimateBlockHeader (size_t value) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 200 | { |
| 201 | value >>= (7-CCN_TT_BITS); |
| 202 | size_t n = 1; |
| 203 | while (value>0) |
| 204 | { |
| 205 | value >>= 7; |
| 206 | n++; |
| 207 | } |
| 208 | return n; |
| 209 | } |
| 210 | |
| 211 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 212 | EncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 213 | { |
| 214 | std::ostringstream os; |
| 215 | os << number; |
| 216 | |
| 217 | size_t written = 0; |
| 218 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 219 | written += os.str().size(); |
| 220 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 221 | |
| 222 | return written; |
| 223 | } |
| 224 | |
| 225 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 226 | EncodingHelper::EstimateNumber (uint32_t number) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 227 | { |
| 228 | std::ostringstream os; |
| 229 | os << number; |
| 230 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 231 | } |
| 232 | |
| 233 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 234 | EncodingHelper::AppendCloser (Buffer::Iterator &start) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 235 | { |
| 236 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | size_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 241 | EncodingHelper::AppendName (Buffer::Iterator &start, const Name &name) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 242 | { |
| 243 | size_t written = 0; |
| 244 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 245 | { |
| 246 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
| 247 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 248 | } |
| 249 | return written; |
| 250 | } |
| 251 | |
| 252 | size_t |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 253 | EncodingHelper::EstimateName (const Name &name) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 254 | { |
| 255 | size_t written = 0; |
| 256 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 257 | { |
| 258 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size()); |
| 259 | } |
| 260 | return written; |
| 261 | } |
| 262 | |
| 263 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 264 | EncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 265 | { |
| 266 | // the original function implements Markers... thought not sure what are these markers for... |
| 267 | |
| 268 | // Determine miminal number of bytes required to store the timestamp |
| 269 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 270 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 271 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 272 | required_bytes++; |
| 273 | |
| 274 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 275 | |
| 276 | // write part with seconds |
| 277 | ts = time.ToInteger (Time::S) >> 4; |
| 278 | for (int i = 0; i < required_bytes - 2; i++) |
| 279 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 280 | |
| 281 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 282 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 283 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 284 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 285 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 286 | |
| 287 | return len + required_bytes; |
| 288 | } |
| 289 | |
| 290 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 291 | EncodingHelper::EstimateTimestampBlob (const Time &time) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 292 | { |
| 293 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 294 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 295 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 296 | required_bytes++; |
| 297 | |
| 298 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 299 | } |
| 300 | |
| 301 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 302 | EncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 303 | const uint8_t *data, size_t size) |
| 304 | { |
| 305 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 306 | /* 2 */ |
| 307 | if (size>0) |
| 308 | { |
| 309 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 310 | start.Write (data, size); |
| 311 | written += size; |
| 312 | /* size */ |
| 313 | } |
| 314 | written += AppendCloser (start); |
| 315 | /* 1 */ |
| 316 | |
| 317 | return written; |
| 318 | } |
| 319 | |
| 320 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 321 | EncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size) |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 322 | { |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 323 | if (size>0) |
| 324 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 325 | else |
| 326 | return EstimateBlockHeader (dtag) + 1; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 329 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 330 | EncodingHelper::AppendString (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 331 | const std::string &string) |
| 332 | { |
| 333 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 334 | { |
| 335 | written += AppendBlockHeader (start, string.size (), CcnbParser::CCN_UDATA); |
| 336 | start.Write (reinterpret_cast<const uint8_t*> (string.c_str ()), string.size ()); |
| 337 | written += string.size (); |
| 338 | } |
| 339 | written += AppendCloser (start); |
| 340 | |
| 341 | return written; |
| 342 | } |
| 343 | |
| 344 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 345 | EncodingHelper::EstimateString (CcnbParser::ccn_dtag dtag, const std::string &string) |
Alexander Afanasyev | 8c5046a | 2012-06-05 16:22:14 -0700 | [diff] [blame] | 346 | { |
| 347 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (string.size ()) + string.size () + 1; |
| 348 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 349 | |
| 350 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 351 | } // namespace ndn |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 352 | } // namespace ns3 |