A Discrete-Event Network Simulator
API
queue-size.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Universita' degli Studi di Napoli Federico II
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Stefano Avallone <stavallo@unina.it>
18  */
19 
20 #include "queue-size.h"
21 
22 #include "ns3/log.h"
23 
24 namespace ns3
25 {
26 
27 NS_LOG_COMPONENT_DEFINE("QueueSize");
28 
30 
31 /* static */
32 bool
33 QueueSize::DoParse(const std::string s, QueueSizeUnit* unit, uint32_t* value)
34 {
35  NS_LOG_FUNCTION(s << unit << value);
36  std::string::size_type n = s.find_first_not_of("0123456789.");
37  if (n != std::string::npos)
38  { // Found non-numeric
39  std::istringstream iss;
40  iss.str(s.substr(0, n));
41  double r;
42  iss >> r;
43  std::string trailer = s.substr(n, std::string::npos);
44  if (trailer == "B")
45  {
46  // bytes
47  *unit = QueueSizeUnit::BYTES;
48  *value = static_cast<uint32_t>(r);
49  }
50  else if (trailer == "kB" || trailer == "KB")
51  {
52  // kilobytes
53  *unit = QueueSizeUnit::BYTES;
54  *value = static_cast<uint32_t>(r * 1000);
55  }
56  else if (trailer == "KiB")
57  {
58  // kibibytes
59  *unit = QueueSizeUnit::BYTES;
60  *value = static_cast<uint32_t>(r * 1024);
61  }
62  else if (trailer == "MB")
63  {
64  // MegaBytes
65  *unit = QueueSizeUnit::BYTES;
66  *value = static_cast<uint32_t>(r * 1000000);
67  }
68  else if (trailer == "MiB")
69  {
70  // MebiBytes
71  *unit = QueueSizeUnit::BYTES;
72  *value = static_cast<uint32_t>(r * 1048576);
73  }
74  else if (trailer == "p")
75  {
76  // packets
77  *unit = QueueSizeUnit::PACKETS;
78  *value = static_cast<uint32_t>(r);
79  }
80  else if (trailer == "kp" || trailer == "Kp")
81  {
82  // kilopackets
83  *unit = QueueSizeUnit::PACKETS;
84  *value = static_cast<uint32_t>(r * 1000);
85  }
86  else if (trailer == "Kip")
87  {
88  // kibipackets
89  *unit = QueueSizeUnit::PACKETS;
90  *value = static_cast<uint32_t>(r * 1024);
91  }
92  else if (trailer == "Mp")
93  {
94  // MegaPackets
95  *unit = QueueSizeUnit::PACKETS;
96  *value = static_cast<uint32_t>(r * 1000000);
97  }
98  else if (trailer == "Mip")
99  {
100  // MebiPackets
101  *unit = QueueSizeUnit::PACKETS;
102  *value = static_cast<uint32_t>(r * 1048576);
103  }
104  else
105  {
106  return false; // unsupported unit string
107  }
108  return true;
109  }
110  return false; // a unit string is required
111 }
112 
114  : m_unit(QueueSizeUnit::PACKETS),
115  m_value(0)
116 {
117  NS_LOG_FUNCTION(this);
118 }
119 
120 QueueSize::QueueSize(QueueSizeUnit unit, uint32_t value)
121  : m_unit(unit),
122  m_value(value)
123 {
124  NS_LOG_FUNCTION(this << static_cast<uint16_t>(unit) << value);
125 }
126 
127 bool
128 QueueSize::operator<(const QueueSize& rhs) const
129 {
130  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
131 
132  return m_value < rhs.m_value;
133 }
134 
135 bool
136 QueueSize::operator<=(const QueueSize& rhs) const
137 {
138  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
139 
140  return m_value <= rhs.m_value;
141 }
142 
143 bool
145 {
146  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
147 
148  return m_value > rhs.m_value;
149 }
150 
151 bool
153 {
154  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
155 
156  return m_value >= rhs.m_value;
157 }
158 
159 bool
161 {
162  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
163 
164  return m_value == rhs.m_value;
165 }
166 
167 bool
169 {
170  NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
171 
172  return m_value != rhs.m_value;
173 }
174 
177 {
178  NS_LOG_FUNCTION(this);
179  return m_unit;
180 }
181 
182 uint32_t
184 {
185  NS_LOG_FUNCTION(this);
186  return m_value;
187 }
188 
189 QueueSize::QueueSize(std::string size)
190 {
191  NS_LOG_FUNCTION(this << size);
192  bool ok = DoParse(size, &m_unit, &m_value);
193  NS_ABORT_MSG_IF(!ok, "Could not parse queue size: " << size);
194 }
195 
196 /* For printing of queue size */
197 std::ostream&
198 operator<<(std::ostream& os, const QueueSize& size)
199 {
200  os << size.GetValue() << (size.GetUnit() == QueueSizeUnit::PACKETS ? "p" : "B");
201  return os;
202 }
203 
204 /* Initialize a queue size from an input stream */
205 std::istream&
206 operator>>(std::istream& is, QueueSize& size)
207 {
208  std::string value;
209  is >> value;
211  uint32_t l;
212  bool ok = QueueSize::DoParse(value, &m, &l);
213  if (!ok)
214  {
215  is.setstate(std::ios_base::failbit);
216  }
217  size = QueueSize(m, l);
218  return is;
219 }
220 
221 } // namespace ns3
Class for representing queue sizes.
Definition: queue-size.h:96
bool operator>(const QueueSize &rhs) const
Definition: queue-size.cc:144
bool operator<(const QueueSize &rhs) const
Definition: queue-size.cc:128
bool operator<=(const QueueSize &rhs) const
Definition: queue-size.cc:136
QueueSizeUnit GetUnit() const
Get the underlying unit.
Definition: queue-size.cc:176
bool operator!=(const QueueSize &rhs) const
Definition: queue-size.cc:168
uint32_t m_value
queue size [bytes or packets]
Definition: queue-size.h:200
bool operator>=(const QueueSize &rhs) const
Definition: queue-size.cc:152
bool operator==(const QueueSize &rhs) const
Definition: queue-size.cc:160
QueueSizeUnit m_unit
unit
Definition: queue-size.h:199
static bool DoParse(const std::string s, QueueSizeUnit *unit, uint32_t *value)
Parse a string representing a QueueSize.
Definition: queue-size.cc:33
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:183
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:44
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:46
@ PACKETS
Use number of packets for queue size.
Definition: queue-size.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(ValueClassTest)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159