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// SPDX-License-Identifier: GPL-2.0-only
5//
6// Author: Rajib Bhattacharjea<raj.b@gatech.edu>
7//
8
9#include "data-rate.h"
10
11#include "ns3/fatal-error.h"
12#include "ns3/log.h"
13#include "ns3/nstime.h"
14
15namespace ns3
16{
17
18NS_LOG_COMPONENT_DEFINE("DataRate");
19
21
22/* static */
23bool
24DataRate::DoParse(const std::string s, uint64_t* v)
25{
26 NS_LOG_FUNCTION(s << v);
27 std::string::size_type n = s.find_first_not_of("0123456789.");
28 if (n != std::string::npos)
29 { // Found non-numeric
30 std::istringstream iss;
31 iss.str(s.substr(0, n));
32 double r;
33 iss >> r;
34 std::string trailer = s.substr(n, std::string::npos);
35 if (trailer == "bps" || trailer == "b/s")
36 {
37 // bit/s
38 *v = (uint64_t)r;
39 }
40 else if (trailer == "Bps" || trailer == "B/s")
41 {
42 // byte/s
43 *v = (uint64_t)(r * 8);
44 }
45 else if (trailer == "kbps" || trailer == "kb/s" || trailer == "Kbps" || trailer == "Kb/s")
46 {
47 // kilobits/s
48 *v = (uint64_t)(r * 1000);
49 }
50 else if (trailer == "kBps" || trailer == "kB/s" || trailer == "KBps" || trailer == "KB/s")
51 {
52 // KiloByte/s
53 *v = (uint64_t)(r * 8000);
54 }
55 else if (trailer == "Kib/s")
56 {
57 // kibibit/s
58 *v = (uint64_t)(r * 1024);
59 }
60 else if (trailer == "KiB/s")
61 {
62 // kibibyte/s
63 *v = (uint64_t)(r * 8192);
64 }
65 else if (trailer == "Mbps" || trailer == "Mb/s")
66 {
67 // MegaBits/s
68 *v = (uint64_t)(r * 1000000);
69 }
70 else if (trailer == "MBps" || trailer == "MB/s")
71 {
72 // MegaBytes/s
73 *v = (uint64_t)(r * 8000000);
74 }
75 else if (trailer == "Mib/s")
76 {
77 // MebiBits/s
78 *v = (uint64_t)(r * 1048576);
79 }
80 else if (trailer == "MiB/s")
81 {
82 // MebiByte/s
83 *v = (uint64_t)(r * 1048576 * 8);
84 }
85 else if (trailer == "Gbps" || trailer == "Gb/s")
86 {
87 // GigaBit/s
88 *v = (uint64_t)(r * 1000000000);
89 }
90 else if (trailer == "GBps" || trailer == "GB/s")
91 {
92 // GigaByte/s
93 *v = (uint64_t)(r * 8 * 1000000000);
94 }
95 else if (trailer == "Gib/s")
96 {
97 // GibiBits/s
98 *v = (uint64_t)(r * 1048576 * 1024);
99 }
100 else if (trailer == "GiB/s")
101 {
102 // GibiByte/s
103 *v = (uint64_t)(r * 1048576 * 1024 * 8);
104 }
105 else
106 {
107 return false;
108 }
109 return true;
110 }
111 std::istringstream iss;
112 iss.str(s);
113 iss >> *v;
114 return true;
115}
116
118 : m_bps(0)
119{
120 NS_LOG_FUNCTION(this);
121}
122
124 : m_bps(bps)
125{
126 NS_LOG_FUNCTION(this << bps);
127}
128
131{
132 return DataRate(m_bps + rhs.m_bps);
133}
134
137{
138 m_bps += rhs.m_bps;
139 return *this;
140}
141
144{
145 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
146 return DataRate(m_bps - rhs.m_bps);
147}
148
151{
152 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
153 m_bps -= rhs.m_bps;
154 return *this;
155}
156
158DataRate::operator*(double rhs) const
159{
160 return DataRate((uint64_t)(m_bps * rhs));
161}
162
165{
166 m_bps *= rhs;
167 return *this;
168}
169
171DataRate::operator*(uint64_t rhs) const
172{
173 return DataRate(m_bps * rhs);
174}
175
178{
179 m_bps *= rhs;
180 return *this;
181}
182
183bool
184DataRate::operator<(const DataRate& rhs) const
185{
186 return m_bps < rhs.m_bps;
187}
188
189bool
190DataRate::operator<=(const DataRate& rhs) const
191{
192 return m_bps <= rhs.m_bps;
193}
194
195bool
197{
198 return m_bps > rhs.m_bps;
199}
200
201bool
203{
204 return m_bps >= rhs.m_bps;
205}
206
207bool
209{
210 return m_bps == rhs.m_bps;
211}
212
213bool
215{
216 return m_bps != rhs.m_bps;
217}
218
219Time
221{
222 NS_LOG_FUNCTION(this << bytes);
223 return CalculateBitsTxTime(bytes * 8);
224}
225
226Time
228{
229 NS_LOG_FUNCTION(this << bits);
230 return Seconds(int64x64_t(bits) / m_bps);
231}
232
233uint64_t
235{
236 NS_LOG_FUNCTION(this);
237 return m_bps;
238}
239
240DataRate::DataRate(std::string rate)
241{
242 NS_LOG_FUNCTION(this << rate);
243 bool ok = DoParse(rate, &m_bps);
244 if (!ok)
245 {
246 NS_FATAL_ERROR("Could not parse rate: " << rate);
247 }
248}
249
250/* For printing of data rate */
251std::ostream&
252operator<<(std::ostream& os, const DataRate& rate)
253{
254 os << rate.GetBitRate() << "bps";
255 return os;
256}
257
258/* Initialize a data rate from an input stream */
259std::istream&
260operator>>(std::istream& is, DataRate& rate)
261{
262 std::string value;
263 is >> value;
264 uint64_t v;
265 bool ok = DataRate::DoParse(value, &v);
266 if (!ok)
267 {
268 is.setstate(std::ios_base::failbit);
269 }
270 rate = DataRate(v);
271 return is;
272}
273
274double
275operator*(const DataRate& lhs, const Time& rhs)
276{
277 return rhs.GetSeconds() * lhs.GetBitRate();
278}
279
280double
281operator*(const Time& lhs, const DataRate& rhs)
282{
283 return lhs.GetSeconds() * rhs.GetBitRate();
284}
285
286} // namespace ns3
Class for representing data rates.
Definition data-rate.h:78
DataRate & operator*=(double rhs)
Scales the DataRate.
Definition data-rate.cc:164
bool operator==(const DataRate &rhs) const
Definition data-rate.cc:208
bool operator<(const DataRate &rhs) const
Definition data-rate.cc:184
bool operator>(const DataRate &rhs) const
Definition data-rate.cc:196
bool operator!=(const DataRate &rhs) const
Definition data-rate.cc:214
bool operator>=(const DataRate &rhs) const
Definition data-rate.cc:202
DataRate operator-(DataRate rhs) const
Definition data-rate.cc:143
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition data-rate.cc:227
static bool DoParse(const std::string s, uint64_t *v)
Parse a string representing a DataRate into an uint64_t.
Definition data-rate.cc:24
uint64_t m_bps
data rate [bps]
Definition data-rate.h:264
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition data-rate.cc:234
bool operator<=(const DataRate &rhs) const
Definition data-rate.cc:190
DataRate & operator+=(DataRate rhs)
Definition data-rate.cc:136
DataRate operator+(DataRate rhs) const
Definition data-rate.cc:130
DataRate operator*(double rhs) const
Scales the DataRate.
Definition data-rate.cc:158
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition data-rate.cc:220
DataRate & operator-=(DataRate rhs)
Definition data-rate.cc:150
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
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:75
#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.
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition int64x64.h:106
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:1344
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:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172