implement Packet::Print
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 17 Mar 2008 17:37:25 -0700
changeset 2647 3e9474e1d77b
parent 2646 c1fef7686472
child 2648 736213ce179d
implement Packet::Print
src/common/chunk.cc
src/common/chunk.h
src/common/header.cc
src/common/header.h
src/common/packet-metadata.cc
src/common/packet-metadata.h
src/common/packet.cc
src/common/trailer.cc
src/common/trailer.h
src/common/wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/chunk.cc	Mon Mar 17 17:37:25 2008 -0700
@@ -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<ObjectBase> ()
+    ;
+  return tid;
+}
+
+} // namespace ns3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/common/chunk.h	Mon Mar 17 17:37:25 2008 -0700
@@ -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 */
--- a/src/common/header.cc	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/header.cc	Mon Mar 17 17:37:25 2008 -0700
@@ -11,7 +11,7 @@
 Header::GetTypeId (void)
 {
   static TypeId tid = TypeId ("ns3::Header")
-    .SetParent<ObjectBase> ()
+    .SetParent<Chunk> ()
     ;
   return tid;
 }
--- a/src/common/header.h	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/header.h	Mon Mar 17 17:37:25 2008 -0700
@@ -22,7 +22,7 @@
 #ifndef HEADER_H
 #define HEADER_H
 
-#include "ns3/object-base.h"
+#include "chunk.h"
 #include "buffer.h"
 #include <stdint.h>
 
@@ -40,7 +40,7 @@
  * 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);
--- a/src/common/packet-metadata.cc	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/packet-metadata.cc	Mon Mar 17 17:37:25 2008 -0700
@@ -1065,7 +1065,7 @@
     }
   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;
--- a/src/common/packet-metadata.h	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/packet-metadata.h	Mon Mar 17 17:37:25 2008 -0700
@@ -25,6 +25,7 @@
 #include <vector>
 #include "ns3/callback.h"
 #include "ns3/assert.h"
+#include "ns3/type-id.h"
 #include "buffer.h"
 
 namespace ns3 {
@@ -88,9 +89,10 @@
      * 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. 
      */
--- a/src/common/packet.cc	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/packet.cc	Mon Mar 17 17:37:25 2008 -0700
@@ -187,6 +187,65 @@
 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<ObjectBase *> constructor = item.tid.GetConstructor ();
+              NS_ASSERT (constructor.IsNull ());
+              ObjectBase *instance = constructor ();
+              NS_ASSERT (instance != 0);
+              Chunk *chunk = dynamic_cast<Chunk *> (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 
--- a/src/common/trailer.cc	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/trailer.cc	Mon Mar 17 17:37:25 2008 -0700
@@ -11,7 +11,7 @@
 Trailer::GetTypeId (void)
 {
   static TypeId tid = TypeId ("ns3::Trailer")
-    .SetParent<ObjectBase> ()
+    .SetParent<Chunk> ()
     ;
   return tid;
 }
--- a/src/common/trailer.h	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/trailer.h	Mon Mar 17 17:37:25 2008 -0700
@@ -22,7 +22,7 @@
 #ifndef TRAILER_H
 #define TRAILER_H
 
-#include "ns3/object-base.h"
+#include "chunk.h"
 #include "buffer.h"
 #include <stdint.h>
 
@@ -38,7 +38,7 @@
  *   - 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);
--- a/src/common/wscript	Mon Mar 17 14:49:52 2008 -0700
+++ b/src/common/wscript	Mon Mar 17 17:37:25 2008 -0700
@@ -9,6 +9,7 @@
         'packet.cc',
         'tags.cc',
         'tag-registry.cc',
+        'chunk.cc',
         'header.cc',
         'trailer.cc',
         'pcap-writer.cc',
@@ -20,6 +21,7 @@
     headers.module = 'common'
     headers.source = [
         'buffer.h',
+        'chunk.h',
         'header.h',
         'trailer.h',
         'tags.h',