Files
unison/src/common/packet.cc

317 lines
6.5 KiB
C++
Raw Normal View History

2006-11-01 13:11:30 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2006-08-29 17:42:13 +02:00
/*
* Copyright (c) 2005,2006 INRIA
* All rights reserved.
*
* 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>
*/
#include "packet.h"
#include "ns3/assert.h"
2006-08-29 17:42:13 +02:00
2006-08-29 17:55:34 +02:00
namespace ns3 {
2006-08-29 17:42:13 +02:00
uint32_t Packet::m_globalUid = 0;
2006-09-08 18:51:47 +02:00
2007-10-01 14:14:31 +02:00
void
Packet::Ref (void) const
{
m_refCount++;
}
void
Packet::Unref (void) const
{
m_refCount--;
if (m_refCount == 0)
{
delete this;
}
}
Ptr<Packet>
Packet::Copy (void) const
{
// we need to invoke the copy constructor directly
// rather than calling Create because the copy constructor
// is private.
return Ptr<Packet> (new Packet (*this), false);
}
2006-08-29 17:42:13 +02:00
Packet::Packet ()
2006-11-01 13:11:30 +01:00
: m_buffer (),
2007-10-01 14:14:31 +02:00
m_tags (),
m_metadata (m_globalUid, 0),
m_refCount (1)
2006-09-08 18:51:47 +02:00
{
m_globalUid++;
2006-09-08 18:51:47 +02:00
}
2006-08-29 17:42:13 +02:00
2007-10-01 14:14:31 +02:00
Packet::Packet (const Packet &o)
: m_buffer (o.m_buffer),
m_tags (o.m_tags),
m_metadata (o.m_metadata),
m_refCount (1)
{}
Packet &
Packet::operator = (const Packet &o)
{
if (this == &o)
{
return *this;
}
m_buffer = o.m_buffer;
m_tags = o.m_tags;
m_metadata = o.m_metadata;
return *this;
}
2006-08-29 17:42:13 +02:00
Packet::Packet (uint32_t size)
2006-11-01 13:11:30 +01:00
: m_buffer (size),
2007-10-01 14:14:31 +02:00
m_tags (),
m_metadata (m_globalUid, size),
m_refCount (1)
2006-09-08 18:51:47 +02:00
{
m_globalUid++;
2006-09-08 18:51:47 +02:00
}
Packet::Packet (uint8_t const*buffer, uint32_t size)
: m_buffer (),
2007-10-01 14:14:31 +02:00
m_tags (),
m_metadata (m_globalUid, size),
m_refCount (1)
{
m_globalUid++;
m_buffer.AddAtStart (size);
Buffer::Iterator i = m_buffer.Begin ();
i.Write (buffer, size);
}
2007-06-12 22:52:56 +02:00
Packet::Packet (Buffer buffer, Tags tags, PacketMetadata metadata)
2006-11-01 13:11:30 +01:00
: m_buffer (buffer),
m_tags (tags),
2007-10-01 14:14:31 +02:00
m_metadata (metadata),
m_refCount (1)
2006-08-29 17:42:13 +02:00
{}
2007-10-01 14:14:31 +02:00
Ptr<Packet>
2006-10-06 13:37:25 +02:00
Packet::CreateFragment (uint32_t start, uint32_t length) const
2006-08-29 17:42:13 +02:00
{
Buffer buffer = m_buffer.CreateFragment (start, length);
NS_ASSERT (m_buffer.GetSize () >= start + length);
uint32_t end = m_buffer.GetSize () - (start + length);
2007-06-12 22:52:56 +02:00
PacketMetadata metadata = m_metadata.CreateFragment (start, end);
2007-10-01 14:14:31 +02:00
// again, call the constructor directly rather than
// through Create because it is private.
return Ptr<Packet> (new Packet (buffer, m_tags, metadata), false);
2006-08-29 17:42:13 +02:00
}
uint32_t
2006-10-06 13:37:25 +02:00
Packet::GetSize (void) const
2006-08-29 17:42:13 +02:00
{
2006-11-01 13:11:30 +01:00
return m_buffer.GetSize ();
2006-08-29 17:42:13 +02:00
}
void
2007-10-01 14:14:31 +02:00
Packet::AddAtEnd (Ptr<const Packet> packet)
2006-08-29 17:42:13 +02:00
{
2007-10-01 14:14:31 +02:00
Buffer src = packet->m_buffer.CreateFullCopy ();
Buffer dst = m_buffer.CreateFullCopy ();
dst.AddAtEnd (src.GetSize ());
Buffer::Iterator destStart = dst.End ();
2006-11-01 13:11:30 +01:00
destStart.Prev (src.GetSize ());
destStart.Write (src.Begin (), src.End ());
m_buffer = dst;
2006-11-01 13:11:30 +01:00
/**
* XXX: we might need to merge the tag list of the
* other packet into the current packet.
*/
2007-10-01 14:14:31 +02:00
m_metadata.AddAtEnd (packet->m_metadata);
2006-08-29 17:42:13 +02:00
}
void
Packet::AddPaddingAtEnd (uint32_t size)
2006-08-29 17:42:13 +02:00
{
m_buffer.AddAtEnd (size);
2007-06-12 22:52:56 +02:00
m_metadata.AddPaddingAtEnd (size);
2006-08-29 17:42:13 +02:00
}
void
2006-10-06 13:37:25 +02:00
Packet::RemoveAtEnd (uint32_t size)
2006-08-29 17:42:13 +02:00
{
2006-11-01 13:11:30 +01:00
m_buffer.RemoveAtEnd (size);
2007-06-12 22:52:56 +02:00
m_metadata.RemoveAtEnd (size);
2006-08-29 17:42:13 +02:00
}
void
2006-10-06 13:37:25 +02:00
Packet::RemoveAtStart (uint32_t size)
2006-08-29 17:42:13 +02:00
{
2006-11-01 13:11:30 +01:00
m_buffer.RemoveAtStart (size);
2007-06-12 22:52:56 +02:00
m_metadata.RemoveAtStart (size);
2006-08-29 17:42:13 +02:00
}
void
2006-10-06 13:37:25 +02:00
Packet::RemoveAllTags (void)
2006-08-29 17:42:13 +02:00
{
2006-11-01 13:11:30 +01:00
m_tags.RemoveAll ();
2006-08-29 17:42:13 +02:00
}
2006-09-08 18:45:48 +02:00
uint8_t const *
2006-10-06 13:37:25 +02:00
Packet::PeekData (void) const
2006-09-08 18:45:48 +02:00
{
2006-11-01 13:11:30 +01:00
return m_buffer.PeekData ();
2006-09-08 18:45:48 +02:00
}
2006-09-08 18:51:47 +02:00
uint32_t
2006-10-06 13:37:25 +02:00
Packet::GetUid (void) const
2006-09-08 18:51:47 +02:00
{
2007-06-12 22:52:56 +02:00
return m_metadata.GetUid ();
2006-09-08 18:51:47 +02:00
}
void
Packet::PrintTags (std::ostream &os) const
{
m_tags.Print (os, " ");
}
void
Packet::Print (std::ostream &os) const
2007-05-29 16:09:20 +02:00
{
2008-03-17 11:45:36 -07:00
//XXX
2007-05-29 16:09:20 +02:00
}
2008-03-17 11:45:36 -07:00
PacketMetadata::ItemIterator
Packet::BeginItem (void) const
{
2008-03-17 11:45:36 -07:00
return m_metadata.BeginItem (m_buffer);
}
void
Packet::EnableMetadata (void)
{
2007-06-07 12:51:57 +02:00
PacketMetadata::Enable ();
}
Buffer
2007-08-04 13:28:17 +02:00
Packet::Serialize (void) const
{
Buffer buffer;
uint32_t reserve;
// write metadata
reserve = m_metadata.GetSerializedSize ();
buffer.AddAtStart (reserve);
m_metadata.Serialize (buffer.Begin (), reserve);
// write tags
reserve = m_tags.GetSerializedSize ();
buffer.AddAtStart (reserve);
m_tags.Serialize (buffer.Begin (), reserve);
// aggregate byte buffer, metadata, and tags
Buffer tmp = m_buffer.CreateFullCopy ();
buffer.AddAtStart (tmp.GetSize ());
buffer.Begin ().Write (tmp.Begin (), tmp.End ());
// write byte buffer size.
buffer.AddAtStart (4);
buffer.Begin ().WriteU32 (m_buffer.GetSize ());
return buffer;
}
void
Packet::Deserialize (Buffer buffer)
{
Buffer buf = buffer;
// read size
uint32_t packetSize = buf.Begin ().ReadU32 ();
buf.RemoveAtStart (4);
// read buffer.
buf.RemoveAtEnd (buf.GetSize () - packetSize);
m_buffer = buf;
// read tags
buffer.RemoveAtStart (4 + packetSize);
uint32_t tagsDeserialized = m_tags.Deserialize (buffer.Begin ());
buffer.RemoveAtStart (tagsDeserialized);
// read metadata
uint32_t metadataDeserialized =
m_metadata.Deserialize (buffer.Begin ());
buffer.RemoveAtStart (metadataDeserialized);
}
std::ostream& operator<< (std::ostream& os, const Packet &packet)
{
packet.Print (os);
return os;
}
} // namespace ns3
2007-06-15 18:10:40 +01:00
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
#include <string>
namespace ns3 {
class PacketTest: public Test {
public:
virtual bool RunTests (void);
PacketTest ();
};
PacketTest::PacketTest ()
: Test ("Packet") {}
bool
PacketTest::RunTests (void)
{
bool ok = true;
2007-10-01 14:14:31 +02:00
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
Ptr<Packet> pkt2 = Create<Packet> (reinterpret_cast<const uint8_t*> (" world"), 6);
Ptr<Packet> packet = Create<Packet> ();
packet->AddAtEnd (pkt1);
packet->AddAtEnd (pkt2);
2007-06-15 18:10:40 +01:00
2007-10-01 14:14:31 +02:00
if (packet->GetSize () != 11)
2007-06-15 18:10:40 +01:00
{
2007-10-01 14:14:31 +02:00
Failure () << "expected size 11, got " << packet->GetSize () << std::endl;
2007-06-15 18:10:40 +01:00
ok = false;
}
2007-10-01 14:14:31 +02:00
std::string msg = std::string (reinterpret_cast<const char *>(packet->PeekData ()),
packet->GetSize ());
2007-06-15 18:10:40 +01:00
if (msg != "hello world")
{
2007-09-10 16:55:03 +02:00
Failure () << "expected 'hello world', got '" << msg << "'" << std::endl;
2007-06-15 18:10:40 +01:00
ok = false;
}
return ok;
}
static PacketTest gPacketTest;
}; // namespace ns3
#endif /* RUN_SELF_TESTS */