new methods to look in the packet queue
authorMirko Banchi <mk.banchi@gmail.com>
Fri, 24 Apr 2009 09:10:14 +0200
changeset 4402 f6243535620f
parent 4401 427cec7079f3
child 4403 54141b0e8f9f
new methods to look in the packet queue
src/devices/wifi/wifi-mac-queue.cc
src/devices/wifi/wifi-mac-queue.h
--- a/src/devices/wifi/wifi-mac-queue.cc	Fri Apr 24 09:07:00 2009 +0200
+++ b/src/devices/wifi/wifi-mac-queue.cc	Fri Apr 24 09:10:14 2009 +0200
@@ -1,6 +1,7 @@
 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2005 INRIA
+ * Copyright (c) 2005, 2009 INRIA
+ * Copyright (c) 2009 MIRKO BANCHI
  *
  * 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 
@@ -16,8 +17,8 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ * Author: Mirko Banchi <mk.banchi@gmail.com>
  */
-
 #include "ns3/simulator.h"
 #include "ns3/packet.h"
 #include "ns3/uinteger.h"
@@ -30,7 +31,6 @@
 
 NS_OBJECT_ENSURE_REGISTERED (WifiMacQueue);
 
-
 WifiMacQueue::Item::Item (Ptr<const Packet> packet, 
                           WifiMacHeader const &hdr, 
                           Time tstamp)
@@ -69,16 +69,19 @@
 {
   m_maxSize = maxSize;
 }
-void 
+
+void
 WifiMacQueue::SetMaxDelay (Time delay)
 {
   m_maxDelay = delay;
 }
+
 uint32_t 
 WifiMacQueue::GetMaxSize (void) const
 {
   return m_maxSize;
 }
+
 Time 
 WifiMacQueue::GetMaxDelay (void) const
 {
@@ -97,6 +100,7 @@
   m_queue.push_back (Item (packet, hdr, now));
   m_size++;
 }
+
 void
 WifiMacQueue::Cleanup (void)
 {
@@ -136,6 +140,72 @@
   return 0;
 }
 
+Ptr<const Packet>
+WifiMacQueue::Peek (WifiMacHeader *hdr)
+{
+  Cleanup ();
+  if (!m_queue.empty ()) 
+    {
+      Item i = m_queue.front ();
+      *hdr = i.hdr;
+      return i.packet;
+    }
+  return 0;
+}
+
+Ptr<const Packet>
+WifiMacQueue::DequeueByTidAndAddress (WifiMacHeader *hdr, uint8_t tid, 
+                                      WifiMacHeader::AddressType index, Mac48Address dest)
+{
+  Cleanup ();
+  Ptr<const Packet> packet = 0;
+  if (!m_queue.empty ())
+    {
+      PacketQueueI it;
+      NS_ASSERT (index <= 4);
+      for (it = m_queue.begin (); it != m_queue.end (); ++it)
+        {
+          if (it->hdr.IsQosData ())
+            {
+              if (GetAddressForPacket (index, it) == dest &&
+                  it->hdr.GetQosTid () == tid)
+                {
+                  packet = it->packet;
+                  *hdr = it->hdr;
+                  m_queue.erase (it);
+                  m_size--;
+                  break;
+                }
+            }
+        }
+    }
+  return packet;
+}
+
+Ptr<const Packet>
+WifiMacQueue::PeekByTidAndAddress (WifiMacHeader *hdr, uint8_t tid, 
+                                   WifiMacHeader::AddressType index, Mac48Address dest)
+{
+  Cleanup ();
+  if (!m_queue.empty ())
+    {
+      PacketQueueI it;
+      NS_ASSERT (index <= 4);
+      for (it = m_queue.begin (); it != m_queue.end (); ++it)
+        {
+          if (it->hdr.IsQosData ())
+            {
+              if (GetAddressForPacket (index, it) == dest &&
+                  it->hdr.GetQosTid () == tid)
+                {
+                  *hdr = it->hdr;
+                  return it->packet;
+                }
+            }
+        }
+    }
+  return 0;
+}
 
 bool
 WifiMacQueue::IsEmpty (void)
@@ -144,7 +214,6 @@
   return m_queue.empty ();
 }
 
-
 uint32_t
 WifiMacQueue::GetSize (void)
 {
@@ -158,4 +227,37 @@
   m_size = 0;
 }
 
+Mac48Address
+WifiMacQueue::GetAddressForPacket (uint8_t index, PacketQueueI it)
+{
+  if (index == WifiMacHeader::ADDR1)
+    {
+      return it->hdr.GetAddr1 ();
+    }
+  if (index == WifiMacHeader::ADDR2)
+    {
+      return it->hdr.GetAddr2 ();
+    }
+  if (index == WifiMacHeader::ADDR3)
+    {
+      return it->hdr.GetAddr3 ();
+    }
+  return 0;
+}
+
+bool
+WifiMacQueue::Remove (Ptr<const Packet> packet)
+{
+  PacketQueueI it = m_queue.begin ();
+  for (; it != m_queue.end (); it++)
+    {
+      if (it->packet == packet)
+        {
+          m_queue.erase (it);
+          return true;
+        }
+    }
+  return false;
+}
+
 } // namespace ns3
--- a/src/devices/wifi/wifi-mac-queue.h	Fri Apr 24 09:07:00 2009 +0200
+++ b/src/devices/wifi/wifi-mac-queue.h	Fri Apr 24 09:10:14 2009 +0200
@@ -1,6 +1,7 @@
 /* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2005 INRIA
+ * Copyright (c) 2005, 2009 INRIA
+ * Copyright (c) 2009 MIRKO BANCHI
  *
  * 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 
@@ -16,11 +17,12 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
+ * Author: Mirko Banchi <mk.banchi@gmail.com>
  */
 #ifndef WIFI_MAC_QUEUE_H
 #define WIFI_MAC_QUEUE_H
 
-#include <deque>
+#include <list>
 #include <utility>
 #include "ns3/packet.h"
 #include "ns3/nstime.h"
@@ -60,25 +62,60 @@
 
   void Enqueue (Ptr<const Packet> packet, WifiMacHeader const &hdr);
   Ptr<const Packet> Dequeue (WifiMacHeader *hdr);
-
+  Ptr<const Packet> Peek (WifiMacHeader *hdr);
+  /**
+   * Searchs and returns, if is present in this queue, first packet having 
+   * address indicated by <i>index</i> equals to <i>addr</i>, and tid 
+   * equals to <i>tid</i>. This method removes the packet from this queue. 
+   * Is typically used by ns3::EdcaTxopN in order to perform correct MSDU 
+   * aggregation (A-MSDU).
+   */
+  Ptr<const Packet> DequeueByTidAndAddress (WifiMacHeader *hdr,
+                                            uint8_t tid, 
+                                            WifiMacHeader::AddressType index,
+                                            Mac48Address addr);
+  /**
+   * Searchs and returns, if is present in this queue, first packet having
+   * address indicated by <i>index</i> equals to <i>addr</i>, and tid 
+   * equals to <i>tid</i>. This method doesn't remove the packet from this queue.
+   * Is typically used by ns3::EdcaTxopN in order to perform correct MSDU
+   * aggregation (A-MSDU).
+   */
+  Ptr<const Packet> PeekByTidAndAddress (WifiMacHeader *hdr,
+                                         uint8_t tid,
+                                         WifiMacHeader::AddressType index,
+                                         Mac48Address addr);
+  /**
+   * If exists, removes <i>packet</i> from queue and returns true. Otherwise it
+   * takes no effects and return false. Deletion of the packet is
+   * performed in linear time (O(n)).  
+   */
+  bool Remove (Ptr<const Packet> packet);
+  
   void Flush (void);
 
   bool IsEmpty (void);
   uint32_t GetSize (void);
 
 private:
+  struct Item;
+  
+  typedef std::list<struct Item> PacketQueue;
+  typedef std::list<struct Item>::reverse_iterator PacketQueueRI;
+  typedef std::list<struct Item>::iterator PacketQueueI;
+  
   void Cleanup (void);
+  Mac48Address GetAddressForPacket (uint8_t index, PacketQueueI);
+  
   struct Item {
     Item (Ptr<const Packet> packet, 
-          WifiMacHeader const&hdr, 
+          WifiMacHeader const &hdr, 
           Time tstamp);
     Ptr<const Packet> packet;
     WifiMacHeader hdr;
     Time tstamp;
   };
-  typedef std::deque<struct Item> PacketQueue;
-  typedef std::deque<struct Item>::reverse_iterator PacketQueueRI;
-  typedef std::deque<struct Item>::iterator PacketQueueI;
+
   PacketQueue m_queue;
   WifiMacParameters *m_parameters;
   uint32_t m_size;
@@ -88,5 +125,4 @@
 
 } // namespace ns3
 
-
 #endif /* WIFI_MAC_QUEUE_H */