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
183Time
185{
186 NS_LOG_FUNCTION(this << bytes);
187 return CalculateBitsTxTime(bytes * 8);
188}
189
190Time
192{
193 NS_LOG_FUNCTION(this << bits);
194 return Seconds(int64x64_t(bits) / m_bps);
195}
196
197uint64_t
199{
200 NS_LOG_FUNCTION(this);
201 return m_bps;
202}
203
204DataRate::DataRate(std::string rate)
205{
206 NS_LOG_FUNCTION(this << rate);
207 bool ok = DoParse(rate, &m_bps);
208 if (!ok)
209 {
210 NS_FATAL_ERROR("Could not parse rate: " << rate);
211 }
212}
213
214/* For printing of data rate */
215std::ostream&
216operator<<(std::ostream& os, const DataRate& rate)
217{
218 os << rate.GetBitRate() << "bps";
219 return os;
220}
221
222/* Initialize a data rate from an input stream */
223std::istream&
224operator>>(std::istream& is, DataRate& rate)
225{
226 std::string value;
227 std::getline(is, value);
228 uint64_t v;
229 bool ok = DataRate::DoParse(value, &v);
230 if (!ok)
231 {
232 is.setstate(std::ios_base::failbit);
233 }
234 rate = DataRate(v);
235 return is;
236}
237
238double
239operator*(const DataRate& lhs, const Time& rhs)
240{
241 return rhs.GetSeconds() * lhs.GetBitRate();
242}
243
244double
245operator*(const Time& lhs, const DataRate& rhs)
246{
247 return lhs.GetSeconds() * rhs.GetBitRate();
248}
249
250} // namespace ns3
uint32_t r
uint32_t v
Class for representing data rates.
Definition data-rate.h:78
DataRate & operator*=(double rhs)
Scales the DataRate.
Definition data-rate.cc:164
DataRate operator-(DataRate rhs) const
Definition data-rate.cc:143
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition data-rate.cc:191
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:231
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition data-rate.cc:198
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:184
DataRate & operator-=(DataRate rhs)
Definition data-rate.cc:150
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
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:194
#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:1273
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