00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ns3/simulator.h"
00022 #include "ns3/packet.h"
00023 #include "ns3/uinteger.h"
00024
00025 #include "wifi-mac-queue.h"
00026
00027 using namespace std;
00028
00029 namespace ns3 {
00030
00031 WifiMacQueue::Item::Item (Ptr<const Packet> packet,
00032 WifiMacHeader const &hdr,
00033 Time tstamp)
00034 : packet (packet), hdr (hdr), tstamp (tstamp)
00035 {}
00036
00037 TypeId
00038 WifiMacQueue::GetTypeId (void)
00039 {
00040 static TypeId tid = TypeId ("WifiMacQueue")
00041 .SetParent<Object> ()
00042 .AddConstructor<WifiMacQueue> ()
00043 .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are already this number of packets, it is dropped.",
00044 UintegerValue (400),
00045 MakeUintegerAccessor (&WifiMacQueue::m_maxSize),
00046 MakeUintegerChecker<uint32_t> ())
00047 .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the queue, it is dropped.",
00048 TimeValue (Seconds (10.0)),
00049 MakeTimeAccessor (&WifiMacQueue::m_maxDelay),
00050 MakeTimeChecker ())
00051 ;
00052 return tid;
00053 }
00054
00055 WifiMacQueue::WifiMacQueue ()
00056 : m_size (0)
00057 {}
00058
00059 WifiMacQueue::~WifiMacQueue ()
00060 {
00061 Flush ();
00062 }
00063
00064 void
00065 WifiMacQueue::SetMaxSize (uint32_t maxSize)
00066 {
00067 m_maxSize = maxSize;
00068 }
00069 void
00070 WifiMacQueue::SetMaxDelay (Time delay)
00071 {
00072 m_maxDelay = delay;
00073 }
00074 uint32_t
00075 WifiMacQueue::GetMaxSize (void) const
00076 {
00077 return m_maxSize;
00078 }
00079 Time
00080 WifiMacQueue::GetMaxDelay (void) const
00081 {
00082 return m_maxDelay;
00083 }
00084
00085 void
00086 WifiMacQueue::Enqueue (Ptr<const Packet> packet, WifiMacHeader const &hdr)
00087 {
00088 Cleanup ();
00089 if (m_size == m_maxSize)
00090 {
00091 return;
00092 }
00093 Time now = Simulator::Now ();
00094 m_queue.push_back (Item (packet, hdr, now));
00095 m_size++;
00096 }
00097 void
00098 WifiMacQueue::Cleanup (void)
00099 {
00100 if (m_queue.empty ())
00101 {
00102 return;
00103 }
00104
00105 Time now = Simulator::Now ();
00106 uint32_t n = 0;
00107 PacketQueueI end = m_queue.begin ();
00108 for (PacketQueueI i = m_queue.begin (); i != m_queue.end (); i++)
00109 {
00110 if (i->tstamp + m_maxDelay > now)
00111 {
00112 end = i;
00113 break;
00114 }
00115 n++;
00116 }
00117 m_size -= n;
00118 m_queue.erase (m_queue.begin (), end);
00119 }
00120
00121 Ptr<const Packet>
00122 WifiMacQueue::Dequeue (WifiMacHeader *hdr)
00123 {
00124 Cleanup ();
00125 if (!m_queue.empty ())
00126 {
00127 Item i = m_queue.front ();
00128 m_queue.pop_front ();
00129 m_size--;
00130 *hdr = i.hdr;
00131 return i.packet;
00132 }
00133 return 0;
00134 }
00135
00136
00137 bool
00138 WifiMacQueue::IsEmpty (void)
00139 {
00140 Cleanup ();
00141 return m_queue.empty ();
00142 }
00143
00144
00145 uint32_t
00146 WifiMacQueue::GetSize (void)
00147 {
00148 return m_size;
00149 }
00150
00151 void
00152 WifiMacQueue::Flush (void)
00153 {
00154 m_queue.erase (m_queue.begin (), m_queue.end ());
00155 m_size = 0;
00156 }
00157
00158 }