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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "ns3/packet.h"
9#include "ns3/simulator.h"
10#include "ns3/tag.h"
11#include "ns3/uinteger.h"
12
13#include <iostream>
14
15using namespace ns3;
16
17/**
18 * \ingroup network
19 * A simple example of an Tag implementation
20 */
21class MyTag : public Tag
22{
23 public:
24 /**
25 * \brief Get the type ID.
26 * \return the object TypeId
27 */
28 static TypeId GetTypeId();
29 TypeId GetInstanceTypeId() const override;
30 uint32_t GetSerializedSize() const override;
31 void Serialize(TagBuffer i) const override;
32 void Deserialize(TagBuffer i) override;
33 void Print(std::ostream& os) const override;
34
35 // these are our accessors to our tag structure
36 /**
37 * Set the tag value
38 * \param value The tag value.
39 */
40 void SetSimpleValue(uint8_t value);
41 /**
42 * Get the tag value
43 * \return the tag value.
44 */
45 uint8_t GetSimpleValue() const;
46
47 private:
48 uint8_t m_simpleValue; //!< tag value
49};
50
53{
54 static TypeId tid = TypeId("ns3::MyTag")
55 .SetParent<Tag>()
56 .AddConstructor<MyTag>()
57 .AddAttribute("SimpleValue",
58 "A simple value",
62 return tid;
63}
64
67{
68 return GetTypeId();
69}
70
73{
74 return 1;
75}
76
77void
82
83void
88
89void
90MyTag::Print(std::ostream& os) const
91{
92 os << "v=" << (uint32_t)m_simpleValue;
93}
94
95void
97{
98 m_simpleValue = value;
99}
100
101uint8_t
103{
104 return m_simpleValue;
105}
106
107int
108main(int argc, char* argv[])
109{
110 // create a tag.
111 MyTag tag;
112 tag.SetSimpleValue(0x56);
113
114 // store the tag in a packet.
116 p->AddPacketTag(tag);
117
118 // create a copy of the packet
119 Ptr<Packet> aCopy = p->Copy();
120
121 // read the tag from the packet copy
122 MyTag tagCopy;
123 p->PeekPacketTag(tagCopy);
124
125 // the copy and the original are the same !
126 NS_ASSERT(tagCopy.GetSimpleValue() == tag.GetSimpleValue());
127
128 aCopy->PrintPacketTags(std::cout);
129 std::cout << std::endl;
130
131 return 0;
132}
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:231
Smart pointer class similar to boost::intrusive_ptr.
read and write tag data
Definition tag-buffer.h:41
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition tag-buffer.h:161
TAG_BUFFER_INLINE uint8_t ReadU8()
Definition tag-buffer.h:185
tag a set of bytes in a packet
Definition tag.h:28
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition uinteger.h:35