From e7abed2defc53542ef321067f5eee07dd1d07e87 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 30 May 2007 12:45:32 +0200 Subject: [PATCH] try to design a packer printer class --- src/common/packet-printer.cc | 55 +++++++++++++ src/common/packet-printer.h | 150 +++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 src/common/packet-printer.cc create mode 100644 src/common/packet-printer.h diff --git a/src/common/packet-printer.cc b/src/common/packet-printer.cc new file mode 100644 index 000000000..e8fcd44a7 --- /dev/null +++ b/src/common/packet-printer.cc @@ -0,0 +1,55 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 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 + */ + +#include "packet-printer.h" + +namespace ns3 { + +PacketPrinter::PacketPrinter () + : m_forward (true) +{} + +void +PacketPrinter::PrintForward (void) +{} +void +PacketPrinter::PrintBackward (void) +{} +void +PacketPrinter::AddPayloadPrinter (Callback printer) +{ + m_payloadPrinter = printer; +} + /** + * \param printer Arguments: output stream, packet uid, size, header/trailer name, fragment information + */ + void AddDefaultPrinter (Callback printer); + +} // namespace ns3 diff --git a/src/common/packet-printer.h b/src/common/packet-printer.h new file mode 100644 index 000000000..d298d60db --- /dev/null +++ b/src/common/packet-printer.h @@ -0,0 +1,150 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2007 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 + */ +#ifndef PACKET_PRINTER_H +#define PACKET_PRINTER_H + +#include "ns3/callback.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class PacketPrinter +{ +public: + /** + * \brief callback to print payload. + * + * Arguments: output stream, packet uid, size, fragment information + */ + typedef Callback + PayloadPrinter; + + /** + * \brief callback to print whole chunks. + * + * Arguments: output stream, packet uid, size, header/trailer type instance + */ + typedef Callback ChunkPrinter; + + /** + * \brief callback to print fragmented chunks. + * + * Arguments: output stream, packet uid, size, header/trailer name, fragment information + */ + typedef Callback + ChunkFragmentPrinter; + + /** + * \brief callback to print chunks for which no specific callback was specified. + * + * Arguments: output stream, packet uid, size, header/trailer name, fragment information + */ + typedef Callback + DefaultPrinter; + + struct FragmentInformation + { + uint32_t start; + uint32_t end; + }; + + /** + * Print the content of the packet forward. + */ + void PrintForward (void); + /** + * Print the content of the packet backward. + */ + void PrintBackward (void); + /** + * \param printer printer for payload + */ + void AddPayloadPrinter (PayloadPrinter printer); + /** + * \param printer printer for the specified chunk + * \param fragmentPrinter printer for a fragment of the specified chunk + */ + template + void AddPrinter (Callback printer, + ChunkFragmentPrinter fragmentPrinter); + /** + * \param printer printer for a chunk for which no callback was specified explicitely + */ + void AddDefaultPrinter (DefaultPrinter printer); + +private: + friend class PacketHistory; + + static PacketPrinter GetDefault (void); + template + static uint32_t GetUid (void) const; + static std::string GetName (uint32_t uid) const; + static T &CreateStatic (uint32_t uid) const; + + void PrintChunk (uint32_t uid, + Buffer::iterator i, + std::ostream &os, + uint32_t packetUid, + uint32_t size); + void PrintChunkFragment (uint32_t uid, + std::ostream &os, + uint32_t packetUid, + uint32_t size, + uint32_t fragmentStart, + uint32_t fragmentEnd); + void PrintPayload (std::ostream &os, uint32_t packetUid, uint32_t size, + uint32_t fragmentStart, uint32_t fragmentEnd); + struct Printer + { + uint32_t m_uid; + Ptr m_printer; + Callback + m_fragmentPrinter; + }; + std::vector m_printers; + bool m_forward; +}; + +} // namespace ns3 + +namespace ns3 { + +template +void +PacketHistory::AddPrinter (Callback printer, + Callback fragmentPrinter) +{ + uint32_t uid = PacketHistory::GetUid (); + struct Printer printer; + printer.m_uid = uid; + printer.m_printer = printer.PeekImpl (); + printer.m_fragmentPrinter = fragmentPrinter; + m_printers.push_back (printer); +} + +} // namespace ns3 + +#endif /* PACKET_PRINTER_H */