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
}
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:268
ns3::Ptr< Packet >
main
int main(int argc, char *argv[])
Definition:
main-packet-header.cc:101
MyHeader::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
main-packet-header.cc:50
MyHeader::~MyHeader
virtual ~MyHeader()
Definition:
main-packet-header.cc:36
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
MyHeader::SetData
void SetData(uint16_t data)
Definition:
main-packet-header.cc:89
visualizer.core.start
def start
Definition:
core.py:1482
ns3::Packet::Print
void Print(std::ostream &os) const
Definition:
packet.cc:429
ns3::Buffer::Iterator
iterator in a Buffer instance
Definition:
buffer.h:98
MyHeader
Definition:
main-packet-header.cc:11
ns3::Packet::EnablePrinting
static void EnablePrinting(void)
By default, packets do not keep around enough metadata to perform the operations requested by the Pri...
Definition:
packet.cc:552
data
uint8_t data[writeSize]
Definition:
socket-bound-tcp-static-routing.cc:53
ns3::Buffer::Iterator::WriteHtonU16
void WriteHtonU16(uint16_t data)
Definition:
buffer.h:726
MyHeader::m_data
uint16_t m_data
Definition:
main-packet-header.cc:28
MyHeader::GetTypeId
static TypeId GetTypeId(void)
Definition:
main-packet-header.cc:41
MyHeader::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
main-packet-header.cc:64
MyHeader::GetData
uint16_t GetData(void) const
Definition:
main-packet-header.cc:94
MyHeader::Deserialize
virtual uint32_t Deserialize(Buffer::Iterator start)
Definition:
main-packet-header.cc:77
MyHeader::Print
virtual void Print(std::ostream &os) const
Definition:
main-packet-header.cc:56
ns3::Buffer::Iterator::ReadNtohU16
uint16_t ReadNtohU16(void)
Definition:
buffer.h:767
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:611
MyHeader::Serialize
virtual void Serialize(Buffer::Iterator start) const
Definition:
main-packet-header.cc:70
ns3::Packet::AddHeader
void AddHeader(const Header &header)
Add header to this packet.
Definition:
packet.cc:253
MyHeader::MyHeader
MyHeader()
Definition:
main-packet-header.cc:31
src
network
examples
main-packet-header.cc
Generated on Sat Apr 19 2014 14:07:05 for ns-3 by
1.8.6