Files
unison/samples/main-packet-tag.cc

121 lines
2.8 KiB
C++
Raw Normal View History

2007-08-08 11:15:45 +02:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2006,2007 INRIA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
*/
2008-04-24 16:06:33 -07:00
#include "ns3/tag.h"
2007-08-08 11:15:45 +02:00
#include "ns3/packet.h"
2008-04-24 15:44:35 -07:00
#include "ns3/uinteger.h"
2007-08-08 11:15:45 +02:00
#include <iostream>
using namespace ns3;
// define this class in a public header
2008-04-24 16:03:13 -07:00
class MyTag : public Tag
2007-08-08 11:15:45 +02:00
{
public:
2008-04-24 15:44:35 -07:00
static TypeId GetTypeId (void);
virtual TypeId GetInstanceTypeId (void) const;
virtual uint32_t GetSerializedSize (void) const;
2008-04-24 16:03:13 -07:00
virtual void Serialize (TagBuffer i) const;
virtual void Deserialize (TagBuffer i);
virtual void Print (std::ostream &os) const;
2007-08-08 11:15:45 +02:00
// these are our accessors to our tag structure
void SetSimpleValue (uint8_t value);
uint8_t GetSimpleValue (void) const;
private:
uint8_t m_simpleValue;
};
2008-04-24 15:44:35 -07:00
TypeId
MyTag::GetTypeId (void)
2007-08-08 11:15:45 +02:00
{
2008-04-24 15:44:35 -07:00
static TypeId tid = TypeId ("ns3::MyTag")
2008-04-24 16:03:13 -07:00
.SetParent<Tag> ()
2008-04-24 15:44:35 -07:00
.AddConstructor<MyTag> ()
.AddAttribute ("SimpleValue",
"A simple value",
EmptyAttributeValue (),
MakeUintegerAccessor (&MyTag::GetSimpleValue),
MakeUintegerChecker<uint8_t> ())
;
return tid;
2007-08-08 11:15:45 +02:00
}
2008-04-24 15:44:35 -07:00
TypeId
MyTag::GetInstanceTypeId (void) const
2007-08-08 11:15:45 +02:00
{
2008-04-24 15:44:35 -07:00
return GetTypeId ();
2007-08-08 11:15:45 +02:00
}
uint32_t
MyTag::GetSerializedSize (void) const
{
2008-04-24 15:44:35 -07:00
return 1;
2007-08-08 11:15:45 +02:00
}
void
2008-04-24 16:03:13 -07:00
MyTag::Serialize (TagBuffer i) const
2007-08-08 11:15:45 +02:00
{
2008-04-24 15:44:35 -07:00
i.WriteU8 (m_simpleValue);
2007-08-08 11:15:45 +02:00
}
2008-04-24 15:44:35 -07:00
void
2008-04-24 16:03:13 -07:00
MyTag::Deserialize (TagBuffer i)
2007-08-08 11:15:45 +02:00
{
2008-04-24 15:44:35 -07:00
m_simpleValue = i.ReadU8 ();
2007-08-08 11:15:45 +02:00
}
void
MyTag::Print (std::ostream &os) const
{
os << "v=" << (uint32_t)m_simpleValue;
}
void
2007-08-08 11:15:45 +02:00
MyTag::SetSimpleValue (uint8_t value)
{
m_simpleValue = value;
}
uint8_t
MyTag::GetSimpleValue (void) const
{
return m_simpleValue;
}
int main (int argc, char *argv[])
{
// create a tag.
MyTag tag;
tag.SetSimpleValue (0x56);
// store the tag in a packet.
Ptr<Packet> p = Create<Packet> (100);
2009-06-03 08:49:40 +02:00
p->AddPacketTag (tag);
2007-08-08 11:15:45 +02:00
// create a copy of the packet
Ptr<Packet> aCopy = p->Copy ();
2007-08-08 11:15:45 +02:00
// read the tag from the packet copy
MyTag tagCopy;
2009-06-03 08:49:40 +02:00
p->PeekPacketTag (tagCopy);
2007-08-08 11:15:45 +02:00
// the copy and the original are the same !
NS_ASSERT (tagCopy.GetSimpleValue () == tag.GetSimpleValue ());
2009-06-03 08:49:40 +02:00
aCopy->PrintPacketTags (std::cout);
2007-08-08 11:20:09 +02:00
std::cout << std::endl;
2007-08-08 11:15:45 +02:00
return 0;
}