A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
data-rate.h
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#ifndef DATA_RATE_H
10#define DATA_RATE_H
11
12#include "ns3/attribute-helper.h"
13#include "ns3/attribute.h"
14#include "ns3/nstime.h"
15
16#include <iostream>
17#include <stdint.h>
18#include <string>
19
20namespace ns3
21{
22
23/**
24 * @ingroup network
25 * @defgroup datarate Data Rate
26 */
27/**
28 * @ingroup datarate
29 * @brief Class for representing data rates
30 *
31 * Allows for natural and familiar use of data rates. Allows construction
32 * from strings, natural multiplication e.g.:
33 * @code
34 * DataRate x("56kbps");
35 * double nBits = x*ns3::Seconds (19.2);
36 * uint32_t nBytes = 20;
37 * Time txtime = x.CalculateBytesTxTime (nBytes);
38 * @endcode
39 * This class also supports the regular comparison operators \c <, \c >,
40 * \c <=, \c >=, \c ==, and \c !=
41 *
42 * Data rate specifiers consist of
43 * * A numeric value,
44 * * An optional multiplier prefix and
45 * * A unit.
46 *
47 * Whitespace is allowed but not required between the numeric value and
48 * multiplier or unit.
49 *
50 * Supported multiplier prefixes:
51 *
52 * | Prefix | Value |
53 * | :------- | ----------: |
54 * | "k", "K" | 1000 |
55 * | "Ki" | 1024 |
56 * | "M" | 1000000 |
57 * | "Mi" | 1024 Ki |
58 * | "G" | 10^9 |
59 * | "Gi " | 1024 Mi |
60 *
61 * Supported unit strings:
62 *
63 * | Symbol | Meaning |
64 * | :------- | :---------- |
65 * | "b" | bits |
66 * | "B" | 8-bit bytes |
67 * | "s", "/s"| per second |
68 *
69 * Examples:
70 * * "56kbps" = 56,000 bits/s
71 * * "128 kb/s" = 128,000 bits/s
72 * * "8Kib/s" = 1 KiB/s = 8192 bits/s
73 * * "1kB/s" = 8000 bits/s
74 *
75 * @see attribute_DataRate
76 */
78{
79 public:
80 DataRate();
81 /**
82 * @brief Integer constructor
83 *
84 * Construct a data rate from an integer. This class only supports positive
85 * integer data rates in units of bits/s, meaning 1bit/s is the smallest
86 * non-trivial bitrate available.
87 * @param bps bit/s value
88 */
89 DataRate(uint64_t bps);
90 /**
91 * @brief String constructor
92 *
93 * Construct a data rate from a string. Many different unit strings are supported
94 * Supported unit strings:
95 * bps, b/s, Bps, B/s \n
96 * kbps, kb/s, Kbps, Kb/s, kBps, kB/s, KBps, KB/s, Kib/s, KiB/s \n
97 * Mbps, Mb/s, MBps, MB/s, Mib/s, MiB/s \n
98 * Gbps, Gb/s, GBps, GB/s, Gib/s, GiB/s \n
99 *
100 * Examples:
101 * "56kbps" = 56,000 bits/s \n
102 * "128 kb/s" = 128,000 bits/s \n
103 * "8Kib/s" = 1 KiB/s = 8192 bits/s \n
104 * "1kB/s" = 8000 bits/s
105 *
106 * @param rate string representing the desired rate
107 */
108 DataRate(std::string rate);
109
110 /**
111 * @return the DataRate representing the sum of this object with rhs
112 *
113 * @param rhs the DataRate to add to this DataRate
114 */
115 DataRate operator+(DataRate rhs) const;
116
117 /**
118 * @return the DataRate representing the sum of this object with rhs
119 *
120 * @param rhs the DataRate to add to this DataRate
121 */
123
124 /**
125 * @return the DataRate representing the difference of this object with rhs
126 *
127 * @param rhs the DataRate to subtract from this DataRate
128 */
129 DataRate operator-(DataRate rhs) const;
130
131 /**
132 * @return the DataRate representing the difference of this object with rhs
133 *
134 * @param rhs the DataRate to subtract from this DataRate
135 */
137
138 /**
139 * @brief Scales the DataRate
140 *
141 * Multiplies with double and is re-casted to an int
142 *
143 * @return DataRate object representing the product of this object with rhs
144 *
145 * @param rhs the double to multiply to this datarate
146 */
147 DataRate operator*(double rhs) const;
148
149 /**
150 * @brief Scales the DataRate
151 *
152 * Multiplies with double and is re-casted to an int
153 *
154 * @return DataRate object representing the product of this object with rhs
155 *
156 * @param rhs the double to multiply to this datarate
157 */
158 DataRate& operator*=(double rhs);
159
160 /**
161 * @brief Scales the DataRate
162 *
163 * @return DataRate object representing the product of this object with rhs
164 *
165 * @param rhs the uint64_t to multiply to this datarate
166 */
167 DataRate operator*(uint64_t rhs) const;
168
169 /**
170 * @brief Scales the DataRate
171 *
172 * @return DataRate object representing the product of this object with rhs
173 *
174 * @param rhs the uint64_t to multiply to this datarate
175 */
176 DataRate& operator*=(uint64_t rhs);
177
178 /**
179 * Spaceship comparison operator. All the other comparison operators
180 * are automatically generated from this one.
181 *
182 * @param rhs the datarate to compare to this datarate
183 * @returns The result of the comparison.
184 */
185 auto operator<=>(const DataRate& rhs) const = default;
186
187 /**
188 * @brief Calculate transmission time
189 *
190 * Calculates the transmission time at this data rate
191 * @param bytes The number of bytes (not bits) for which to calculate
192 * @return The transmission time for the number of bytes specified
193 */
195
196 /**
197 * @brief Calculate transmission time
198 *
199 * Calculates the transmission time at this data rate
200 * @param bits The number of bits (not bytes) for which to calculate
201 * @return The transmission time for the number of bits specified
202 */
204
205 /**
206 * Get the underlying bitrate
207 * @return The underlying bitrate in bits per second
208 */
209 uint64_t GetBitRate() const;
210
211 private:
212 /**
213 * @brief Parse a string representing a DataRate into an uint64_t
214 *
215 * Allowed unit representations include all combinations of
216 *
217 * * An SI prefix: k, K, M, G
218 * * Decimal or kibibit (as in "Kibps", meaning 1024 bps)
219 * * Bits or bytes (8 bits)
220 * * "bps" or "/s"
221 *
222 * @param [in] s The string representation, including unit
223 * @param [in,out] v The location to put the value, in bits/sec.
224 * @return true if parsing was successful.
225 */
226 static bool DoParse(const std::string s, uint64_t* v);
227
228 // Uses DoParse
229 friend std::istream& operator>>(std::istream& is, DataRate& rate);
230
231 uint64_t m_bps; //!< data rate [bps]
232};
233
234/**
235 * @brief Stream insertion operator.
236 *
237 * @param os the stream
238 * @param rate the data rate
239 * @returns a reference to the stream
240 */
241std::ostream& operator<<(std::ostream& os, const DataRate& rate);
242
243/**
244 * @brief Stream extraction operator.
245 *
246 * @param is the stream
247 * @param rate the data rate
248 * @returns a reference to the stream
249 */
250std::istream& operator>>(std::istream& is, DataRate& rate);
251
253
254/**
255 * @brief Multiply datarate by a time value
256 *
257 * Calculates the number of bits that have been transmitted over a period of time
258 * @param lhs rate
259 * @param rhs time
260 * @return the number of bits over the period of time
261 */
262double operator*(const DataRate& lhs, const Time& rhs);
263/**
264 * @brief Multiply time value by a data rate
265 *
266 * Calculates the number of bits that have been transmitted over a period of time
267 * @param lhs time
268 * @param rhs rate
269 * @return the number of bits over the period of time
270 */
271double operator*(const Time& lhs, const DataRate& rhs);
272
273namespace TracedValueCallback
274{
275
276/**
277 * @ingroup network
278 * TracedValue callback signature for DataRate
279 *
280 * @param [in] oldValue original value of the traced variable
281 * @param [in] newValue new value of the traced variable
282 */
283typedef void (*DataRate)(DataRate oldValue, DataRate newValue);
284
285} // namespace TracedValueCallback
286
287} // namespace ns3
288
289#endif /* DATA_RATE_H */
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
auto operator<=>(const DataRate &rhs) const =default
Spaceship comparison operator.
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
friend std::istream & operator>>(std::istream &is, DataRate &rate)
Stream extraction operator.
Definition data-rate.cc:224
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type.
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition int64x64.h:106
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition data-rate.h:283
TracedValue Callback function types.
Definition nstime.h:881
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