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