A Discrete-Event Network Simulator
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
26using namespace ns3;
27
32class MyTag : public Tag
33{
34public:
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;
57private:
58 uint8_t m_simpleValue;
59};
60
61TypeId
63{
64 static TypeId tid = TypeId ("ns3::MyTag")
65 .SetParent<Tag> ()
66 .AddConstructor<MyTag> ()
67 .AddAttribute ("SimpleValue",
68 "A simple value",
71 MakeUintegerChecker<uint8_t> ())
72 ;
73 return tid;
74}
75TypeId
77{
78 return GetTypeId ();
79}
82{
83 return 1;
84}
85void
87{
89}
90void
92{
93 m_simpleValue = i.ReadU8 ();
94}
95void
96MyTag::Print (std::ostream &os) const
97{
98 os << "v=" << (uint32_t)m_simpleValue;
99}
100void
102{
103 m_simpleValue = value;
104}
105uint8_t
107{
108 return m_simpleValue;
109}
110
111
112int 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}
A simple example of an Tag implementation.
virtual void Serialize(TagBuffer i) const
static TypeId GetTypeId(void)
Get the type ID.
void SetSimpleValue(uint8_t value)
Set the tag value.
uint8_t m_simpleValue
tag value
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
uint8_t GetSimpleValue(void) const
Get the tag value.
virtual uint32_t GetSerializedSize(void) const
virtual void Print(std::ostream &os) const
virtual void Deserialize(TagBuffer i)
A class for an empty attribute value.
Definition: attribute.h:233
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:991
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:956
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:978
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
read and write tag data
Definition: tag-buffer.h:52
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
tag a set of bytes in a packet
Definition: tag.h:37
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
#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
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.