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 <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
62 {
63  static TypeId tid = TypeId ("ns3::MyTag")
64  .SetParent<Tag> ()
65  .AddConstructor<MyTag> ()
66  .AddAttribute ("SimpleValue",
67  "A simple value",
70  MakeUintegerChecker<uint8_t> ())
71  ;
72  return tid;
73 }
74 TypeId
76 {
77  return GetTypeId ();
78 }
79 uint32_t
81 {
82  return 1;
83 }
84 void
86 {
87  i.WriteU8 (m_simpleValue);
88 }
89 void
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
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 }
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:849
static TypeId GetTypeId(void)
Get the type ID.
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:814
#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
uint8_t m_simpleValue
tag value
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
void SetSimpleValue(uint8_t value)
Set the tag value.
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:836
A class for an empty attribute value.
Definition: attribute.h:232
virtual void Serialize(TagBuffer i) const
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
tag a set of bytes in a packet
Definition: tag.h:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual uint32_t GetSerializedSize(void) const
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
virtual void Deserialize(TagBuffer i)
A simple example of an Tag implementation.
read and write tag data
Definition: tag-buffer.h:51
void Print(ComponentCarrier cc)
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
a unique identifier for an interface.
Definition: type-id.h:58
virtual void Print(std::ostream &os) const
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
uint8_t GetSimpleValue(void) const
Get the tag value.