A Discrete-Event Network Simulator
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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#include "ns3/packet.h"
20#include "ns3/simulator.h"
21#include "ns3/tag.h"
22#include "ns3/uinteger.h"
23
24#include <iostream>
25
26using namespace ns3;
27
32class MyTag : public Tag
33{
34 public:
39 static TypeId GetTypeId();
40 TypeId GetInstanceTypeId() const override;
41 uint32_t GetSerializedSize() const override;
42 void Serialize(TagBuffer i) const override;
43 void Deserialize(TagBuffer i) override;
44 void Print(std::ostream& os) const override;
45
46 // these are our accessors to our tag structure
51 void SetSimpleValue(uint8_t value);
56 uint8_t GetSimpleValue() const;
57
58 private:
59 uint8_t m_simpleValue;
60};
61
64{
65 static TypeId tid = TypeId("ns3::MyTag")
66 .SetParent<Tag>()
67 .AddConstructor<MyTag>()
68 .AddAttribute("SimpleValue",
69 "A simple value",
72 MakeUintegerChecker<uint8_t>());
73 return tid;
74}
75
78{
79 return GetTypeId();
80}
81
84{
85 return 1;
86}
87
88void
90{
92}
93
94void
96{
98}
99
100void
101MyTag::Print(std::ostream& os) const
102{
103 os << "v=" << (uint32_t)m_simpleValue;
104}
105
106void
108{
109 m_simpleValue = value;
110}
111
112uint8_t
114{
115 return m_simpleValue;
116}
117
118int
119main(int argc, char* argv[])
120{
121 // create a tag.
122 MyTag tag;
123 tag.SetSimpleValue(0x56);
124
125 // store the tag in a packet.
126 Ptr<Packet> p = Create<Packet>(100);
127 p->AddPacketTag(tag);
128
129 // create a copy of the packet
130 Ptr<Packet> aCopy = p->Copy();
131
132 // read the tag from the packet copy
133 MyTag tagCopy;
134 p->PeekPacketTag(tagCopy);
135
136 // the copy and the original are the same !
137 NS_ASSERT(tagCopy.GetSimpleValue() == tag.GetSimpleValue());
138
139 aCopy->PrintPacketTags(std::cout);
140 std::cout << std::endl;
141
142 return 0;
143}
A simple example of an Tag implementation.
static TypeId GetTypeId()
Get the type ID.
void Serialize(TagBuffer i) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint32_t GetSerializedSize() const override
uint8_t GetSimpleValue() const
Get the tag value.
void SetSimpleValue(uint8_t value)
Set the tag value.
uint8_t m_simpleValue
tag value
void Print(std::ostream &os) const override
void Deserialize(TagBuffer i) override
A class for an empty attribute value.
Definition: attribute.h:242
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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()
Definition: tag-buffer.h:196
tag a set of bytes in a packet
Definition: tag.h:39
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.