Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -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 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 21 | #include "block.h" |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 22 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 23 | #include "blob.h" |
| 24 | #include "udata.h" |
| 25 | #include "tag.h" |
| 26 | #include "dtag.h" |
| 27 | #include "attr.h" |
| 28 | #include "dattr.h" |
| 29 | #include "ext.h" |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 795f9b5 | 2011-11-21 11:47:35 -0800 | [diff] [blame] | 31 | #include "ns3/log.h" |
| 32 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 33 | NS_LOG_COMPONENT_DEFINE ("ndn.CcnbParser.Block"); |
Alexander Afanasyev | 795f9b5 | 2011-11-21 11:47:35 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 35 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 36 | namespace ndn { |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 37 | namespace CcnbParser { |
| 38 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 39 | /// @cond include_hidden |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 40 | const uint8_t CCN_TT_BITS = 3; |
| 41 | const uint8_t CCN_TT_MASK = ((1 << CCN_TT_BITS) - 1); |
| 42 | const uint8_t CCN_MAX_TINY= ((1 << (7-CCN_TT_BITS)) - 1); |
| 43 | const uint8_t CCN_TT_HBIT = ((uint8_t)(1 << 7)); |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 44 | /// @endcond |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 46 | // int Block::counter = 0; |
| 47 | |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 48 | Ptr<Block> Block::ParseBlock (Buffer::Iterator &start) |
| 49 | { |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 50 | // std::cout << "<< pos: " << counter << "\n"; |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 51 | uint32_t value = 0; |
| 52 | |
| 53 | // We will have problems if length field is more than 32 bits. Though it's really impossible |
| 54 | uint8_t byte = 0; |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 55 | while (!start.IsEnd() && !(byte & CCN_TT_HBIT)) |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 56 | { |
| 57 | value <<= 8; |
| 58 | value += byte; |
| 59 | byte = start.ReadU8 (); |
Alexander Afanasyev | e91ab75 | 2011-08-31 19:13:40 -0700 | [diff] [blame] | 60 | // Block::counter ++; |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 61 | } |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 62 | if (start.IsEnd()) |
| 63 | CcnbDecodingException (); |
| 64 | |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 65 | value <<= 4; |
| 66 | value += ( (byte&(~CCN_TT_HBIT)) >> 3); |
Alexander Afanasyev | 795f9b5 | 2011-11-21 11:47:35 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 85a3bca | 2011-08-31 16:51:03 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Huh. After fighting with NS-3, it became apparent that Create<T>(...) construct |
| 70 | * doesn't work with references. Just simply doesn't work. wtf? |
| 71 | */ |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 72 | switch (byte & CCN_TT_MASK) |
| 73 | { |
| 74 | case CCN_BLOB: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 75 | return Ptr<Blob> (new Blob(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 76 | case CCN_UDATA: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 77 | return Ptr<Udata> (new Udata(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 78 | case CCN_TAG: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 79 | return Ptr<Tag> (new Tag(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 80 | case CCN_ATTR: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 81 | return Ptr<Attr> (new Attr(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 82 | case CCN_DTAG: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 83 | return Ptr<Dtag> (new Dtag(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 84 | case CCN_DATTR: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 85 | return Ptr<Dattr> (new Dattr(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 86 | case CCN_EXT: |
Alexander Afanasyev | d02a5d6 | 2011-11-21 11:01:51 -0800 | [diff] [blame] | 87 | return Ptr<Ext> (new Ext(start, value), false); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 88 | default: |
Alexander Afanasyev | e709f3d | 2011-08-21 17:55:45 -0700 | [diff] [blame] | 89 | throw CcnbDecodingException (); |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Alexander Afanasyev | 795f9b5 | 2011-11-21 11:47:35 -0800 | [diff] [blame] | 93 | Block::~Block () |
| 94 | { |
Alexander Afanasyev | 795f9b5 | 2011-11-21 11:47:35 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | 8b37905 | 2011-08-21 16:58:20 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 99 | } |