/* -*- 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 * * Adapted from main-packet-tag.cc */ #include "ns3/tag.h" #include "ns3/packet.h" #include "ns3/uinteger.h" #include using namespace ns3; // define this class in a public header class MyTag : public Tag { public: static TypeId GetTypeId (void); virtual TypeId GetInstanceTypeId (void) const; virtual uint32_t GetSerializedSize (void) const; virtual void Serialize (TagBuffer i) const; virtual void Deserialize (TagBuffer i); virtual void Print (std::ostream &os) const; // these are our accessors to our tag structure void SetSimpleValue (uint8_t value); uint8_t GetSimpleValue (void) const; private: uint8_t m_simpleValue; }; TypeId MyTag::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MyTag") .SetParent () .AddConstructor () .AddAttribute ("SimpleValue", "A simple value", EmptyAttributeValue (), MakeUintegerAccessor (&MyTag::GetSimpleValue), MakeUintegerChecker ()) ; return tid; } TypeId MyTag::GetInstanceTypeId (void) const { return GetTypeId (); } uint32_t MyTag::GetSerializedSize (void) const { return 1; } void MyTag::Serialize (TagBuffer i) const { i.WriteU8 (m_simpleValue); } void MyTag::Deserialize (TagBuffer i) { m_simpleValue = i.ReadU8 (); } void MyTag::Print (std::ostream &os) const { os << "v=" << (uint32_t)m_simpleValue; } void MyTag::SetSimpleValue (uint8_t value) { m_simpleValue = value; } uint8_t MyTag::GetSimpleValue (void) const { return m_simpleValue; } int main (int argc, char *argv[]) { // Create 500 byte packet Ptr p = Create (500); MyTag myByteTag; myByteTag.SetSimpleValue (0x01); p->AddByteTag (myByteTag); std::cout << "First packet of 500 bytes tagged:" << std::endl; p->PrintByteTags (std::cout); std::cout << std::endl << std::endl; // Create second 500 byte packet Ptr p2 = Create (500); MyTag mySecondByteTag; mySecondByteTag.SetSimpleValue (0x02); p2->AddByteTag (mySecondByteTag); std::cout << "Second packet of 500 bytes tagged:" << std::endl; p2->PrintByteTags (std::cout); std::cout << std::endl << std::endl; // Concatenate the packets p2->AddAtEnd (p); std::cout << "First packet added to end of second packet, new packet of 1000 bytes:" << std::endl; p2->PrintByteTags (std::cout); std::cout << std::endl << std::endl; std::cout << "Fragment packet back into two 500 byte packets" << std::endl; Ptr p3 = p2->CreateFragment (0, 500); Ptr p4 = p2->CreateFragment (500, 500);; std::cout << "Fragment 1:" << std::endl; p3->PrintByteTags (std::cout); std::cout << std::endl << std::endl; std::cout << "Fragment 2:" << std::endl; p4->PrintByteTags (std::cout); std::cout << std::endl; return 0; }