A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
data-rate.cc
Go to the documentation of this file.
1//
2// Copyright (c) 2006 Georgia Tech Research Corporation
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: Rajib Bhattacharjea<raj.b@gatech.edu>
18//
19
20#include "data-rate.h"
21
22#include "ns3/fatal-error.h"
23#include "ns3/log.h"
24#include "ns3/nstime.h"
25
26namespace ns3
27{
28
29NS_LOG_COMPONENT_DEFINE("DataRate");
30
32
33/* static */
34bool
35DataRate::DoParse(const std::string s, uint64_t* v)
36{
37 NS_LOG_FUNCTION(s << v);
38 std::string::size_type n = s.find_first_not_of("0123456789.");
39 if (n != std::string::npos)
40 { // Found non-numeric
41 std::istringstream iss;
42 iss.str(s.substr(0, n));
43 double r;
44 iss >> r;
45 std::string trailer = s.substr(n, std::string::npos);
46 if (trailer == "bps")
47 {
48 // bit/s
49 *v = (uint64_t)r;
50 }
51 else if (trailer == "b/s")
52 {
53 // bit/s
54 *v = (uint64_t)r;
55 }
56 else if (trailer == "Bps")
57 {
58 // byte/s
59 *v = (uint64_t)(r * 8);
60 }
61 else if (trailer == "B/s")
62 {
63 // byte/s
64 *v = (uint64_t)(r * 8);
65 }
66 else if (trailer == "kbps")
67 {
68 // kilobits/s
69 *v = (uint64_t)(r * 1000);
70 }
71 else if (trailer == "kb/s")
72 {
73 // kilobits/s
74 *v = (uint64_t)(r * 1000);
75 }
76 else if (trailer == "Kbps")
77 {
78 // kilobits/s
79 *v = (uint64_t)(r * 1000);
80 }
81 else if (trailer == "Kb/s")
82 {
83 // kilobits/s
84 *v = (uint64_t)(r * 1000);
85 }
86 else if (trailer == "kBps")
87 {
88 // kiloByte/s
89 *v = (uint64_t)(r * 8000);
90 }
91 else if (trailer == "kB/s")
92 {
93 // KiloByte/s
94 *v = (uint64_t)(r * 8000);
95 }
96 else if (trailer == "KBps")
97 {
98 // kiloByte/s
99 *v = (uint64_t)(r * 8000);
100 }
101 else if (trailer == "KB/s")
102 {
103 // KiloByte/s
104 *v = (uint64_t)(r * 8000);
105 }
106 else if (trailer == "Kib/s")
107 {
108 // kibibit/s
109 *v = (uint64_t)(r * 1024);
110 }
111 else if (trailer == "KiB/s")
112 {
113 // kibibyte/s
114 *v = (uint64_t)(r * 8192);
115 }
116 else if (trailer == "Mbps")
117 {
118 // MegaBits/s
119 *v = (uint64_t)(r * 1000000);
120 }
121 else if (trailer == "Mb/s")
122 {
123 // MegaBits/s
124 *v = (uint64_t)(r * 1000000);
125 }
126 else if (trailer == "MBps")
127 {
128 // MegaBytes/s
129 *v = (uint64_t)(r * 8000000);
130 }
131 else if (trailer == "MB/s")
132 {
133 // MegaBytes/s
134 *v = (uint64_t)(r * 8000000);
135 }
136 else if (trailer == "Mib/s")
137 {
138 // MebiBits/s
139 *v = (uint64_t)(r * 1048576);
140 }
141 else if (trailer == "MiB/s")
142 {
143 // MebiByte/s
144 *v = (uint64_t)(r * 1048576 * 8);
145 }
146 else if (trailer == "Gbps")
147 {
148 // GigaBit/s
149 *v = (uint64_t)(r * 1000000000);
150 }
151 else if (trailer == "Gb/s")
152 {
153 // GigaBit/s
154 *v = (uint64_t)(r * 1000000000);
155 }
156 else if (trailer == "GBps")
157 {
158 // GigaByte/s
159 *v = (uint64_t)(r * 8 * 1000000000);
160 }
161 else if (trailer == "GB/s")
162 {
163 // GigaByte/s
164 *v = (uint64_t)(r * 8 * 1000000000);
165 }
166 else if (trailer == "Gib/s")
167 {
168 // GibiBits/s
169 *v = (uint64_t)(r * 1048576 * 1024);
170 }
171 else if (trailer == "GiB/s")
172 {
173 // GibiByte/s
174 *v = (uint64_t)(r * 1048576 * 1024 * 8);
175 }
176 else
177 {
178 return false;
179 }
180 return true;
181 }
182 std::istringstream iss;
183 iss.str(s);
184 iss >> *v;
185 return true;
186}
187
189 : m_bps(0)
190{
191 NS_LOG_FUNCTION(this);
192}
193
195 : m_bps(bps)
196{
197 NS_LOG_FUNCTION(this << bps);
198}
199
202{
203 return DataRate(m_bps + rhs.m_bps);
204}
205
208{
209 m_bps += rhs.m_bps;
210 return *this;
211}
212
215{
216 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
217 return DataRate(m_bps - rhs.m_bps);
218}
219
222{
223 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
224 m_bps -= rhs.m_bps;
225 return *this;
226}
227
229DataRate::operator*(double rhs) const
230{
231 return DataRate(((uint64_t)(m_bps * rhs)));
232}
233
236{
237 m_bps *= rhs;
238 return *this;
239}
240
242DataRate::operator*(uint64_t rhs) const
243{
244 return DataRate(m_bps * rhs);
245}
246
249{
250 m_bps *= rhs;
251 return *this;
252}
253
254bool
255DataRate::operator<(const DataRate& rhs) const
256{
257 return m_bps < rhs.m_bps;
258}
259
260bool
261DataRate::operator<=(const DataRate& rhs) const
262{
263 return m_bps <= rhs.m_bps;
264}
265
266bool
268{
269 return m_bps > rhs.m_bps;
270}
271
272bool
274{
275 return m_bps >= rhs.m_bps;
276}
277
278bool
280{
281 return m_bps == rhs.m_bps;
282}
283
284bool
286{
287 return m_bps != rhs.m_bps;
288}
289
290Time
292{
293 NS_LOG_FUNCTION(this << bytes);
294 return CalculateBitsTxTime(bytes * 8);
295}
296
297Time
299{
300 NS_LOG_FUNCTION(this << bits);
301 return Seconds(int64x64_t(bits) / m_bps);
302}
303
304uint64_t
306{
307 NS_LOG_FUNCTION(this);
308 return m_bps;
309}
310
311DataRate::DataRate(std::string rate)
312{
313 NS_LOG_FUNCTION(this << rate);
314 bool ok = DoParse(rate, &m_bps);
315 if (!ok)
316 {
317 NS_FATAL_ERROR("Could not parse rate: " << rate);
318 }
319}
320
321/* For printing of data rate */
322std::ostream&
323operator<<(std::ostream& os, const DataRate& rate)
324{
325 os << rate.GetBitRate() << "bps";
326 return os;
327}
328
329/* Initialize a data rate from an input stream */
330std::istream&
331operator>>(std::istream& is, DataRate& rate)
332{
333 std::string value;
334 is >> value;
335 uint64_t v;
336 bool ok = DataRate::DoParse(value, &v);
337 if (!ok)
338 {
339 is.setstate(std::ios_base::failbit);
340 }
341 rate = DataRate(v);
342 return is;
343}
344
345double
346operator*(const DataRate& lhs, const Time& rhs)
347{
348 return rhs.GetSeconds() * lhs.GetBitRate();
349}
350
351double
352operator*(const Time& lhs, const DataRate& rhs)
353{
354 return lhs.GetSeconds() * rhs.GetBitRate();
355}
356
357} // namespace ns3
Class for representing data rates.
Definition: data-rate.h:89
DataRate & operator*=(double rhs)
Scales the DataRate.
Definition: data-rate.cc:235
bool operator==(const DataRate &rhs) const
Definition: data-rate.cc:279
bool operator<(const DataRate &rhs) const
Definition: data-rate.cc:255
bool operator>(const DataRate &rhs) const
Definition: data-rate.cc:267
bool operator!=(const DataRate &rhs) const
Definition: data-rate.cc:285
bool operator>=(const DataRate &rhs) const
Definition: data-rate.cc:273
DataRate operator-(DataRate rhs) const
Definition: data-rate.cc:214
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:298
static bool DoParse(const std::string s, uint64_t *v)
Parse a string representing a DataRate into an uint64_t.
Definition: data-rate.cc:35
uint64_t m_bps
data rate [bps]
Definition: data-rate.h:275
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:305
bool operator<=(const DataRate &rhs) const
Definition: data-rate.cc:261
DataRate & operator+=(DataRate rhs)
Definition: data-rate.cc:207
DataRate operator+(DataRate rhs) const
Definition: data-rate.cc:201
DataRate operator*(double rhs) const
Scales the DataRate.
Definition: data-rate.cc:229
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:291
DataRate & operator-=(DataRate rhs)
Definition: data-rate.cc:221
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
High precision numerical type, implementing Q64.64 fixed precision.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:118
#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 ",...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
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