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 <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
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_NOARGS
();
90
}
91
92
ErrorModel::~ErrorModel
()
93
{
94
NS_LOG_FUNCTION_NOARGS
();
95
}
96
97
bool
98
ErrorModel::IsCorrupt
(
Ptr<Packet>
p)
99
{
100
NS_LOG_FUNCTION_NOARGS
();
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_NOARGS
();
112
DoReset
();
113
}
114
115
void
116
ErrorModel::Enable
(
void
)
117
{
118
NS_LOG_FUNCTION_NOARGS
();
119
m_enable
=
true
;
120
}
121
122
void
123
ErrorModel::Disable
(
void
)
124
{
125
NS_LOG_FUNCTION_NOARGS
();
126
m_enable
=
false
;
127
}
128
129
bool
130
ErrorModel::IsEnabled
(
void
)
const
131
{
132
NS_LOG_FUNCTION_NOARGS
();
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_NOARGS
();
169
}
170
171
RateErrorModel::~RateErrorModel
()
172
{
173
NS_LOG_FUNCTION_NOARGS
();
174
}
175
176
RateErrorModel::ErrorUnit
177
RateErrorModel::GetUnit
(
void
)
const
178
{
179
NS_LOG_FUNCTION_NOARGS
();
180
return
m_unit
;
181
}
182
183
void
184
RateErrorModel::SetUnit
(
enum
ErrorUnit
error_unit)
185
{
186
NS_LOG_FUNCTION_NOARGS
();
187
m_unit
= error_unit;
188
}
189
190
double
191
RateErrorModel::GetRate
(
void
)
const
192
{
193
NS_LOG_FUNCTION_NOARGS
();
194
return
m_rate
;
195
}
196
197
void
198
RateErrorModel::SetRate
(
double
rate)
199
{
200
NS_LOG_FUNCTION_NOARGS
();
201
m_rate
= rate;
202
}
203
204
void
205
RateErrorModel::SetRandomVariable
(
Ptr<RandomVariableStream>
ranvar)
206
{
207
NS_LOG_FUNCTION_NOARGS
();
208
m_ranvar
= ranvar;
209
}
210
211
int64_t
212
RateErrorModel::AssignStreams
(int64_t stream)
213
{
214
m_ranvar
->
SetStream
(stream);
215
return
1;
216
}
217
218
bool
219
RateErrorModel::DoCorrupt
(
Ptr<Packet>
p)
220
{
221
NS_LOG_FUNCTION_NOARGS
();
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
242
RateErrorModel::DoCorruptPkt
(
Ptr<Packet>
p)
243
{
244
NS_LOG_FUNCTION_NOARGS
();
245
return
(
m_ranvar
->
GetValue
() <
m_rate
);
246
}
247
248
bool
249
RateErrorModel::DoCorruptByte
(
Ptr<Packet>
p)
250
{
251
NS_LOG_FUNCTION_NOARGS
();
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
258
RateErrorModel::DoCorruptBit
(
Ptr<Packet>
p)
259
{
260
NS_LOG_FUNCTION_NOARGS
();
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
267
RateErrorModel::DoReset
(
void
)
268
{
269
NS_LOG_FUNCTION_NOARGS
();
270
/* re-initialize any state; no-op for now */
271
}
272
273
//
274
// ListErrorModel
275
//
276
277
NS_OBJECT_ENSURE_REGISTERED
(
ListErrorModel
);
278
279
TypeId
ListErrorModel::GetTypeId
(
void
)
280
{
281
static
TypeId
tid =
TypeId
(
"ns3::ListErrorModel"
)
282
.
SetParent
<
ErrorModel
> ()
283
.AddConstructor<ListErrorModel> ()
284
;
285
return
tid;
286
}
287
288
ListErrorModel::ListErrorModel
()
289
{
290
NS_LOG_FUNCTION_NOARGS
();
291
}
292
293
ListErrorModel::~ListErrorModel
()
294
{
295
NS_LOG_FUNCTION_NOARGS
();
296
}
297
298
std::list<uint32_t>
299
ListErrorModel::GetList
(
void
)
const
300
{
301
NS_LOG_FUNCTION_NOARGS
();
302
return
m_packetList
;
303
}
304
305
void
306
ListErrorModel::SetList
(
const
std::list<uint32_t> &packetlist)
307
{
308
NS_LOG_FUNCTION_NOARGS
();
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
316
ListErrorModel::DoCorrupt
(
Ptr<Packet>
p)
317
{
318
NS_LOG_FUNCTION_NOARGS
();
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
336
ListErrorModel::DoReset
(
void
)
337
{
338
NS_LOG_FUNCTION_NOARGS
();
339
m_packetList
.clear ();
340
}
341
342
//
343
// ReceiveListErrorModel
344
//
345
346
NS_OBJECT_ENSURE_REGISTERED
(
ReceiveListErrorModel
);
347
348
TypeId
ReceiveListErrorModel::GetTypeId
(
void
)
349
{
350
static
TypeId
tid =
TypeId
(
"ns3::ReceiveListErrorModel"
)
351
.
SetParent
<
ErrorModel
> ()
352
.AddConstructor<ReceiveListErrorModel> ()
353
;
354
return
tid;
355
}
356
357
358
ReceiveListErrorModel::ReceiveListErrorModel
() :
359
m_timesInvoked (0)
360
{
361
NS_LOG_FUNCTION_NOARGS
();
362
}
363
364
ReceiveListErrorModel::~ReceiveListErrorModel
()
365
{
366
NS_LOG_FUNCTION_NOARGS
();
367
}
368
369
std::list<uint32_t>
370
ReceiveListErrorModel::GetList
(
void
)
const
371
{
372
NS_LOG_FUNCTION_NOARGS
();
373
return
m_packetList
;
374
}
375
376
void
377
ReceiveListErrorModel::SetList
(
const
std::list<uint32_t> &packetlist)
378
{
379
NS_LOG_FUNCTION_NOARGS
();
380
m_packetList
= packetlist;
381
}
382
383
bool
384
ReceiveListErrorModel::DoCorrupt
(
Ptr<Packet>
p)
385
{
386
NS_LOG_FUNCTION_NOARGS
();
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
404
ReceiveListErrorModel::DoReset
(
void
)
405
{
406
NS_LOG_FUNCTION_NOARGS
();
407
m_packetList
.clear ();
408
}
409
410
411
}
// namespace ns3
src
network
utils
error-model.cc
Generated on Tue Oct 9 2012 16:45:44 for ns-3 by
1.8.1.2