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 
200 {
201  return DataRate(m_bps + rhs.m_bps);
202 }
203 
205 {
206  m_bps += rhs.m_bps;
207  return *this;
208 }
209 
211 {
212  NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
213  return DataRate(m_bps - rhs.m_bps);
214 }
215 
217 {
218  NS_ASSERT_MSG(m_bps >= rhs.m_bps, "Data Rate cannot be negative.");
219  m_bps -= rhs.m_bps;
220  return *this;
221 }
222 
224 {
225  return DataRate(((uint64_t)(m_bps * rhs)));
226 }
227 
229 {
230  m_bps *= rhs;
231  return *this;
232 }
233 
235 {
236  return DataRate(m_bps * rhs);
237 }
238 
240 {
241  m_bps *= rhs;
242  return *this;
243 }
244 
245 bool DataRate::operator < (const DataRate& rhs) const
246 {
247  return m_bps<rhs.m_bps;
248 }
249 
250 bool DataRate::operator <= (const DataRate& rhs) const
251 {
252  return m_bps<=rhs.m_bps;
253 }
254 
255 bool DataRate::operator > (const DataRate& rhs) const
256 {
257  return m_bps>rhs.m_bps;
258 }
259 
260 bool DataRate::operator >= (const DataRate& rhs) const
261 {
262  return m_bps>=rhs.m_bps;
263 }
264 
265 bool DataRate::operator == (const DataRate& rhs) const
266 {
267  return m_bps==rhs.m_bps;
268 }
269 
270 bool DataRate::operator != (const DataRate& rhs) const
271 {
272  return m_bps!=rhs.m_bps;
273 }
274 
275 Time DataRate::CalculateBytesTxTime (uint32_t bytes) const
276 {
277  NS_LOG_FUNCTION (this << bytes);
278  return Seconds (bytes * 8) / m_bps;
279 }
280 
281 Time DataRate::CalculateBitsTxTime (uint32_t bits) const
282 {
283  NS_LOG_FUNCTION (this << bits);
284  return Seconds (bits) / m_bps;
285 }
286 
287 uint64_t DataRate::GetBitRate () const
288 {
289  NS_LOG_FUNCTION (this);
290  return m_bps;
291 }
292 
293 DataRate::DataRate (std::string rate)
294 {
295  NS_LOG_FUNCTION (this << rate);
296  bool ok = DoParse (rate, &m_bps);
297  if (!ok)
298  {
299  NS_FATAL_ERROR ("Could not parse rate: "<<rate);
300  }
301 }
302 
303 /* For printing of data rate */
304 std::ostream &operator << (std::ostream &os, const DataRate &rate)
305 {
306  os << rate.GetBitRate () << "bps";
307  return os;
308 }
309 /* Initialize a data rate from an input stream */
310 std::istream &operator >> (std::istream &is, DataRate &rate)
311 {
312  std::string value;
313  is >> value;
314  uint64_t v;
315  bool ok = DataRate::DoParse (value, &v);
316  if (!ok)
317  {
318  is.setstate (std::ios_base::failbit);
319  }
320  rate = DataRate (v);
321  return is;
322 }
323 
324 double operator* (const DataRate& lhs, const Time& rhs)
325 {
326  return rhs.GetSeconds ()*lhs.GetBitRate ();
327 }
328 
329 double operator* (const Time& lhs, const DataRate& rhs)
330 {
331  return lhs.GetSeconds ()*rhs.GetBitRate ();
332 }
333 
334 } // namespace ns3
Class for representing data rates.
Definition: data-rate.h:89
DataRate & operator*=(double rhs)
Scales the DataRate.
Definition: data-rate.cc:228
bool operator==(const DataRate &rhs) const
Definition: data-rate.cc:265
bool operator<(const DataRate &rhs) const
Definition: data-rate.cc:245
bool operator>(const DataRate &rhs) const
Definition: data-rate.cc:255
bool operator!=(const DataRate &rhs) const
Definition: data-rate.cc:270
bool operator>=(const DataRate &rhs) const
Definition: data-rate.cc:260
Time CalculateBitsTxTime(uint32_t bits) const
Calculate transmission time.
Definition: data-rate.cc:281
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 m_bps
data rate [bps]
Definition: data-rate.h:277
uint64_t GetBitRate() const
Get the underlying bitrate.
Definition: data-rate.cc:287
bool operator<=(const DataRate &rhs) const
Definition: data-rate.cc:250
DataRate operator-(DataRate rhs)
Definition: data-rate.cc:210
DataRate operator*(double rhs)
Scales the DataRate.
Definition: data-rate.cc:223
DataRate operator+(DataRate rhs)
Definition: data-rate.cc:199
DataRate & operator+=(DataRate rhs)
Definition: data-rate.cc:204
Time CalculateBytesTxTime(uint32_t bytes) const
Calculate transmission time.
Definition: data-rate.cc:275
DataRate & operator-=(DataRate rhs)
Definition: data-rate.cc:216
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:329
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139