A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
8
using namespace
ns3
;
9
10
/**
11
* \ingroup network
12
* A simple example of an Header implementation
13
*/
14
class
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
;
39
uint32_t
Deserialize
(
Buffer::Iterator
start)
override
;
40
uint32_t
GetSerializedSize
()
const override
;
41
42
private
:
43
uint16_t
m_data
;
//!< Header data
44
};
45
46
MyHeader::MyHeader
()
47
{
48
// we must provide a public default constructor,
49
// implicit or explicit, but never private.
50
}
51
52
MyHeader::~MyHeader
()
53
{
54
}
55
56
TypeId
57
MyHeader::GetTypeId
()
58
{
59
static
TypeId
tid =
TypeId
(
"ns3::MyHeader"
).
SetParent
<
Header
>().AddConstructor<MyHeader>();
60
return
tid;
61
}
62
63
TypeId
64
MyHeader::GetInstanceTypeId
()
const
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
79
MyHeader::GetSerializedSize
()
const
80
{
81
// we reserve 2 bytes for our header.
82
return
2;
83
}
84
85
void
86
MyHeader::Serialize
(
Buffer::Iterator
start)
const
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
94
MyHeader::Deserialize
(
Buffer::Iterator
start)
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
106
MyHeader::SetData
(uint16_t
data
)
107
{
108
m_data
=
data
;
109
}
110
111
uint16_t
112
MyHeader::GetData
()
const
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.
121
Packet::EnablePrinting
();
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
}
MyHeader
A simple example of an Header implementation.
Definition
main-packet-header.cc:15
MyHeader::m_data
uint16_t m_data
Header data.
Definition
main-packet-header.cc:43
MyHeader::Serialize
void Serialize(Buffer::Iterator start) const override
Definition
main-packet-header.cc:86
MyHeader::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
main-packet-header.cc:64
MyHeader::MyHeader
MyHeader()
Definition
main-packet-header.cc:46
MyHeader::GetData
uint16_t GetData() const
Get the header data.
Definition
main-packet-header.cc:112
MyHeader::Deserialize
uint32_t Deserialize(Buffer::Iterator start) override
Definition
main-packet-header.cc:94
MyHeader::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
main-packet-header.cc:57
MyHeader::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
main-packet-header.cc:79
MyHeader::Print
void Print(std::ostream &os) const override
Definition
main-packet-header.cc:70
MyHeader::SetData
void SetData(uint16_t data)
Set the header data.
Definition
main-packet-header.cc:106
MyHeader::~MyHeader
~MyHeader() override
Definition
main-packet-header.cc:52
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition
buffer.h:89
ns3::Header
Protocol header serialization and deserialization.
Definition
header.h:33
ns3::Packet::EnablePrinting
static void EnablePrinting()
Enable printing packets metadata.
Definition
packet.cc:585
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
uint32_t
NS_ASSERT
#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
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
data
uint8_t data[writeSize]
Definition
socket-bound-tcp-static-routing.cc:41
src
network
examples
main-packet-header.cc
Generated on Fri Nov 8 2024 13:59:04 for ns-3 by
1.11.0