Tests: reorganize ErrorChannel and ErrorBinaryModel
This commit is contained in:
151
src/network/utils/error-channel.cc
Normal file
151
src/network/utils/error-channel.cc
Normal file
@@ -0,0 +1,151 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2013 Universita' di Firenze, Italy
|
||||
*
|
||||
* 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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
|
||||
*/
|
||||
|
||||
#include "ns3/simple-net-device.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns3/packet.h"
|
||||
#include "ns3/node.h"
|
||||
#include "ns3/log.h"
|
||||
#include "error-channel.h"
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
NS_LOG_COMPONENT_DEFINE ("ErrorChannel");
|
||||
|
||||
NS_OBJECT_ENSURE_REGISTERED (ErrorChannel);
|
||||
|
||||
TypeId
|
||||
ErrorChannel::GetTypeId (void)
|
||||
{
|
||||
static TypeId tid = TypeId ("ns3::ErrorChannel")
|
||||
.SetParent<SimpleChannel> ()
|
||||
.SetGroupName ("Network")
|
||||
.AddConstructor<ErrorChannel> ()
|
||||
;
|
||||
return tid;
|
||||
}
|
||||
|
||||
ErrorChannel::ErrorChannel ()
|
||||
{
|
||||
m_jumpingTime = Seconds (0.5);
|
||||
m_jumping = false;
|
||||
m_jumpingState = 0;
|
||||
m_duplicateTime = Seconds (0.1);
|
||||
m_duplicate = false;
|
||||
m_duplicateState = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::SetJumpingTime (Time delay)
|
||||
{
|
||||
m_jumpingTime = delay;
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::SetJumpingMode (bool mode)
|
||||
{
|
||||
m_jumping = mode;
|
||||
m_jumpingState = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::SetDuplicateTime (Time delay)
|
||||
{
|
||||
m_duplicateTime = delay;
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::SetDuplicateMode (bool mode)
|
||||
{
|
||||
m_duplicate = mode;
|
||||
m_duplicateState = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::Send (Ptr<Packet> p, uint16_t protocol,
|
||||
Mac48Address to, Mac48Address from,
|
||||
Ptr<SimpleNetDevice> sender)
|
||||
{
|
||||
NS_LOG_FUNCTION (p << protocol << to << from << sender);
|
||||
for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
|
||||
{
|
||||
Ptr<SimpleNetDevice> tmp = *i;
|
||||
if (tmp == sender)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (m_jumping)
|
||||
{
|
||||
if (m_jumpingState % 2)
|
||||
{
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
}
|
||||
else
|
||||
{
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_jumpingTime,
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
}
|
||||
m_jumpingState++;
|
||||
}
|
||||
else if (m_duplicate)
|
||||
{
|
||||
if (m_duplicateState % 2)
|
||||
{
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
}
|
||||
else
|
||||
{
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_duplicateTime,
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
}
|
||||
m_duplicateState++;
|
||||
}
|
||||
else
|
||||
{
|
||||
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
|
||||
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ErrorChannel::Add (Ptr<SimpleNetDevice> device)
|
||||
{
|
||||
m_devices.push_back (device);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
ErrorChannel::GetNDevices (void) const
|
||||
{
|
||||
return m_devices.size ();
|
||||
}
|
||||
|
||||
Ptr<NetDevice>
|
||||
ErrorChannel::GetDevice (uint32_t i) const
|
||||
{
|
||||
return m_devices[i];
|
||||
}
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
95
src/network/utils/error-channel.h
Normal file
95
src/network/utils/error-channel.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
||||
/*
|
||||
* Copyright (c) 2013 Universita' di Firenze, Italy
|
||||
*
|
||||
* 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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
|
||||
*/
|
||||
#ifndef ERROR_CHANNEL_H
|
||||
#define ERROR_CHANNEL_H
|
||||
|
||||
#include "ns3/channel.h"
|
||||
#include "ns3/simple-channel.h"
|
||||
#include "ns3/error-model.h"
|
||||
#include "ns3/mac48-address.h"
|
||||
#include "ns3/nstime.h"
|
||||
#include <vector>
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
class SimpleNetDevice;
|
||||
class Packet;
|
||||
|
||||
/**
|
||||
* \ingroup channel
|
||||
* \brief A Error channel, introducing deterministic delays on even/odd packets. Used for testing
|
||||
*/
|
||||
class ErrorChannel : public SimpleChannel
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* \brief Get the type ID.
|
||||
* \return the object TypeId
|
||||
*/
|
||||
static TypeId GetTypeId (void);
|
||||
ErrorChannel ();
|
||||
|
||||
// inherited from ns3::SimpleChannel
|
||||
virtual void Send (Ptr<Packet> p, uint16_t protocol, Mac48Address to, Mac48Address from,
|
||||
Ptr<SimpleNetDevice> sender);
|
||||
|
||||
virtual void Add (Ptr<SimpleNetDevice> device);
|
||||
|
||||
// inherited from ns3::Channel
|
||||
virtual uint32_t GetNDevices (void) const;
|
||||
virtual Ptr<NetDevice> GetDevice (uint32_t i) const;
|
||||
|
||||
/**
|
||||
* \brief Set the delay for the odd packets (even ones are not delayed)
|
||||
* \param delay Delay for the odd packets.
|
||||
*/
|
||||
void SetJumpingTime (Time delay);
|
||||
|
||||
/**
|
||||
* \brief Set if the odd packets are delayed (even ones are not delayed ever)
|
||||
* \param mode true if the odd packets should be delayed.
|
||||
*/
|
||||
void SetJumpingMode (bool mode);
|
||||
|
||||
/**
|
||||
* \brief Set the delay for the odd duplicate packets (even ones are not duplicated)
|
||||
* \param delay Delay for the odd packets.
|
||||
*/
|
||||
void SetDuplicateTime (Time delay);
|
||||
|
||||
/**
|
||||
* \brief Set if the odd packets are duplicated (even ones are not duplicated ever)
|
||||
* \param mode true if the odd packets should be duplicated.
|
||||
*/
|
||||
void SetDuplicateMode (bool mode);
|
||||
|
||||
private:
|
||||
std::vector<Ptr<SimpleNetDevice> > m_devices; //!< devices connected by the channel
|
||||
Time m_jumpingTime; //!< Delay time in Jumping mode.
|
||||
uint8_t m_jumpingState; //!< Counter for even/odd packets in Jumping mode.
|
||||
bool m_jumping; //!< Flag for Jumping mode.
|
||||
Time m_duplicateTime; //!< Duplicate time in Duplicate mode.
|
||||
bool m_duplicate; //!< Flag for Duplicate mode.
|
||||
uint8_t m_duplicateState; //!< Counter for even/odd packets in Duplicate mode.
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* ERROR_CHANNEL_H */
|
||||
@@ -551,5 +551,50 @@ ReceiveListErrorModel::DoReset (void)
|
||||
}
|
||||
|
||||
|
||||
NS_OBJECT_ENSURE_REGISTERED (BinaryErrorModel);
|
||||
|
||||
TypeId BinaryErrorModel::GetTypeId (void)
|
||||
{
|
||||
static TypeId tid = TypeId ("ns3::BinaryErrorModel")
|
||||
.SetParent<ErrorModel> ()
|
||||
.AddConstructor<BinaryErrorModel> ()
|
||||
;
|
||||
return tid;
|
||||
}
|
||||
|
||||
BinaryErrorModel::BinaryErrorModel ()
|
||||
{
|
||||
NS_LOG_FUNCTION (this);
|
||||
m_counter = 0;
|
||||
}
|
||||
|
||||
BinaryErrorModel::~BinaryErrorModel ()
|
||||
{
|
||||
NS_LOG_FUNCTION (this);
|
||||
}
|
||||
|
||||
bool
|
||||
BinaryErrorModel::DoCorrupt (Ptr<Packet> p)
|
||||
{
|
||||
NS_LOG_FUNCTION (this);
|
||||
if (!IsEnabled ())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool ret = m_counter%2;
|
||||
m_counter++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
BinaryErrorModel::DoReset (void)
|
||||
{
|
||||
NS_LOG_FUNCTION (this);
|
||||
m_counter = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
|
||||
@@ -459,6 +459,24 @@ private:
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief The simplest error model, corrupts even packets and does not corrupt odd ones.
|
||||
*/
|
||||
class BinaryErrorModel : public ErrorModel
|
||||
{
|
||||
public:
|
||||
static TypeId GetTypeId (void);
|
||||
|
||||
BinaryErrorModel ();
|
||||
virtual ~BinaryErrorModel ();
|
||||
|
||||
private:
|
||||
virtual bool DoCorrupt (Ptr<Packet> p);
|
||||
virtual void DoReset (void);
|
||||
|
||||
uint8_t m_counter; //!< internal state counter.
|
||||
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
#endif
|
||||
|
||||
@@ -29,6 +29,7 @@ def build(bld):
|
||||
'utils/data-rate.cc',
|
||||
'utils/drop-tail-queue.cc',
|
||||
'utils/dynamic-queue-limits.cc',
|
||||
'utils/error-channel.cc',
|
||||
'utils/error-model.cc',
|
||||
'utils/ethernet-header.cc',
|
||||
'utils/ethernet-trailer.cc',
|
||||
@@ -112,6 +113,7 @@ def build(bld):
|
||||
'utils/data-rate.h',
|
||||
'utils/drop-tail-queue.h',
|
||||
'utils/dynamic-queue-limits.h',
|
||||
'utils/error-channel.h',
|
||||
'utils/error-model.h',
|
||||
'utils/ethernet-header.h',
|
||||
'utils/ethernet-trailer.h',
|
||||
|
||||
Reference in New Issue
Block a user