A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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 <iostream>
24
25
using namespace
ns3
;
26
31
class
MyTag
:
public
Tag
32
{
33
public
:
38
static
TypeId
GetTypeId (
void
);
39
virtual
TypeId
GetInstanceTypeId (
void
)
const
;
40
virtual
uint32_t GetSerializedSize (
void
)
const
;
41
virtual
void
Serialize (
TagBuffer
i)
const
;
42
virtual
void
Deserialize (
TagBuffer
i);
43
virtual
void
Print
(std::ostream &os)
const
;
44
45
// these are our accessors to our tag structure
50
void
SetSimpleValue (uint8_t value);
55
uint8_t GetSimpleValue (
void
)
const
;
56
private
:
57
uint8_t
m_simpleValue
;
58
};
59
60
TypeId
61
MyTag::GetTypeId
(
void
)
62
{
63
static
TypeId
tid =
TypeId
(
"ns3::MyTag"
)
64
.
SetParent
<
Tag
> ()
65
.AddConstructor<MyTag> ()
66
.AddAttribute (
"SimpleValue"
,
67
"A simple value"
,
68
EmptyAttributeValue
(),
69
MakeUintegerAccessor
(&
MyTag::GetSimpleValue
),
70
MakeUintegerChecker<uint8_t> ())
71
;
72
return
tid;
73
}
74
TypeId
75
MyTag::GetInstanceTypeId
(
void
)
const
76
{
77
return
GetTypeId ();
78
}
79
uint32_t
80
MyTag::GetSerializedSize
(
void
)
const
81
{
82
return
1;
83
}
84
void
85
MyTag::Serialize
(
TagBuffer
i)
const
86
{
87
i.
WriteU8
(m_simpleValue);
88
}
89
void
90
MyTag::Deserialize
(
TagBuffer
i)
91
{
92
m_simpleValue = i.
ReadU8
();
93
}
94
void
95
MyTag::Print
(std::ostream &os)
const
96
{
97
os <<
"v="
<< (uint32_t)m_simpleValue;
98
}
99
void
100
MyTag::SetSimpleValue
(uint8_t value)
101
{
102
m_simpleValue = value;
103
}
104
uint8_t
105
MyTag::GetSimpleValue
(
void
)
const
106
{
107
return
m_simpleValue;
108
}
109
110
111
int
main (
int
argc,
char
*argv[])
112
{
113
// create a tag.
114
MyTag
tag;
115
tag.
SetSimpleValue
(0x56);
116
117
// store the tag in a packet.
118
Ptr<Packet>
p = Create<Packet> (100);
119
p->
AddPacketTag
(tag);
120
121
// create a copy of the packet
122
Ptr<Packet>
aCopy = p->
Copy
();
123
124
// read the tag from the packet copy
125
MyTag
tagCopy;
126
p->
PeekPacketTag
(tagCopy);
127
128
// the copy and the original are the same !
129
NS_ASSERT
(tagCopy.
GetSimpleValue
() == tag.
GetSimpleValue
());
130
131
aCopy->
PrintPacketTags
(std::cout);
132
std::cout << std::endl;
133
134
return
0;
135
}
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
MyTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
main-packet-tag.cc:80
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:923
MyTag::GetSimpleValue
uint8_t GetSimpleValue(void) const
Get the tag value.
Definition:
main-packet-tag.cc:105
ns3::Ptr< Packet >
MyTag::Print
virtual void Print(std::ostream &os) const
Definition:
main-packet-tag.cc:95
ns3::Tag
tag a set of bytes in a packet
Definition:
tag.h:37
ns3::TagBuffer::ReadU8
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition:
tag-buffer.h:195
MyTag::SetSimpleValue
void SetSimpleValue(uint8_t value)
Set the tag value.
Definition:
main-packet-tag.cc:100
ns3::TagBuffer::WriteU8
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition:
tag-buffer.h:172
Print
void Print(ComponentCarrier cc)
Definition:
lena-cc-helper.cc:69
ns3::Packet::AddPacketTag
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition:
packet.cc:956
MyTag::m_simpleValue
uint8_t m_simpleValue
tag value
Definition:
main-packet-tag.cc:57
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition:
attribute.h:233
ns3::Packet::Copy
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition:
packet.cc:121
ns3::MakeUintegerAccessor
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition:
uinteger.h:45
ns3::TagBuffer
read and write tag data
Definition:
tag-buffer.h:52
MyTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition:
main-packet-tag.cc:75
MyTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition:
main-packet-tag.cc:85
MyTag
A simple example of an Tag implementation.
Definition:
main-packet-tag.cc:32
MyTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition:
main-packet-tag.cc:90
MyTag::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition:
main-packet-tag.cc:61
ns3::Packet::PrintPacketTags
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition:
packet.cc:991
src
network
examples
main-packet-tag.cc
Generated on Fri Oct 1 2021 17:03:28 for ns-3 by
1.8.20