implement ItemList::AddAtEnd
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 30 May 2007 09:48:53 +0200
changeset 794 f30d69393a24
parent 792 99423210502e
child 795 cd108c9817d0
implement ItemList::AddAtEnd
src/common/packet-history.cc
src/common/packet-history.h
--- a/src/common/packet-history.cc	Wed May 30 07:42:56 2007 +0200
+++ b/src/common/packet-history.cc	Wed May 30 09:48:53 2007 +0200
@@ -43,6 +43,8 @@
 
   void AddAtEnd (ItemList const *other);
 
+  void Print (std::ostream &os, ns3::Buffer buffer) const;
+
 private:
   enum Type {
     PAYLOAD,
@@ -66,6 +68,7 @@
   NS_ASSERT (m_itemList.empty ());
   struct Item item;
   item.m_type = ItemList::PAYLOAD;
+  item.m_chunkType = 0;
   item.m_size = size;
   item.m_fragmentStart = 0;
   item.m_fragmentEnd = item.m_size;
@@ -168,7 +171,7 @@
         {
           m_itemList.pop_back ();
           leftToRemove -= item.m_size;
-                }
+        }
       else
         {
           item.m_size -= leftToRemove;
@@ -176,7 +179,8 @@
           leftToRemove = 0;
         }
       NS_ASSERT (item.m_size == item.m_fragmentEnd - item.m_fragmentStart &&
-                 item.m_fragmentStart <= item.m_fragmentEnd);
+                 item.m_fragmentStart <= item.m_fragmentEnd &&
+                 item.m_fragmentEnd <= item.m_size);
     }
   NS_ASSERT (leftToRemove == 0);
 }
@@ -184,10 +188,30 @@
 void 
 ItemList::AddAtEnd (ItemList const *other)
 {
-  
+  for (std::list<ItemList::Item>::const_iterator i = other->m_itemList.begin (); 
+       i != other->m_itemList.end (); i++)
+    {
+      const ItemList::Item &item = *i;
+      ItemList::Item &last = m_itemList.back ();
+      if (item.m_uid == last.m_uid &&
+          item.m_type == last.m_type &&
+          item.m_chunkType == last.m_chunkType &&
+          item.m_size == last.m_size &&
+          last.m_fragmentEnd != last.m_size && 
+          item.m_fragmentStart == last.m_fragmentEnd)
+        {
+          last.m_fragmentEnd = item.m_fragmentEnd;
+        }
+      else
+        {
+          m_itemList.push_back (item);
+        }
+    }
 }
 
-
+void 
+ItemList::Print (std::ostream &os, ns3::Buffer buffer) const
+{}
 
 } // anonymous namespace
 
@@ -525,7 +549,7 @@
       /* This means that the LEB128 number was not valid.
        * ie: the last (5th) byte did not have the high-order bit zeroed.
        */
-      result = -1;
+      result = 0xffffffff;
     }
   return result;
 }
@@ -600,6 +624,7 @@
     {
       m_aggregated = true;
       uint32_t n = GetUleb128Size (PacketHistory::ADD_AT_END);
+      n += GetUleb128Size (o.m_end); 
       n += o.m_end;
       Reserve (n);
       memcpy (&m_data->m_data[m_end], o.m_data->m_data, o.m_end);
@@ -608,7 +633,7 @@
         {
           m_data->m_dirtyEnd = m_end;
         }
-      AppendOneCommand (PacketHistory::ADD_AT_END, 0);
+      AppendOneCommand (PacketHistory::ADD_AT_END, o.m_end);
     }
 }
 void
@@ -795,6 +820,17 @@
   // which are stored in this packet.
   uint8_t *dataBuffer = &m_data->m_data[0];
   ItemList itemList;
+  BuildItemList (&itemList, dataBuffer, m_end);
+  itemList.Print (os, buffer);
+}
+
+void 
+PacketHistory::BuildItemList (ItemList *list, uint8_t *buffer, uint32_t size) const
+{
+  // we need to build a linked list of the different fragments 
+  // which are stored in this packet.
+  uint8_t *dataBuffer = buffer;
+  ItemList itemList;
   for (uint32_t i = 0; i < m_n; i++)
     {
       uint32_t type = ReadForwardValue (&dataBuffer);
@@ -822,8 +858,11 @@
           uint32_t size = ReadForwardValue (&dataBuffer);
           itemList.RemTrailer (data, size);
         } break;
-        case ADD_AT_END:
-          break;
+        case ADD_AT_END: {
+          ItemList other;
+          BuildItemList (&other, dataBuffer, data);
+          itemList.AddAtEnd (&other);
+        } break;
         case REM_AT_START: {
           itemList.RemAtStart (data);
         } break;
--- a/src/common/packet-history.h	Wed May 30 07:42:56 2007 +0200
+++ b/src/common/packet-history.h	Wed May 30 09:48:53 2007 +0200
@@ -25,6 +25,10 @@
 #include <vector>
 #include "ns3/callback.h"
 
+namespace {
+class ItemList;
+}
+
 namespace ns3 {
 
 class Chunk;
@@ -69,8 +73,6 @@
   void RemoveAtEnd (uint32_t end);
 
   void PrintDefault (std::ostream &os, Buffer buffer) const;
-  void PrintSimple (std::ostream &os, Buffer buffer) const;
-  void PrintComplex (std::ostream &os, Buffer buffer) const;
   void Print (std::ostream &os, Buffer buffer, PacketPrinter printer) const;
 
 private:
@@ -121,6 +123,10 @@
   void RemoveHeader (uint32_t uid, Chunk const & header, uint32_t size);
   void AddTrailer (uint32_t uid, Chunk const & trailer, uint32_t size);
   void RemoveTrailer (uint32_t uid, Chunk const & trailer, uint32_t size);
+  void PrintSimple (std::ostream &os, Buffer buffer) const;
+  void PrintComplex (std::ostream &os, Buffer buffer) const;
+  void BuildItemList (ItemList *list, uint8_t *buffer, uint32_t size) const;
+
   static struct PacketHistory::CommandData *Create (uint32_t size);
   static void Recycle (struct CommandData *data);
   static struct PacketHistory::CommandData *Allocate (uint32_t n);