A Discrete-Event Network Simulator
API
 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
47 {
48  static TypeId tid = TypeId ("ns3::MyTag")
49  .SetParent<Tag> ()
50  .AddConstructor<MyTag> ()
51  .AddAttribute ("SimpleValue",
52  "A simple value",
54  MakeUintegerAccessor (&MyTag::GetSimpleValue),
55  MakeUintegerChecker<uint8_t> ())
56  ;
57  return tid;
58 }
59 TypeId
61 {
62  return GetTypeId ();
63 }
64 uint32_t
66 {
67  return 1;
68 }
69 void
71 {
72  i.WriteU8 (m_simpleValue);
73 }
74 void
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
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 }
void PrintPacketTags(std::ostream &os) const
Print the list of packet tags.
Definition: packet.cc:876
static TypeId GetTypeId(void)
void AddPacketTag(const Tag &tag) const
Add a packet tag.
Definition: packet.cc:841
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
uint8_t m_simpleValue
TAG_BUFFER_INLINE uint8_t ReadU8(void)
Definition: tag-buffer.h:195
void SetSimpleValue(uint8_t value)
bool PeekPacketTag(Tag &tag) const
Search a matching tag and call Tag::Deserialize if it is found.
Definition: packet.cc:863
A class for an empty attribute value.
Definition: attribute.h:222
virtual void Serialize(TagBuffer i) const
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:122
tag a set of bytes in a packet
Definition: tag.h:36
virtual uint32_t GetSerializedSize(void) const
TAG_BUFFER_INLINE void WriteU8(uint8_t v)
Definition: tag-buffer.h:172
virtual void Deserialize(TagBuffer i)
read and write tag data
Definition: tag-buffer.h:51
int main(int argc, char *argv[])
a unique identifier for an interface.
Definition: type-id.h:49
virtual void Print(std::ostream &os) const
virtual TypeId GetInstanceTypeId(void) const
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
uint8_t GetSimpleValue(void) const