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-data.h"
18 #include "ns3/ndn-header-helper.h"
21 #include "ndnsim/wire-ndnsim.h"
23 #include "ccnb/wire-ccnb.h"
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> ());
35 static inline uint32_t
38 static int32_t format = -1;
43 g_wireFormat.GetValue (value);
44 format = value.Get ();
50 Wire::FromInterest (Ptr<const Interest> interest, int8_t wireFormat)
52 if (wireFormat == WIRE_FORMAT_DEFAULT)
53 wireFormat = GetWireFormat ();
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);
61 NS_FATAL_ERROR (
"Unsupported format requested");
67 Wire::ToInterest (Ptr<Packet> packet, int8_t wireFormat)
69 if (wireFormat == WIRE_FORMAT_AUTODETECT)
73 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
76 case HeaderHelper::INTEREST_NDNSIM:
78 return wire::ndnSIM::Interest::FromWire (packet);
80 case HeaderHelper::INTEREST_CCNB:
82 return wire::ccnb::Interest::FromWire (packet);
84 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
85 case HeaderHelper::CONTENT_OBJECT_CCNB:
86 NS_FATAL_ERROR (
"Data packet supplied for InterestFromWire function");
89 NS_FATAL_ERROR (
"Unsupported format");
95 catch (UnknownHeaderException)
97 NS_FATAL_ERROR (
"Unknown NDN header");
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);
109 NS_FATAL_ERROR (
"Unsupported format requested");
116 Wire::FromData (Ptr<const Data> data, int8_t wireFormat)
118 if (wireFormat == WIRE_FORMAT_DEFAULT)
119 wireFormat = GetWireFormat ();
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);
127 NS_FATAL_ERROR (
"Unsupported format requested");
133 Wire::ToData (Ptr<Packet> packet, int8_t wireFormat)
135 if (wireFormat == WIRE_FORMAT_AUTODETECT)
139 HeaderHelper::Type type = HeaderHelper::GetNdnHeaderType (packet);
142 case HeaderHelper::CONTENT_OBJECT_NDNSIM:
144 return wire::ndnSIM::Data::FromWire (packet);
146 case HeaderHelper::CONTENT_OBJECT_CCNB:
148 return wire::ccnb::Data::FromWire (packet);
150 case HeaderHelper::INTEREST_NDNSIM:
151 case HeaderHelper::INTEREST_CCNB:
152 NS_FATAL_ERROR (
"Interest supplied for DataFromWire function");
155 NS_FATAL_ERROR (
"Unsupported format");
161 catch (UnknownHeaderException)
163 NS_FATAL_ERROR (
"Unknown NDN header");
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);
175 NS_FATAL_ERROR (
"Unsupported format requested");
183 Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat)
185 Ptr<Packet> pkt = FromInterest (interest, wireFormat);
187 wire.resize (pkt->GetSize ());
188 pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
194 Wire::ToInterestStr (
const std::string &wire, int8_t type)
196 Ptr<Packet> pkt = Create<Packet> (
reinterpret_cast<const uint8_t*
> (&wire[0]), wire.size ());
197 return ToInterest (pkt, type);
201 Wire::FromDataStr (Ptr<const Data> data, int8_t wireFormat)
203 Ptr<Packet> pkt = FromData (data, wireFormat);
205 wire.resize (pkt->GetSize ());
206 pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
212 Wire::ToDataStr (
const std::string &wire, int8_t type)
214 Ptr<Packet> pkt = Create<Packet> (
reinterpret_cast<const uint8_t*
> (&wire[0]), wire.size ());
215 return ToData (pkt, type);
239 Wire::FromName (Ptr<const Name> name, int8_t wireFormat)
242 if (wireFormat == WIRE_FORMAT_DEFAULT)
243 wireFormat = GetWireFormat ();
245 if (wireFormat == WIRE_FORMAT_NDNSIM)
248 buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
249 Buffer::Iterator i = buf.Begin ();
251 wire::NdnSim::SerializeName (i, *name);
254 wire.resize (buf.GetSize ());
255 buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
259 else if (wireFormat == WIRE_FORMAT_CCNB)
262 buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
263 Buffer::Iterator i = buf.Begin ();
265 wire::Ccnb::SerializeName (i, *name);
268 wire.resize (buf.GetSize ());
269 buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
275 NS_FATAL_ERROR (
"Unsupported format requested");
280 Wire::ToName (
const std::string &name, int8_t wireFormat)
283 buf.AddAtStart (name.size ());
284 Buffer::Iterator i = buf.Begin ();
285 i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ());
289 if (wireFormat == WIRE_FORMAT_DEFAULT)
290 wireFormat = GetWireFormat ();
292 if (wireFormat == WIRE_FORMAT_NDNSIM)
294 return wire::NdnSim::DeserializeName (i);
296 else if (wireFormat == WIRE_FORMAT_CCNB)
298 return wire::Ccnb::DeserializeName (i);
302 NS_FATAL_ERROR (
"Unsupported format requested");