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
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
*/
8
#include "ns3/packet.h"
9
#include "ns3/simulator.h"
10
#include "ns3/tag.h"
11
#include "ns3/uinteger.h"
12
13
#include <iostream>
14
15
using namespace
ns3
;
16
17
/**
18
* \ingroup network
19
* A simple example of an Tag implementation
20
*/
21
class
MyTag
:
public
Tag
22
{
23
public
:
24
/**
25
* \brief Get the type ID.
26
* \return the object TypeId
27
*/
28
static
TypeId
GetTypeId
();
29
TypeId
GetInstanceTypeId
()
const override
;
30
uint32_t
GetSerializedSize
()
const override
;
31
void
Serialize
(
TagBuffer
i)
const override
;
32
void
Deserialize
(
TagBuffer
i)
override
;
33
void
Print
(std::ostream& os)
const override
;
34
35
// these are our accessors to our tag structure
36
/**
37
* Set the tag value
38
* \param value The tag value.
39
*/
40
void
SetSimpleValue
(uint8_t value);
41
/**
42
* Get the tag value
43
* \return the tag value.
44
*/
45
uint8_t
GetSimpleValue
()
const
;
46
47
private
:
48
uint8_t
m_simpleValue
;
//!< tag value
49
};
50
51
TypeId
52
MyTag::GetTypeId
()
53
{
54
static
TypeId
tid =
TypeId
(
"ns3::MyTag"
)
55
.
SetParent
<
Tag
>()
56
.AddConstructor<MyTag>()
57
.AddAttribute(
"SimpleValue"
,
58
"A simple value"
,
59
EmptyAttributeValue
(),
60
MakeUintegerAccessor
(&
MyTag::GetSimpleValue
),
61
MakeUintegerChecker<uint8_t>
());
62
return
tid;
63
}
64
65
TypeId
66
MyTag::GetInstanceTypeId
()
const
67
{
68
return
GetTypeId
();
69
}
70
71
uint32_t
72
MyTag::GetSerializedSize
()
const
73
{
74
return
1;
75
}
76
77
void
78
MyTag::Serialize
(
TagBuffer
i)
const
79
{
80
i.
WriteU8
(
m_simpleValue
);
81
}
82
83
void
84
MyTag::Deserialize
(
TagBuffer
i)
85
{
86
m_simpleValue
= i.
ReadU8
();
87
}
88
89
void
90
MyTag::Print
(std::ostream& os)
const
91
{
92
os <<
"v="
<< (
uint32_t
)
m_simpleValue
;
93
}
94
95
void
96
MyTag::SetSimpleValue
(uint8_t value)
97
{
98
m_simpleValue
= value;
99
}
100
101
uint8_t
102
MyTag::GetSimpleValue
()
const
103
{
104
return
m_simpleValue
;
105
}
106
107
int
108
main(
int
argc,
char
* argv[])
109
{
110
// create a tag.
111
MyTag
tag;
112
tag.
SetSimpleValue
(0x56);
113
114
// store the tag in a packet.
115
Ptr<Packet>
p =
Create<Packet>
(100);
116
p->AddPacketTag(tag);
117
118
// create a copy of the packet
119
Ptr<Packet>
aCopy = p->Copy();
120
121
// read the tag from the packet copy
122
MyTag
tagCopy;
123
p->PeekPacketTag(tagCopy);
124
125
// the copy and the original are the same !
126
NS_ASSERT
(tagCopy.
GetSimpleValue
() == tag.
GetSimpleValue
());
127
128
aCopy->PrintPacketTags(std::cout);
129
std::cout << std::endl;
130
131
return
0;
132
}
MyTag
A simple example of an Tag implementation.
Definition
main-packet-tag.cc:22
MyTag::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition
main-packet-tag.cc:52
MyTag::Serialize
void Serialize(TagBuffer i) const override
Definition
main-packet-tag.cc:78
MyTag::GetInstanceTypeId
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Definition
main-packet-tag.cc:66
MyTag::GetSerializedSize
uint32_t GetSerializedSize() const override
Definition
main-packet-tag.cc:72
MyTag::GetSimpleValue
uint8_t GetSimpleValue() const
Get the tag value.
Definition
main-packet-tag.cc:102
MyTag::SetSimpleValue
void SetSimpleValue(uint8_t value)
Set the tag value.
Definition
main-packet-tag.cc:96
MyTag::m_simpleValue
uint8_t m_simpleValue
tag value
Definition
main-packet-tag.cc:48
MyTag::Print
void Print(std::ostream &os) const override
Definition
main-packet-tag.cc:90
MyTag::Deserialize
void Deserialize(TagBuffer i) override
Definition
main-packet-tag.cc:84
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition
attribute.h:231
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition
mpi-test-fixtures.h:37
ns3::TagBuffer
read and write tag data
Definition
tag-buffer.h:41
ns3::TagBuffer::WriteU8
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition
tag-buffer.h:161
ns3::TagBuffer::ReadU8
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition
tag-buffer.h:185
ns3::Tag
tag a set of bytes in a packet
Definition
tag.h:28
ns3::TypeId
a unique identifier for an interface.
Definition
type-id.h:48
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition
type-id.cc:1001
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:55
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeUintegerChecker
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition
uinteger.h:85
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition
uinteger.h:35
src
network
examples
main-packet-tag.cc
Generated on Fri Nov 8 2024 13:59:04 for ns-3 by
1.11.0