A Discrete-Event Network Simulator
API
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 namespace ns3 {
24 
25 NS_LOG_COMPONENT_DEFINE ("Queue");
26 
28 
29 TypeId
31 {
32  static TypeId tid = TypeId ("ns3::Queue")
33  .SetParent<Object> ()
34  .SetGroupName("Network")
35  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
37  "ns3::Packet::TracedCallback")
38  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
40  "ns3::Packet::TracedCallback")
41  .AddTraceSource ("Drop", "Drop a packet stored in the queue.",
43  "ns3::Packet::TracedCallback")
44  ;
45  return tid;
46 }
47 
49  m_nBytes (0),
50  m_nTotalReceivedBytes (0),
51  m_nPackets (0),
52  m_nTotalReceivedPackets (0),
53  m_nTotalDroppedBytes (0),
54  m_nTotalDroppedPackets (0)
55 {
56  NS_LOG_FUNCTION (this);
57 }
58 
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
64 
65 bool
67 {
68  NS_LOG_FUNCTION (this << p);
69 
70  //
71  // If DoEnqueue fails, Queue::Drop is called by the subclass
72  //
73  bool retval = DoEnqueue (p);
74  if (retval)
75  {
76  NS_LOG_LOGIC ("m_traceEnqueue (p)");
77  m_traceEnqueue (p);
78 
79  uint32_t size = p->GetSize ();
80  m_nBytes += size;
81  m_nTotalReceivedBytes += size;
82 
83  m_nPackets++;
85  }
86  return retval;
87 }
88 
91 {
92  NS_LOG_FUNCTION (this);
93 
94  Ptr<Packet> packet = DoDequeue ();
95 
96  if (packet != 0)
97  {
98  NS_ASSERT (m_nBytes >= packet->GetSize ());
99  NS_ASSERT (m_nPackets > 0);
100 
101  m_nBytes -= packet->GetSize ();
102  m_nPackets--;
103 
104  NS_LOG_LOGIC ("m_traceDequeue (packet)");
105  m_traceDequeue (packet);
106  }
107  return packet;
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION (this);
114  while (!IsEmpty ())
115  {
116  Dequeue ();
117  }
118 }
119 
121 Queue::Peek (void) const
122 {
123  NS_LOG_FUNCTION (this);
124  return DoPeek ();
125 }
126 
127 
128 uint32_t
129 Queue::GetNPackets (void) const
130 {
131  NS_LOG_FUNCTION (this);
132  NS_LOG_LOGIC ("returns " << m_nPackets);
133  return m_nPackets;
134 }
135 
136 uint32_t
137 Queue::GetNBytes (void) const
138 {
139  NS_LOG_FUNCTION (this);
140  NS_LOG_LOGIC (" returns " << m_nBytes);
141  return m_nBytes;
142 }
143 
144 bool
145 Queue::IsEmpty (void) const
146 {
147  NS_LOG_FUNCTION (this);
148  NS_LOG_LOGIC ("returns " << (m_nPackets == 0));
149  return m_nPackets == 0;
150 }
151 
152 uint32_t
154 {
155  NS_LOG_FUNCTION (this);
156  NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
157  return m_nTotalReceivedBytes;
158 }
159 
160 uint32_t
162 {
163  NS_LOG_FUNCTION (this);
164  NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
166 }
167 
168 uint32_t
170 {
171  NS_LOG_FUNCTION (this);
172  NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
173  return m_nTotalDroppedBytes;
174 }
175 
176 uint32_t
178 {
179  NS_LOG_FUNCTION (this);
180  NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
181  return m_nTotalDroppedPackets;
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this);
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this << p);
198 
200  m_nTotalDroppedBytes += p->GetSize ();
201 
202  NS_LOG_LOGIC ("m_traceDrop (p)");
203  m_traceDrop (p);
204 }
205 
206 } // namespace ns3
Queue()
Definition: queue.cc:48
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool Enqueue(Ptr< Packet > p)
Place a packet into the rear of the Queue.
Definition: queue.cc:66
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:194
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual Ptr< Packet > DoDequeue(void)=0
Pull a packet from the queue.
bool IsEmpty(void) const
Definition: queue.cc:145
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:177
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:191
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:185
uint32_t m_nPackets
Number of packets in the queue.
Definition: queue.h:192
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:153
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:30
virtual bool DoEnqueue(Ptr< Packet > p)=0
Push a packet in the queue.
virtual ~Queue()
Definition: queue.cc:59
Ptr< Packet > Dequeue(void)
Remove a packet from the front of the Queue.
Definition: queue.cc:90
uint32_t GetNPackets(void) const
Definition: queue.cc:129
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
uint32_t m_nBytes
Number of bytes in the queue.
Definition: queue.h:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback< Ptr< const Packet > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:186
TracedCallback< Ptr< const Packet > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:188
uint32_t GetNBytes(void) const
Definition: queue.cc:137
Ptr< const Packet > Peek(void) const
Get a copy of the item at the front of the queue without removing it.
Definition: queue.cc:121
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:161
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:193
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:195
void DequeueAll(void)
Flush the queue.
Definition: queue.cc:111
TracedCallback< Ptr< const Packet > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:184
virtual Ptr< const Packet > DoPeek(void) const =0
Peek the front packet in the queue.
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:169
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
void Drop(Ptr< Packet > packet)
Drop a packet.
Definition: queue.cc:195