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" || trailer == "b/s")
47 {
48 // bit/s
49 *v = (uint64_t)r;
50 }
51 else if (trailer == "Bps" || trailer == "B/s")
52 {
53 // byte/s
54 *v = (uint64_t)(r * 8);
55 }
56 else if (trailer == "kbps" || trailer == "kb/s" || trailer == "Kbps" || trailer == "Kb/s")
57 {
58 // kilobits/s
59 *v = (uint64_t)(r * 1000);
60 }
61 else if (trailer == "kBps" || trailer == "kB/s" || trailer == "KBps" || trailer == "KB/s")
62 {
63 // KiloByte/s
64 *v = (uint64_t)(r * 8000);
65 }
66 else if (trailer == "Kib/s")
67 {
68 // kibibit/s
69 *v = (uint64_t)(r * 1024);
70 }
71 else if (trailer == "KiB/s")
72 {
73 // kibibyte/s
74 *v = (uint64_t)(r * 8192);
75 }
76 else if (trailer == "Mbps" || trailer == "Mb/s")
77 {
78 // MegaBits/s
79 *v = (uint64_t)(r * 1000000);
80 }
81 else if (trailer == "MBps" || trailer == "MB/s")
82 {
83 // MegaBytes/s
84 *v = (uint64_t)(r * 8000000);
85 }
86 else if (trailer == "Mib/s")
87 {
88 // MebiBits/s
89 *v = (uint64_t)(r * 1048576);
90 }
91 else if (trailer == "MiB/s")
92 {
93 // MebiByte/s
94 *v = (uint64_t)(r * 1048576 * 8);
95 }
96 else if (trailer == "Gbps" || trailer == "Gb/s")
97 {
98 // GigaBit/s
99 *v = (uint64_t)(r * 1000000000);
100 }
101 else if (trailer == "GBps" || trailer == "GB/s")
102 {
103 // GigaByte/s
104 *v = (uint64_t)(r * 8 * 1000000000);
105 }
106 else if (trailer == "Gib/s")
107 {
108 // GibiBits/s
109 *v = (uint64_t)(r * 1048576 * 1024);
110 }
111 else if (trailer == "GiB/s")
112 {
113 // GibiByte/s
114 *v = (uint64_t)(r * 1048576 * 1024 * 8);
115 }
116 else
117 {
118 return false;
119 }
120 return true;
121 }
122 std::istringstream iss;
123 iss.str(s);
124 iss >> *v;
125 return true;
126}
127
129 : m_bps(0)
130{
131 NS_LOG_FUNCTION(this);
132}
133
135 : m_bps(bps)
136{
137 NS_LOG_FUNCTION(this << bps);
138}
139
142{
143 return DataRate(m_bps + rhs.m_bps);
144}
145
148{
149 m_bps += rhs.m_bps;
150 return *this;
151}
152
155{
156 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
157 return DataRate(m_bps - rhs.m_bps);
158}
159
162{
163 NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
164 m_bps -= rhs.m_bps;
165 return *this;
166}
167
169DataRate::operator*(double rhs) const
170{
171 return DataRate((uint64_t)(m_bps * rhs));
172}
173
176{
177 m_bps *= rhs;
178 return *this;
179}
180
182DataRate::operator*(uint64_t rhs) const
183{
184 return DataRate(m_bps * rhs);
185}
186
189{
190 m_bps *= rhs;
191 return *this;
192}
193
194bool
195DataRate::operator<(const DataRate& rhs) const
196{
197 return m_bps < rhs.m_bps;
198}
199
200bool
201DataRate::operator<=(const DataRate& rhs) const
202{
203 return m_bps <= rhs.m_bps;
204}
205
206bool
208{
209 return m_bps > rhs.m_bps;
210}
211
212bool
214{
215 return m_bps >= rhs.m_bps;
216}
217
218bool
220{
221 return m_bps == rhs.m_bps;
222}
223
224bool
226{
227 return m_bps != rhs.m_bps;
228}
229
230Time
232{
233 NS_LOG_FUNCTION(this << bytes);
234 return CalculateBitsTxTime(bytes * 8);
235}
236
237Time
239{
240 NS_LOG_FUNCTION(this << bits);
241 return Seconds(int64x64_t(bits) / m_bps);
242}
243
244uint64_t
246{
247 NS_LOG_FUNCTION(this);
248 return m_bps;
249}
250
251DataRate::DataRate(std::string rate)
252{
253 NS_LOG_FUNCTION(this << rate);
254 bool ok = DoParse(rate, &m_bps);
255 if (!ok)
256 {
257 NS_FATAL_ERROR("Could not parse rate: " << rate);
258 }
259}
260
261/* For printing of data rate */
262std::ostream&
263operator<<(std::ostream& os, const DataRate& rate)
264{
265 os << rate.GetBitRate() << "bps";
266 return os;
267}
268
269/* Initialize a data rate from an input stream */
270std::istream&
271operator>>(std::istream& is, DataRate& rate)
272{
273 std::string value;
274 is >> value;
275 uint64_t v;
276 bool ok = DataRate::DoParse(value, &v);
277 if (!ok)
278 {
279 is.setstate(std::ios_base::failbit);
280 }
281 rate = DataRate(v);
282 return is;
283}
284
285double
286operator*(const DataRate& lhs, const Time& rhs)
287{
288 return rhs.GetSeconds() * lhs.GetBitRate();
289}
290
291double
292operator*(const Time& lhs, const DataRate& rhs)
293{
294 return lhs.GetSeconds() * rhs.GetBitRate();
295}
296
297} // namespace ns3
Class for representing data rates.
Definition: data-rate.h:89
DataRate & operator*=(double rhs)
Scales the DataRate.
Definition: data-rate.cc:175
bool operator==(const DataRate &rhs) const
Definition: data-rate.cc:219
bool operator<(const DataRate &rhs) const
Definition: data-rate.cc:195
bool operator>(const DataRate &rhs) const
Definition: data-rate.cc:207
bool operator!=(const DataRate &rhs) const
Definition: data-rate.cc:225
bool operator>=(const DataRate &rhs) const
Definition: data-rate.cc:213
DataRate operator-(DataRate rhs) const
Definition: data-rate.cc:154
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:238
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:245
bool operator<=(const DataRate &rhs) const
Definition: data-rate.cc:201
DataRate & operator+=(DataRate rhs)
Definition: data-rate.cc:147
DataRate operator+(DataRate rhs) const
Definition: data-rate.cc:141
DataRate operator*(double rhs) const
Scales the DataRate.
Definition: data-rate.cc:169
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:231
DataRate & operator-=(DataRate rhs)
Definition: data-rate.cc:161
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:403
High precision numerical type, implementing Q64.64 fixed precision.
Definition: int64x64-128.h:56
#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:1319
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:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183