15 #include <ns3/header.h>
16 #include <ns3/packet.h>
19 #include "ndnsim/wire-ndnsim.h"
21 NS_LOG_COMPONENT_DEFINE (
"ndn.wire.ndnSIM");
28 NS_OBJECT_ENSURE_REGISTERED (
Interest);
29 NS_OBJECT_ENSURE_REGISTERED (
Data);
36 : m_interest (Create<ndn::Interest> ())
40 Interest::Interest (Ptr<ndn::Interest> interest)
41 : m_interest (interest)
46 Interest::GetInterest ()
53 Interest::GetTypeId (
void)
55 static TypeId tid = TypeId (
"ns3::ndn::Interest::ndnSIM")
58 .AddConstructor<Interest> ()
64 Interest::GetInstanceTypeId (
void)
const
70 Interest::ToWire (Ptr<const ndn::Interest> interest)
72 Ptr<const Packet> p = interest->GetWire ();
75 Ptr<Packet> packet = Create<Packet> (*interest->GetPayload ());
76 Interest wireEncoding (ConstCast<ndn::Interest> (interest));
77 packet->AddHeader (wireEncoding);
78 interest->SetWire (packet);
87 Interest::FromWire (Ptr<Packet> packet)
89 Ptr<ndn::Interest> interest = Create<ndn::Interest> ();
90 Ptr<Packet> wire = packet->Copy ();
93 packet->RemoveHeader (wireEncoding);
95 interest->SetPayload (packet);
96 interest->SetWire (wire);
102 Interest::GetSerializedSize (
void)
const
107 NdnSim::SerializedSizeName (m_interest->GetName ()) +
110 (m_interest->GetExclude () == 0 ? 0 : (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ())))
115 NS_LOG_INFO (
"Serialize size = " << size);
120 Interest::Serialize (Buffer::Iterator start)
const
122 start.WriteU8 (0x80);
123 start.WriteU8 (0x00);
125 start.WriteU16 (GetSerializedSize () - 4);
127 start.WriteU32 (m_interest->GetNonce ());
128 start.WriteU8 (m_interest->GetScope ());
129 start.WriteU8 (m_interest->GetNack ());
131 NS_ASSERT_MSG (0 <= m_interest->GetInterestLifetime ().ToInteger (Time::S) && m_interest->GetInterestLifetime ().ToInteger (Time::S) < 65535,
132 "Incorrect InterestLifetime (should not be smaller than 0 and larger than 65535");
135 start.WriteU16 (static_cast<uint16_t> (m_interest->GetInterestLifetime ().ToInteger (Time::S)));
137 NdnSim::SerializeName (start, m_interest->GetName ());
139 if (m_interest->GetExclude () == 0)
145 start.WriteU16 (1 + NdnSim::SerializedSizeExclude (*m_interest->GetExclude ()));
146 start.WriteU8 (0x01);
147 NdnSim::SerializeExclude (start, *m_interest->GetExclude ());
154 Interest::Deserialize (Buffer::Iterator start)
156 Buffer::Iterator i = start;
158 if (i.ReadU8 () != 0x80)
161 if (i.ReadU8 () != 0x00)
166 m_interest->SetNonce (i.ReadU32 ());
167 m_interest->SetScope (i.ReadU8 ());
168 m_interest->SetNack (i.ReadU8 ());
170 m_interest->SetInterestLifetime (Seconds (i.ReadU16 ()));
172 m_interest->SetName (NdnSim::DeserializeName (i));
174 uint32_t selectorsLen = i.ReadU16 ();
175 if (selectorsLen > 0)
177 if (i.ReadU8 () != 0x01)
180 m_interest->SetExclude (NdnSim::DeserializeExclude (i));
184 NS_ASSERT (GetSerializedSize () == (i.GetDistanceFrom (start)));
186 return i.GetDistanceFrom (start);
190 Interest::Print (std::ostream &os)
const
192 m_interest->Print (os);
201 Data::GetTypeId (
void)
203 static TypeId tid = TypeId (
"ns3::ndn::Data::ndnSIM")
204 .SetGroupName (
"Ndn")
205 .SetParent<Header> ()
206 .AddConstructor<Data> ()
212 Data::GetInstanceTypeId (
void)
const
219 : m_data (Create<ndn::Data> ())
223 Data::Data (Ptr<ndn::Data> data)
235 Data::ToWire (Ptr<const ndn::Data> data)
237 Ptr<const Packet> p = data->GetWire ();
240 Ptr<Packet> packet = Create<Packet> (*data->GetPayload ());
241 Data wireEncoding (ConstCast<ndn::Data> (data));
242 packet->AddHeader (wireEncoding);
243 data->SetWire (packet);
252 Data::FromWire (Ptr<Packet> packet)
254 Ptr<ndn::Data> data = Create<ndn::Data> ();
255 Ptr<Packet> wire = packet->Copy ();
257 Data wireEncoding (data);
258 packet->RemoveHeader (wireEncoding);
260 data->SetPayload (packet);
261 data->SetWire (wire);
267 Data::GetSerializedSize ()
const
269 uint32_t size = 1 + 1 + 2 +
271 NdnSim::SerializedSizeName (m_data->GetName ()) +
272 (2 + 2 + 4 + 2 + 2 + (2 + 0)));
273 if (m_data->GetSignature () != 0)
276 NS_LOG_INFO (
"Serialize size = " << size);
281 Data::Serialize (Buffer::Iterator start)
const
283 start.WriteU8 (0x80);
284 start.WriteU8 (0x01);
285 start.WriteU16 (GetSerializedSize () - 4);
287 if (m_data->GetSignature () != 0)
290 start.WriteU16 (0xFF00);
291 start.WriteU32 (m_data->GetSignature ());
300 NdnSim::SerializeName (start, m_data->GetName ());
304 start.WriteU16 (2 + 4 + 2 + 2 + (2 + 0));
305 start.WriteU16 (4 + 2 + 2 + (2 + 0));
306 start.WriteU32 (static_cast<uint32_t> (m_data->GetTimestamp ().ToInteger (Time::S)));
307 start.WriteU16 (static_cast<uint16_t> (m_data->GetFreshness ().ToInteger (Time::S)));
315 Data::Deserialize (Buffer::Iterator start)
317 Buffer::Iterator i = start;
319 if (i.ReadU8 () != 0x80)
322 if (i.ReadU8 () != 0x01)
327 uint32_t signatureLength = i.ReadU16 ();
328 if (signatureLength == 6)
330 if (i.ReadU16 () != 0xFF00)
332 m_data->SetSignature (i.ReadU32 ());
334 else if (signatureLength == 2)
336 if (i.ReadU16 () != 0)
338 m_data->SetSignature (0);
343 m_data->SetName (NdnSim::DeserializeName (i));
345 if (i.ReadU16 () != (2 + 4 + 2 + 2 + (2 + 0)))
348 if (i.ReadU16 () != (4 + 2 + 2 + (2 + 0)))
351 m_data->SetTimestamp (Seconds (i.ReadU32 ()));
352 m_data->SetFreshness (Seconds (i.ReadU16 ()));
354 if (i.ReadU16 () != 0)
356 if (i.ReadU16 () != 0)
359 NS_ASSERT_MSG (i.GetDistanceFrom (start) == GetSerializedSize (),
360 "Something wrong with Data::Deserialize");
362 return i.GetDistanceFrom (start);
366 Data::Print (std::ostream &os)
const
Class for Interest parsing exception.
Class for Data parsing exception.
Routines to serialize/deserialize Interest packet in ndnSIM format.
Routines to serialize/deserialize Data packet in ndnSIM format.