Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -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 "ndn-wire.h" |
| 12 | |
| 13 | #include "ns3/global-value.h" |
| 14 | #include "ns3/integer.h" |
| 15 | #include "ns3/ndn-common.h" |
| 16 | #include "ns3/ndn-interest.h" |
| 17 | #include "ns3/ndn-content-object.h" |
| 18 | #include "ns3/ndn-header-helper.h" |
| 19 | |
| 20 | #include "ndnsim.h" |
| 21 | #include "ndnsim/wire-ndnsim.h" |
| 22 | #include "ccnb.h" |
| 23 | #include "ccnb/wire-ccnb.h" |
| 24 | |
| 25 | NDN_NAMESPACE_BEGIN |
| 26 | |
| 27 | static |
| 28 | GlobalValue g_wireFormat ("ndn::WireFormat", |
| 29 | "Default wire format for ndnSIM. ndnSIM will be accepting packets " |
| 30 | "in any supported packet formats, but if encoding requested, it will " |
| 31 | "use default wire format to encode (0 for ndnSIM (default), 1 for CCNb)", |
| 32 | IntegerValue (Wire::WIRE_FORMAT_NDNSIM), |
| 33 | MakeIntegerChecker<int32_t> ()); |
| 34 | |
| 35 | static inline uint32_t |
| 36 | GetWireFormat () |
| 37 | { |
| 38 | static int32_t format = -1; |
| 39 | if (format >= 0) |
| 40 | return format; |
| 41 | |
| 42 | IntegerValue value; |
| 43 | g_wireFormat.GetValue (value); |
| 44 | format = value.Get (); |
| 45 | |
| 46 | return format; |
| 47 | } |
| 48 | |
| 49 | Ptr<Packet> |
| 50 | Wire::FromInterest (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 51 | { |
| 52 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 53 | wireFormat = GetWireFormat (); |
| 54 | |
| 55 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 56 | return wire::ndnSIM::Interest::ToWire (interest); |
| 57 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 58 | return wire::ccnb::Interest::ToWire (interest); |
| 59 | else |
| 60 | { |
| 61 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Ptr<Interest> |
| 67 | Wire::ToInterest (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/) |
| 68 | { |
| 69 | if (wireFormat == WIRE_FORMAT_AUTODETECT) |
| 70 | { |
| 71 | try |
| 72 | { |
| 73 | HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet); |
| 74 | switch (type) |
| 75 | { |
| 76 | case HeaderHelper::INTEREST_NDNSIM: |
| 77 | { |
| 78 | return wire::ndnSIM::Interest::FromWire (packet); |
| 79 | } |
| 80 | case HeaderHelper::INTEREST_CCNB: |
| 81 | { |
| 82 | return wire::ccnb::Interest::FromWire (packet); |
| 83 | } |
| 84 | case HeaderHelper::CONTENT_OBJECT_NDNSIM: |
| 85 | case HeaderHelper::CONTENT_OBJECT_CCNB: |
| 86 | NS_FATAL_ERROR ("Data packet supplied for InterestFromWire function"); |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | // exception will be thrown if packet is not recognized |
| 91 | } |
| 92 | catch (UnknownHeaderException) |
| 93 | { |
| 94 | NS_FATAL_ERROR ("Unknown NDN header"); |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 101 | return wire::ndnSIM::Interest::FromWire (packet); |
| 102 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 103 | return wire::ccnb::Interest::FromWire (packet); |
| 104 | else |
| 105 | { |
| 106 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Ptr<Packet> |
| 113 | Wire::FromData (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 114 | { |
| 115 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 116 | wireFormat = GetWireFormat (); |
| 117 | |
| 118 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 119 | return wire::ndnSIM::Data::ToWire (data); |
| 120 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 121 | return wire::ccnb::Data::ToWire (data); |
| 122 | else |
| 123 | { |
| 124 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | Ptr<ContentObject> |
| 130 | Wire::ToData (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/) |
| 131 | { |
| 132 | if (wireFormat == WIRE_FORMAT_AUTODETECT) |
| 133 | { |
| 134 | try |
| 135 | { |
| 136 | HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet); |
| 137 | switch (type) |
| 138 | { |
| 139 | case HeaderHelper::CONTENT_OBJECT_NDNSIM: |
| 140 | { |
| 141 | return wire::ndnSIM::Data::FromWire (packet); |
| 142 | } |
| 143 | case HeaderHelper::CONTENT_OBJECT_CCNB: |
| 144 | { |
| 145 | return wire::ccnb::Data::FromWire (packet); |
| 146 | } |
| 147 | case HeaderHelper::INTEREST_NDNSIM: |
| 148 | case HeaderHelper::INTEREST_CCNB: |
| 149 | NS_FATAL_ERROR ("Interest supplied for DataFromWire function"); |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | // exception will be thrown if packet is not recognized |
| 154 | } |
| 155 | catch (UnknownHeaderException) |
| 156 | { |
| 157 | NS_FATAL_ERROR ("Unknown NDN header"); |
| 158 | return 0; |
| 159 | } |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 164 | return wire::ndnSIM::Data::FromWire (packet); |
| 165 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 166 | return wire::ccnb::Data::FromWire (packet); |
| 167 | else |
| 168 | { |
| 169 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | uint32_t |
| 177 | Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 178 | { |
| 179 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 180 | wireFormat = GetWireFormat (); |
| 181 | |
| 182 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 183 | return wire::NdnSim::SerializedSizeName (*name); |
| 184 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 185 | return wire::Ccnb::SerializedSizeName (*name); |
| 186 | else |
| 187 | { |
| 188 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | Wire::FromName (Buffer::Iterator start, Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 195 | { |
| 196 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 197 | wireFormat = GetWireFormat (); |
| 198 | |
| 199 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 200 | wire::NdnSim::SerializedSizeName (*name); |
| 201 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 202 | wire::Ccnb::SerializedSizeName (*name); |
| 203 | else |
| 204 | { |
| 205 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | Ptr<Name> |
| 210 | Wire::ToName (Buffer::Iterator start, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 211 | { |
| 212 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 213 | wireFormat = GetWireFormat (); |
| 214 | |
| 215 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 216 | return wire::NdnSim::DeserializeName (start); |
| 217 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 218 | { |
| 219 | return wire::Ccnb::DeserializeName (start); |
| 220 | return 0; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | NDN_NAMESPACE_END |