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-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
27
// define this class in a public header
28
class
MyTag
:
public
Tag
29
{
30
public
:
31
static
TypeId
GetTypeId (
void
);
32
virtual
TypeId
GetInstanceTypeId (
void
)
const
;
33
virtual
uint32_t GetSerializedSize (
void
)
const
;
34
virtual
void
Serialize (
TagBuffer
i)
const
;
35
virtual
void
Deserialize (
TagBuffer
i);
36
virtual
void
Print (std::ostream &os)
const
;
37
38
// these are our accessors to our tag structure
39
void
SetSimpleValue (uint8_t value);
40
uint8_t GetSimpleValue (
void
)
const
;
41
private
:
42
uint8_t
m_simpleValue
;
43
};
44
45
TypeId
46
MyTag::GetTypeId
(
void
)
47
{
48
static
TypeId
tid =
TypeId
(
"ns3::MyTag"
)
49
.
SetParent
<
Tag
> ()
50
.AddConstructor<MyTag> ()
51
.AddAttribute (
"SimpleValue"
,
52
"A simple value"
,
53
EmptyAttributeValue
(),
54
MakeUintegerAccessor (&
MyTag::GetSimpleValue
),
55
MakeUintegerChecker<uint8_t> ())
56
;
57
return
tid;
58
}
59
TypeId
60
MyTag::GetInstanceTypeId
(
void
)
const
61
{
62
return
GetTypeId ();
63
}
64
uint32_t
65
MyTag::GetSerializedSize
(
void
)
const
66
{
67
return
1;
68
}
69
void
70
MyTag::Serialize
(
TagBuffer
i)
const
71
{
72
i.
WriteU8
(m_simpleValue);
73
}
74
void
75
MyTag::Deserialize
(
TagBuffer
i)
76
{
77
m_simpleValue = i.
ReadU8
();
78
}
79
void
80
MyTag::Print
(std::ostream &os)
const
81
{
82
os <<
"v="
<< (uint32_t)m_simpleValue;
83
}
84
void
85
MyTag::SetSimpleValue
(uint8_t value)
86
{
87
m_simpleValue = value;
88
}
89
uint8_t
90
MyTag::GetSimpleValue
(
void
)
const
91
{
92
return
m_simpleValue;
93
}
94
95
96
int
main
(
int
argc,
char
*argv[])
97
{
98
// create a tag.
99
MyTag
tag;
100
tag.
SetSimpleValue
(0x56);
101
102
// store the tag in a packet.
103
Ptr<Packet>
p = Create<Packet> (100);
104
p->
AddPacketTag
(tag);
105
106
// create a copy of the packet
107
Ptr<Packet>
aCopy = p->
Copy
();
108
109
// read the tag from the packet copy
110
MyTag
tagCopy;
111
p->
PeekPacketTag
(tagCopy);
112
113
// the copy and the original are the same !
114
NS_ASSERT
(tagCopy.
GetSimpleValue
() == tag.
GetSimpleValue
());
115
116
aCopy->
PrintPacketTags
(std::cout);
117
std::cout << std::endl;
118
119
return
0;
120
}
ns3::Ptr< Packet >
ns3::Packet::PrintPacketTags
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition:
packet.cc:876
MyTag::GetTypeId
static TypeId GetTypeId(void)
Definition:
main-packet-tag.cc:46
ns3::Packet::AddPacketTag
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition:
packet.cc:841
NS_ASSERT
#define NS_ASSERT(condition)
Definition:
assert.h:64
MyTag::m_simpleValue
uint8_t m_simpleValue
Definition:
main-packet-tag.cc:42
ns3::TagBuffer::ReadU8
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition:
tag-buffer.h:179
MyTag::SetSimpleValue
void SetSimpleValue(uint8_t value)
Definition:
main-packet-tag.cc:85
ns3::Packet::PeekPacketTag
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition:
packet.cc:863
ns3::EmptyAttributeValue
A class for an empty attribute value.
Definition:
attribute.h:222
MyTag::Serialize
virtual void Serialize(TagBuffer i) const
Definition:
main-packet-tag.cc:70
ns3::Packet::Copy
Ptr< Packet > Copy(void) const
Definition:
packet.cc:122
ns3::Tag
tag a set of bytes in a packet
Definition:
tag.h:36
MyTag::GetSerializedSize
virtual uint32_t GetSerializedSize(void) const
Definition:
main-packet-tag.cc:65
ns3::TagBuffer::WriteU8
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition:
tag-buffer.h:156
MyTag::Deserialize
virtual void Deserialize(TagBuffer i)
Definition:
main-packet-tag.cc:75
MyTag
Definition:
main-packet-tag.cc:28
ns3::TagBuffer
read and write tag data
Definition:
tag-buffer.h:51
main
int main(int argc, char *argv[])
Definition:
main-packet-tag.cc:96
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:49
MyTag::Print
virtual void Print(std::ostream &os) const
Definition:
main-packet-tag.cc:80
MyTag::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Definition:
main-packet-tag.cc:60
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Definition:
type-id.cc:611
MyTag::GetSimpleValue
uint8_t GetSimpleValue(void) const
Definition:
main-packet-tag.cc:90
src
network
examples
main-packet-tag.cc
Generated on Sat Apr 19 2014 14:07:05 for ns-3 by
1.8.6