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-tag.cc
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2006,2007 INRIA
3
*
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 as
6
* published by the Free Software Foundation;
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*
17
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18
*/
19
#include "ns3/packet.h"
20
#include "ns3/simulator.h"
21
#include "ns3/tag.h"
22
#include "ns3/uinteger.h"
23
24
#include <iostream>
25
26
using namespace
ns3
;
27
28
/**
29
* \ingroup network
30
* A simple example of an Tag implementation
31
*/
32
class
MyTag
:
public
Tag
33
{
34
public
:
35
/**
36
* \brief Get the type ID.
37
* \return the object TypeId
38
*/
39
static
TypeId
GetTypeId
();
40
TypeId
GetInstanceTypeId
()
const override
;
41
uint32_t
GetSerializedSize
()
const override
;
42
void
Serialize
(
TagBuffer
i)
const override
;
43
void
Deserialize
(
TagBuffer
i)
override
;
44
void
Print
(std::ostream& os)
const override
;
45
46
// these are our accessors to our tag structure
47
/**
48
* Set the tag value
49
* \param value The tag value.
50
*/
51
void
SetSimpleValue
(uint8_t value);
52
/**
53
* Get the tag value
54
* \return the tag value.
55
*/
56
uint8_t
GetSimpleValue
()
const
;
57
58
private
:
59
uint8_t
m_simpleValue
;
//!< tag value
60
};
61
62
TypeId
63
MyTag::GetTypeId
()
64
{
65
static
TypeId
tid =
TypeId
(
"ns3::MyTag"
)
66
.
SetParent
<
Tag
>()
67
.AddConstructor<MyTag>()
68
.AddAttribute(
"SimpleValue"
,
69
"A simple value"
,
70
EmptyAttributeValue
(),
71
MakeUintegerAccessor
(&
MyTag::GetSimpleValue
),
72
MakeUintegerChecker<uint8_t>());
73
return
tid;
74
}
75
76
TypeId
77
MyTag::GetInstanceTypeId
()
const
78
{
79
return
GetTypeId
();
80
}
81
82
uint32_t
83
MyTag::GetSerializedSize
()
const
84
{
85
return
1;
86
}
87
88
void
89
MyTag::Serialize
(
TagBuffer
i)
const
90
{
91
i.
WriteU8
(
m_simpleValue
);
92
}
93
94
void
95
MyTag::Deserialize
(
TagBuffer
i)
96
{
97
m_simpleValue
= i.
ReadU8
();
98
}
99
100
void
101
MyTag::Print
(std::ostream& os)
const
102
{
103
os <<
"v="
<< (
uint32_t
)
m_simpleValue
;
104
}
105
106
void
107
MyTag::SetSimpleValue
(uint8_t value)
108
{
109
m_simpleValue
= value;
110
}
111
112
uint8_t
113
MyTag::GetSimpleValue
()
const
114
{
115
return
m_simpleValue
;
116
}
117
118
int
119
main(
int
argc,
char
* argv[])
120
{
121
// create a tag.
122
MyTag
tag;
123
tag.
SetSimpleValue
(0x56);
124
125
// store the tag in a packet.
126
Ptr<Packet>
p = Create<Packet>(100);
127
p->AddPacketTag(tag);
128
129
// create a copy of the packet
130
Ptr<Packet>
aCopy = p->Copy();
131
132
// read the tag from the packet copy
133
MyTag
tagCopy;
134
p->PeekPacketTag(tagCopy);
135
136
// the copy and the original are the same !
137
NS_ASSERT
(tagCopy.
GetSimpleValue
() == tag.
GetSimpleValue
());
138
139
aCopy->PrintPacketTags(std::cout);
140
std::cout << std::endl;
141
142
return
0;
143
}
MyTag
A simple example of an Tag implementation.
Definition:
main-packet-tag.cc:33
MyTag::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition:
main-packet-tag.cc:63
MyTag::Serialize
void Serialize(TagBuffer i) const override
Definition:
main-packet-tag.cc:89
MyTag::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition:
main-packet-tag.cc:77
MyTag::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition:
main-packet-tag.cc:83
MyTag::GetSimpleValue
uint8_t GetSimpleValue() const
Get the tag value.
Definition:
main-packet-tag.cc:113
MyTag::SetSimpleValue
void SetSimpleValue(uint8_t value)
Set the tag value.
Definition:
main-packet-tag.cc:107
MyTag::m_simpleValue
uint8_t m_simpleValue
tag value
Definition:
main-packet-tag.cc:59
MyTag::Print
void Print(std::ostream &os) const override
Definition:
main-packet-tag.cc:101
MyTag::Deserialize
void Deserialize(TagBuffer i) override
Definition:
main-packet-tag.cc:95
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition:
attribute.h:242
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:77
ns3::TagBuffer
read and write tag data
Definition:
tag-buffer.h:52
ns3::TagBuffer::WriteU8
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition:
tag-buffer.h:172
ns3::TagBuffer::ReadU8
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition:
tag-buffer.h:196
ns3::Tag
tag a set of bytes in a packet
Definition:
tag.h:39
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:932
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:66
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition:
uinteger.h:46
src
network
examples
main-packet-tag.cc
Generated on Mon Aug 12 2024 11:16:17 for ns-3 by
1.9.6