A Discrete-Event Network Simulator
API
ns3::DataRate Class Reference

Class for representing data rates. More...

#include "data-rate.h"

Public Member Functions

 DataRate ()
 
 DataRate (uint64_t bps)
 Integer constructor. More...
 
 DataRate (std::string rate)
 String 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
 
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);

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.

◆ 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 249 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 236 of file data-rate.cc.

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

+ Here is the call 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 229 of file data-rate.cc.

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

Referenced by ns3::CsmaNetDevice::Attach(), ns3::TbfQueueDisc::DoDequeue(), ns3::SimpleNetDevice::SendFrom(), ns3::HalfDuplexIdealPhy::StartTx(), ns3::SimpleNetDevice::TransmitComplete(), ns3::PointToPointNetDevice::TransmitStart(), and ns3::CsmaNetDevice::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 sample-rng-plot::n, and NS_LOG_FUNCTION.

Referenced by DataRate(), and ns3::operator>>().

+ Here is the caller graph for this function:

◆ GetBitRate()

uint64_t ns3::DataRate::GetBitRate ( ) const

◆ 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 224 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 rhs
Parameters
rhsthe datarate to compare to this datarate

Definition at line 199 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 204 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 219 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 209 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 214 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 266 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: