A Discrete-Event Network Simulator
API
main-packet-header.cc
Go to the documentation of this file.
1 #include "ns3/header.h"
2 #include "ns3/packet.h"
3 #include "ns3/ptr.h"
4 #include "ns3/simulator.h"
5 
6 #include <iostream>
7 
8 using namespace ns3;
9 
14 class MyHeader : public Header
15 {
16  public:
17  MyHeader();
18  ~MyHeader() override;
19 
24  void SetData(uint16_t data);
29  uint16_t GetData() const;
30 
35  static TypeId GetTypeId();
36  TypeId GetInstanceTypeId() const override;
37  void Print(std::ostream& os) const override;
38  void Serialize(Buffer::Iterator start) const override;
39  uint32_t Deserialize(Buffer::Iterator start) override;
40  uint32_t GetSerializedSize() const override;
41 
42  private:
43  uint16_t m_data;
44 };
45 
47 {
48  // we must provide a public default constructor,
49  // implicit or explicit, but never private.
50 }
51 
53 {
54 }
55 
56 TypeId
58 {
59  static TypeId tid = TypeId("ns3::MyHeader").SetParent<Header>().AddConstructor<MyHeader>();
60  return tid;
61 }
62 
63 TypeId
65 {
66  return GetTypeId();
67 }
68 
69 void
70 MyHeader::Print(std::ostream& os) const
71 {
72  // This method is invoked by the packet printing
73  // routines to print the content of my header.
74  // os << "data=" << m_data << std::endl;
75  os << "data=" << m_data;
76 }
77 
78 uint32_t
80 {
81  // we reserve 2 bytes for our header.
82  return 2;
83 }
84 
85 void
87 {
88  // we can serialize two bytes at the start of the buffer.
89  // we write them in network byte order.
90  start.WriteHtonU16(m_data);
91 }
92 
93 uint32_t
95 {
96  // we can deserialize two bytes from the start of the buffer.
97  // we read them in network byte order and store them
98  // in host byte order.
99  m_data = start.ReadNtohU16();
100 
101  // we return the number of bytes effectively read.
102  return 2;
103 }
104 
105 void
107 {
108  m_data = data;
109 }
110 
111 uint16_t
113 {
114  return m_data;
115 }
116 
117 int
118 main(int argc, char* argv[])
119 {
120  // Enable the packet printing through Packet::Print command.
122 
123  // instantiate a header.
124  MyHeader sourceHeader;
125  sourceHeader.SetData(2);
126 
127  // instantiate a packet
128  Ptr<Packet> p = Create<Packet>();
129 
130  // and store my header into the packet.
131  p->AddHeader(sourceHeader);
132 
133  // print the content of my packet on the standard output.
134  p->Print(std::cout);
135  std::cout << std::endl;
136 
137  // you can now remove the header from the packet:
138  MyHeader destinationHeader;
139  p->RemoveHeader(destinationHeader);
140 
141  // and check that the destination and source
142  // headers contain the same values.
143  NS_ASSERT(sourceHeader.GetData() == destinationHeader.GetData());
144 
145  return 0;
146 }
A simple example of an Header implementation.
uint16_t m_data
Header data.
void Serialize(Buffer::Iterator start) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint16_t GetData() const
Get the header data.
static TypeId GetTypeId()
Get the type ID.
uint32_t GetSerializedSize() const override
void Print(std::ostream &os) const override
void SetData(uint16_t data)
Set the header data.
~MyHeader() override
iterator in a Buffer instance
Definition: buffer.h:100
Protocol header serialization and deserialization.
Definition: header.h:44
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:294
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
void Print(std::ostream &os) const
Print the packet contents.
Definition: packet.cc:456
static void EnablePrinting()
Enable printing packets metadata.
Definition: packet.cc:596
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
void Print(ComponentCarrier cc)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]