A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
8using namespace ns3;
9
10/**
11 * @ingroup network
12 * A simple example of an Header implementation
13 */
14class MyHeader : public Header
15{
16 public:
17 MyHeader();
18 ~MyHeader() override;
19
20 /**
21 * Set the header data.
22 * @param data The data.
23 */
24 void SetData(uint16_t data);
25 /**
26 * Get the header data.
27 * @return The data.
28 */
29 uint16_t GetData() const;
30
31 /**
32 * @brief Get the type ID.
33 * @return the object TypeId
34 */
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;
40 uint32_t GetSerializedSize() const override;
41
42 private:
43 uint16_t m_data; //!< Header data
44};
45
47{
48 // we must provide a public default constructor,
49 // implicit or explicit, but never private.
50}
51
55
58{
59 static TypeId tid = TypeId("ns3::MyHeader").SetParent<Header>().AddConstructor<MyHeader>();
60 return tid;
61}
62
65{
66 return GetTypeId();
67}
68
69void
70MyHeader::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
80{
81 // we reserve 2 bytes for our header.
82 return 2;
83}
84
85void
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
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
105void
107{
108 m_data = data;
109}
110
111uint16_t
113{
114 return m_data;
115}
116
117int
118main(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
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.
uint32_t Deserialize(Buffer::Iterator start) override
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:89
Protocol header serialization and deserialization.
Definition header.h:33
static void EnablePrinting()
Enable printing packets metadata.
Definition packet.cc:585
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint8_t data[writeSize]