00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 // 00003 // Copyright (c) 2006 Georgia Tech Research Corporation 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License version 2 as 00007 // published by the Free Software Foundation; 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 // 00018 // Author: Rajib Bhattacharjea<raj.b@gatech.edu> 00019 // 00020 00021 #ifndef DATA_RATE_H 00022 #define DATA_RATE_H 00023 00024 #include <string> 00025 #include <iostream> 00026 #include <stdint.h> 00027 #include "ns3/nstime.h" 00028 #include "ns3/attribute.h" 00029 #include "ns3/attribute-helper.h" 00030 00031 namespace ns3 { 00032 00033 /** 00034 * \ingroup common 00035 * \defgroup datarate Data Rate 00036 */ 00037 /** 00038 * \ingroup datarate 00039 * \brief Class for representing data rates 00040 * 00041 * Allows for natural and familiar use of data rates. Allows construction 00042 * from strings, natural multiplication e.g.: 00043 * \code 00044 * DataRate x("56kbps"); 00045 * double nBits = x*ns3::Seconds(19.2); 00046 * uint32_t nBytes = 20; 00047 * double txtime = x.CalclulateTxTime(nBytes); 00048 * \endcode 00049 * This class also supports the regular comparison operators <, >, <=, >=, ==, 00050 * and != 00051 * 00052 * Conventions used: 00053 * "b" stands for bits, "B" for bytes (8 bits) \n 00054 * "k" stands for 1000, "K" also stands for 1000, "Ki" stands for 1024 \n 00055 * "M" stand for 1000000, "Mib" stands for 1024 kibibits, or 1048576 bits \n 00056 * "G" stand for 10^9, "Gib" stands for 1024 mebibits \n 00057 * whitespace is allowed but not required between the numeric value and units 00058 * 00059 * Supported unit strings: 00060 * bps, b/s, Bps, B/s \n 00061 * kbps, kb/s, Kbps, Kb/s, kBps, kB/s, KBps, KB/s, Kib/s, KiB/s \n 00062 * Mbps, Mb/s, MBps, MB/s, Mib/s, MiB/s \n 00063 * Gbps, Gb/s, GBps, GB/s, Gib/s, GiB/s \n 00064 * 00065 * Examples: 00066 * "56kbps" = 56,000 bits/s \n 00067 * "128 kb/s" = 128,000 bits/s \n 00068 * "8Kib/s" = 1 KiB/s = 8192 bits/s \n 00069 * "1kB/s" = 8000 bits/s 00070 */ 00071 class DataRate 00072 { 00073 public: 00074 DataRate (); 00075 /** 00076 * \brief Integer constructor 00077 * 00078 * Construct a data rate from an integer. This class only supports positive 00079 * integer data rates in units of bits/s, meaning 1bit/s is the smallest 00080 * non-trivial bitrate availiable. 00081 */ 00082 DataRate (uint64_t bps); 00083 DataRate (std::string rate); 00084 00085 bool operator < (const DataRate& rhs) const; 00086 bool operator <= (const DataRate& rhs) const; 00087 bool operator > (const DataRate& rhs) const; 00088 bool operator >= (const DataRate& rhs) const; 00089 bool operator == (const DataRate& rhs) const; 00090 bool operator != (const DataRate& rhs) const; 00091 00092 /** 00093 * \brief Calculate transmission time 00094 * 00095 * Calculates the transmission time at this data rate 00096 * \param bytes The number of bytes (not bits) for which to calculate 00097 * \return The tranmission time in seconds for the number of bytes specified 00098 */ 00099 double CalculateTxTime(uint32_t bytes) const; 00100 00101 /** 00102 * Get the underlying bitrate 00103 * \return The underlying bitrate in bits per second 00104 */ 00105 uint64_t GetBitRate() const; 00106 00107 private: 00108 uint64_t m_bps; 00109 static uint64_t Parse(const std::string); 00110 }; 00111 00112 std::ostream &operator << (std::ostream &os, const DataRate &rate); 00113 std::istream &operator >> (std::istream &is, DataRate &rate); 00114 00115 /** 00116 * \class ns3::DataRateValue 00117 * \brief hold objects of type ns3::DataRate 00118 */ 00119 00120 ATTRIBUTE_HELPER_HEADER (DataRate); 00121 00122 /** 00123 * \param lhs 00124 * \param rhs 00125 * \return Bits transmitted in rhs seconds at lhs b/s 00126 */ 00127 double operator*(const DataRate& lhs, const TimeUnit<1>& rhs); 00128 double operator*(const TimeUnit<1>& lhs, const DataRate& rhs); 00129 00130 } //namespace ns3 00131 00132 #endif /* DATA_RATE_H */