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; |
| 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 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 176 | std::string |
| 177 | Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 178 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 179 | Ptr<Packet> pkt = FromInterest (interest, wireFormat); |
| 180 | std::string wire; |
| 181 | wire.resize (pkt->GetSize ()); |
| 182 | pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ()); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 184 | return wire; |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 185 | } |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 186 | |
| 187 | Ptr<Interest> |
| 188 | Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/) |
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 | Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ()); |
| 191 | return ToInterest (pkt, type); |
| 192 | } |
| 193 | |
| 194 | std::string |
| 195 | Wire::FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 196 | { |
| 197 | Ptr<Packet> pkt = FromData (data, wireFormat); |
| 198 | std::string wire; |
| 199 | wire.resize (pkt->GetSize ()); |
| 200 | pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ()); |
| 201 | |
| 202 | return wire; |
| 203 | } |
| 204 | |
| 205 | Ptr<ContentObject> |
| 206 | Wire::ToDataStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/) |
| 207 | { |
| 208 | Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ()); |
| 209 | return ToData (pkt, type); |
| 210 | } |
| 211 | |
| 212 | // uint32_t |
| 213 | // Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 214 | // { |
| 215 | // if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 216 | // wireFormat = GetWireFormat (); |
| 217 | |
| 218 | // if (wireFormat == WIRE_FORMAT_NDNSIM) |
| 219 | // { |
| 220 | // std::cout << wire::NdnSim::SerializedSizeName (*name) << std::endl; |
| 221 | // return wire::NdnSim::SerializedSizeName (*name); |
| 222 | // } |
| 223 | // else if (wireFormat == WIRE_FORMAT_CCNB) |
| 224 | // return wire::Ccnb::SerializedSizeName (*name); |
| 225 | // else |
| 226 | // { |
| 227 | // NS_FATAL_ERROR ("Unsupported format requested"); |
| 228 | // return 0; |
| 229 | // } |
| 230 | // } |
| 231 | |
| 232 | std::string |
| 233 | Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
| 234 | { |
| 235 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 236 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 237 | wireFormat = GetWireFormat (); |
| 238 | |
| 239 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 240 | { |
| 241 | Buffer buf; |
| 242 | buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name)); |
| 243 | Buffer::Iterator i = buf.Begin (); |
| 244 | |
| 245 | wire::NdnSim::SerializeName (i, *name); |
| 246 | |
| 247 | std::string wire; |
| 248 | wire.resize (buf.GetSize ()); |
| 249 | buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ()); |
| 250 | |
| 251 | return wire; |
| 252 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 253 | else if (wireFormat == WIRE_FORMAT_CCNB) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 254 | { |
| 255 | Buffer buf; |
| 256 | buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name)); |
| 257 | Buffer::Iterator i = buf.Begin (); |
| 258 | |
| 259 | wire::Ccnb::SerializeName (i, *name); |
| 260 | |
| 261 | std::string wire; |
| 262 | wire.resize (buf.GetSize ()); |
| 263 | buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ()); |
| 264 | |
| 265 | return wire; |
| 266 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 267 | else |
| 268 | { |
| 269 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | Ptr<Name> |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 274 | Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/) |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 275 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 276 | Buffer buf; |
| 277 | buf.AddAtStart (name.size ()); |
| 278 | Buffer::Iterator i = buf.Begin (); |
| 279 | i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ()); |
| 280 | |
| 281 | i = buf.Begin (); |
| 282 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 283 | if (wireFormat == WIRE_FORMAT_DEFAULT) |
| 284 | wireFormat = GetWireFormat (); |
| 285 | |
| 286 | if (wireFormat == WIRE_FORMAT_NDNSIM) |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 287 | { |
| 288 | return wire::NdnSim::DeserializeName (i); |
| 289 | } |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 290 | else if (wireFormat == WIRE_FORMAT_CCNB) |
| 291 | { |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 292 | return wire::Ccnb::DeserializeName (i); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 293 | } |
| 294 | else |
| 295 | { |
| 296 | NS_FATAL_ERROR ("Unsupported format requested"); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | NDN_NAMESPACE_END |