Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 1 | /** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * GNU 3.0 license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "wire-ndnsim.h" |
| 12 | #include <boost/foreach.hpp> |
| 13 | |
| 14 | NDN_NAMESPACE_BEGIN |
| 15 | |
| 16 | namespace wire { |
| 17 | |
| 18 | ////////////////////////////////////////////////////////////////////// |
| 19 | ////////////////////////////////////////////////////////////////////// |
| 20 | ////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | size_t |
| 23 | NdnSim::SerializeName (Buffer::Iterator &i, const Name &name) |
| 24 | { |
| 25 | Buffer::Iterator start = i; |
| 26 | |
| 27 | i.WriteU16 (static_cast<uint16_t> (SerializedSizeName (name)-2)); |
| 28 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 29 | for (Name::const_iterator item = name.begin (); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 30 | item != name.end (); |
| 31 | item++) |
| 32 | { |
| 33 | i.WriteU16 (static_cast<uint16_t> (item->size ())); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 34 | i.Write (reinterpret_cast<const uint8_t*> (item->buf ()), item->size ()); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | return i.GetDistanceFrom (start); |
| 38 | } |
| 39 | |
| 40 | size_t |
| 41 | NdnSim::SerializedSizeName (const Name &name) |
| 42 | { |
| 43 | size_t nameSerializedSize = 2; |
| 44 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 45 | for (Name::const_iterator i = name.begin (); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 46 | i != name.end (); |
| 47 | i++) |
| 48 | { |
| 49 | nameSerializedSize += 2 + i->size (); |
| 50 | } |
| 51 | NS_ASSERT_MSG (nameSerializedSize < 30000, "Name is too long (> 30kbytes)"); |
| 52 | |
| 53 | return nameSerializedSize; |
| 54 | } |
| 55 | |
| 56 | Ptr<Name> |
| 57 | NdnSim::DeserializeName (Buffer::Iterator &i) |
| 58 | { |
| 59 | Ptr<Name> name = Create<Name> (); |
| 60 | |
| 61 | uint16_t nameLength = i.ReadU16 (); |
| 62 | while (nameLength > 0) |
| 63 | { |
| 64 | uint16_t length = i.ReadU16 (); |
| 65 | nameLength = nameLength - 2 - length; |
| 66 | |
| 67 | uint8_t tmp[length]; |
| 68 | i.Read (tmp, length); |
| 69 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 70 | name->append (tmp, length); |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return name; |
| 74 | } |
| 75 | |
Alexander Afanasyev | abb493a | 2013-07-19 15:31:33 -0700 | [diff] [blame] | 76 | |
| 77 | size_t |
| 78 | NdnSim::SerializeExclude (Buffer::Iterator &i, const Exclude &exclude) |
| 79 | { |
| 80 | Buffer::Iterator start = i; |
| 81 | |
| 82 | i.WriteU16 (static_cast<uint16_t> (SerializedSizeExclude (exclude)-2)); |
| 83 | |
| 84 | for (Exclude::const_reverse_iterator item = exclude.rbegin (); |
| 85 | item != exclude.rend (); |
| 86 | item++) |
| 87 | { |
| 88 | if (!item->first.empty ()) |
| 89 | { |
| 90 | i.WriteU8 (ExcludeNameType); |
| 91 | i.WriteU16 (static_cast<uint16_t> (item->first.size ())); |
| 92 | i.Write (reinterpret_cast<const uint8_t*> (item->first.buf ()), item->first.size ()); |
| 93 | } |
| 94 | if (item->second) |
| 95 | { |
| 96 | i.WriteU8 (ExcludeAnyType); |
| 97 | } |
| 98 | } |
| 99 | return i.GetDistanceFrom (start); |
| 100 | } |
| 101 | |
| 102 | size_t |
| 103 | NdnSim::SerializedSizeExclude (const Exclude &exclude) |
| 104 | { |
| 105 | size_t excludeSerializedSize = 2; |
| 106 | |
| 107 | for (Exclude::const_reverse_iterator item = exclude.rbegin (); |
| 108 | item != exclude.rend (); |
| 109 | item++) |
| 110 | { |
| 111 | if (!item->first.empty ()) |
| 112 | { |
| 113 | excludeSerializedSize += 1 + 2 + item->first.size (); |
| 114 | } |
| 115 | if (item->second) |
| 116 | { |
| 117 | excludeSerializedSize += 1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return excludeSerializedSize; |
| 122 | } |
| 123 | |
| 124 | Ptr<Exclude> |
| 125 | NdnSim::DeserializeExclude (Buffer::Iterator &i) |
| 126 | { |
| 127 | Ptr<Exclude> exclude = Create<Exclude> (); |
| 128 | |
| 129 | uint16_t excludeLength = i.ReadU16 (); |
| 130 | while (excludeLength > 0) |
| 131 | { |
| 132 | uint8_t type = i.ReadU8 (); |
| 133 | excludeLength --; |
| 134 | |
| 135 | if (type == ExcludeAnyType) |
| 136 | { |
| 137 | exclude->appendExclude (name::Component (), true); |
| 138 | } |
| 139 | else if (type == ExcludeNameType) |
| 140 | { |
| 141 | uint16_t length = i.ReadU16 (); |
| 142 | excludeLength = excludeLength - 2 - length; |
| 143 | |
| 144 | uint8_t tmp[length]; |
| 145 | i.Read (tmp, length); |
| 146 | |
| 147 | bool any = false; |
| 148 | if (excludeLength > 0) |
| 149 | { |
| 150 | uint8_t type = i.ReadU8 (); |
| 151 | if (type != ExcludeAnyType) |
| 152 | { |
| 153 | i.Prev (); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | any = true; |
| 158 | excludeLength --; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | exclude->appendExclude (name::Component (tmp, length), any); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | NS_FATAL_ERROR ("Incorrect format of Exclude filter"); |
| 167 | } |
| 168 | } |
| 169 | return exclude; |
| 170 | } |
| 171 | |
| 172 | |
Alexander Afanasyev | a89bc10 | 2013-07-16 10:17:31 -0700 | [diff] [blame] | 173 | } // wire |
| 174 | |
| 175 | NDN_NAMESPACE_END |