A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 
26 static bool
27 DoParse (const std::string s, uint64_t *v)
28 {
29  std::string::size_type n = s.find_first_not_of ("0123456789.");
30  if (n != std::string::npos)
31  { // Found non-numeric
32  std::istringstream iss;
33  iss.str (s.substr (0, n));
34  double r;
35  iss >> r;
36  std::string trailer = s.substr (n, std::string::npos);
37  if (trailer == "bps")
38  {
39  // bit/s
40  *v = (uint64_t)r;
41  }
42  else if (trailer == "b/s")
43  {
44  // bit/s
45  *v = (uint64_t)r;
46  }
47  else if (trailer == "Bps")
48  {
49  // byte/s
50  *v = (uint64_t)(r * 8);
51  }
52  else if (trailer == "B/s")
53  {
54  // byte/s
55  *v = (uint64_t)(r * 8);
56  }
57  else if (trailer == "kbps")
58  {
59  // kilobits/s
60  *v = (uint64_t)(r * 1000);
61  }
62  else if (trailer == "kb/s")
63  {
64  // kilobits/s
65  *v = (uint64_t)(r * 1000);
66  }
67  else if (trailer == "Kbps")
68  {
69  // kilobits/s
70  *v = (uint64_t)(r * 1000);
71  }
72  else if (trailer == "Kb/s")
73  {
74  // kilobits/s
75  *v = (uint64_t)(r * 1000);
76  }
77  else if (trailer == "kBps")
78  {
79  // kiloByte/s
80  *v = (uint64_t)(r * 8000);
81  }
82  else if (trailer == "kB/s")
83  {
84  // KiloByte/s
85  *v = (uint64_t)(r * 8000);
86  }
87  else if (trailer == "KBps")
88  {
89  // kiloByte/s
90  *v = (uint64_t)(r * 8000);
91  }
92  else if (trailer == "KB/s")
93  {
94  // KiloByte/s
95  *v = (uint64_t)(r * 8000);
96  }
97  else if (trailer == "Kib/s")
98  {
99  // kibibit/s
100  *v = (uint64_t)(r * 1024);
101  }
102  else if (trailer == "KiB/s")
103  {
104  // kibibyte/s
105  *v = (uint64_t)(r * 8192);
106  }
107  else if (trailer == "Mbps")
108  {
109  // MegaBits/s
110  *v = (uint64_t)(r * 1000000);
111  }
112  else if (trailer == "Mb/s")
113  {
114  // MegaBits/s
115  *v = (uint64_t)(r * 1000000);
116  }
117  else if (trailer == "MBps")
118  {
119  // MegaBytes/s
120  *v = (uint64_t)(r * 8000000);
121  }
122  else if (trailer == "MB/s")
123  {
124  // MegaBytes/s
125  *v = (uint64_t)(r * 8000000);
126  }
127  else if (trailer == "Mib/s")
128  {
129  // MebiBits/s
130  *v = (uint64_t)(r * 1048576);
131  }
132  else if (trailer == "MiB/s")
133  {
134  // MebiByte/s
135  *v = (uint64_t)(r * 1048576 * 8);
136  }
137  else if (trailer == "Gbps")
138  {
139  // GigaBit/s
140  *v = (uint64_t)(r * 1000000000);
141  }
142  else if (trailer == "Gb/s")
143  {
144  // GigaBit/s
145  *v = (uint64_t)(r * 1000000000);
146  }
147  else if (trailer == "GBps")
148  {
149  // GigaByte/s
150  *v = (uint64_t)(r * 8*1000000000);
151  }
152  else if (trailer == "GB/s")
153  {
154  // GigaByte/s
155  *v = (uint64_t)(r * 8*1000000000);
156  }
157  else if (trailer == "Gib/s")
158  {
159  // GibiBits/s
160  *v = (uint64_t)(r * 1048576 * 1024);
161  }
162  else if (trailer == "GiB/s")
163  {
164  // GibiByte/s
165  *v = (uint64_t)(r * 1048576 * 1024 * 8);
166  }
167  else
168  {
169  return false;
170  }
171  return true;
172  }
173  std::istringstream iss;
174  iss.str (s);
175  iss >> *v;
176  return true;
177 }
178 
179 
180 namespace ns3 {
181 
182 ATTRIBUTE_HELPER_CPP (DataRate);
183 
185  : m_bps (0)
186 {
187 }
188 
189 DataRate::DataRate(uint64_t bps)
190  : m_bps (bps)
191 {
192 }
193 
194 bool DataRate::operator < (const DataRate& rhs) const
195 {
196  return m_bps<rhs.m_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 double DataRate::CalculateTxTime (uint32_t bytes) const
225 {
226  return static_cast<double>(bytes)*8/m_bps;
227 }
228 
229 uint64_t DataRate::GetBitRate () const
230 {
231  return m_bps;
232 }
233 
234 DataRate::DataRate (std::string rate)
235 {
236  bool ok = DoParse (rate, &m_bps);
237  if (!ok)
238  {
239  NS_FATAL_ERROR ("Could not parse rate: "<<rate);
240  }
241 }
242 
243 std::ostream &operator << (std::ostream &os, const DataRate &rate)
244 {
245  os << rate.GetBitRate () << "bps";
246  return os;
247 }
248 std::istream &operator >> (std::istream &is, DataRate &rate)
249 {
250  std::string value;
251  is >> value;
252  uint64_t v;
253  bool ok = DoParse (value, &v);
254  if (!ok)
255  {
256  is.setstate (std::ios_base::failbit);
257  }
258  rate = DataRate (v);
259  return is;
260 }
261 
262 
263 
264 double operator* (const DataRate& lhs, const Time& rhs)
265 {
266  return rhs.GetSeconds ()*lhs.GetBitRate ();
267 }
268 
269 double operator* (const Time& lhs, const DataRate& rhs)
270 {
271  return lhs.GetSeconds ()*rhs.GetBitRate ();
272 }
273 
274 } // namespace ns3