A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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 <cmath>
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
74
TypeId
ErrorModel::GetTypeId
(
void
)
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
86
ErrorModel::ErrorModel
() :
87
m_enable (true)
88
{
89
NS_LOG_FUNCTION
(
this
);
90
}
91
92
ErrorModel::~ErrorModel
()
93
{
94
NS_LOG_FUNCTION
(
this
);
95
}
96
97
bool
98
ErrorModel::IsCorrupt
(
Ptr<Packet>
p)
99
{
100
NS_LOG_FUNCTION
(
this
<< p);
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
109
ErrorModel::Reset
(
void
)
110
{
111
NS_LOG_FUNCTION
(
this
);
112
DoReset
();
113
}
114
115
void
116
ErrorModel::Enable
(
void
)
117
{
118
NS_LOG_FUNCTION
(
this
);
119
m_enable
=
true
;
120
}
121
122
void
123
ErrorModel::Disable
(
void
)
124
{
125
NS_LOG_FUNCTION
(
this
);
126
m_enable
=
false
;
127
}
128
129
bool
130
ErrorModel::IsEnabled
(
void
)
const
131
{
132
NS_LOG_FUNCTION
(
this
);
133
return
m_enable
;
134
}
135
136
//
137
// RateErrorModel
138
//
139
140
NS_OBJECT_ENSURE_REGISTERED
(
RateErrorModel
);
141
142
TypeId
RateErrorModel::GetTypeId
(
void
)
143
{
144
static
TypeId
tid =
TypeId
(
"ns3::RateErrorModel"
)
145
.
SetParent
<
ErrorModel
> ()
146
.AddConstructor<RateErrorModel> ()
147
.AddAttribute (
"ErrorUnit"
,
"The error unit"
,
148
EnumValue
(
ERROR_UNIT_BYTE
),
149
MakeEnumAccessor
(&
RateErrorModel::m_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
166
RateErrorModel::RateErrorModel
()
167
{
168
NS_LOG_FUNCTION
(
this
);
169
}
170
171
RateErrorModel::~RateErrorModel
()
172
{
173
NS_LOG_FUNCTION
(
this
);
174
}
175
176
RateErrorModel::ErrorUnit
177
RateErrorModel::GetUnit
(
void
)
const
178
{
179
NS_LOG_FUNCTION
(
this
);
180
return
m_unit
;
181
}
182
183
void
184
RateErrorModel::SetUnit
(
enum
ErrorUnit
error_unit)
185
{
186
NS_LOG_FUNCTION
(
this
<< error_unit);
187
m_unit
= error_unit;
188
}
189
190
double
191
RateErrorModel::GetRate
(
void
)
const
192
{
193
NS_LOG_FUNCTION
(
this
);
194
return
m_rate
;
195
}
196
197
void
198
RateErrorModel::SetRate
(
double
rate)
199
{
200
NS_LOG_FUNCTION
(
this
<< rate);
201
m_rate
= rate;
202
}
203
204
void
205
RateErrorModel::SetRandomVariable
(
Ptr<RandomVariableStream>
ranvar)
206
{
207
NS_LOG_FUNCTION
(
this
<< ranvar);
208
m_ranvar
= ranvar;
209
}
210
211
int64_t
212
RateErrorModel::AssignStreams
(int64_t stream)
213
{
214
NS_LOG_FUNCTION
(
this
<< stream);
215
m_ranvar
->
SetStream
(stream);
216
return
1;
217
}
218
219
bool
220
RateErrorModel::DoCorrupt
(
Ptr<Packet>
p)
221
{
222
NS_LOG_FUNCTION
(
this
<< p);
223
if
(!
IsEnabled
())
224
{
225
return
false
;
226
}
227
switch
(
m_unit
)
228
{
229
case
ERROR_UNIT_PACKET
:
230
return
DoCorruptPkt
(p);
231
case
ERROR_UNIT_BYTE
:
232
return
DoCorruptByte
(p);
233
case
ERROR_UNIT_BIT
:
234
return
DoCorruptBit
(p);
235
default
:
236
NS_ASSERT_MSG
(
false
,
"m_unit not supported yet"
);
237
break
;
238
}
239
return
false
;
240
}
241
242
bool
243
RateErrorModel::DoCorruptPkt
(
Ptr<Packet>
p)
244
{
245
NS_LOG_FUNCTION
(
this
<< p);
246
return
(
m_ranvar
->
GetValue
() <
m_rate
);
247
}
248
249
bool
250
RateErrorModel::DoCorruptByte
(
Ptr<Packet>
p)
251
{
252
NS_LOG_FUNCTION
(
this
<< p);
253
// compute pkt error rate, assume uniformly distributed byte error
254
double
per = 1 - std::pow (1.0 -
m_rate
, static_cast<double> (p->
GetSize
()));
255
return
(
m_ranvar
->
GetValue
() < per);
256
}
257
258
bool
259
RateErrorModel::DoCorruptBit
(
Ptr<Packet>
p)
260
{
261
NS_LOG_FUNCTION
(
this
<< p);
262
// compute pkt error rate, assume uniformly distributed bit error
263
double
per = 1 - std::pow (1.0 -
m_rate
, static_cast<double> (8 * p->
GetSize
()) );
264
return
(
m_ranvar
->
GetValue
() < per);
265
}
266
267
void
268
RateErrorModel::DoReset
(
void
)
269
{
270
NS_LOG_FUNCTION
(
this
);
271
/* re-initialize any state; no-op for now */
272
}
273
274
//
275
// ListErrorModel
276
//
277
278
NS_OBJECT_ENSURE_REGISTERED
(
ListErrorModel
);
279
280
TypeId
ListErrorModel::GetTypeId
(
void
)
281
{
282
static
TypeId
tid =
TypeId
(
"ns3::ListErrorModel"
)
283
.
SetParent
<
ErrorModel
> ()
284
.AddConstructor<ListErrorModel> ()
285
;
286
return
tid;
287
}
288
289
ListErrorModel::ListErrorModel
()
290
{
291
NS_LOG_FUNCTION
(
this
);
292
}
293
294
ListErrorModel::~ListErrorModel
()
295
{
296
NS_LOG_FUNCTION
(
this
);
297
}
298
299
std::list<uint32_t>
300
ListErrorModel::GetList
(
void
)
const
301
{
302
NS_LOG_FUNCTION
(
this
);
303
return
m_packetList
;
304
}
305
306
void
307
ListErrorModel::SetList
(
const
std::list<uint32_t> &packetlist)
308
{
309
NS_LOG_FUNCTION
(
this
<< &packetlist);
310
m_packetList
= packetlist;
311
}
312
313
// When performance becomes a concern, the list provided could be
314
// converted to a dynamically-sized array of uint32_t to avoid
315
// list iteration below.
316
bool
317
ListErrorModel::DoCorrupt
(
Ptr<Packet>
p)
318
{
319
NS_LOG_FUNCTION
(
this
<< p);
320
if
(!
IsEnabled
())
321
{
322
return
false
;
323
}
324
uint32_t uid = p->
GetUid
();
325
for
(
PacketListCI
i =
m_packetList
.begin ();
326
i !=
m_packetList
.end (); i++)
327
{
328
if
(uid == *i)
329
{
330
return
true
;
331
}
332
}
333
return
false
;
334
}
335
336
void
337
ListErrorModel::DoReset
(
void
)
338
{
339
NS_LOG_FUNCTION
(
this
);
340
m_packetList
.clear ();
341
}
342
343
//
344
// ReceiveListErrorModel
345
//
346
347
NS_OBJECT_ENSURE_REGISTERED
(
ReceiveListErrorModel
);
348
349
TypeId
ReceiveListErrorModel::GetTypeId
(
void
)
350
{
351
static
TypeId
tid =
TypeId
(
"ns3::ReceiveListErrorModel"
)
352
.
SetParent
<
ErrorModel
> ()
353
.AddConstructor<ReceiveListErrorModel> ()
354
;
355
return
tid;
356
}
357
358
359
ReceiveListErrorModel::ReceiveListErrorModel
() :
360
m_timesInvoked (0)
361
{
362
NS_LOG_FUNCTION
(
this
);
363
}
364
365
ReceiveListErrorModel::~ReceiveListErrorModel
()
366
{
367
NS_LOG_FUNCTION
(
this
);
368
}
369
370
std::list<uint32_t>
371
ReceiveListErrorModel::GetList
(
void
)
const
372
{
373
NS_LOG_FUNCTION
(
this
);
374
return
m_packetList
;
375
}
376
377
void
378
ReceiveListErrorModel::SetList
(
const
std::list<uint32_t> &packetlist)
379
{
380
NS_LOG_FUNCTION
(
this
<< &packetlist);
381
m_packetList
= packetlist;
382
}
383
384
bool
385
ReceiveListErrorModel::DoCorrupt
(
Ptr<Packet>
p)
386
{
387
NS_LOG_FUNCTION
(
this
<< p);
388
if
(!
IsEnabled
())
389
{
390
return
false
;
391
}
392
m_timesInvoked
+= 1;
393
for
(
PacketListCI
i =
m_packetList
.begin ();
394
i !=
m_packetList
.end (); i++)
395
{
396
if
(
m_timesInvoked
- 1 == *i)
397
{
398
return
true
;
399
}
400
}
401
return
false
;
402
}
403
404
void
405
ReceiveListErrorModel::DoReset
(
void
)
406
{
407
NS_LOG_FUNCTION
(
this
);
408
m_packetList
.clear ();
409
}
410
411
412
}
// namespace ns3
src
network
utils
error-model.cc
Generated on Fri Dec 21 2012 19:00:44 for ns-3 by
1.8.1.2