From 4edddaeb2dfdec992ebe1d1c0d77ce2f694308ca Mon Sep 17 00:00:00 2001 From: Raj Bhattacharjea Date: Thu, 22 Mar 2007 17:20:15 -0400 Subject: [PATCH] Added in DataRate to common module --- SConstruct | 2 + src/common/data-rate.cc | 101 ++++++++++++++++++++++++++++++++++++++++ src/common/data-rate.h | 97 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 200 insertions(+) create mode 100644 src/common/data-rate.cc create mode 100644 src/common/data-rate.h diff --git a/SConstruct b/SConstruct index dfd93dcd7..326ca4925 100644 --- a/SConstruct +++ b/SConstruct @@ -153,6 +153,7 @@ common.add_sources([ 'empty-trace-resolver.cc', 'composite-trace-resolver.cc', 'trace-root.cc', + 'data-rate.cc', ]) common.add_headers ([ ]) @@ -177,6 +178,7 @@ common.add_inst_headers([ 'terminal-trace-resolver.h', 'smartvector.h', 'smartset.h', + 'data-rate.h', ]) node = build.Ns3Module ('node', 'src/node') diff --git a/src/common/data-rate.cc b/src/common/data-rate.cc new file mode 100644 index 000000000..1ff938d6c --- /dev/null +++ b/src/common/data-rate.cc @@ -0,0 +1,101 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +// +// Copyright (c) 2006 Georgia Tech Research Corporation +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// Author: Rajib Bhattacharjea +// + +#include "data-rate.h" +#include "ns3/nstime.h" + +namespace ns3{ + +DataRate::DataRate(uint64_t bps) + :m_bps(bps) +{ +} + +DataRate::DataRate (const std::string s) + : m_bps(DataRate::Parse(s)) +{ +} + +uint64_t DataRate::Parse(const std::string s) +{ + std::string::size_type n = s.find_first_not_of("0123456789."); + if (n != std::string::npos) + { // Found non-numeric + double r = atof(s.substr(0, n).c_str()); + std::string trailer = s.substr(n, std::string::npos); + if (trailer == std::string("bps")) + return (uint64_t)r; // Bit/s + if (trailer == std::string("b/s")) + return (uint64_t)r; // Bit/s + if (trailer == std::string("Bps")) + return (uint64_t)(r * 8); // Byte/s + if (trailer == std::string("B/s")) + return (uint64_t)(r * 8); // Byte/s + if (trailer == std::string("kbps")) + return (uint64_t)(r * 1000); // KiloBit/s + if (trailer == std::string("kb/s")) + return (uint64_t)(r * 1000); // KiloBit/s + if (trailer == std::string("kBps")) + return (uint64_t)(r * 1000); // KiloBit/s + if (trailer == std::string("kB/s")) + return (uint64_t)(r * 1000); // KiloBit/s + if (trailer == std::string("Mbps")) + return (uint64_t)(r * 1000000); // MegaBit/s + if (trailer == std::string("Mb/s")) + return (uint64_t)(r * 1000000); // MegaBit/s + if (trailer == std::string("MBps")) + return (uint64_t)(r * 8000000); // MegaByte/s + if (trailer == std::string("MB/s")) + return (uint64_t)(r * 8000000); // MegaByte/s + if (trailer == std::string("Gbps")) + return (uint64_t)(r * 1000000000); // GigaBit/s + if (trailer == std::string("Gb/s")) + return (uint64_t)(r * 1000000000); // GigaBit/s + if (trailer == std::string("GBps")) + return (uint64_t)(r * 8*1000000000); // GigaByte/s + if (trailer == std::string("GB/s")) + return (uint64_t)(r * 8*1000000000); // GigaByte/s + NS_FATAL_ERROR("Can't Parse data rate "< +// + +#include "ns3/fatal-error.h" +#include +#include +#include +#include "ns3/nstime.h" + +namespace ns3{ + +/** + * \brief Class for representing data rates + * + * Allows for natural and familiar use of data rates. Allows construction + * from strings, natural multiplication e.g.: + * \code + * DataRate x("56kbps"); + * double nBits = x*ns3::Seconds(19.2); + * uint32_t nBytes = 20; + * double txtime = x.CalclulateTxTime(nBytes); + * \endcode + */ +class DataRate +{ + public: + /** + * \brief 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 availiable. + */ + DataRate (uint64_t bps); + + /** + * \brief String constructor + * + * Construct a DataRate from a string. The supported strings have a + * numerical portion, followed by units in the following format: + * - Prefix: nothing, "k", "M", "G" + * - Data Unit: "b, "B" + * - Time Suffix: "ps", "/s" + * The prefixes are SI powers of 10 (10^0,10^3,10^6,10^9 respectively).\n + * The units are the bit, and the (8-bit) byte respectively.\n + * Both time suffixes denote "per second". Some supported examples include + * "20B/s", "56kbps", "4.4MB/s", and "100Gb/s". Any malformed string causes + * a fatal error. + */ + DataRate (const std::string s); + + /** + * \brief Calculate transmission time + * + * Calculates the transmission time at this data rate + * \param bytes The number of bytes (not bits) for which to calculate + * \return The tranmission time in seconds for the number of bytes specified + */ + double CalculateTxTime(uint32_t bytes) const; + + /** + * Get the underlying bitrate + * \return The underlying bitrate in bits per second + */ + uint64_t GetBitRate() const; + + private: + uint64_t m_bps; + static uint64_t Parse(const std::string); +}; +/** + * \param lhs + * \param rhs + * \return Bits transmitted in rhs seconds at lhs b/s + */ +double operator*(const DataRate& lhs, const TimeUnit<1>& rhs); +double operator*(const TimeUnit<1>& lhs, const DataRate& rhs); + +};//namespace ns3