A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-mac-queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005, 2009 INRIA
4  * Copyright (c) 2009 MIRKO BANCHI
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Author: Mirko Banchi <mk.banchi@gmail.com>
21  */
22 
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/uinteger.h"
26 
27 #include "wifi-mac-queue.h"
29 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (WifiMacQueue)
33  ;
34 
36  const WifiMacHeader &hdr,
37  Time tstamp)
38  : packet (packet),
39  hdr (hdr),
40  tstamp (tstamp)
41 {
42 }
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::WifiMacQueue")
48  .SetParent<Object> ()
49  .AddConstructor<WifiMacQueue> ()
50  .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are already this number of packets, it is dropped.",
51  UintegerValue (400),
52  MakeUintegerAccessor (&WifiMacQueue::m_maxSize),
53  MakeUintegerChecker<uint32_t> ())
54  .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the queue, it is dropped.",
55  TimeValue (Seconds (10.0)),
56  MakeTimeAccessor (&WifiMacQueue::m_maxDelay),
57  MakeTimeChecker ())
58  ;
59  return tid;
60 }
61 
63  : m_size (0)
64 {
65 }
66 
68 {
69  Flush ();
70 }
71 
72 void
73 WifiMacQueue::SetMaxSize (uint32_t maxSize)
74 {
75  m_maxSize = maxSize;
76 }
77 
78 void
80 {
81  m_maxDelay = delay;
82 }
83 
84 uint32_t
86 {
87  return m_maxSize;
88 }
89 
90 Time
92 {
93  return m_maxDelay;
94 }
95 
96 void
98 {
99  Cleanup ();
100  if (m_size == m_maxSize)
101  {
102  return;
103  }
104  Time now = Simulator::Now ();
105  m_queue.push_back (Item (packet, hdr, now));
106  m_size++;
107 }
108 
109 void
111 {
112  if (m_queue.empty ())
113  {
114  return;
115  }
116 
117  Time now = Simulator::Now ();
118  uint32_t n = 0;
119  for (PacketQueueI i = m_queue.begin (); i != m_queue.end ();)
120  {
121  if (i->tstamp + m_maxDelay > now)
122  {
123  i++;
124  }
125  else
126  {
127  i = m_queue.erase (i);
128  n++;
129  }
130  }
131  m_size -= n;
132 }
133 
136 {
137  Cleanup ();
138  if (!m_queue.empty ())
139  {
140  Item i = m_queue.front ();
141  m_queue.pop_front ();
142  m_size--;
143  *hdr = i.hdr;
144  return i.packet;
145  }
146  return 0;
147 }
148 
151 {
152  Cleanup ();
153  if (!m_queue.empty ())
154  {
155  Item i = m_queue.front ();
156  *hdr = i.hdr;
157  return i.packet;
158  }
159  return 0;
160 }
161 
165 {
166  Cleanup ();
167  Ptr<const Packet> packet = 0;
168  if (!m_queue.empty ())
169  {
170  PacketQueueI it;
171  for (it = m_queue.begin (); it != m_queue.end (); ++it)
172  {
173  if (it->hdr.IsQosData ())
174  {
175  if (GetAddressForPacket (type, it) == dest
176  && it->hdr.GetQosTid () == tid)
177  {
178  packet = it->packet;
179  *hdr = it->hdr;
180  m_queue.erase (it);
181  m_size--;
182  break;
183  }
184  }
185  }
186  }
187  return packet;
188 }
189 
193 {
194  Cleanup ();
195  if (!m_queue.empty ())
196  {
197  PacketQueueI it;
198  for (it = m_queue.begin (); it != m_queue.end (); ++it)
199  {
200  if (it->hdr.IsQosData ())
201  {
202  if (GetAddressForPacket (type, it) == dest
203  && it->hdr.GetQosTid () == tid)
204  {
205  *hdr = it->hdr;
206  return it->packet;
207  }
208  }
209  }
210  }
211  return 0;
212 }
213 
214 bool
216 {
217  Cleanup ();
218  return m_queue.empty ();
219 }
220 
221 uint32_t
223 {
224  return m_size;
225 }
226 
227 void
229 {
230  m_queue.erase (m_queue.begin (), m_queue.end ());
231  m_size = 0;
232 }
233 
236 {
237  if (type == WifiMacHeader::ADDR1)
238  {
239  return it->hdr.GetAddr1 ();
240  }
241  if (type == WifiMacHeader::ADDR2)
242  {
243  return it->hdr.GetAddr2 ();
244  }
245  if (type == WifiMacHeader::ADDR3)
246  {
247  return it->hdr.GetAddr3 ();
248  }
249  return 0;
250 }
251 
252 bool
254 {
255  PacketQueueI it = m_queue.begin ();
256  for (; it != m_queue.end (); it++)
257  {
258  if (it->packet == packet)
259  {
260  m_queue.erase (it);
261  m_size--;
262  return true;
263  }
264  }
265  return false;
266 }
267 
268 void
270 {
271  Cleanup ();
272  if (m_size == m_maxSize)
273  {
274  return;
275  }
276  Time now = Simulator::Now ();
277  m_queue.push_front (Item (packet, hdr, now));
278  m_size++;
279 }
280 
281 uint32_t
283  Mac48Address addr)
284 {
285  Cleanup ();
286  uint32_t nPackets = 0;
287  if (!m_queue.empty ())
288  {
289  PacketQueueI it;
290  for (it = m_queue.begin (); it != m_queue.end (); it++)
291  {
292  if (GetAddressForPacket (type, it) == addr)
293  {
294  if (it->hdr.IsQosData () && it->hdr.GetQosTid () == tid)
295  {
296  nPackets++;
297  }
298  }
299  }
300  }
301  return nPackets;
302 }
303 
306  const QosBlockedDestinations *blockedPackets)
307 {
308  Cleanup ();
309  Ptr<const Packet> packet = 0;
310  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
311  {
312  if (!it->hdr.IsQosData ()
313  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
314  {
315  *hdr = it->hdr;
316  timestamp = it->tstamp;
317  packet = it->packet;
318  m_queue.erase (it);
319  m_size--;
320  return packet;
321  }
322  }
323  return packet;
324 }
325 
328  const QosBlockedDestinations *blockedPackets)
329 {
330  Cleanup ();
331  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
332  {
333  if (!it->hdr.IsQosData ()
334  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
335  {
336  *hdr = it->hdr;
337  timestamp = it->tstamp;
338  return it->packet;
339  }
340  }
341  return 0;
342 }
343 
344 } // namespace ns3
Time m_maxDelay
Time to live for packets in the queue.
Keep track of destination address - TID pairs that are waiting for a block ACK response.
uint32_t GetSize(void)
Return the current queue size.
WifiMacHeader hdr
Wifi MAC header associated with the packet.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
void SetMaxDelay(Time delay)
Set the maximum delay before the packet is discarded.
Ptr< const Packet > packet
Actual packet.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
void Flush(void)
Flush the queue.
Ptr< const Packet > PeekByTidAndAddress(WifiMacHeader *hdr, uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr)
Searchs and returns, if is present in this queue, first packet having address indicated by type equal...
virtual void Cleanup(void)
Clean up the queue by removing packets that exceeded the maximum delay.
uint32_t GetNPacketsByTidAndAddress(uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr)
Returns number of QoS packets having tid equals to tid and address specified by type equals to addr...
bool Remove(Ptr< const Packet > packet)
If exists, removes packet from queue and returns true.
AddressType
Address types.
void SetMaxSize(uint32_t maxSize)
Set the maximum queue size.
Ptr< const Packet > DequeueFirstAvailable(WifiMacHeader *hdr, Time &tStamp, const QosBlockedDestinations *blockedPackets)
Returns first available packet for transmission.
Ptr< const Packet > DequeueByTidAndAddress(WifiMacHeader *hdr, uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr)
Searchs and returns, if is present in this queue, first packet having address indicated by type equal...
hold objects of type ns3::Time
Definition: nstime.h:961
bool IsEmpty(void)
Return if the queue is empty.
Hold an unsigned integer type.
Definition: uinteger.h:46
static TypeId GetTypeId(void)
uint32_t GetMaxSize(void) const
Return the maximum queue size.
Ptr< const Packet > Dequeue(WifiMacHeader *hdr)
Dequeue the packet in the front of the queue.
uint32_t m_size
Current queue size.
Ptr< const Packet > Peek(WifiMacHeader *hdr)
Peek the packet in the front of the queue.
void PushFront(Ptr< const Packet > packet, const WifiMacHeader &hdr)
Enqueue the given packet and its corresponding WifiMacHeader at the front of the queue.
an EUI-48 address
Definition: mac48-address.h:41
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
Mac48Address GetAddressForPacket(enum WifiMacHeader::AddressType type, PacketQueueI it)
Return the appropriate address for the given packet (given by PacketQueue iterator).
Ptr< const Packet > PeekFirstAvailable(WifiMacHeader *hdr, Time &tStamp, const QosBlockedDestinations *blockedPackets)
Returns first available packet for transmission.
void Enqueue(Ptr< const Packet > packet, const WifiMacHeader &hdr)
Enqueue the given packet and its corresponding WifiMacHeader at the end of the queue.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
Time GetMaxDelay(void) const
Return the maximum delay before the packet is discarded.
a base class which provides memory management and object aggregation
Definition: object.h:63
A struct that holds information about a packet for putting in a packet queue.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
std::list< struct Item >::iterator PacketQueueI
typedef for packet (struct Item) queue iterator.
Implements the IEEE 802.11 MAC header.
PacketQueue m_queue
Packet (struct Item) queue.
uint32_t m_maxSize
Queue capacity.
Item(Ptr< const Packet > packet, const WifiMacHeader &hdr, Time tstamp)
Create a struct with the given parameters.
bool IsBlocked(Mac48Address dest, uint8_t tid) const
Check if the given destination address and TID are blocked from sending (e.g.