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/abort.h"
20 #include "ns3/enum.h"
21 #include "ns3/uinteger.h"
22 #include "ns3/trace-source-accessor.h"
23 #include "queue.h"
24 
25 namespace ns3 {
26 
27 NS_LOG_COMPONENT_DEFINE ("Queue");
28 
29 NS_OBJECT_ENSURE_REGISTERED (QueueBase);
30 NS_OBJECT_TEMPLATE_CLASS_DEFINE (Queue,Packet);
31 NS_OBJECT_TEMPLATE_CLASS_DEFINE (Queue,QueueDiscItem);
32 
33 TypeId
GetTypeId(void)34 QueueBase::GetTypeId (void)
35 {
36   static TypeId tid = TypeId ("ns3::QueueBase")
37     .SetParent<Object> ()
38     .SetGroupName ("Network")
39     .AddTraceSource ("PacketsInQueue",
40                      "Number of packets currently stored in the queue",
41                      MakeTraceSourceAccessor (&QueueBase::m_nPackets),
42                      "ns3::TracedValueCallback::Uint32")
43     .AddTraceSource ("BytesInQueue",
44                      "Number of bytes currently stored in the queue",
45                      MakeTraceSourceAccessor (&QueueBase::m_nBytes),
46                      "ns3::TracedValueCallback::Uint32")
47   ;
48   return tid;
49 }
50 
QueueBase()51 QueueBase::QueueBase () :
52   m_nBytes (0),
53   m_nTotalReceivedBytes (0),
54   m_nPackets (0),
55   m_nTotalReceivedPackets (0),
56   m_nTotalDroppedBytes (0),
57   m_nTotalDroppedBytesBeforeEnqueue (0),
58   m_nTotalDroppedBytesAfterDequeue (0),
59   m_nTotalDroppedPackets (0),
60   m_nTotalDroppedPacketsBeforeEnqueue (0),
61   m_nTotalDroppedPacketsAfterDequeue (0)
62 {
63   NS_LOG_FUNCTION (this);
64   m_maxSize = QueueSize (QueueSizeUnit::PACKETS, std::numeric_limits<uint32_t>::max ());
65 }
66 
~QueueBase()67 QueueBase::~QueueBase ()
68 {
69   NS_LOG_FUNCTION (this);
70 }
71 
72 void
AppendItemTypeIfNotPresent(std::string & typeId,const std::string & itemType)73 QueueBase::AppendItemTypeIfNotPresent (std::string& typeId, const std::string& itemType)
74 {
75   if (typeId.back () != '>')
76     {
77       typeId.append ("<" + itemType + ">");
78     }
79 }
80 
81 bool
IsEmpty(void) const82 QueueBase::IsEmpty (void) const
83 {
84   NS_LOG_FUNCTION (this);
85   NS_LOG_LOGIC ("returns " << (m_nPackets.Get () == 0));
86   return m_nPackets.Get () == 0;
87 }
88 
89 uint32_t
GetNPackets(void) const90 QueueBase::GetNPackets (void) const
91 {
92   NS_LOG_FUNCTION (this);
93   NS_LOG_LOGIC ("returns " << m_nPackets);
94   return m_nPackets;
95 }
96 
97 uint32_t
GetNBytes(void) const98 QueueBase::GetNBytes (void) const
99 {
100   NS_LOG_FUNCTION (this);
101   NS_LOG_LOGIC (" returns " << m_nBytes);
102   return m_nBytes;
103 }
104 
105 QueueSize
GetCurrentSize(void) const106 QueueBase::GetCurrentSize (void) const
107 {
108   NS_LOG_FUNCTION (this);
109 
110   if (m_maxSize.GetUnit () == QueueSizeUnit::PACKETS)
111     {
112       return QueueSize (QueueSizeUnit::PACKETS, m_nPackets);
113     }
114   if (m_maxSize.GetUnit () == QueueSizeUnit::BYTES)
115     {
116       return QueueSize (QueueSizeUnit::BYTES, m_nBytes);
117     }
118   NS_ABORT_MSG ("Unknown queue size unit");
119 }
120 
121 uint32_t
GetTotalReceivedBytes(void) const122 QueueBase::GetTotalReceivedBytes (void) const
123 {
124   NS_LOG_FUNCTION (this);
125   NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
126   return m_nTotalReceivedBytes;
127 }
128 
129 uint32_t
GetTotalReceivedPackets(void) const130 QueueBase::GetTotalReceivedPackets (void) const
131 {
132   NS_LOG_FUNCTION (this);
133   NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
134   return m_nTotalReceivedPackets;
135 }
136 
137 uint32_t
GetTotalDroppedBytes(void) const138 QueueBase:: GetTotalDroppedBytes (void) const
139 {
140   NS_LOG_FUNCTION (this);
141   NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
142   return m_nTotalDroppedBytes;
143 }
144 
145 uint32_t
GetTotalDroppedBytesBeforeEnqueue(void) const146 QueueBase:: GetTotalDroppedBytesBeforeEnqueue (void) const
147 {
148   NS_LOG_FUNCTION (this);
149   NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytesBeforeEnqueue);
150   return m_nTotalDroppedBytesBeforeEnqueue;
151 }
152 
153 uint32_t
GetTotalDroppedBytesAfterDequeue(void) const154 QueueBase:: GetTotalDroppedBytesAfterDequeue (void) const
155 {
156   NS_LOG_FUNCTION (this);
157   NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytesAfterDequeue);
158   return m_nTotalDroppedBytesAfterDequeue;
159 }
160 
161 uint32_t
GetTotalDroppedPackets(void) const162 QueueBase::GetTotalDroppedPackets (void) const
163 {
164   NS_LOG_FUNCTION (this);
165   NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
166   return m_nTotalDroppedPackets;
167 }
168 
169 uint32_t
GetTotalDroppedPacketsBeforeEnqueue(void) const170 QueueBase::GetTotalDroppedPacketsBeforeEnqueue (void) const
171 {
172   NS_LOG_FUNCTION (this);
173   NS_LOG_LOGIC ("returns " << m_nTotalDroppedPacketsBeforeEnqueue);
174   return m_nTotalDroppedPacketsBeforeEnqueue;
175 }
176 
177 uint32_t
GetTotalDroppedPacketsAfterDequeue(void) const178 QueueBase::GetTotalDroppedPacketsAfterDequeue (void) const
179 {
180   NS_LOG_FUNCTION (this);
181   NS_LOG_LOGIC ("returns " << m_nTotalDroppedPacketsAfterDequeue);
182   return m_nTotalDroppedPacketsAfterDequeue;
183 }
184 
185 void
ResetStatistics(void)186 QueueBase::ResetStatistics (void)
187 {
188   NS_LOG_FUNCTION (this);
189   m_nTotalReceivedBytes = 0;
190   m_nTotalReceivedPackets = 0;
191   m_nTotalDroppedBytes = 0;
192   m_nTotalDroppedBytesBeforeEnqueue = 0;
193   m_nTotalDroppedBytesAfterDequeue = 0;
194   m_nTotalDroppedPackets = 0;
195   m_nTotalDroppedPacketsBeforeEnqueue = 0;
196   m_nTotalDroppedPacketsAfterDequeue = 0;
197 }
198 
199 void
SetMaxSize(QueueSize size)200 QueueBase::SetMaxSize (QueueSize size)
201 {
202   NS_LOG_FUNCTION (this << size);
203 
204   // do nothing if the size is null
205   if (!size.GetValue ())
206     {
207       return;
208     }
209 
210   m_maxSize = size;
211 
212   NS_ABORT_MSG_IF (size < GetCurrentSize (),
213                    "The new maximum queue size cannot be less than the current size");
214 }
215 
216 QueueSize
GetMaxSize(void) const217 QueueBase::GetMaxSize (void) const
218 {
219   NS_LOG_FUNCTION (this);
220   return m_maxSize;
221 }
222 
223 } // namespace ns3
224