From 8dc5510518178979cf128ba19707c153ddb46098 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Mon, 17 Mar 2008 17:37:25 -0700 Subject: [PATCH] implement Packet::Print --- src/common/chunk.cc | 16 ++++++++++ src/common/chunk.h | 19 +++++++++++ src/common/header.cc | 2 +- src/common/header.h | 4 +-- src/common/packet-metadata.cc | 2 +- src/common/packet-metadata.h | 6 ++-- src/common/packet.cc | 59 +++++++++++++++++++++++++++++++++++ src/common/trailer.cc | 2 +- src/common/trailer.h | 4 +-- src/common/wscript | 2 ++ 10 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 src/common/chunk.cc create mode 100644 src/common/chunk.h diff --git a/src/common/chunk.cc b/src/common/chunk.cc new file mode 100644 index 000000000..247a5f6a9 --- /dev/null +++ b/src/common/chunk.cc @@ -0,0 +1,16 @@ +#include "chunk.h" + +namespace ns3 { + +NS_OBJECT_ENSURE_REGISTERED (Chunk); + +TypeId +Chunk::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::Chunk") + .SetParent () + ; + return tid; +} + +} // namespace ns3 diff --git a/src/common/chunk.h b/src/common/chunk.h new file mode 100644 index 000000000..7e35fdba5 --- /dev/null +++ b/src/common/chunk.h @@ -0,0 +1,19 @@ +#ifndef CHUNK_H +#define CHUNK_H + +#include "ns3/object-base.h" +#include "buffer.h" + +namespace ns3 { + +class Chunk : public ObjectBase +{ + public: + static TypeId GetTypeId (void); + + virtual uint32_t Deserialize (Buffer::Iterator start) = 0; +}; + +} // namespace ns3 + +#endif /* CHUNK_H */ diff --git a/src/common/header.cc b/src/common/header.cc index 6a17e72c3..c31ed5ab0 100644 --- a/src/common/header.cc +++ b/src/common/header.cc @@ -11,7 +11,7 @@ TypeId Header::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Header") - .SetParent () + .SetParent () ; return tid; } diff --git a/src/common/header.h b/src/common/header.h index 71aba7752..af4e14e04 100644 --- a/src/common/header.h +++ b/src/common/header.h @@ -22,7 +22,7 @@ #ifndef HEADER_H #define HEADER_H -#include "ns3/object-base.h" +#include "chunk.h" #include "buffer.h" #include @@ -40,7 +40,7 @@ namespace ns3 { * Sample code which shows how to create a new type of Header, and how to use it, * is shown in the sample file samples/main-packet-header.cc */ -class Header : public ObjectBase +class Header : public Chunk { public: static TypeId GetTypeId (void); diff --git a/src/common/packet-metadata.cc b/src/common/packet-metadata.cc index 5a0d4d7ff..fb0c78be4 100644 --- a/src/common/packet-metadata.cc +++ b/src/common/packet-metadata.cc @@ -1065,7 +1065,7 @@ PacketMetadata::ItemIterator::Next (void) } m_current = smallItem.next; uint32_t uid = (smallItem.typeUid & 0xfffffffe) >> 1; - item.uid = uid; + item.tid.SetUid (uid); item.currentTrimedFromStart = extraItem.fragmentStart; item.currentTrimedFromEnd = extraItem.fragmentEnd - smallItem.size; item.currentSize = extraItem.fragmentEnd - extraItem.fragmentStart; diff --git a/src/common/packet-metadata.h b/src/common/packet-metadata.h index c1ca921ba..b02320672 100644 --- a/src/common/packet-metadata.h +++ b/src/common/packet-metadata.h @@ -25,6 +25,7 @@ #include #include "ns3/callback.h" #include "ns3/assert.h" +#include "ns3/type-id.h" #include "buffer.h" namespace ns3 { @@ -88,9 +89,10 @@ public: * false: this is a whole header, trailer, or, payload. */ bool isFragment; - /* uid of header or trailer. valid only if isPayload is false. + /* TypeId of Header or Trailer. Valid only if type is + * header or trailer. */ - uint32_t uid; + TypeId tid; /* size of item. If fragment, size of fragment. Otherwise, * size of original item. */ diff --git a/src/common/packet.cc b/src/common/packet.cc index 22bca8349..cbebbe93b 100644 --- a/src/common/packet.cc +++ b/src/common/packet.cc @@ -187,6 +187,65 @@ void Packet::Print (std::ostream &os) const { //XXX + PacketMetadata::ItemIterator i = m_metadata.BeginItem (m_buffer); + while (i.HasNext ()) + { + PacketMetadata::Item item = i.Next (); + if (item.isFragment) + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName (); + break; + } + os << " Fragment [" << item.currentTrimedFromStart<<":" + << (item.currentTrimedFromStart + item.currentSize) << "]"; + } + else + { + switch (item.type) { + case PacketMetadata::Item::PAYLOAD: + os << "Payload (size=" << item.currentSize << ")"; + break; + case PacketMetadata::Item::HEADER: + case PacketMetadata::Item::TRAILER: + os << item.tid.GetName () << "("; + { + NS_ASSERT (item.tid.HasConstructor ()); + Callback constructor = item.tid.GetConstructor (); + NS_ASSERT (constructor.IsNull ()); + ObjectBase *instance = constructor (); + NS_ASSERT (instance != 0); + Chunk *chunk = dynamic_cast (instance); + NS_ASSERT (chunk != 0); + chunk->Deserialize (item.current); + for (uint32_t j = 0; j < item.tid.GetAttributeListN (); j++) + { + std::string attrName = item.tid.GetAttributeName (j); + std::string value; + bool ok = chunk->GetAttribute (attrName, value); + NS_ASSERT (ok); + os << attrName << "=" << value; + if ((j + 1) < item.tid.GetAttributeListN ()) + { + os << ","; + } + } + } + os << ")"; + break; + } + } + if (i.HasNext ()) + { + os << " "; + } + } + } PacketMetadata::ItemIterator diff --git a/src/common/trailer.cc b/src/common/trailer.cc index 98cdaa6b6..fbe972945 100644 --- a/src/common/trailer.cc +++ b/src/common/trailer.cc @@ -11,7 +11,7 @@ TypeId Trailer::GetTypeId (void) { static TypeId tid = TypeId ("ns3::Trailer") - .SetParent () + .SetParent () ; return tid; } diff --git a/src/common/trailer.h b/src/common/trailer.h index f515211eb..d4aaea028 100644 --- a/src/common/trailer.h +++ b/src/common/trailer.h @@ -22,7 +22,7 @@ #ifndef TRAILER_H #define TRAILER_H -#include "ns3/object-base.h" +#include "chunk.h" #include "buffer.h" #include @@ -38,7 +38,7 @@ namespace ns3 { * - a default constructor: is used by the internal implementation * if the Packet class. */ -class Trailer : public ObjectBase +class Trailer : public Chunk { public: static TypeId GetTypeId (void); diff --git a/src/common/wscript b/src/common/wscript index 13a27d50b..89db5ace1 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -9,6 +9,7 @@ def build(bld): 'packet.cc', 'tags.cc', 'tag-registry.cc', + 'chunk.cc', 'header.cc', 'trailer.cc', 'pcap-writer.cc', @@ -20,6 +21,7 @@ def build(bld): headers.module = 'common' headers.source = [ 'buffer.h', + 'chunk.h', 'header.h', 'trailer.h', 'tags.h',