Tests: reorganize ErrorChannel and ErrorBinaryModel

This commit is contained in:
Tommaso Pecorella
2017-01-29 03:15:41 +01:00
parent 8a8629afe2
commit 4e9537008a
12 changed files with 107 additions and 345 deletions

View File

@@ -1,153 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2011 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 "error-channel.h"
#include "ns3/simple-net-device.h"
#include "ns3/simulator.h"
#include "ns3/packet.h"
#include "ns3/node.h"
#include "ns3/log.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("ErrorChannel");
NS_OBJECT_ENSURE_REGISTERED (ErrorChannel);
TypeId
ErrorChannel::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::ErrorChannel")
.SetParent<Channel> ()
.AddConstructor<ErrorChannel> ()
;
return tid;
}
ErrorChannel::ErrorChannel ()
{
jumping = false;
jumpingState = 0;
}
void
ErrorChannel::SetJumpingTime(Time delay)
{
jumpingTime = delay;
}
void
ErrorChannel::SetJumpingMode(bool mode)
{
jumping = mode;
jumpingState = 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( !jumping || jumpingState%2 )
{
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), Seconds (0),
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
jumpingState++;
}
else
{
Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), jumpingTime,
&SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
jumpingState++;
}
}
}
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];
}
NS_OBJECT_ENSURE_REGISTERED (BinaryErrorModel);
TypeId BinaryErrorModel::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::BinaryErrorModel")
.SetParent<ErrorModel> ()
.AddConstructor<BinaryErrorModel> ()
;
return tid;
}
BinaryErrorModel::BinaryErrorModel ()
{
counter = 0;
}
BinaryErrorModel::~BinaryErrorModel ()
{
}
bool
BinaryErrorModel::DoCorrupt (Ptr<Packet> p)
{
if (!IsEnabled ())
{
return false;
}
bool ret = counter%2;
counter++;
return ret;
}
void
BinaryErrorModel::Reset (void)
{
DoReset();
}
void
BinaryErrorModel::DoReset (void)
{
counter = 0;
}
} // namespace ns3

View File

@@ -1,92 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2011 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/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:
static TypeId GetTypeId (void);
ErrorChannel ();
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);
private:
std::vector<Ptr<SimpleNetDevice> > m_devices;
Time jumpingTime;
uint8_t jumpingState;
bool jumping;
};
class BinaryErrorModel : public ErrorModel
{
public:
static TypeId GetTypeId (void);
BinaryErrorModel ();
virtual ~BinaryErrorModel ();
void Reset (void);
private:
virtual bool DoCorrupt (Ptr<Packet> p);
virtual void DoReset (void);
uint8_t counter;
};
} // namespace ns3
#endif /* ERROR_CHANNEL_H */

View File

@@ -28,7 +28,6 @@
#include "ns3/ipv4-raw-socket-factory.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/simulator.h"
#include "error-channel.h"
#include "ns3/simple-net-device.h"
#include "ns3/simple-net-device-helper.h"
#include "ns3/socket.h"
@@ -46,6 +45,7 @@
#include "ns3/ipv4-static-routing.h"
#include "ns3/udp-l4-protocol.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/error-channel.h"
#include <string>
#include <limits>

View File

@@ -31,7 +31,6 @@
#include "ns3/ipv6-raw-socket-factory.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/simulator.h"
#include "error-channel.h"
#include "ns3/simple-net-device.h"
#include "ns3/socket.h"
#include "ns3/udp-socket.h"
@@ -56,6 +55,7 @@
#include "ns3/icmpv6-l4-protocol.h"
#include "ns3/traffic-control-layer.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/error-channel.h"
#include <string>
#include <limits>

View File

@@ -232,7 +232,6 @@ def build(bld):
'test/ipv4-header-test.cc',
'test/ipv4-fragmentation-test.cc',
'test/ipv4-forwarding-test.cc',
'test/error-channel.cc',
'test/ipv4-test.cc',
'test/ipv4-static-routing-test-suite.cc',
'test/ipv4-global-routing-test-suite.cc',

View File

@@ -17,68 +17,69 @@
*
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
*/
#include "error-channel-sixlow.h"
#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 ("ErrorChannelSixlow");
NS_LOG_COMPONENT_DEFINE ("ErrorChannel");
NS_OBJECT_ENSURE_REGISTERED (ErrorChannelSixlow);
NS_OBJECT_ENSURE_REGISTERED (ErrorChannel);
TypeId
ErrorChannelSixlow::GetTypeId (void)
ErrorChannel::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::ErrorChannelSixlow")
static TypeId tid = TypeId ("ns3::ErrorChannel")
.SetParent<SimpleChannel> ()
.SetGroupName ("SixLowPan")
.AddConstructor<ErrorChannelSixlow> ()
.SetGroupName ("Network")
.AddConstructor<ErrorChannel> ()
;
return tid;
}
ErrorChannelSixlow::ErrorChannelSixlow ()
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
ErrorChannelSixlow::SetJumpingTime (Time delay)
ErrorChannel::SetJumpingTime (Time delay)
{
m_jumpingTime = delay;
}
void
ErrorChannelSixlow::SetJumpingMode (bool mode)
ErrorChannel::SetJumpingMode (bool mode)
{
m_jumping = mode;
m_jumpingState = 0;
}
void
ErrorChannelSixlow::SetDuplicateTime (Time delay)
ErrorChannel::SetDuplicateTime (Time delay)
{
m_duplicateTime = delay;
}
void
ErrorChannelSixlow::SetDuplicateMode (bool mode)
ErrorChannel::SetDuplicateMode (bool mode)
{
m_duplicate = mode;
m_duplicateState = 0;
}
void
ErrorChannelSixlow::Send (Ptr<Packet> p, uint16_t protocol,
ErrorChannel::Send (Ptr<Packet> p, uint16_t protocol,
Mac48Address to, Mac48Address from,
Ptr<SimpleNetDevice> sender)
{
@@ -129,67 +130,22 @@ ErrorChannelSixlow::Send (Ptr<Packet> p, uint16_t protocol,
}
void
ErrorChannelSixlow::Add (Ptr<SimpleNetDevice> device)
ErrorChannel::Add (Ptr<SimpleNetDevice> device)
{
m_devices.push_back (device);
}
uint32_t
ErrorChannelSixlow::GetNDevices (void) const
ErrorChannel::GetNDevices (void) const
{
return m_devices.size ();
}
Ptr<NetDevice>
ErrorChannelSixlow::GetDevice (uint32_t i) const
ErrorChannel::GetDevice (uint32_t i) const
{
return m_devices[i];
}
NS_OBJECT_ENSURE_REGISTERED (BinaryErrorSixlowModel);
TypeId BinaryErrorSixlowModel::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::BinaryErrorSixlowModel")
.SetParent<ErrorModel> ()
.SetGroupName ("SixLowPan")
.AddConstructor<BinaryErrorSixlowModel> ()
;
return tid;
}
BinaryErrorSixlowModel::BinaryErrorSixlowModel ()
{
m_counter = 0;
}
BinaryErrorSixlowModel::~BinaryErrorSixlowModel ()
{
}
bool
BinaryErrorSixlowModel::DoCorrupt (Ptr<Packet> p)
{
if (!IsEnabled ())
{
return false;
}
bool ret = m_counter % 2;
m_counter++;
return ret;
}
void
BinaryErrorSixlowModel::Reset (void)
{
DoReset ();
}
void
BinaryErrorSixlowModel::DoReset (void)
{
m_counter = 0;
}
} // namespace ns3

View File

@@ -17,8 +17,8 @@
*
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
*/
#ifndef ERROR_CHANNEL_SIXLOW_H
#define ERROR_CHANNEL_SIXLOW_H
#ifndef ERROR_CHANNEL_H
#define ERROR_CHANNEL_H
#include "ns3/channel.h"
#include "ns3/simple-channel.h"
@@ -36,12 +36,17 @@ class Packet;
* \ingroup channel
* \brief A Error channel, introducing deterministic delays on even/odd packets. Used for testing
*/
class ErrorChannelSixlow : public SimpleChannel
class ErrorChannel : public SimpleChannel
{
public:
/**
* \brief Get the type ID.
* \return the object TypeId
*/
static TypeId GetTypeId (void);
ErrorChannelSixlow ();
ErrorChannel ();
// inherited from ns3::SimpleChannel
virtual void Send (Ptr<Packet> p, uint16_t protocol, Mac48Address to, Mac48Address from,
Ptr<SimpleNetDevice> sender);
@@ -76,32 +81,15 @@ public:
void SetDuplicateMode (bool mode);
private:
std::vector<Ptr<SimpleNetDevice> > m_devices;
Time m_jumpingTime;
uint8_t m_jumpingState;
bool m_jumping;
Time m_duplicateTime;
bool m_duplicate;
uint8_t m_duplicateState;
};
class BinaryErrorSixlowModel : public ErrorModel
{
public:
static TypeId GetTypeId (void);
BinaryErrorSixlowModel ();
virtual ~BinaryErrorSixlowModel ();
void Reset (void);
private:
virtual bool DoCorrupt (Ptr<Packet> p);
virtual void DoReset (void);
uint8_t m_counter;
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_SIXLOW_H */
#endif /* ERROR_CHANNEL_H */

View File

@@ -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

View File

@@ -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

View File

@@ -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',

View File

@@ -24,7 +24,6 @@
#include "ns3/ipv6-raw-socket-factory.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/simulator.h"
#include "error-channel-sixlow.h"
#include "ns3/simple-net-device.h"
#include "ns3/socket.h"
#include "ns3/udp-socket.h"
@@ -37,6 +36,7 @@
#include "ns3/sixlowpan-net-device.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/icmpv6-l4-protocol.h"
#include "ns3/error-channel.h"
#include <string>
#include <limits>
@@ -271,7 +271,7 @@ SixlowpanFragmentationTest::DoRun (void)
Ptr<Node> serverNode = CreateObject<Node> ();
internet.Install (serverNode);
Ptr<SimpleNetDevice> serverDev;
Ptr<BinaryErrorSixlowModel> serverDevErrorModel = CreateObject<BinaryErrorSixlowModel> ();
Ptr<BinaryErrorModel> serverDevErrorModel = CreateObject<BinaryErrorModel> ();
{
Ptr<Icmpv6L4Protocol> icmpv6l4 = serverNode->GetObject<Icmpv6L4Protocol> ();
icmpv6l4->SetAttribute ("DAD", BooleanValue (false));
@@ -302,7 +302,7 @@ SixlowpanFragmentationTest::DoRun (void)
Ptr<Node> clientNode = CreateObject<Node> ();
internet.Install (clientNode);
Ptr<SimpleNetDevice> clientDev;
Ptr<BinaryErrorSixlowModel> clientDevErrorModel = CreateObject<BinaryErrorSixlowModel> ();
Ptr<BinaryErrorModel> clientDevErrorModel = CreateObject<BinaryErrorModel> ();
{
Ptr<Icmpv6L4Protocol> icmpv6l4 = clientNode->GetObject<Icmpv6L4Protocol> ();
icmpv6l4->SetAttribute ("DAD", BooleanValue (false));
@@ -329,7 +329,7 @@ SixlowpanFragmentationTest::DoRun (void)
StartClient (clientNode);
// link the two nodes
Ptr<ErrorChannelSixlow> channel = CreateObject<ErrorChannelSixlow> ();
Ptr<ErrorChannel> channel = CreateObject<ErrorChannel> ();
serverDev->SetChannel (channel);
clientDev->SetChannel (channel);

View File

@@ -13,7 +13,6 @@ def build(bld):
module_test.source = [
'test/sixlowpan-hc1-test.cc',
'test/sixlowpan-iphc-test.cc',
'test/error-channel-sixlow.cc',
'test/sixlowpan-fragmentation-test.cc',
]