src/common/data-rate.cc
author Tom Henderson <tomh@tomh.org>
Sun, 25 Mar 2007 15:33:24 -0700
changeset 373 960c8e8721de
parent 365 2a5cd7125778
child 614 fb21d7015386
permissions -rw-r--r--
Move simple-p2p.cc to DataRate; fix small bug in DataRate and add include guards
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
365
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     2
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     3
// Copyright (c) 2006 Georgia Tech Research Corporation
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     4
// All rights reserved.
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     5
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     6
// This program is free software; you can redistribute it and/or modify
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     7
// it under the terms of the GNU General Public License version 2 as
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     8
// published by the Free Software Foundation;
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
     9
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    10
// This program is distributed in the hope that it will be useful,
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    13
// GNU General Public License for more details.
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    14
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    15
// You should have received a copy of the GNU General Public License
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    16
// along with this program; if not, write to the Free Software
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    18
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    19
// Author: Rajib Bhattacharjea<raj.b@gatech.edu>
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    20
//
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    21
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    22
#include "data-rate.h"
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    23
#include "ns3/nstime.h"
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    24
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    25
namespace ns3{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    26
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    27
DataRate::DataRate(uint64_t bps)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    28
  :m_bps(bps)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    29
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    30
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    31
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    32
DataRate::DataRate (const std::string s)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    33
  : m_bps(DataRate::Parse(s))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    34
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    35
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    36
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    37
uint64_t DataRate::Parse(const std::string s)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    38
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    39
  std::string::size_type n = s.find_first_not_of("0123456789.");
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    40
  if (n != std::string::npos)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    41
  { // Found non-numeric
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    42
    double r = atof(s.substr(0, n).c_str());
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    43
    std::string trailer = s.substr(n, std::string::npos);
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    44
    if (trailer == std::string("bps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    45
      return (uint64_t)r;                // Bit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    46
    if (trailer == std::string("b/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    47
      return (uint64_t)r;                // Bit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    48
    if (trailer == std::string("Bps")) 
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    49
      return  (uint64_t)(r * 8);          // Byte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    50
    if (trailer == std::string("B/s")) 
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    51
      return  (uint64_t)(r * 8);          // Byte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    52
    if (trailer == std::string("kbps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    53
      return  (uint64_t)(r * 1000);       // KiloBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    54
    if (trailer == std::string("kb/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    55
      return  (uint64_t)(r * 1000);       // KiloBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    56
    if (trailer == std::string("kBps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    57
      return  (uint64_t)(r * 1000);       // KiloBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    58
    if (trailer == std::string("kB/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    59
      return  (uint64_t)(r * 1000);       // KiloBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    60
    if (trailer == std::string("Mbps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    61
      return  (uint64_t)(r * 1000000);    // MegaBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    62
    if (trailer == std::string("Mb/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    63
      return  (uint64_t)(r * 1000000);    // MegaBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    64
    if (trailer == std::string("MBps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    65
      return  (uint64_t)(r * 8000000);    // MegaByte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    66
    if (trailer == std::string("MB/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    67
      return  (uint64_t)(r * 8000000);    // MegaByte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    68
    if (trailer == std::string("Gbps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    69
      return  (uint64_t)(r * 1000000000); // GigaBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    70
    if (trailer == std::string("Gb/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    71
      return  (uint64_t)(r * 1000000000); // GigaBit/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    72
    if (trailer == std::string("GBps"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    73
      return  (uint64_t)(r * 8*1000000000); // GigaByte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    74
    if (trailer == std::string("GB/s"))
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    75
      return  (uint64_t)(r * 8*1000000000); // GigaByte/s
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    76
    NS_FATAL_ERROR("Can't Parse data rate "<<s);
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    77
  }
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    78
  return atoll(s.c_str());
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    79
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    80
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    81
double DataRate::CalculateTxTime(uint32_t bytes) const
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    82
{
373
960c8e8721de Move simple-p2p.cc to DataRate; fix small bug in DataRate and add include guards
Tom Henderson <tomh@tomh.org>
parents: 365
diff changeset
    83
  return static_cast<double>(bytes)*8/m_bps;
365
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    84
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    85
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    86
uint64_t DataRate::GetBitRate() const
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    87
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    88
  return m_bps;
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    89
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    90
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    91
double operator*(const DataRate& lhs, const Time& rhs)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    92
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    93
  return rhs.GetSeconds()*lhs.GetBitRate();
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    94
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    95
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    96
double operator*(const Time& lhs, const DataRate& rhs)
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    97
{
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    98
  return lhs.GetSeconds()*rhs.GetBitRate();
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
    99
}
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   100
2a5cd7125778 Added in DataRate to common module
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
diff changeset
   101
};//namespace ns3