Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 1 | /** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 5 | * |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 6 | * GNU 3.0 license, See the LICENSE file for more information |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 28 | GlobalValue g_wireFormat ("ndn::WireFormat", |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 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 (); |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 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; |
Saran Tarnoi | 0bb088a | 2013-07-27 19:51:20 +0900 | [diff] [blame^] | 88 | default: |
| 89 | NS_FATAL_ERROR ("Unsupported format"); |
| 90 | return 0; |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // exception will be thrown if packet is not recognized |
| 94 | } |
| 95 | catch (UnknownHeaderException) |
| 96 | { |
| 97 | NS_FATAL_ERROR ("Unknown NDN header"); |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 104 | return wire::ndnSIM::Interest::FromWire (packet); |
| 105 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 106 | return wire::ccnb::Interest::FromWire (packet); |
| 107 | else |
| 108 | { |
| 109 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 110 | return 0; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Ptr<Packet> |
| 116 | Wire::FromData (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 117 | { |
| 118 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 119 | wireFormat = GetWireFormat (); |
| 120 | |
| 121 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 122 | return wire::ndnSIM::Data::ToWire (data); |
| 123 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 124 | return wire::ccnb::Data::ToWire (data); |
| 125 | else |
| 126 | { |
| 127 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | Ptr<ContentObject> |
| 133 | Wire::ToData (Ptr<Packet> packet, int8_t wireFormat/* = WIRE_FORMAT_AUTODETECT*/) |
| 134 | { |
| 135 | if (wireFormat == WIRE_FORMAT_AUTODETECT) |
| 136 | { |
| 137 | try |
| 138 | { |
| 139 | HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet); |
| 140 | switch (type) |
| 141 | { |
| 142 | case HeaderHelper::CONTENT_OBJECT_NDNSIM: |
| 143 | { |
| 144 | return wire::ndnSIM::Data::FromWire (packet); |
| 145 | } |
| 146 | case HeaderHelper::CONTENT_OBJECT_CCNB: |
| 147 | { |
| 148 | return wire::ccnb::Data::FromWire (packet); |
| 149 | } |
| 150 | case HeaderHelper::INTEREST_NDNSIM: |
| 151 | case HeaderHelper::INTEREST_CCNB: |
| 152 | NS_FATAL_ERROR ("Interest supplied for DataFromWire function"); |
| 153 | break; |
Saran Tarnoi | 0bb088a | 2013-07-27 19:51:20 +0900 | [diff] [blame^] | 154 | default: |
| 155 | NS_FATAL_ERROR ("Unsupported format"); |
| 156 | return 0; |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // exception will be thrown if packet is not recognized |
| 160 | } |
| 161 | catch (UnknownHeaderException) |
| 162 | { |
| 163 | NS_FATAL_ERROR ("Unknown NDN header"); |
| 164 | return 0; |
| 165 | } |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 170 | return wire::ndnSIM::Data::FromWire (packet); |
| 171 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 172 | return wire::ccnb::Data::FromWire (packet); |
| 173 | else |
| 174 | { |
| 175 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 182 | std::string |
| 183 | Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 184 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 185 | Ptr<Packet> pkt = FromInterest (interest, wireFormat); |
| 186 | std::string wire; |
| 187 | wire.resize (pkt->GetSize ()); |
| 188 | pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ()); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 189 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 190 | return wire; |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 191 | } |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 192 | |
| 193 | Ptr<Interest> |
| 194 | Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/) |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 195 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 196 | Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ()); |
| 197 | return ToInterest (pkt, type); |
| 198 | } |
| 199 | |
| 200 | std::string |
| 201 | Wire::FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 202 | { |
| 203 | Ptr<Packet> pkt = FromData (data, wireFormat); |
| 204 | std::string wire; |
| 205 | wire.resize (pkt->GetSize ()); |
| 206 | pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ()); |
| 207 | |
| 208 | return wire; |
| 209 | } |
| 210 | |
| 211 | Ptr<ContentObject> |
| 212 | Wire::ToDataStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/) |
| 213 | { |
| 214 | Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ()); |
| 215 | return ToData (pkt, type); |
| 216 | } |
| 217 | |
| 218 | // uint32_t |
| 219 | // Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 220 | // { |
| 221 | // if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 222 | // wireFormat = GetWireFormat (); |
| 223 | |
| 224 | // if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 225 | // { |
| 226 | // std::cout << wire::NdnSim::SerializedSizeName (*name) << std::endl; |
| 227 | // return wire::NdnSim::SerializedSizeName (*name); |
| 228 | // } |
| 229 | // else if (wireFormat == WIRE_FORMAT_CCNB) |
| 230 | // return wire::Ccnb::SerializedSizeName (*name); |
| 231 | // else |
| 232 | // { |
| 233 | // NS_FATAL_ERROR ("Unsupported format requested"); |
| 234 | // return 0; |
| 235 | // } |
| 236 | // } |
| 237 | |
| 238 | std::string |
| 239 | Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 240 | { |
| 241 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 242 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 243 | wireFormat = GetWireFormat (); |
| 244 | |
| 245 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 246 | { |
| 247 | Buffer buf; |
| 248 | buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name)); |
| 249 | Buffer::Iterator i = buf.Begin (); |
| 250 | |
| 251 | wire::NdnSim::SerializeName (i, *name); |
| 252 | |
| 253 | std::string wire; |
| 254 | wire.resize (buf.GetSize ()); |
| 255 | buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ()); |
| 256 | |
| 257 | return wire; |
| 258 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 259 | else if (wireFormat == WIRE_FORMAT_CCNB) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 260 | { |
| 261 | Buffer buf; |
| 262 | buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name)); |
| 263 | Buffer::Iterator i = buf.Begin (); |
| 264 | |
| 265 | wire::Ccnb::SerializeName (i, *name); |
| 266 | |
| 267 | std::string wire; |
| 268 | wire.resize (buf.GetSize ()); |
| 269 | buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ()); |
| 270 | |
| 271 | return wire; |
| 272 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 273 | else |
| 274 | { |
| 275 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | Ptr<Name> |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 280 | Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 281 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 282 | Buffer buf; |
| 283 | buf.AddAtStart (name.size ()); |
| 284 | Buffer::Iterator i = buf.Begin (); |
| 285 | i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ()); |
| 286 | |
| 287 | i = buf.Begin (); |
| 288 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 289 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 290 | wireFormat = GetWireFormat (); |
| 291 | |
| 292 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 293 | { |
| 294 | return wire::NdnSim::DeserializeName (i); |
| 295 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 296 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 297 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 298 | return wire::Ccnb::DeserializeName (i); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
| 302 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | NDN_NAMESPACE_END |