A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
24namespace ns3
25{
26
27NS_LOG_COMPONENT_DEFINE("QueueSize");
28
30
31/* static */
32bool
33QueueSize::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
48 *value = static_cast<uint32_t>(r);
49 }
50 else if (trailer == "kB" || trailer == "KB")
51 {
52 // kilobytes
54 *value = static_cast<uint32_t>(r * 1000);
55 }
56 else if (trailer == "KiB")
57 {
58 // kibibytes
60 *value = static_cast<uint32_t>(r * 1024);
61 }
62 else if (trailer == "MB")
63 {
64 // MegaBytes
66 *value = static_cast<uint32_t>(r * 1000000);
67 }
68 else if (trailer == "MiB")
69 {
70 // MebiBytes
72 *value = static_cast<uint32_t>(r * 1048576);
73 }
74 else if (trailer == "p")
75 {
76 // packets
78 *value = static_cast<uint32_t>(r);
79 }
80 else if (trailer == "kp" || trailer == "Kp")
81 {
82 // kilopackets
84 *value = static_cast<uint32_t>(r * 1000);
85 }
86 else if (trailer == "Kip")
87 {
88 // kibipackets
90 *value = static_cast<uint32_t>(r * 1024);
91 }
92 else if (trailer == "Mp")
93 {
94 // MegaPackets
96 *value = static_cast<uint32_t>(r * 1000000);
97 }
98 else if (trailer == "Mip")
99 {
100 // MebiPackets
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
121 : m_unit(unit),
122 m_value(value)
123{
124 NS_LOG_FUNCTION(this << static_cast<uint16_t>(unit) << value);
125}
126
127bool
128QueueSize::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
135bool
136QueueSize::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
143bool
145{
146 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
147
148 return m_value > rhs.m_value;
149}
150
151bool
153{
154 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
155
156 return m_value >= rhs.m_value;
157}
158
159bool
161{
162 NS_ABORT_MSG_IF(m_unit != rhs.GetUnit(), "Cannot compare heterogeneous sizes");
163
164 return m_value == rhs.m_value;
165}
166
167bool
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
184{
185 NS_LOG_FUNCTION(this);
186 return m_value;
187}
188
189QueueSize::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 */
197std::ostream&
198operator<<(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 */
205std::istream&
206operator>>(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 ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#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.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153