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