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 | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 21 | #include "ccnx-encoding-helper.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 22 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 23 | #include "ns3/ccnx-name-components.h" |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 24 | #include "ns3/ccnx-interest-header.h" |
| 25 | #include "ns3/ccnx-content-object-header.h" |
| 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 { |
| 31 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 32 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 33 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxInterestHeader &interest) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 34 | { |
| 35 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 36 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Interest, CcnbParser::CCN_DTAG); // <Interest> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 38 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 39 | written += AppendNameComponents (start, interest.GetName()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 40 | written += AppendCloser (start); // </Name> |
| 41 | |
| 42 | if (interest.GetMinSuffixComponents() >= 0) |
| 43 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 44 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MinSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 45 | written += AppendNumber (start, interest.GetMinSuffixComponents ()); |
| 46 | written += AppendCloser (start); |
| 47 | } |
| 48 | if (interest.GetMaxSuffixComponents() >= 0) |
| 49 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 50 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_MaxSuffixComponents, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 51 | written += AppendNumber (start, interest.GetMaxSuffixComponents ()); |
| 52 | written += AppendCloser (start); |
| 53 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 54 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 56 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Exclude, CcnbParser::CCN_DTAG); // <Exclude> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 57 | written += AppendNameComponents (start, interest.GetExclude()); // <Component>...</Component>... |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 58 | written += AppendCloser (start); // </Exclude> |
| 59 | } |
| 60 | if (interest.IsEnabledChildSelector()) |
| 61 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 62 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ChildSelector, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 63 | written += AppendNumber (start, 1); |
| 64 | written += AppendCloser (start); |
| 65 | } |
| 66 | if (interest.IsEnabledAnswerOriginKind()) |
| 67 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 68 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_AnswerOriginKind, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 69 | written += AppendNumber (start, 1); |
| 70 | written += AppendCloser (start); |
| 71 | } |
| 72 | if (interest.GetScope() >= 0) |
| 73 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 74 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Scope, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 75 | written += AppendNumber (start, interest.GetScope ()); |
| 76 | written += AppendCloser (start); |
| 77 | } |
| 78 | if (!interest.GetInterestLifetime().IsZero()) |
| 79 | { |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 80 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_InterestLifetime, CcnbParser::CCN_DTAG); |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 81 | written += AppendTimestampBlob (start, interest.GetInterestLifetime ()); |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 82 | written += AppendCloser (start); |
| 83 | } |
| 84 | if (interest.GetNonce()>0) |
| 85 | { |
| 86 | uint32_t nonce = interest.GetNonce(); |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 87 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Nonce, |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 88 | reinterpret_cast<const uint8_t*>(&nonce), |
| 89 | sizeof(nonce)); |
| 90 | } |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 92 | if (interest.GetNack ()>0) |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 93 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 94 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Nack, CcnbParser::CCN_DTAG); |
| 95 | written += AppendNumber (start, interest.GetNack ()); |
Ilya Moiseenko | d1f1951 | 2011-11-16 14:31:19 -0800 | [diff] [blame] | 96 | written += AppendCloser (start); |
| 97 | } |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 98 | written += AppendCloser (start); // </Interest> |
| 99 | |
| 100 | return written; |
| 101 | } |
| 102 | |
| 103 | size_t |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 104 | CcnxEncodingHelper::GetSerializedSize (const CcnxInterestHeader &interest) |
| 105 | { |
| 106 | size_t written = 0; |
| 107 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Interest); // <Interest> |
| 108 | |
| 109 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 110 | written += EstimateNameComponents (interest.GetName()); // <Component>...</Component>... |
| 111 | written += 1; // </Name> |
| 112 | |
| 113 | if (interest.GetMinSuffixComponents() >= 0) |
| 114 | { |
| 115 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MinSuffixComponents); |
| 116 | written += EstimateNumber (interest.GetMinSuffixComponents ()); |
| 117 | written += 1; |
| 118 | } |
| 119 | if (interest.GetMaxSuffixComponents() >= 0) |
| 120 | { |
| 121 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_MaxSuffixComponents); |
| 122 | written += EstimateNumber (interest.GetMaxSuffixComponents ()); |
| 123 | written += 1; |
| 124 | } |
| 125 | if (interest.IsEnabledExclude() && interest.GetExclude().size() > 0) |
| 126 | { |
| 127 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Exclude); |
| 128 | written += EstimateNameComponents (interest.GetExclude()); // <Component>...</Component>... |
| 129 | written += 1; // </Exclude> |
| 130 | } |
| 131 | if (interest.IsEnabledChildSelector()) |
| 132 | { |
| 133 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ChildSelector); |
| 134 | written += EstimateNumber (1); |
| 135 | written += 1; |
| 136 | } |
| 137 | if (interest.IsEnabledAnswerOriginKind()) |
| 138 | { |
| 139 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_AnswerOriginKind); |
| 140 | written += EstimateNumber (1); |
| 141 | written += 1; |
| 142 | } |
| 143 | if (interest.GetScope() >= 0) |
| 144 | { |
| 145 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Scope); |
| 146 | written += EstimateNumber (interest.GetScope ()); |
| 147 | written += 1; |
| 148 | } |
| 149 | if (!interest.GetInterestLifetime().IsZero()) |
| 150 | { |
| 151 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_InterestLifetime); |
| 152 | written += EstimateTimestampBlob (interest.GetInterestLifetime()); |
| 153 | written += 1; |
| 154 | } |
| 155 | if (interest.GetNonce()>0) |
| 156 | { |
| 157 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Nonce, sizeof(uint32_t)); |
| 158 | } |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 159 | if (interest.GetNack ()>0) |
Ilya Moiseenko | aa17c8e | 2011-10-28 13:04:30 -0700 | [diff] [blame] | 160 | { |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame] | 161 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Nack); |
| 162 | written += EstimateNumber (interest.GetNack ()); |
Ilya Moiseenko | aa17c8e | 2011-10-28 13:04:30 -0700 | [diff] [blame] | 163 | written += 1; |
| 164 | } |
| 165 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 166 | written += 1; // </Interest> |
| 167 | |
| 168 | return written; |
| 169 | } |
| 170 | |
| 171 | size_t |
Alexander Afanasyev | 834f35c | 2011-08-16 17:13:50 -0700 | [diff] [blame] | 172 | CcnxEncodingHelper::Serialize (Buffer::Iterator start, const CcnxContentObjectHeader &contentObject) |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 173 | { |
| 174 | size_t written = 0; |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 175 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_ContentObject, CcnbParser::CCN_DTAG); // <ContentObject> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 176 | |
| 177 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 178 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Signature, CcnbParser::CCN_DTAG); // <Signature> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 179 | // Signature ::= DigestAlgorithm? |
| 180 | // Witness? |
| 181 | // SignatureBits |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 182 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_SignatureBits, 0, 0); // <SignatureBits /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 183 | written += AppendCloser (start); // </Signature> |
| 184 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 185 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Name, CcnbParser::CCN_DTAG); // <Name> |
Alexander Afanasyev | 2a5df20 | 2011-08-15 22:39:05 -0700 | [diff] [blame] | 186 | written += AppendNameComponents (start, contentObject.GetName()); // <Component>...</Component>... |
| 187 | written += AppendCloser (start); // </Name> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 188 | |
| 189 | // fake signature |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 190 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_SignedInfo, CcnbParser::CCN_DTAG); // <SignedInfo> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 191 | // SignedInfo ::= PublisherPublicKeyDigest |
| 192 | // Timestamp |
| 193 | // Type? |
| 194 | // FreshnessSeconds? |
| 195 | // FinalBlockID? |
| 196 | // KeyLocator? |
Alexander Afanasyev | 9568f95 | 2012-04-05 16:09:14 -0700 | [diff] [blame] | 197 | if (!contentObject.GetTimestamp ().IsZero()) |
| 198 | { |
| 199 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Timestamp, CcnbParser::CCN_DTAG); |
| 200 | written += AppendTimestampBlob (start, contentObject.GetTimestamp ()); |
| 201 | written += AppendCloser (start); |
| 202 | } |
| 203 | if (contentObject.GetFreshness () >= Seconds(0)) |
| 204 | { |
| 205 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_FreshnessSeconds, CcnbParser::CCN_DTAG); |
| 206 | written += AppendNumber (start, contentObject.GetFreshness ().ToInteger (Time::S)); |
| 207 | written += AppendCloser (start); |
| 208 | } |
| 209 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 210 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0, 0); // <PublisherPublicKeyDigest /> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 211 | written += AppendCloser (start); // </SignedInfo> |
| 212 | |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 213 | written += AppendBlockHeader (start, CcnbParser::CCN_DTAG_Content, CcnbParser::CCN_DTAG); // <Content> |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 214 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 215 | // there are no closing tags !!! |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 216 | return written; |
| 217 | } |
| 218 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 219 | size_t |
| 220 | CcnxEncodingHelper::GetSerializedSize (const CcnxContentObjectHeader &contentObject) |
| 221 | { |
| 222 | size_t written = 0; |
| 223 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_ContentObject); // <ContentObject> |
| 224 | |
| 225 | // fake signature |
| 226 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Signature); // <Signature> |
| 227 | // Signature ::= DigestAlgorithm? |
| 228 | // Witness? |
| 229 | // SignatureBits |
| 230 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_SignatureBits, 0); // <SignatureBits /> |
| 231 | written += 1; // </Signature> |
| 232 | |
| 233 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Name); // <Name> |
| 234 | written += EstimateNameComponents (contentObject.GetName()); // <Component>...</Component>... |
| 235 | written += 1; // </Name> |
| 236 | |
| 237 | // fake signature |
| 238 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_SignedInfo); // <SignedInfo> |
| 239 | // SignedInfo ::= PublisherPublicKeyDigest |
| 240 | // Timestamp |
| 241 | // Type? |
| 242 | // FreshnessSeconds? |
| 243 | // FinalBlockID? |
| 244 | // KeyLocator? |
Alexander Afanasyev | 9568f95 | 2012-04-05 16:09:14 -0700 | [diff] [blame] | 245 | if (!contentObject.GetTimestamp ().IsZero()) |
| 246 | { |
| 247 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Timestamp); |
| 248 | written += EstimateTimestampBlob (contentObject.GetTimestamp ()); |
| 249 | written += 1; |
| 250 | } |
| 251 | if (contentObject.GetFreshness () >= Seconds(0)) |
| 252 | { |
| 253 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_FreshnessSeconds); |
| 254 | written += EstimateNumber (contentObject.GetFreshness ().ToInteger (Time::S)); |
| 255 | written += 1; |
| 256 | } |
| 257 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 258 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_PublisherPublicKeyDigest, 0); // <PublisherPublicKeyDigest /> |
| 259 | written += 1; // </SignedInfo> |
| 260 | |
| 261 | written += EstimateBlockHeader (CcnbParser::CCN_DTAG_Content); // <Content> |
| 262 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 263 | // there are no closing tags !!! |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 264 | return written; |
| 265 | } |
| 266 | |
| 267 | ////////////////////////////////////////////////////////////////////// |
| 268 | ////////////////////////////////////////////////////////////////////// |
| 269 | ////////////////////////////////////////////////////////////////////// |
| 270 | |
| 271 | #define CCN_TT_BITS 3 |
| 272 | #define CCN_TT_MASK ((1 << CCN_TT_BITS) - 1) |
| 273 | #define CCN_MAX_TINY ((1 << (7-CCN_TT_BITS)) - 1) |
| 274 | #define CCN_TT_HBIT ((unsigned char)(1 << 7)) |
| 275 | |
| 276 | size_t |
| 277 | CcnxEncodingHelper::AppendBlockHeader (Buffer::Iterator &start, size_t val, CcnbParser::ccn_tt tt) |
| 278 | { |
| 279 | unsigned char buf[1+8*((sizeof(val)+6)/7)]; |
| 280 | unsigned char *p = &(buf[sizeof(buf)-1]); |
| 281 | size_t n = 1; |
| 282 | p[0] = (CCN_TT_HBIT & ~CcnbParser::CCN_CLOSE) | |
| 283 | ((val & CCN_MAX_TINY) << CCN_TT_BITS) | |
| 284 | (CCN_TT_MASK & tt); |
| 285 | val >>= (7-CCN_TT_BITS); |
| 286 | while (val != 0) { |
| 287 | (--p)[0] = (((unsigned char)val) & ~CCN_TT_HBIT) | CcnbParser::CCN_CLOSE; |
| 288 | n++; |
| 289 | val >>= 7; |
| 290 | } |
| 291 | start.Write (p,n); |
| 292 | return n; |
| 293 | } |
| 294 | |
| 295 | size_t |
| 296 | CcnxEncodingHelper::EstimateBlockHeader (size_t value) |
| 297 | { |
| 298 | value >>= (7-CCN_TT_BITS); |
| 299 | size_t n = 1; |
| 300 | while (value>0) |
| 301 | { |
| 302 | value >>= 7; |
| 303 | n++; |
| 304 | } |
| 305 | return n; |
| 306 | } |
| 307 | |
| 308 | size_t |
| 309 | CcnxEncodingHelper::AppendNumber (Buffer::Iterator &start, uint32_t number) |
| 310 | { |
| 311 | std::ostringstream os; |
| 312 | os << number; |
| 313 | |
| 314 | size_t written = 0; |
| 315 | written += AppendBlockHeader (start, os.str().size(), CcnbParser::CCN_UDATA); |
| 316 | written += os.str().size(); |
| 317 | start.Write (reinterpret_cast<const unsigned char*>(os.str().c_str()), os.str().size()); |
| 318 | |
| 319 | return written; |
| 320 | } |
| 321 | |
| 322 | size_t |
| 323 | CcnxEncodingHelper::EstimateNumber (uint32_t number) |
| 324 | { |
| 325 | std::ostringstream os; |
| 326 | os << number; |
| 327 | return EstimateBlockHeader (os.str ().size ()) + os.str ().size (); |
| 328 | } |
| 329 | |
| 330 | size_t |
| 331 | CcnxEncodingHelper::AppendCloser (Buffer::Iterator &start) |
| 332 | { |
| 333 | start.WriteU8 (CcnbParser::CCN_CLOSE); |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | size_t |
| 338 | CcnxEncodingHelper::AppendNameComponents (Buffer::Iterator &start, const CcnxNameComponents &name) |
| 339 | { |
| 340 | size_t written = 0; |
| 341 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 342 | { |
| 343 | written += AppendTaggedBlob (start, CcnbParser::CCN_DTAG_Component, |
| 344 | reinterpret_cast<const uint8_t*>(component.c_str()), component.size()); |
| 345 | } |
| 346 | return written; |
| 347 | } |
| 348 | |
| 349 | size_t |
| 350 | CcnxEncodingHelper::EstimateNameComponents (const CcnxNameComponents &name) |
| 351 | { |
| 352 | size_t written = 0; |
| 353 | BOOST_FOREACH (const std::string &component, name.GetComponents()) |
| 354 | { |
| 355 | written += EstimateTaggedBlob (CcnbParser::CCN_DTAG_Component, component.size()); |
| 356 | } |
| 357 | return written; |
| 358 | } |
| 359 | |
| 360 | size_t |
| 361 | CcnxEncodingHelper::AppendTimestampBlob (Buffer::Iterator &start, const Time &time) |
| 362 | { |
| 363 | // the original function implements Markers... thought not sure what are these markers for... |
| 364 | |
| 365 | // Determine miminal number of bytes required to store the timestamp |
| 366 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 367 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 368 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 369 | required_bytes++; |
| 370 | |
| 371 | size_t len = AppendBlockHeader(start, required_bytes, CcnbParser::CCN_BLOB); |
| 372 | |
| 373 | // write part with seconds |
| 374 | ts = time.ToInteger (Time::S) >> 4; |
| 375 | for (int i = 0; i < required_bytes - 2; i++) |
| 376 | start.WriteU8 ( ts >> (8 * (required_bytes - 3 - i)) ); |
| 377 | |
| 378 | /* arithmetic contortions are to avoid overflowing 31 bits */ |
| 379 | ts = ((time.ToInteger (Time::S) & 15) << 12) + |
| 380 | (((time.ToInteger (Time::NS) % 1000000000) / 5 * 8 + 195312) / 390625); |
| 381 | for (int i = required_bytes - 2; i < required_bytes; i++) |
| 382 | start.WriteU8 ( ts >> (8 * (required_bytes - 1 - i)) ); |
| 383 | |
| 384 | return len + required_bytes; |
| 385 | } |
| 386 | |
| 387 | size_t |
| 388 | CcnxEncodingHelper::EstimateTimestampBlob (const Time &time) |
| 389 | { |
| 390 | int required_bytes = 2; // 12 bits for fractions of a second, 4 bits left for seconds. Sometimes it is enough |
| 391 | intmax_t ts = time.ToInteger (Time::S) >> 4; |
| 392 | for (; required_bytes < 7 && ts != 0; ts >>= 8) // not more than 6 bytes? |
| 393 | required_bytes++; |
| 394 | |
| 395 | return EstimateBlockHeader (required_bytes) + required_bytes; |
| 396 | } |
| 397 | |
| 398 | size_t |
| 399 | CcnxEncodingHelper::AppendTaggedBlob (Buffer::Iterator &start, CcnbParser::ccn_dtag dtag, |
| 400 | const uint8_t *data, size_t size) |
| 401 | { |
| 402 | size_t written = AppendBlockHeader (start, dtag, CcnbParser::CCN_DTAG); |
| 403 | /* 2 */ |
| 404 | if (size>0) |
| 405 | { |
| 406 | written += AppendBlockHeader (start, size, CcnbParser::CCN_BLOB); |
| 407 | start.Write (data, size); |
| 408 | written += size; |
| 409 | /* size */ |
| 410 | } |
| 411 | written += AppendCloser (start); |
| 412 | /* 1 */ |
| 413 | |
| 414 | return written; |
| 415 | } |
| 416 | |
| 417 | size_t |
| 418 | CcnxEncodingHelper::EstimateTaggedBlob (CcnbParser::ccn_dtag dtag, size_t size) |
| 419 | { |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 420 | if (size>0) |
| 421 | return EstimateBlockHeader (dtag) + EstimateBlockHeader (size) + size + 1; |
| 422 | else |
| 423 | return EstimateBlockHeader (dtag) + 1; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | |
Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 428 | } // namespace ns3 |