2007-11-14 20:40:05 -08:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2007 University of Washington
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*
|
|
|
|
|
* Author: Tom Henderson <tomhend@u.washington.edu>
|
|
|
|
|
* This code has been ported from ns-2 (queue/errmodel.{cc,h}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
#include "error-model.h"
|
|
|
|
|
|
|
|
|
|
#include "ns3/packet.h"
|
|
|
|
|
#include "ns3/assert.h"
|
|
|
|
|
#include "ns3/log.h"
|
|
|
|
|
#include "ns3/random-variable.h"
|
2008-02-26 18:33:10 +01:00
|
|
|
#include "ns3/boolean.h"
|
|
|
|
|
#include "ns3/enum.h"
|
|
|
|
|
#include "ns3/double.h"
|
2007-11-14 20:40:05 -08:00
|
|
|
|
|
|
|
|
NS_LOG_COMPONENT_DEFINE ("ErrorModel");
|
|
|
|
|
|
|
|
|
|
namespace ns3 {
|
|
|
|
|
|
2008-01-03 11:39:45 +01:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (ErrorModel);
|
2007-11-14 20:40:05 -08:00
|
|
|
|
2008-01-15 12:43:07 +01:00
|
|
|
TypeId ErrorModel::GetTypeId (void)
|
2008-01-02 10:33:39 +01:00
|
|
|
{
|
2008-01-15 12:44:09 +01:00
|
|
|
static TypeId tid = TypeId ("ErrorModel")
|
2008-02-26 18:33:10 +01:00
|
|
|
.SetParent<Object> ()
|
|
|
|
|
.AddAttribute ("IsEnabled", "Whether this ErrorModel is enabled or not.",
|
|
|
|
|
Boolean (true),
|
|
|
|
|
MakeBooleanAccessor (&ErrorModel::m_enable),
|
|
|
|
|
MakeBooleanChecker ())
|
|
|
|
|
;
|
2008-01-15 12:44:09 +01:00
|
|
|
return tid;
|
2008-01-02 10:33:39 +01:00
|
|
|
}
|
2007-11-14 20:40:05 -08:00
|
|
|
|
|
|
|
|
ErrorModel::ErrorModel () :
|
|
|
|
|
m_enable (true)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorModel::~ErrorModel ()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
ErrorModel::IsCorrupt (Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
bool result;
|
|
|
|
|
// Insert any pre-conditions here
|
|
|
|
|
result = DoCorrupt (p);
|
|
|
|
|
// Insert any post-conditions here
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ErrorModel::Reset (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
DoReset ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ErrorModel::Enable (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_enable = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ErrorModel::Disable (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_enable = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
ErrorModel::IsEnabled (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
return m_enable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// RateErrorModel
|
|
|
|
|
//
|
|
|
|
|
|
2008-01-03 11:39:45 +01:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (RateErrorModel);
|
|
|
|
|
|
2008-01-15 12:43:07 +01:00
|
|
|
TypeId RateErrorModel::GetTypeId (void)
|
2008-01-02 10:33:39 +01:00
|
|
|
{
|
2008-01-15 12:44:09 +01:00
|
|
|
static TypeId tid = TypeId ("RateErrorModel")
|
2008-01-03 11:39:45 +01:00
|
|
|
.SetParent<ErrorModel> ()
|
2008-02-26 18:33:10 +01:00
|
|
|
.AddConstructor<RateErrorModel> ()
|
2008-02-26 23:27:19 +01:00
|
|
|
.AddAttribute ("ErrorUnit", "The error unit",
|
2008-02-26 18:33:10 +01:00
|
|
|
Enum (EU_BYTE),
|
|
|
|
|
MakeEnumAccessor (&RateErrorModel::m_unit),
|
|
|
|
|
MakeEnumChecker (EU_BYTE, "EU_BYTE",
|
|
|
|
|
EU_PKT, "EU_PKT",
|
|
|
|
|
EU_BIT, "EU_BIT"))
|
2008-02-26 23:27:19 +01:00
|
|
|
.AddAttribute ("ErrorRate", "The error rate.",
|
2008-02-26 18:33:10 +01:00
|
|
|
Double (0.0),
|
|
|
|
|
MakeDoubleAccessor (&RateErrorModel::m_rate),
|
|
|
|
|
MakeDoubleChecker<double> ())
|
|
|
|
|
.AddAttribute ("RanVar", "The decision variable attached to this error model.",
|
|
|
|
|
UniformVariable (0.0, 1.0),
|
|
|
|
|
MakeRandomVariableAccessor (&RateErrorModel::m_ranvar),
|
|
|
|
|
MakeRandomVariableChecker ())
|
|
|
|
|
;
|
2008-01-15 12:44:09 +01:00
|
|
|
return tid;
|
2008-01-02 10:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-02-26 18:33:10 +01:00
|
|
|
RateErrorModel::RateErrorModel ()
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RateErrorModel::~RateErrorModel ()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ErrorUnit
|
|
|
|
|
RateErrorModel::GetUnit (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
return m_unit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
RateErrorModel::SetUnit (enum ErrorUnit error_unit)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_unit = error_unit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double
|
|
|
|
|
RateErrorModel::GetRate (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
return m_rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
RateErrorModel::SetRate (double rate)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_rate = rate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
RateErrorModel::SetRandomVariable (const RandomVariable &ranvar)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
2008-02-07 19:57:21 +01:00
|
|
|
m_ranvar = ranvar;
|
2007-11-14 20:40:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
RateErrorModel::DoCorrupt (Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
if (!m_enable)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
switch (m_unit)
|
|
|
|
|
{
|
|
|
|
|
case EU_PKT:
|
|
|
|
|
return DoCorruptPkt (p);
|
|
|
|
|
case EU_BYTE:
|
|
|
|
|
return DoCorruptByte (p);
|
|
|
|
|
case EU_BIT:
|
|
|
|
|
return DoCorruptBit (p);
|
|
|
|
|
default:
|
|
|
|
|
NS_ASSERT_MSG (false, "m_unit not supported yet");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
RateErrorModel::DoCorruptPkt (Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
2008-02-07 19:57:21 +01:00
|
|
|
return (m_ranvar.GetValue () < m_rate);
|
2007-11-14 20:40:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
RateErrorModel::DoCorruptByte (Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
// compute pkt error rate, assume uniformly distributed byte error
|
2007-11-22 10:30:29 +01:00
|
|
|
double per = 1 - pow (1.0 - m_rate, p->GetSize ());
|
2008-02-07 19:57:21 +01:00
|
|
|
return (m_ranvar.GetValue () < per);
|
2007-11-14 20:40:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
RateErrorModel::DoCorruptBit(Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
// compute pkt error rate, assume uniformly distributed bit error
|
2007-11-22 10:30:29 +01:00
|
|
|
double per = 1 - pow (1.0 - m_rate, (8 * p->GetSize ()) );
|
2008-02-07 19:57:21 +01:00
|
|
|
return (m_ranvar.GetValue () < per);
|
2007-11-14 20:40:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
RateErrorModel::DoReset (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
/* re-initialize any state; no-op for now */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// ListErrorModel
|
|
|
|
|
//
|
|
|
|
|
|
2008-01-03 11:39:45 +01:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (ListErrorModel);
|
2008-01-02 10:33:39 +01:00
|
|
|
|
2008-01-15 12:43:07 +01:00
|
|
|
TypeId ListErrorModel::GetTypeId (void)
|
2008-01-02 10:33:39 +01:00
|
|
|
{
|
2008-01-15 12:44:09 +01:00
|
|
|
static TypeId tid = TypeId ("ListErrorModel")
|
2008-01-03 11:39:45 +01:00
|
|
|
.SetParent<ErrorModel> ()
|
2008-02-26 18:33:10 +01:00
|
|
|
.AddConstructor<ListErrorModel> ()
|
|
|
|
|
;
|
2008-01-15 12:44:09 +01:00
|
|
|
return tid;
|
2008-01-02 10:33:39 +01:00
|
|
|
}
|
2007-11-14 20:40:05 -08:00
|
|
|
|
|
|
|
|
ListErrorModel::ListErrorModel ()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ListErrorModel::~ListErrorModel ()
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::list<uint32_t>
|
|
|
|
|
ListErrorModel::GetList (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
return m_packetList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ListErrorModel::SetList (const std::list<uint32_t> &packetlist)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_packetList = packetlist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When performance becomes a concern, the list provided could be
|
|
|
|
|
// converted to a dynamically-sized array of uint32_t to avoid
|
|
|
|
|
// list iteration below.
|
|
|
|
|
bool
|
2007-11-22 10:30:29 +01:00
|
|
|
ListErrorModel::DoCorrupt (Ptr<Packet> p)
|
2007-11-14 20:40:05 -08:00
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
if (!m_enable)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2007-11-22 10:30:29 +01:00
|
|
|
uint32_t uid = p->GetUid ();
|
2007-11-14 20:40:05 -08:00
|
|
|
for (PacketListCI i = m_packetList.begin ();
|
|
|
|
|
i != m_packetList.end (); i++)
|
|
|
|
|
{
|
|
|
|
|
if (uid == *i)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ListErrorModel::DoReset (void)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION;
|
|
|
|
|
m_packetList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace ns3
|