src/common/packet.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 08 Sep 2006 18:51:47 +0200
changeset 91 8066ef8d9274
parent 90 d670ba9f726e
child 92 cd2ed251318a
permissions -rw-r--r--
add packet uid

/* -*-    Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:f -*- */
/*
 * 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 <cassert>

namespace ns3 {

uint32_t Packet::m_global_uid = 0;

Packet::Packet ()
    : m_buffer (),
	  m_uid (m_global_uid)
{
	m_global_uid++;
}

Packet::Packet (uint32_t size)
    : m_buffer (size),
	  m_uid (m_global_uid)
{
	m_global_uid++;
}
Packet::Packet (Buffer buffer, Tags tags)
    : m_buffer (buffer),
      m_tags (tags)
{}

Packet 
Packet::createFragment (uint32_t start, uint32_t length) const
{
    Buffer tmp = m_buffer.createFragment (start, length);
    return Packet (tmp, m_tags);
}

uint32_t 
Packet::getSize (void) const
{
    return m_buffer.getSize ();
}

void 
Packet::add (Chunk const&chunk)
{
    chunk.add (&m_buffer);
}

void 
Packet::peek (Chunk &chunk) const
{
    chunk.peek (&m_buffer);
}

void 
Packet::remove (Chunk &chunk)
{
    chunk.remove (&m_buffer);
}

void 
Packet::addAtEnd (Packet packet)
{
    Buffer src = packet.m_buffer;
    m_buffer.addAtEnd (src.getSize ());
    Buffer::Iterator destStart = m_buffer.end ();
    destStart.prev (src.getSize ());
    destStart.write (src.begin (), src.end ());
    /**
     * XXX: we might need to merge the tag list of the
     * other packet into the current packet.
     */
}
void 
Packet::addAtEnd (Packet packet, uint32_t start, uint32_t size)
{
    assert (packet.getSize () <= start + size);
    Buffer src = packet.m_buffer;
    m_buffer.addAtEnd (src.getSize ());
    Buffer::Iterator destStart = m_buffer.end ();
    destStart.prev (size);
    Buffer::Iterator srcStart = src.begin ();
    srcStart.next (start);
    Buffer::Iterator srcEnd = srcStart;
    srcEnd.next (size);
    destStart.write (srcStart, srcEnd);
    /**
     * XXX: we might need to merge the tag list of the
     * other packet into the current packet.
     */
}
void 
Packet::removeAtEnd (uint32_t size)
{
    m_buffer.removeAtEnd (size);
}
void 
Packet::removeAtStart (uint32_t size)
{
    m_buffer.removeAtStart (size);
}

void 
Packet::removeAllTags (void)
{
    m_tags.removeAll ();
}

uint8_t const *
Packet::peekData (void) const
{
	return m_buffer.peekData ();
}

uint32_t 
Packet::getUid (void)
{
	return m_uid;
}

}; // namespace ns3