Hello,
I am currently trying to tag packets (with my own tags) with the following code :
class TagTesT : public Tag
{
public:
/**
* \brief Get the type ID.
* \return the object TypeId
*/
static TypeId GetTypeId (void);
virtual TypeId GetInstanceTypeId (void) const;
virtual uint32_t GetSerializedSize (void) const;
virtual void Serialize (TagBuffer buf) const;
virtual void Deserialize (TagBuffer buf);
virtual void Print (std::ostream &os) const;
TagTesT ();
TagTesT (uint32_t txNumber);
void SetTxNumber (uint32_t txNumber);
uint32_t GetTxNumber (void) const;
private:
uint32_t m_txNumber;
};
TypeId
TagTesT::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TagTesT")
.SetParent<Tag> ()
.AddConstructor<TagTesT> ()
;
return tid;
}
TypeId
TagTesT::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
uint32_t
TagTesT::GetSerializedSize (void) const
{
return 4;
}
void
TagTesT::Serialize (TagBuffer buf) const
{
buf.WriteU32 (m_txNumber);
}
void
TagTesT::Deserialize (TagBuffer buf)
{
m_txNumber = buf.ReadU32 ();
}
void
TagTesT::Print (std::ostream &os) const
{
os << "TxNumber=" << m_txNumber;
}
TagTesT::TagTesT ()
: Tag (), m_txNumber (0)
{
}
TagTesT::TagTesT (uint32_t txNumber)
: Tag (), m_txNumber (txNumber)
{
}
void
TagTesT::SetTxNumber (uint32_t id)
{
m_txNumber = id;
}
uint32_t
TagTesT::GetTxNumber (void) const
{
return m_txNumber;
}
int main (int argc, char *argv[])
{
Packet::EnablePrinting ();
uint32_t size = 256;
Ptr<Packet> packetUn = Create<Packet>(reinterpret_cast<const uint8_t*> ("u") ,size);
Ptr<Packet> packetDeux = Create<Packet>(size);
Ptr<Packet> packetTrois =Create<Packet>(size);
TagTesT packetTagUn = TagTesT(1);
TagTesT packetTagDeux = TagTesT(2);
TagTesT packetTagTrois = TagTesT(3);
std::cout<<"packetTagUn : "<<packetTagUn.GetTxNumber()<<std::endl;
std::cout<<"packetTagDeux : "<<packetTagDeux.GetTxNumber()<<std::endl;
std::cout<<"packetTagTrois : "<<packetTagTrois.GetTxNumber()<<std::endl;
packetUn->AddByteTag(packetTagUn);
packetDeux->AddByteTag(packetTagDeux);
packetTrois->AddByteTag(packetTagTrois);
packetUn->AddAtEnd(packetDeux);
packetUn->AddAtEnd(packetTrois);
Ptr<Packet> newPacketUn = packetUn->CreateFragment(0,256);
Ptr<Packet> newPacketDeux = packetUn->CreateFragment(256,256);
Ptr<Packet> newPacketTrois = packetUn->CreateFragment(512,256);
TagTesT tagUn;
TagTesT tagDeux;
TagTesT tagTrois;
bool foundUn = newPacketUn->FindFirstMatchingByteTag (tagUn);
bool foundDeux = newPacketDeux->FindFirstMatchingByteTag (tagDeux);
bool foundTrois = newPacketTrois->FindFirstMatchingByteTag (tagTrois);
if (foundUn)
{
std::cout<<"tagUn : "<<tagUn.GetTxNumber()<<std::endl;
}
if (foundDeux)
{
std::cout<<"tagDeux : "<<tagDeux.GetTxNumber()<<std::endl;
}
if (foundTrois)
{
std::cout<<"tagTrois : "<<tagTrois.GetTxNumber()<<std::endl;
}
return 0;
}
Here is the result :
packetTagUn : 1
packetTagDeux : 2
packetTagTrois : 3
tagUn : 1
tagDeux : 2
tagTrois : 2
Now if I change the creation of my packets in the main:
Ptr<Packet> packetUn = Create<Packet>(reinterpret_cast<const uint8_t*> ("u") ,size);
Ptr<Packet> packetDeux = Create<Packet>(size);
Ptr<Packet> packetTrois =Create<Packet>(size);
by :
Ptr<Packet> packetUn = Create<Packet>(size);
Ptr<Packet> packetDeux = Create<Packet>(size);
Ptr<Packet> packetTrois =Create<Packet>(size);
I obtain :
packetTagUn : 1
packetTagDeux : 2
packetTagTrois : 3
tagUn : 1
tagDeux : 1
tagTrois : 1
Is it a normal behavior ?