samples/main-packet-tag.cc
changeset 3040 e11e106c7c19
parent 3038 5962e2962fa9
child 3041 a624276a897b
equal deleted inserted replaced
3039:722cf749a9e3 3040:e11e106c7c19
    23 #include <iostream>
    23 #include <iostream>
    24 
    24 
    25 using namespace ns3;
    25 using namespace ns3;
    26 
    26 
    27 // define this class in a public header
    27 // define this class in a public header
    28 class MyTag : public Mtag
    28 class MyTag : public Tag
    29 {
    29 {
    30 public:
    30 public:
    31   static TypeId GetTypeId (void);
    31   static TypeId GetTypeId (void);
    32   virtual TypeId GetInstanceTypeId (void) const;
    32   virtual TypeId GetInstanceTypeId (void) const;
    33   virtual uint32_t GetSerializedSize (void) const;
    33   virtual uint32_t GetSerializedSize (void) const;
    34   virtual void Serialize (MtagBuffer i) const;
    34   virtual void Serialize (TagBuffer i) const;
    35   virtual void Deserialize (MtagBuffer i);
    35   virtual void Deserialize (TagBuffer i);
    36   
    36   
    37   // these are our accessors to our tag structure
    37   // these are our accessors to our tag structure
    38   void SetSimpleValue (uint8_t value);
    38   void SetSimpleValue (uint8_t value);
    39   uint8_t GetSimpleValue (void) const;
    39   uint8_t GetSimpleValue (void) const;
    40 private:
    40 private:
    43 
    43 
    44 TypeId 
    44 TypeId 
    45 MyTag::GetTypeId (void)
    45 MyTag::GetTypeId (void)
    46 {
    46 {
    47   static TypeId tid = TypeId ("ns3::MyTag")
    47   static TypeId tid = TypeId ("ns3::MyTag")
    48     .SetParent<Mtag> ()
    48     .SetParent<Tag> ()
    49     .AddConstructor<MyTag> ()
    49     .AddConstructor<MyTag> ()
    50     .AddAttribute ("SimpleValue",
    50     .AddAttribute ("SimpleValue",
    51                    "A simple value",
    51                    "A simple value",
    52                    EmptyAttributeValue (),
    52                    EmptyAttributeValue (),
    53                    MakeUintegerAccessor (&MyTag::GetSimpleValue),
    53                    MakeUintegerAccessor (&MyTag::GetSimpleValue),
    64 MyTag::GetSerializedSize (void) const
    64 MyTag::GetSerializedSize (void) const
    65 {
    65 {
    66   return 1;
    66   return 1;
    67 }
    67 }
    68 void 
    68 void 
    69 MyTag::Serialize (MtagBuffer i) const
    69 MyTag::Serialize (TagBuffer i) const
    70 {
    70 {
    71   i.WriteU8 (m_simpleValue);
    71   i.WriteU8 (m_simpleValue);
    72 }
    72 }
    73 void 
    73 void 
    74 MyTag::Deserialize (MtagBuffer i)
    74 MyTag::Deserialize (TagBuffer i)
    75 {
    75 {
    76   m_simpleValue = i.ReadU8 ();
    76   m_simpleValue = i.ReadU8 ();
    77 }
    77 }
    78 void 
    78 void 
    79 MyTag::SetSimpleValue (uint8_t value)
    79 MyTag::SetSimpleValue (uint8_t value)
    93   MyTag tag;
    93   MyTag tag;
    94   tag.SetSimpleValue (0x56);
    94   tag.SetSimpleValue (0x56);
    95 
    95 
    96   // store the tag in a packet.
    96   // store the tag in a packet.
    97   Ptr<Packet> p = Create<Packet> ();
    97   Ptr<Packet> p = Create<Packet> ();
    98   p->AddMtag (tag);
    98   p->AddTag (tag);
    99 
    99 
   100   // create a copy of the packet
   100   // create a copy of the packet
   101   Ptr<Packet> aCopy = p->Copy ();
   101   Ptr<Packet> aCopy = p->Copy ();
   102 
   102 
   103   // read the tag from the packet copy
   103   // read the tag from the packet copy