A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include "ns3/log.h"
20 #include "ns3/trace-source-accessor.h"
21 #include "queue.h"
22 
23 NS_LOG_COMPONENT_DEFINE ("Queue");
24 
25 namespace ns3 {
26 
28  ;
29 
30 TypeId
32 {
33  static TypeId tid = TypeId ("ns3::Queue")
34  .SetParent<Object> ()
35  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
37  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
39  .AddTraceSource ("Drop", "Drop a packet stored in the queue.",
41  ;
42  return tid;
43 }
44 
46  m_nBytes (0),
47  m_nTotalReceivedBytes (0),
48  m_nPackets (0),
49  m_nTotalReceivedPackets (0),
50  m_nTotalDroppedBytes (0),
51  m_nTotalDroppedPackets (0)
52 {
53  NS_LOG_FUNCTION (this);
54 }
55 
57 {
58  NS_LOG_FUNCTION (this);
59 }
60 
61 
62 bool
64 {
65  NS_LOG_FUNCTION (this << p);
66 
67  //
68  // If DoEnqueue fails, Queue::Drop is called by the subclass
69  //
70  bool retval = DoEnqueue (p);
71  if (retval)
72  {
73  NS_LOG_LOGIC ("m_traceEnqueue (p)");
74  m_traceEnqueue (p);
75 
76  uint32_t size = p->GetSize ();
77  m_nBytes += size;
78  m_nTotalReceivedBytes += size;
79 
80  m_nPackets++;
82  }
83  return retval;
84 }
85 
88 {
89  NS_LOG_FUNCTION (this);
90 
91  Ptr<Packet> packet = DoDequeue ();
92 
93  if (packet != 0)
94  {
95  NS_ASSERT (m_nBytes >= packet->GetSize ());
96  NS_ASSERT (m_nPackets > 0);
97 
98  m_nBytes -= packet->GetSize ();
99  m_nPackets--;
100 
101  NS_LOG_LOGIC ("m_traceDequeue (packet)");
102  m_traceDequeue (packet);
103  }
104  return packet;
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION (this);
111  while (!IsEmpty ())
112  {
113  Dequeue ();
114  }
115 }
116 
118 Queue::Peek (void) const
119 {
120  NS_LOG_FUNCTION (this);
121  return DoPeek ();
122 }
123 
124 
125 uint32_t
126 Queue::GetNPackets (void) const
127 {
128  NS_LOG_FUNCTION (this);
129  NS_LOG_LOGIC ("returns " << m_nPackets);
130  return m_nPackets;
131 }
132 
133 uint32_t
134 Queue::GetNBytes (void) const
135 {
136  NS_LOG_FUNCTION (this);
137  NS_LOG_LOGIC (" returns " << m_nBytes);
138  return m_nBytes;
139 }
140 
141 bool
142 Queue::IsEmpty (void) const
143 {
144  NS_LOG_FUNCTION (this);
145  NS_LOG_LOGIC ("returns " << (m_nPackets == 0));
146  return m_nPackets == 0;
147 }
148 
149 uint32_t
151 {
152  NS_LOG_FUNCTION (this);
153  NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
154  return m_nTotalReceivedBytes;
155 }
156 
157 uint32_t
159 {
160  NS_LOG_FUNCTION (this);
161  NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
163 }
164 
165 uint32_t
167 {
168  NS_LOG_FUNCTION (this);
169  NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
170  return m_nTotalDroppedBytes;
171 }
172 
173 uint32_t
175 {
176  NS_LOG_FUNCTION (this);
177  NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
178  return m_nTotalDroppedPackets;
179 }
180 
181 void
183 {
184  NS_LOG_FUNCTION (this);
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this << p);
195 
197  m_nTotalDroppedBytes += p->GetSize ();
198 
199  NS_LOG_LOGIC ("m_traceDrop (p)");
200  m_traceDrop (p);
201 }
202 
203 } // namespace ns3
Queue()
Definition: queue.cc:45
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
bool Enqueue(Ptr< Packet > p)
Place a packet into the rear of the Queue.
Definition: queue.cc:63
uint32_t m_nTotalDroppedBytes
Definition: queue.h:175
virtual Ptr< Packet > DoDequeue(void)=0
bool IsEmpty(void) const
Definition: queue.cc:142
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:174
uint32_t m_nTotalReceivedBytes
Definition: queue.h:172
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:182
uint32_t m_nPackets
Definition: queue.h:173
NS_LOG_COMPONENT_DEFINE("Queue")
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:150
static TypeId GetTypeId(void)
Definition: queue.cc:31
virtual bool DoEnqueue(Ptr< Packet > p)=0
virtual ~Queue()
Definition: queue.cc:56
Ptr< Packet > Dequeue(void)
Remove a packet from the front of the Queue.
Definition: queue.cc:87
uint32_t GetNPackets(void) const
Definition: queue.cc:126
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
uint32_t m_nBytes
Definition: queue.h:171
TracedCallback< Ptr< const Packet > > m_traceDequeue
Definition: queue.h:168
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
TracedCallback< Ptr< const Packet > > m_traceDrop
Definition: queue.h:169
uint32_t GetNBytes(void) const
Definition: queue.cc:134
Ptr< const Packet > Peek(void) const
Get a copy of the item at the front of the queue without removing it.
Definition: queue.cc:118
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:158
uint32_t m_nTotalReceivedPackets
Definition: queue.h:174
uint32_t m_nTotalDroppedPackets
Definition: queue.h:176
void DequeueAll(void)
Flush the queue.
Definition: queue.cc:108
TracedCallback< Ptr< const Packet > > m_traceEnqueue
Definition: queue.h:167
virtual Ptr< const Packet > DoPeek(void) const =0
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:166
a base class which provides memory management and object aggregation
Definition: object.h:63
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
void Drop(Ptr< Packet > packet)
Drop a packet.
Definition: queue.cc:192