A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
main-packet-header.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
#include "ns3/ptr.h"
3
#include "ns3/packet.h"
4
#include "ns3/header.h"
5
#include "ns3/simulator.h"
6
#include <iostream>
7
8
using namespace
ns3
;
9
14
class
MyHeader
:
public
Header
15
{
16
public
:
17
18
MyHeader
();
19
virtual
~
MyHeader
();
20
25
void
SetData (uint16_t
data
);
30
uint16_t GetData (
void
)
const
;
31
36
static
TypeId
GetTypeId (
void
);
37
virtual
TypeId
GetInstanceTypeId (
void
)
const
;
38
virtual
void
Print
(std::ostream &os)
const
;
39
virtual
void
Serialize (
Buffer::Iterator
start
)
const
;
40
virtual
uint32_t Deserialize (
Buffer::Iterator
start
);
41
virtual
uint32_t GetSerializedSize (
void
)
const
;
42
private
:
43
uint16_t
m_data
;
44
};
45
46
MyHeader::MyHeader
()
47
{
48
// we must provide a public default constructor,
49
// implicit or explicit, but never private.
50
}
51
MyHeader::~MyHeader
()
52
{
53
}
54
55
TypeId
56
MyHeader::GetTypeId
(
void
)
57
{
58
static
TypeId
tid =
TypeId
(
"ns3::MyHeader"
)
59
.
SetParent
<
Header
> ()
60
.AddConstructor<MyHeader> ()
61
;
62
return
tid;
63
}
64
TypeId
65
MyHeader::GetInstanceTypeId
(
void
)
const
66
{
67
return
GetTypeId ();
68
}
69
70
void
71
MyHeader::Print
(std::ostream &os)
const
72
{
73
// This method is invoked by the packet printing
74
// routines to print the content of my header.
75
//os << "data=" << m_data << std::endl;
76
os <<
"data="
<< m_data;
77
}
78
uint32_t
79
MyHeader::GetSerializedSize
(
void
)
const
80
{
81
// we reserve 2 bytes for our header.
82
return
2;
83
}
84
void
85
MyHeader::Serialize
(
Buffer::Iterator
start
)
const
86
{
87
// we can serialize two bytes at the start of the buffer.
88
// we write them in network byte order.
89
start
.WriteHtonU16 (m_data);
90
}
91
uint32_t
92
MyHeader::Deserialize
(
Buffer::Iterator
start
)
93
{
94
// we can deserialize two bytes from the start of the buffer.
95
// we read them in network byte order and store them
96
// in host byte order.
97
m_data =
start
.ReadNtohU16 ();
98
99
// we return the number of bytes effectively read.
100
return
2;
101
}
102
103
void
104
MyHeader::SetData
(uint16_t
data
)
105
{
106
m_data =
data
;
107
}
108
uint16_t
109
MyHeader::GetData
(
void
)
const
110
{
111
return
m_data;
112
}
113
114
115
116
int
main (
int
argc,
char
*argv[])
117
{
118
// Enable the packet printing through Packet::Print command.
119
Packet::EnablePrinting
();
120
121
// instantiate a header.
122
MyHeader
sourceHeader;
123
sourceHeader.
SetData
(2);
124
125
// instantiate a packet
126
Ptr<Packet>
p = Create<Packet> ();
127
128
// and store my header into the packet.
129
p->
AddHeader
(sourceHeader);
130
131
// print the content of my packet on the standard output.
132
p->
Print
(std::cout);
133
std::cout << std::endl;
134
135
// you can now remove the header from the packet:
136
MyHeader
destinationHeader;
137
p->
RemoveHeader
(destinationHeader);
138
139
// and check that the destination and source
140
// headers contain the same values.
141
NS_ASSERT
(sourceHeader.
GetData
() == destinationHeader.
GetData
());
142
143
return
0;
144
}
MyHeader
A simple example of an Header implementation.
Definition:
main-packet-header.cc:15
MyHeader::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
main-packet-header.cc:56
MyHeader::m_data
uint16_t m_data
Header data.
Definition:
main-packet-header.cc:43
MyHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
main-packet-header.cc:85
MyHeader::MyHeader
MyHeader()
Definition:
main-packet-header.cc:46
MyHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
main-packet-header.cc:65
MyHeader::~MyHeader
virtual ~MyHeader()
Definition:
main-packet-header.cc:51
MyHeader::GetData
uint16_t GetData(void) const
Get the header data.
Definition:
main-packet-header.cc:109
MyHeader::Print
virtual void Print(std::ostream &os) const
Definition:
main-packet-header.cc:71
MyHeader::SetData
void SetData(uint16_t data)
Set the header data.
Definition:
main-packet-header.cc:104
MyHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
main-packet-header.cc:79
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:99
ns3::Header
Protocol header serialization and deserialization.
Definition:
header.h:43
ns3::Header::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)=0
Deserialize the object from a buffer iterator.
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition:
packet.cc:280
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition:
packet.cc:256
ns3::Packet::Print
void Print(std::ostream &os) const
Print the packet contents.
Definition:
packet.cc:434
ns3::Packet::EnablePrinting
static void EnablePrinting(void)
Enable printing packets metadata.
Definition:
packet.cc:572
ns3::Ptr< Packet >
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:922
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:67
Print
void Print(ComponentCarrier cc)
Definition:
lena-cc-helper.cc:69
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
visualizer.core.start
def start()
Definition:
core.py:1853
data
uint8_t data[writeSize]
Definition:
socket-bound-tcp-static-routing.cc:53
src
network
examples
main-packet-header.cc
Generated on Tue Feb 6 2024 19:21:25 for ns-3 by
1.9.1