samples/main-packet-tag.cc
changeset 3038 5962e2962fa9
parent 1866 e7dbcc4df546
child 3040 e11e106c7c19
--- a/samples/main-packet-tag.cc	Thu Apr 24 15:44:15 2008 -0700
+++ b/samples/main-packet-tag.cc	Thu Apr 24 15:44:35 2008 -0700
@@ -17,32 +17,22 @@
  *
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
  */
-#include "ns3/tag.h"
+#include "ns3/mtag.h"
 #include "ns3/packet.h"
+#include "ns3/uinteger.h"
 #include <iostream>
 
 using namespace ns3;
 
 // define this class in a public header
-class MyTag : public Tag
+class MyTag : public Mtag
 {
 public:
-  // we have to define a public constructor
-  MyTag ();
-  // we have to define a public copy constructor
-  MyTag (const MyTag &other);
-  // we have to define a public destructor
-  ~MyTag ();
-  // we have to define a public static GetUid method
-  static uint32_t GetUid (void);
-  // we have to define a public Print method
-  void Print (std::ostream &os) const;
-  // we have to define a public GetSerializedSize method
-  uint32_t GetSerializedSize (void) const;
-  // we have to define a public Serialize method
-  void Serialize (Buffer::Iterator i) const;
-  // we have to define a public Deserialize method
-  uint32_t Deserialize (Buffer::Iterator i);
+  static TypeId GetTypeId (void);
+  virtual TypeId GetInstanceTypeId (void) const;
+  virtual uint32_t GetSerializedSize (void) const;
+  virtual void Serialize (MtagBuffer i) const;
+  virtual void Deserialize (MtagBuffer i);
   
   // these are our accessors to our tag structure
   void SetSimpleValue (uint8_t value);
@@ -51,52 +41,40 @@
   uint8_t m_simpleValue;
 };
 
-MyTag::MyTag ()
-{}
-MyTag::MyTag (const MyTag &other)
-  : m_simpleValue (other.m_simpleValue)
-{}
-MyTag::~MyTag ()
-{}
-uint32_t 
-MyTag::GetUid (void)
+TypeId 
+MyTag::GetTypeId (void)
 {
-  // we input a unique string to AllocateUid
-  // to avoid name collisions.
-  static uint32_t uid = AllocateUid<MyTag> ("MyTag.tests.nsnam.org");
-  return uid;
+  static TypeId tid = TypeId ("ns3::MyTag")
+    .SetParent<Mtag> ()
+    .AddConstructor<MyTag> ()
+    .AddAttribute ("SimpleValue",
+                   "A simple value",
+                   EmptyAttributeValue (),
+                   MakeUintegerAccessor (&MyTag::GetSimpleValue),
+                   MakeUintegerChecker<uint8_t> ())
+    ;
+  return tid;
 }
-void 
-MyTag::Print (std::ostream &os) const
+TypeId 
+MyTag::GetInstanceTypeId (void) const
 {
-  // print the content of this tag for Packet::PrintTags
-  os << "MyTag=0x" << std::hex << (uint32_t)m_simpleValue << std::dec;
+  return GetTypeId ();
 }
 uint32_t 
 MyTag::GetSerializedSize (void) const
 {
-  // we do not want to deal with parallel simulations
-  // so we return 0.
-  return 0;
+  return 1;
 }
 void 
-MyTag::Serialize (Buffer::Iterator i) const
-{
-  // we will never be invoked because we are not doing
-  // parallel simulations so, we assert.
-  NS_ASSERT (false);
-}
-uint32_t
-MyTag::Deserialize (Buffer::Iterator i)
+MyTag::Serialize (MtagBuffer i) const
 {
-  // we will never be invoked because we are not doing
-  // parallel simulations so, we assert.
-  NS_ASSERT (false);
-  // theoretically, return the number of bytes read
-  return 0;
+  i.WriteU8 (m_simpleValue);
 }
-
-
+void 
+MyTag::Deserialize (MtagBuffer i)
+{
+  m_simpleValue = i.ReadU8 ();
+}
 void 
 MyTag::SetSimpleValue (uint8_t value)
 {
@@ -117,14 +95,14 @@
 
   // store the tag in a packet.
   Ptr<Packet> p = Create<Packet> ();
-  p->AddTag (tag);
+  p->AddMtag (tag);
 
   // create a copy of the packet
   Ptr<Packet> aCopy = p->Copy ();
 
   // read the tag from the packet copy
   MyTag tagCopy;
-  p->PeekTag (tagCopy);
+  p->FindFirstMatchingTag (tagCopy);
 
   // the copy and the original are the same !
   NS_ASSERT (tagCopy.GetSimpleValue () == tag.GetSimpleValue ());