A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3::DataRate Class Reference

Class for representing data rates. More...

#include "data-rate.h"

+ Collaboration diagram for ns3::DataRate:

Public Member Functions

 DataRate ()
 
 DataRate (std::string rate)
 String constructor.
 
 DataRate (uint64_t bps)
 Integer constructor.
 
Time CalculateBitsTxTime (uint32_t bits) const
 Calculate transmission time.
 
Time CalculateBytesTxTime (uint32_t bytes) const
 Calculate transmission time.
 
uint64_t GetBitRate () const
 Get the underlying bitrate.
 
bool operator!= (const DataRate &rhs) const
 
DataRate operator* (double rhs) const
 Scales the DataRate.
 
DataRate operator* (uint64_t rhs) const
 Scales the DataRate.
 
DataRateoperator*= (double rhs)
 Scales the DataRate.
 
DataRateoperator*= (uint64_t rhs)
 Scales the DataRate.
 
DataRate operator+ (DataRate rhs) const
 
DataRateoperator+= (DataRate rhs)
 
DataRate operator- (DataRate rhs) const
 
DataRateoperator-= (DataRate rhs)
 
bool operator< (const DataRate &rhs) const
 
bool operator<= (const DataRate &rhs) const
 
bool operator== (const DataRate &rhs) const
 
bool operator> (const DataRate &rhs) const
 
bool operator>= (const DataRate &rhs) const
 

Static Private Member Functions

static bool DoParse (const std::string s, uint64_t *v)
 Parse a string representing a DataRate into an uint64_t.
 

Private Attributes

uint64_t m_bps
 data rate [bps]
 

Friends

std::istream & operator>> (std::istream &is, DataRate &rate)
 Stream extraction operator.
 

Detailed Description

Class for representing data rates.

Allows for natural and familiar use of data rates. Allows construction from strings, natural multiplication e.g.:

DataRate x("56kbps");
double nBits = x*ns3::Seconds (19.2);
uint32_t nBytes = 20;
Time txtime = x.CalculateBytesTxTime (nBytes);
Class for representing data rates.
Definition: data-rate.h:89
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336

This class also supports the regular comparison operators <, >, <=, >=, ==, and !=

Data rate specifiers consist of

  • A numeric value,
  • An optional multiplier prefix and
  • A unit.

Whitespace is allowed but not required between the numeric value and multiplier or unit.

Supported multiplier prefixes:

Prefix Value
"k", "K" 1000
"Ki" 1024
"M" 1000000
"Mi" 1024 Ki
"G" 10^9
"Gi " 1024 Mi

Supported unit strings:

Symbol Meaning
"b" bits
"B" 8-bit bytes
"s", "/s" per second

Examples:

  • "56kbps" = 56,000 bits/s
  • "128 kb/s" = 128,000 bits/s
  • "8Kib/s" = 1 KiB/s = 8192 bits/s
  • "1kB/s" = 8000 bits/s
See also
DataRate Attribute

Definition at line 88 of file data-rate.h.

Constructor & Destructor Documentation

◆ DataRate() [1/3]

ns3::DataRate::DataRate ( )

Definition at line 188 of file data-rate.cc.

References NS_LOG_FUNCTION.

Referenced by operator*(), operator+(), and operator-().

+ Here is the caller graph for this function:

◆ DataRate() [2/3]

ns3::DataRate::DataRate ( uint64_t  bps)

Integer constructor.

Construct a data rate from an integer. This class only supports positive integer data rates in units of bits/s, meaning 1bit/s is the smallest non-trivial bitrate available.

Parameters
bpsbit/s value

Definition at line 194 of file data-rate.cc.

References NS_LOG_FUNCTION.

◆ DataRate() [3/3]

ns3::DataRate::DataRate ( std::string  rate)

String constructor.

Construct a data rate from a string. Many different unit strings are supported Supported unit strings: bps, b/s, Bps, B/s
kbps, kb/s, Kbps, Kb/s, kBps, kB/s, KBps, KB/s, Kib/s, KiB/s
Mbps, Mb/s, MBps, MB/s, Mib/s, MiB/s
Gbps, Gb/s, GBps, GB/s, Gib/s, GiB/s
Examples: "56kbps" = 56,000 bits/s
"128 kb/s" = 128,000 bits/s
"8Kib/s" = 1 KiB/s = 8192 bits/s
"1kB/s" = 8000 bits/s

Parameters
ratestring representing the desired rate

Definition at line 311 of file data-rate.cc.

References DoParse(), m_bps, NS_FATAL_ERROR, and NS_LOG_FUNCTION.

+ Here is the call graph for this function:

Member Function Documentation

◆ CalculateBitsTxTime()

Time ns3::DataRate::CalculateBitsTxTime ( uint32_t  bits) const

Calculate transmission time.

Calculates the transmission time at this data rate

Parameters
bitsThe number of bits (not bytes) for which to calculate
Returns
The transmission time for the number of bits specified

Definition at line 298 of file data-rate.cc.

References m_bps, NS_LOG_FUNCTION, and ns3::Seconds().

Referenced by CalculateBytesTxTime(), and DataRateTestCase1::SingleTest().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CalculateBytesTxTime()

Time ns3::DataRate::CalculateBytesTxTime ( uint32_t  bytes) const

Calculate transmission time.

Calculates the transmission time at this data rate

Parameters
bytesThe number of bytes (not bits) for which to calculate
Returns
The transmission time for the number of bytes specified

Definition at line 291 of file data-rate.cc.

References CalculateBitsTxTime(), and NS_LOG_FUNCTION.

Referenced by ns3::CsmaNetDevice::Attach(), ns3::TbfQueueDisc::DoDequeue(), DataRateTestCase1::SingleTest(), ns3::SimpleNetDevice::StartTransmission(), ns3::HalfDuplexIdealPhy::StartTx(), ns3::CsmaNetDevice::TransmitStart(), and ns3::PointToPointNetDevice::TransmitStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DoParse()

bool ns3::DataRate::DoParse ( const std::string  s,
uint64_t *  v 
)
staticprivate

Parse a string representing a DataRate into an uint64_t.

Allowed unit representations include all combinations of

  • An SI prefix: k, K, M, G
  • Decimal or kibibit (as in "Kibps", meaning 1024 bps)
  • Bits or bytes (8 bits)
  • "bps" or "/s"
Parameters
[in]sThe string representation, including unit
[in,out]vThe location to put the value, in bits/sec.
Returns
true if parsing was successful.

Definition at line 35 of file data-rate.cc.

References NS_LOG_FUNCTION.

Referenced by DataRate().

+ Here is the caller graph for this function:

◆ GetBitRate()

◆ operator!=()

bool ns3::DataRate::operator!= ( const DataRate rhs) const
Returns
true if this rate is not equal to rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 285 of file data-rate.cc.

References m_bps.

◆ operator*() [1/2]

DataRate ns3::DataRate::operator* ( double  rhs) const

Scales the DataRate.

Multiplies with double and is re-casted to an int

Returns
DataRate object representing the product of this object with rhs
Parameters
rhsthe double to multiply to this datarate

Definition at line 229 of file data-rate.cc.

References DataRate(), and m_bps.

+ Here is the call graph for this function:

◆ operator*() [2/2]

DataRate ns3::DataRate::operator* ( uint64_t  rhs) const

Scales the DataRate.

Returns
DataRate object representing the product of this object with rhs
Parameters
rhsthe uint64_t to multiply to this datarate

Definition at line 242 of file data-rate.cc.

References DataRate(), and m_bps.

+ Here is the call graph for this function:

◆ operator*=() [1/2]

DataRate & ns3::DataRate::operator*= ( double  rhs)

Scales the DataRate.

Multiplies with double and is re-casted to an int

Returns
DataRate object representing the product of this object with rhs
Parameters
rhsthe double to multiply to this datarate

Definition at line 235 of file data-rate.cc.

References m_bps.

◆ operator*=() [2/2]

DataRate & ns3::DataRate::operator*= ( uint64_t  rhs)

Scales the DataRate.

Returns
DataRate object representing the product of this object with rhs
Parameters
rhsthe uint64_t to multiply to this datarate

Definition at line 248 of file data-rate.cc.

References m_bps.

◆ operator+()

DataRate ns3::DataRate::operator+ ( DataRate  rhs) const
Returns
the DataRate representing the sum of this object with rhs
Parameters
rhsthe DataRate to add to this DataRate

Definition at line 201 of file data-rate.cc.

References DataRate(), and m_bps.

+ Here is the call graph for this function:

◆ operator+=()

DataRate & ns3::DataRate::operator+= ( DataRate  rhs)
Returns
the DataRate representing the sum of this object with rhs
Parameters
rhsthe DataRate to add to this DataRate

Definition at line 207 of file data-rate.cc.

References m_bps.

◆ operator-()

DataRate ns3::DataRate::operator- ( DataRate  rhs) const
Returns
the DataRate representing the difference of this object with rhs
Parameters
rhsthe DataRate to subtract from this DataRate

Definition at line 214 of file data-rate.cc.

References DataRate(), m_bps, and NS_ASSERT_MSG.

+ Here is the call graph for this function:

◆ operator-=()

DataRate & ns3::DataRate::operator-= ( DataRate  rhs)
Returns
the DataRate representing the difference of this object with rhs
Parameters
rhsthe DataRate to subtract from this DataRate

Definition at line 221 of file data-rate.cc.

References m_bps, and NS_ASSERT_MSG.

◆ operator<()

bool ns3::DataRate::operator< ( const DataRate rhs) const
Returns
true if this rate is less than rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 254 of file data-rate.cc.

References m_bps.

◆ operator<=()

bool ns3::DataRate::operator<= ( const DataRate rhs) const
Returns
true if this rate is less than or equal to rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 260 of file data-rate.cc.

References m_bps.

◆ operator==()

bool ns3::DataRate::operator== ( const DataRate rhs) const
Returns
true if this rate is equal to rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 279 of file data-rate.cc.

References m_bps.

◆ operator>()

bool ns3::DataRate::operator> ( const DataRate rhs) const
Returns
true if this rate is greater than rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 267 of file data-rate.cc.

References m_bps.

◆ operator>=()

bool ns3::DataRate::operator>= ( const DataRate rhs) const
Returns
true if this rate is greater than or equal to rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 273 of file data-rate.cc.

References m_bps.

Friends And Related Function Documentation

◆ operator>>

std::istream & operator>> ( std::istream &  is,
DataRate rate 
)
friend

Stream extraction operator.

Parameters
isthe stream
ratethe data rate
Returns
a reference to the stream

Definition at line 331 of file data-rate.cc.

Member Data Documentation

◆ m_bps

uint64_t ns3::DataRate::m_bps
private

The documentation for this class was generated from the following files: