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