A Discrete-Event Network Simulator
API
data-rate.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 //
3 // Copyright (c) 2006 Georgia Tech Research Corporation
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 2 as
7 // published by the Free Software Foundation;
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 //
18 // Author: Rajib Bhattacharjea<raj.b@gatech.edu>
19 //
20 
21 #include "data-rate.h"
22 #include "ns3/nstime.h"
23 #include "ns3/fatal-error.h"
24 #include "ns3/log.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("DataRate");
29 
31 
32 /* static */
33 bool
34 DataRate::DoParse (const std::string s, uint64_t *v)
35 {
36  NS_LOG_FUNCTION (s << v);
37  std::string::size_type n = s.find_first_not_of ("0123456789.");
38  if (n != std::string::npos)
39  { // Found non-numeric
40  std::istringstream iss;
41  iss.str (s.substr (0, n));
42  double r;
43  iss >> r;
44  std::string trailer = s.substr (n, std::string::npos);
45  if (trailer == "bps")
46  {
47  // bit/s
48  *v = (uint64_t)r;
49  }
50  else if (trailer == "b/s")
51  {
52  // bit/s
53  *v = (uint64_t)r;
54  }
55  else if (trailer == "Bps")
56  {
57  // byte/s
58  *v = (uint64_t)(r * 8);
59  }
60  else if (trailer == "B/s")
61  {
62  // byte/s
63  *v = (uint64_t)(r * 8);
64  }
65  else if (trailer == "kbps")
66  {
67  // kilobits/s
68  *v = (uint64_t)(r * 1000);
69  }
70  else if (trailer == "kb/s")
71  {
72  // kilobits/s
73  *v = (uint64_t)(r * 1000);
74  }
75  else if (trailer == "Kbps")
76  {
77  // kilobits/s
78  *v = (uint64_t)(r * 1000);
79  }
80  else if (trailer == "Kb/s")
81  {
82  // kilobits/s
83  *v = (uint64_t)(r * 1000);
84  }
85  else if (trailer == "kBps")
86  {
87  // kiloByte/s
88  *v = (uint64_t)(r * 8000);
89  }
90  else if (trailer == "kB/s")
91  {
92  // KiloByte/s
93  *v = (uint64_t)(r * 8000);
94  }
95  else if (trailer == "KBps")
96  {
97  // kiloByte/s
98  *v = (uint64_t)(r * 8000);
99  }
100  else if (trailer == "KB/s")
101  {
102  // KiloByte/s
103  *v = (uint64_t)(r * 8000);
104  }
105  else if (trailer == "Kib/s")
106  {
107  // kibibit/s
108  *v = (uint64_t)(r * 1024);
109  }
110  else if (trailer == "KiB/s")
111  {
112  // kibibyte/s
113  *v = (uint64_t)(r * 8192);
114  }
115  else if (trailer == "Mbps")
116  {
117  // MegaBits/s
118  *v = (uint64_t)(r * 1000000);
119  }
120  else if (trailer == "Mb/s")
121  {
122  // MegaBits/s
123  *v = (uint64_t)(r * 1000000);
124  }
125  else if (trailer == "MBps")
126  {
127  // MegaBytes/s
128  *v = (uint64_t)(r * 8000000);
129  }
130  else if (trailer == "MB/s")
131  {
132  // MegaBytes/s
133  *v = (uint64_t)(r * 8000000);
134  }
135  else if (trailer == "Mib/s")
136  {
137  // MebiBits/s
138  *v = (uint64_t)(r * 1048576);
139  }
140  else if (trailer == "MiB/s")
141  {
142  // MebiByte/s
143  *v = (uint64_t)(r * 1048576 * 8);
144  }
145  else if (trailer == "Gbps")
146  {
147  // GigaBit/s
148  *v = (uint64_t)(r * 1000000000);
149  }
150  else if (trailer == "Gb/s")
151  {
152  // GigaBit/s
153  *v = (uint64_t)(r * 1000000000);
154  }
155  else if (trailer == "GBps")
156  {
157  // GigaByte/s
158  *v = (uint64_t)(r * 8*1000000000);
159  }
160  else if (trailer == "GB/s")
161  {
162  // GigaByte/s
163  *v = (uint64_t)(r * 8*1000000000);
164  }
165  else if (trailer == "Gib/s")
166  {
167  // GibiBits/s
168  *v = (uint64_t)(r * 1048576 * 1024);
169  }
170  else if (trailer == "GiB/s")
171  {
172  // GibiByte/s
173  *v = (uint64_t)(r * 1048576 * 1024 * 8);
174  }
175  else
176  {
177  return false;
178  }
179  return true;
180  }
181  std::istringstream iss;
182  iss.str (s);
183  iss >> *v;
184  return true;
185 }
186 
188  : m_bps (0)
189 {
190  NS_LOG_FUNCTION (this);
191 }
192 
193 DataRate::DataRate(uint64_t bps)
194  : m_bps (bps)
195 {
196  NS_LOG_FUNCTION (this << bps);
197 }
198 
199 bool DataRate::operator < (const DataRate& rhs) const
200 {
201  return m_bps<rhs.m_bps;
202 }
203 
204 bool DataRate::operator <= (const DataRate& rhs) const
205 {
206  return m_bps<=rhs.m_bps;
207 }
208 
209 bool DataRate::operator > (const DataRate& rhs) const
210 {
211  return m_bps>rhs.m_bps;
212 }
213 
214 bool DataRate::operator >= (const DataRate& rhs) const
215 {
216  return m_bps>=rhs.m_bps;
217 }
218 
219 bool DataRate::operator == (const DataRate& rhs) const
220 {
221  return m_bps==rhs.m_bps;
222 }
223 
224 bool DataRate::operator != (const DataRate& rhs) const
225 {
226  return m_bps!=rhs.m_bps;
227 }
228 
229 double DataRate::CalculateTxTime (uint32_t bytes) const
230 {
231  NS_LOG_FUNCTION (this << bytes);
232  return static_cast<double>(bytes)*8/m_bps;
233 }
234 
235 Time DataRate::CalculateBytesTxTime (uint32_t bytes) const
236 {
237  NS_LOG_FUNCTION (this << bytes);
238  // \todo avoid to use double (if possible).
239  return Seconds (static_cast<double>(bytes)*8/m_bps);
240 }
241 
242 Time DataRate::CalculateBitsTxTime (uint32_t bits) const
243 {
244  NS_LOG_FUNCTION (this << bits);
245  // \todo avoid to use double (if possible).
246  return Seconds (static_cast<double>(bits)/m_bps);
247 }
248 
249 uint64_t DataRate::GetBitRate () const
250 {
251  NS_LOG_FUNCTION (this);
252  return m_bps;
253 }
254 
255 DataRate::DataRate (std::string rate)
256 {
257  NS_LOG_FUNCTION (this << rate);
258  bool ok = DoParse (rate, &m_bps);
259  if (!ok)
260  {
261  NS_FATAL_ERROR ("Could not parse rate: "<<rate);
262  }
263 }
264 
265 /* For printing of data rate */
266 std::ostream &operator << (std::ostream &os, const DataRate &rate)
267 {
268  os << rate.GetBitRate () << "bps";
269  return os;
270 }
271 /* Initialize a data rate from an input stream */
272 std::istream &operator >> (std::istream &is, DataRate &rate)
273 {
274  std::string value;
275  is >> value;
276  uint64_t v;
277  bool ok = DataRate::DoParse (value, &v);
278  if (!ok)
279  {
280  is.setstate (std::ios_base::failbit);
281  }
282  rate = DataRate (v);
283  return is;
284 }
285 
286 double operator* (const DataRate& lhs, const Time& rhs)
287 {
288  return rhs.GetSeconds ()*lhs.GetBitRate ();
289 }
290 
291 double operator* (const Time& lhs, const DataRate& rhs)
292 {
293  return lhs.GetSeconds ()*rhs.GetBitRate ();
294 }
295 
296 } // namespace ns3
bool operator==(const DataRate &rhs) const
Definition: data-rate.cc:219
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
bool operator!=(const DataRate &rhs) const
Definition: data-rate.cc:224
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
NS_DEPRECATED double CalculateTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:229
bool operator>(const DataRate &rhs) const
Definition: data-rate.cc:209
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
bool operator<=(const DataRate &rhs) const
Definition: data-rate.cc:204
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
uint64_t m_bps
data rate [bps]
Definition: data-rate.h:221
bool operator<(const DataRate &rhs) const
Definition: data-rate.cc:199
static bool DoParse(const std::string s, uint64_t *v)
Parse a string representing a DataRate into an uint64_t.
Definition: data-rate.cc:34
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:249
Class for representing data rates.
Definition: data-rate.h:88
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:108
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:235
bool operator>=(const DataRate &rhs) const
Definition: data-rate.cc:214