A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
main-packet-tag.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2006,2007 INRIA
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19
*/
20
#include "ns3/tag.h"
21
#include "ns3/packet.h"
22
#include "ns3/uinteger.h"
23
#include "ns3/simulator.h"
24
#include <iostream>
25
26
using namespace
ns3
;
27
32
class
MyTag
:
public
Tag
33
{
34
public
:
39
static
TypeId
GetTypeId
(
void
);
40
virtual
TypeId
GetInstanceTypeId
(
void
)
const
;
41
virtual
uint32_t
GetSerializedSize
(
void
)
const
;
42
virtual
void
Serialize
(
TagBuffer
i)
const
;
43
virtual
void
Deserialize
(
TagBuffer
i);
44
virtual
void
Print
(std::ostream &os)
const
;
45
46
// these are our accessors to our tag structure
51
void
SetSimpleValue
(uint8_t value);
56
uint8_t
GetSimpleValue
(
void
)
const
;
57
private
:
58
uint8_t
m_simpleValue
;
59
};
60
61
TypeId
62
MyTag::GetTypeId
(
void
)
63
{
64
static
TypeId
tid =
TypeId
(
"ns3::MyTag"
)
65
.
SetParent
<
Tag
> ()
66
.AddConstructor<MyTag> ()
67
.AddAttribute (
"SimpleValue"
,
68
"A simple value"
,
69
EmptyAttributeValue
(),
70
MakeUintegerAccessor
(&
MyTag::GetSimpleValue
),
71
MakeUintegerChecker<uint8_t> ())
72
;
73
return
tid;
74
}
75
TypeId
76
MyTag::GetInstanceTypeId
(
void
)
const
77
{
78
return
GetTypeId
();
79
}
80
uint32_t
81
MyTag::GetSerializedSize
(
void
)
const
82
{
83
return
1;
84
}
85
void
86
MyTag::Serialize
(
TagBuffer
i)
const
87
{
88
i.
WriteU8
(
m_simpleValue
);
89
}
90
void
91
MyTag::Deserialize
(
TagBuffer
i)
92
{
93
m_simpleValue
= i.
ReadU8
();
94
}
95
void
96
MyTag::Print
(std::ostream &os)
const
97
{
98
os <<
"v="
<< (
uint32_t
)
m_simpleValue
;
99
}
100
void
101
MyTag::SetSimpleValue
(uint8_t value)
102
{
103
m_simpleValue
= value;
104
}
105
uint8_t
106
MyTag::GetSimpleValue
(
void
)
const
107
{
108
return
m_simpleValue
;
109
}
110
111
112
int
main (
int
argc,
char
*argv[])
113
{
114
// create a tag.
115
MyTag
tag;
116
tag.
SetSimpleValue
(0x56);
117
118
// store the tag in a packet.
119
Ptr<Packet>
p = Create<Packet> (100);
120
p->
AddPacketTag
(tag);
121
122
// create a copy of the packet
123
Ptr<Packet>
aCopy = p->
Copy
();
124
125
// read the tag from the packet copy
126
MyTag
tagCopy;
127
p->
PeekPacketTag
(tagCopy);
128
129
// the copy and the original are the same !
130
NS_ASSERT
(tagCopy.
GetSimpleValue
() == tag.
GetSimpleValue
());
131
132
aCopy->
PrintPacketTags
(std::cout);
133
std::cout << std::endl;
134
135
return
0;
136
}
MyTag
A simple example of an Tag implementation.
Definition:
main-packet-tag.cc:33
MyTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition:
main-packet-tag.cc:86
MyTag::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
main-packet-tag.cc:62
MyTag::SetSimpleValue
void SetSimpleValue(uint8_t value)
Set the tag value.
Definition:
main-packet-tag.cc:101
MyTag::m_simpleValue
uint8_t m_simpleValue
tag value
Definition:
main-packet-tag.cc:58
MyTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
main-packet-tag.cc:76
MyTag::GetSimpleValue
uint8_t GetSimpleValue(void) const
Get the tag value.
Definition:
main-packet-tag.cc:106
MyTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
main-packet-tag.cc:81
MyTag::Print
virtual void Print(std::ostream &os) const
Definition:
main-packet-tag.cc:96
MyTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition:
main-packet-tag.cc:91
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition:
attribute.h:233
ns3::Packet::PrintPacketTags
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition:
packet.cc:991
ns3::Packet::AddPacketTag
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition:
packet.cc:956
ns3::Packet::PeekPacketTag
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition:
packet.cc:978
ns3::Packet::Copy
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition:
packet.cc:121
ns3::Ptr< Packet >
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(void)
Definition:
tag-buffer.h:195
ns3::Tag
tag a set of bytes in a packet
Definition:
tag.h:37
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:922
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:67
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition:
uinteger.h:45
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
network
examples
main-packet-tag.cc
Generated on Sun May 1 2022 12:01:39 for ns-3 by
1.9.3