A Discrete-Event Network Simulator
API
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. More...
 
 DataRate (uint64_t bps)
 Integer constructor. More...
 
Time CalculateBitsTxTime (uint32_t bits) const
 Calculate transmission time. More...
 
Time CalculateBytesTxTime (uint32_t bytes) const
 Calculate transmission time. More...
 
uint64_t GetBitRate () const
 Get the underlying bitrate. More...
 
bool operator!= (const DataRate &rhs) const
 
DataRate operator* (double rhs)
 Scales the DataRate. More...
 
DataRate operator* (uint64_t rhs)
 Scales the DataRate. More...
 
DataRateoperator*= (double rhs)
 Scales the DataRate. More...
 
DataRateoperator*= (uint64_t rhs)
 Scales the DataRate. More...
 
DataRate operator+ (DataRate rhs)
 
DataRateoperator+= (DataRate rhs)
 
DataRate operator- (DataRate rhs)
 
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. More...
 

Private Attributes

uint64_t m_bps
 data rate [bps] More...
 

Friends

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

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);
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:793
list x
Random number samples.

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 multipler 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 187 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 193 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 293 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 281 of file data-rate.cc.

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

Referenced by 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 275 of file data-rate.cc.

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

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 34 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 270 of file data-rate.cc.

References m_bps.

◆ 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 223 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)

Scales the DataRate.

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

Definition at line 234 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 multipy to this datarate

Definition at line 228 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 multipy to this datarate

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

References m_bps.

◆ 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 199 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 204 of file data-rate.cc.

References m_bps.

◆ 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 210 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 216 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 245 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 250 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 265 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 255 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 260 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 310 of file data-rate.cc.

Member Data Documentation

◆ m_bps


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