A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
9
/* A sample Header implementation
10
*/
11
class
MyHeader
:
public
Header
12
{
13
public
:
14
15
MyHeader
();
16
virtual
~
MyHeader
();
17
18
void
SetData (uint16_t
data
);
19
uint16_t GetData (
void
)
const
;
20
21
static
TypeId
GetTypeId (
void
);
22
virtual
TypeId
GetInstanceTypeId (
void
)
const
;
23
virtual
void
Print (std::ostream &os)
const
;
24
virtual
void
Serialize (
Buffer::Iterator
start
)
const
;
25
virtual
uint32_t Deserialize (
Buffer::Iterator
start);
26
virtual
uint32_t GetSerializedSize (
void
)
const
;
27
private
:
28
uint16_t
m_data
;
29
};
30
31
MyHeader::MyHeader
()
32
{
33
// we must provide a public default constructor,
34
// implicit or explicit, but never private.
35
}
36
MyHeader::~MyHeader
()
37
{
38
}
39
40
TypeId
41
MyHeader::GetTypeId
(
void
)
42
{
43
static
TypeId
tid =
TypeId
(
"ns3::MyHeader"
)
44
.
SetParent
<
Header
> ()
45
.AddConstructor<MyHeader> ()
46
;
47
return
tid;
48
}
49
TypeId
50
MyHeader::GetInstanceTypeId
(
void
)
const
51
{
52
return
GetTypeId ();
53
}
54
55
void
56
MyHeader::Print
(std::ostream &os)
const
57
{
58
// This method is invoked by the packet printing
59
// routines to print the content of my header.
60
//os << "data=" << m_data << std::endl;
61
os <<
"data="
<< m_data;
62
}
63
uint32_t
64
MyHeader::GetSerializedSize
(
void
)
const
65
{
66
// we reserve 2 bytes for our header.
67
return
2;
68
}
69
void
70
MyHeader::Serialize
(
Buffer::Iterator
start
)
const
71
{
72
// we can serialize two bytes at the start of the buffer.
73
// we write them in network byte order.
74
start.
WriteHtonU16
(m_data);
75
}
76
uint32_t
77
MyHeader::Deserialize
(
Buffer::Iterator
start
)
78
{
79
// we can deserialize two bytes from the start of the buffer.
80
// we read them in network byte order and store them
81
// in host byte order.
82
m_data = start.
ReadNtohU16
();
83
84
// we return the number of bytes effectively read.
85
return
2;
86
}
87
88
void
89
MyHeader::SetData
(uint16_t
data
)
90
{
91
m_data =
data
;
92
}
93
uint16_t
94
MyHeader::GetData
(
void
)
const
95
{
96
return
m_data;
97
}
98
99
100
101
int
main
(
int
argc,
char
*argv[])
102
{
103
// Enable the packet printing through Packet::Print command.
104
Packet::EnablePrinting
();
105
106
// instantiate a header.
107
MyHeader
sourceHeader;
108
sourceHeader.
SetData
(2);
109
110
// instantiate a packet
111
Ptr<Packet>
p = Create<Packet> ();
112
113
// and store my header into the packet.
114
p->
AddHeader
(sourceHeader);
115
116
// print the content of my packet on the standard output.
117
p->
Print
(std::cout);
118
std::cout << std::endl;
119
120
// you can now remove the header from the packet:
121
MyHeader
destinationHeader;
122
p->
RemoveHeader
(destinationHeader);
123
124
// and check that the destination and source
125
// headers contain the same values.
126
NS_ASSERT
(sourceHeader.
GetData
() == destinationHeader.
GetData
());
127
128
return
0;
129
}
src
network
examples
main-packet-header.cc
Generated on Tue Oct 9 2012 16:45:43 for ns-3 by
1.8.1.2