A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
error-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
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  *
19  * This file incorporates work covered by the following copyright and
20  * permission notice:
21  *
22  * Copyright (c) 1997 Regents of the University of California.
23  * All rights reserved.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in the
32  * documentation and/or other materials provided with the distribution.
33  * 3. Neither the name of the University nor of the Laboratory may be used
34  * to endorse or promote products derived from this software without
35  * specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  * Contributed by the Daedalus Research Group, UC Berkeley
50  * (http://daedalus.cs.berkeley.edu)
51  *
52  * This code has been ported from ns-2 (queue/errmodel.{cc,h}
53  */
54 
55 #include <math.h>
56 
57 #include "error-model.h"
58 
59 #include "ns3/packet.h"
60 #include "ns3/assert.h"
61 #include "ns3/log.h"
62 #include "ns3/boolean.h"
63 #include "ns3/enum.h"
64 #include "ns3/double.h"
65 #include "ns3/string.h"
66 #include "ns3/pointer.h"
67 
68 NS_LOG_COMPONENT_DEFINE ("ErrorModel");
69 
70 namespace ns3 {
71 
72 NS_OBJECT_ENSURE_REGISTERED (ErrorModel);
73 
75 {
76  static TypeId tid = TypeId ("ns3::ErrorModel")
77  .SetParent<Object> ()
78  .AddAttribute ("IsEnabled", "Whether this ErrorModel is enabled or not.",
79  BooleanValue (true),
80  MakeBooleanAccessor (&ErrorModel::m_enable),
81  MakeBooleanChecker ())
82  ;
83  return tid;
84 }
85 
87  m_enable (true)
88 {
90 }
91 
93 {
95 }
96 
97 bool
99 {
101  bool result;
102  // Insert any pre-conditions here
103  result = DoCorrupt (p);
104  // Insert any post-conditions here
105  return result;
106 }
107 
108 void
110 {
112  DoReset ();
113 }
114 
115 void
117 {
119  m_enable = true;
120 }
121 
122 void
124 {
126  m_enable = false;
127 }
128 
129 bool
131 {
133  return m_enable;
134 }
135 
136 //
137 // RateErrorModel
138 //
139 
141 
143 {
144  static TypeId tid = TypeId ("ns3::RateErrorModel")
145  .SetParent<ErrorModel> ()
146  .AddConstructor<RateErrorModel> ()
147  .AddAttribute ("ErrorUnit", "The error unit",
150  MakeEnumChecker (ERROR_UNIT_BIT, "ERROR_UNIT_BIT",
151  ERROR_UNIT_BYTE, "ERROR_UNIT_BYTE",
152  ERROR_UNIT_PACKET, "ERROR_UNIT_PACKET"))
153  .AddAttribute ("ErrorRate", "The error rate.",
154  DoubleValue (0.0),
155  MakeDoubleAccessor (&RateErrorModel::m_rate),
156  MakeDoubleChecker<double> ())
157  .AddAttribute ("RanVar", "The decision variable attached to this error model.",
158  StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),
159  MakePointerAccessor (&RateErrorModel::m_ranvar),
160  MakePointerChecker<RandomVariableStream> ())
161  ;
162  return tid;
163 }
164 
165 
167 {
169 }
170 
172 {
174 }
175 
178 {
180  return m_unit;
181 }
182 
183 void
185 {
187  m_unit = error_unit;
188 }
189 
190 double
192 {
194  return m_rate;
195 }
196 
197 void
199 {
201  m_rate = rate;
202 }
203 
204 void
206 {
208  m_ranvar = ranvar;
209 }
210 
211 int64_t
213 {
214  m_ranvar->SetStream (stream);
215  return 1;
216 }
217 
218 bool
220 {
222  if (!IsEnabled ())
223  {
224  return false;
225  }
226  switch (m_unit)
227  {
228  case ERROR_UNIT_PACKET:
229  return DoCorruptPkt (p);
230  case ERROR_UNIT_BYTE:
231  return DoCorruptByte (p);
232  case ERROR_UNIT_BIT:
233  return DoCorruptBit (p);
234  default:
235  NS_ASSERT_MSG (false, "m_unit not supported yet");
236  break;
237  }
238  return false;
239 }
240 
241 bool
243 {
245  return (m_ranvar->GetValue () < m_rate);
246 }
247 
248 bool
250 {
252  // compute pkt error rate, assume uniformly distributed byte error
253  double per = 1 - pow (1.0 - m_rate, p->GetSize ());
254  return (m_ranvar->GetValue () < per);
255 }
256 
257 bool
259 {
261  // compute pkt error rate, assume uniformly distributed bit error
262  double per = 1 - pow (1.0 - m_rate, (8 * p->GetSize ()) );
263  return (m_ranvar->GetValue () < per);
264 }
265 
266 void
268 {
270  /* re-initialize any state; no-op for now */
271 }
272 
273 //
274 // ListErrorModel
275 //
276 
278 
280 {
281  static TypeId tid = TypeId ("ns3::ListErrorModel")
282  .SetParent<ErrorModel> ()
283  .AddConstructor<ListErrorModel> ()
284  ;
285  return tid;
286 }
287 
289 {
291 }
292 
294 {
296 }
297 
298 std::list<uint32_t>
300 {
302  return m_packetList;
303 }
304 
305 void
306 ListErrorModel::SetList (const std::list<uint32_t> &packetlist)
307 {
309  m_packetList = packetlist;
310 }
311 
312 // When performance becomes a concern, the list provided could be
313 // converted to a dynamically-sized array of uint32_t to avoid
314 // list iteration below.
315 bool
317 {
319  if (!IsEnabled ())
320  {
321  return false;
322  }
323  uint32_t uid = p->GetUid ();
324  for (PacketListCI i = m_packetList.begin ();
325  i != m_packetList.end (); i++)
326  {
327  if (uid == *i)
328  {
329  return true;
330  }
331  }
332  return false;
333 }
334 
335 void
337 {
339  m_packetList.clear ();
340 }
341 
342 //
343 // ReceiveListErrorModel
344 //
345 
347 
349 {
350  static TypeId tid = TypeId ("ns3::ReceiveListErrorModel")
351  .SetParent<ErrorModel> ()
352  .AddConstructor<ReceiveListErrorModel> ()
353  ;
354  return tid;
355 }
356 
357 
359  m_timesInvoked (0)
360 {
362 }
363 
365 {
367 }
368 
369 std::list<uint32_t>
371 {
373  return m_packetList;
374 }
375 
376 void
377 ReceiveListErrorModel::SetList (const std::list<uint32_t> &packetlist)
378 {
380  m_packetList = packetlist;
381 }
382 
383 bool
385 {
387  if (!IsEnabled ())
388  {
389  return false;
390  }
391  m_timesInvoked += 1;
392  for (PacketListCI i = m_packetList.begin ();
393  i != m_packetList.end (); i++)
394  {
395  if (m_timesInvoked - 1 == *i)
396  {
397  return true;
398  }
399  }
400  return false;
401 }
402 
403 void
405 {
407  m_packetList.clear ();
408 }
409 
410 
411 } // namespace ns3