add trailer
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 02 Oct 2006 11:39:45 +0200
changeset 107 2cbaa2a1b116
parent 106 9b7cf31baf8c
child 108 6fd2357377ed
add trailer
SConstruct
src/common/packet.cc
src/common/packet.h
src/common/trailer.cc
src/common/trailer.h
--- a/SConstruct	Mon Oct 02 11:32:31 2006 +0200
+++ b/SConstruct	Mon Oct 02 11:39:45 2006 +0200
@@ -80,6 +80,7 @@
 common.add_sources ([
 	'buffer.cc',
 	'header.cc',
+	'trailer.cc',
 	'packet.cc',
 	'tags.cc',
 	'pcap-writer.cc',
@@ -90,6 +91,7 @@
 common.add_inst_headers ([
 	'buffer.h',
 	'header.h',
+	'trailer.h',
 	'tags.h',
 	'packet.h',
 	'ui-variable-tracer.h',
--- a/src/common/packet.cc	Mon Oct 02 11:32:31 2006 +0200
+++ b/src/common/packet.cc	Mon Oct 02 11:39:45 2006 +0200
@@ -74,6 +74,27 @@
 	assert (header.isDeserialized ());
 	m_buffer.removeAtStart (header.getSize ());
 }
+void 
+Packet::add (Trailer const &trailer)
+{
+	m_buffer.addAtEnd (trailer.getSize ());
+	Buffer::Iterator start = m_buffer.end ();
+	start.prev (trailer.getSize ());
+	trailer.serialize (start);
+}
+void 
+Packet::peek (Trailer &trailer)
+{
+	Buffer::Iterator start = m_buffer.end ();
+	start.prev (trailer.getSize ());
+	trailer.deserialize (start);
+}
+void 
+Packet::remove (Trailer const &trailer)
+{
+	assert (trailer.isDeserialized ());
+	m_buffer.removeAtEnd (trailer.getSize ());
+}
 
 
 void 
--- a/src/common/packet.h	Mon Oct 02 11:32:31 2006 +0200
+++ b/src/common/packet.h	Mon Oct 02 11:39:45 2006 +0200
@@ -24,6 +24,7 @@
 #include <stdint.h>
 #include "buffer.h"
 #include "header.h"
+#include "trailer.h"
 #include "tags.h"
 #include "ns3/callback.h"
 
@@ -142,6 +143,31 @@
 	 */
 	void remove (Header const &header);
 	/**
+	 * Add trailer to this packet. This method invokes the
+	 * ns3::Trailer::serializeTo method to request the trailer to serialize
+	 * itself in the packet buffer.
+	 *
+	 * \param trailer a reference to the trailer to add to this packet.
+	 */
+	void add (Trailer const &trailer);
+	/**
+	 * Deserialize trailer from this packet. This method invokes the
+	 * ns3::Trailer::deserializeFrom method to request the trailer to deserialize
+	 * itself from the packet buffer. This method does not remove
+	 * the data from the buffer. It merely reads it.
+	 *
+	 * \param trailer a reference to the trailer to deserialize from the buffer
+	 */
+	void peek (Trailer &trailer);
+	/**
+	 * Remove a deserialized trailer from the internal buffer.
+	 * This method removes the bytes read by Packet::peek from
+	 * the packet buffer.
+	 *
+	 * \param trailer a reference to the trailer to remove from the internal buffer.
+	 */
+	void remove (Trailer const &trailer);
+	/**
 	 * Attach a tag to this packet. The tag is fully copied
 	 * in a packet-specific internal buffer. This operation 
 	 * is expected to be really fast.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/trailer.cc	Mon Oct 02 11:39:45 2006 +0200
@@ -0,0 +1,68 @@
+/* -*-    Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:f -*- */
+/*
+ * Copyright (c) 2005 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 "trailer.h"
+#include <cassert>
+
+namespace ns3 {
+
+Trailer::Trailer ()
+    : m_isDeserialized (false) {}
+
+void 
+Trailer::print (std::ostream &os) const
+{
+    printTo (os);
+}
+uint32_t
+Trailer::getSize (void) const
+{
+	return getSerializedSize ();
+}
+void 
+Trailer::serialize (Buffer::Iterator start) const
+{
+    serializeTo (start);
+}
+void 
+Trailer::deserialize (Buffer::Iterator start)
+{
+    deserializeFrom (start);
+    m_isDeserialized = true;
+}
+bool 
+Trailer::isDeserialized (void) const
+{
+	return m_isDeserialized;
+}
+
+
+
+Trailer::~Trailer ()
+{}
+
+std::ostream& operator<< (std::ostream& os, Trailer const& trailer)
+{
+    trailer.print (os);
+    return os;
+}
+
+}; // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/trailer.h	Mon Oct 02 11:39:45 2006 +0200
@@ -0,0 +1,84 @@
+/* -*-    Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:f -*- */
+/*
+ * Copyright (c) 2005 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>
+ */
+
+#ifndef TRAILER_H
+#define TRAILER_H
+
+#include <stdint.h>
+#include <ostream>
+#include "buffer.h"
+
+namespace ns3 {
+
+/**
+ * \brief Protocol trailer serialization and deserialization.
+ *
+ * Every Protocol trailer which needs to be inserted and removed
+ * from a Packet instance must derive from this abstract base class
+ * and implement the private pure virtual methods listed below:
+ *   - ns3::Trailer::serializeTo
+ *   - ns3::Trailer::deserializeFrom
+ *   - ns3::Trailer::getSerializedSize
+ *   - ns3::Trailer::printTo
+ */
+class Trailer {
+public:
+    Trailer ();
+    /**
+     * Derived classes must provide an explicit virtual destructor
+     */
+    virtual ~Trailer () = 0;
+
+    void print (std::ostream &os) const;
+	uint32_t getSize (void) const;
+    void serialize (Buffer::Iterator start) const;
+    void deserialize (Buffer::Iterator start);
+	bool isDeserialized (void) const;
+private:
+    bool m_isDeserialized;
+    /**
+     * \param os the std output stream in which this 
+     *       protocol trailer must print itself.
+     */
+    virtual void printTo (std::ostream &os) const = 0;
+
+	/**
+	 * \returns the size of the serialized Trailer.
+	 */
+	virtual uint32_t getSerializedSize (void) const = 0;
+
+    /**
+     * \param start the buffer iterator in which the protocol trailer
+     *    must serialize itself.
+     */
+    virtual void serializeTo (Buffer::Iterator start) const = 0;
+    /**
+     * \param start the buffer iterator from which the protocol trailer must
+     *    deserialize itself.
+     */
+    virtual void deserializeFrom (Buffer::Iterator start) = 0;
+};
+
+std::ostream& operator<< (std::ostream& os, Trailer const& trailer);
+
+}; // namespace ns3
+
+#endif /* TRAILER_H */