From 4e9537008adbcbc250a5d91df2af7c41058b82dc Mon Sep 17 00:00:00 2001 From: Tommaso Pecorella Date: Sun, 29 Jan 2017 03:15:41 +0100 Subject: [PATCH] Tests: reorganize ErrorChannel and ErrorBinaryModel --- src/internet/test/error-channel.cc | 153 ------------------ src/internet/test/error-channel.h | 92 ----------- src/internet/test/ipv4-fragmentation-test.cc | 2 +- src/internet/test/ipv6-fragmentation-test.cc | 2 +- src/internet/wscript | 1 - .../utils/error-channel.cc} | 82 +++------- .../utils/error-channel.h} | 46 ++---- src/network/utils/error-model.cc | 45 ++++++ src/network/utils/error-model.h | 18 +++ src/network/wscript | 2 + .../test/sixlowpan-fragmentation-test.cc | 8 +- src/sixlowpan/wscript | 1 - 12 files changed, 107 insertions(+), 345 deletions(-) delete mode 100644 src/internet/test/error-channel.cc delete mode 100644 src/internet/test/error-channel.h rename src/{sixlowpan/test/error-channel-sixlow.cc => network/utils/error-channel.cc} (70%) rename src/{sixlowpan/test/error-channel-sixlow.h => network/utils/error-channel.h} (75%) diff --git a/src/internet/test/error-channel.cc b/src/internet/test/error-channel.cc deleted file mode 100644 index b3c74737a..000000000 --- a/src/internet/test/error-channel.cc +++ /dev/null @@ -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 - */ -#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 () - .AddConstructor () - ; - 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 p, uint16_t protocol, - Mac48Address to, Mac48Address from, - Ptr sender) -{ - NS_LOG_FUNCTION (p << protocol << to << from << sender); - for (std::vector >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i) - { - Ptr 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 device) -{ - m_devices.push_back (device); -} - -uint32_t -ErrorChannel::GetNDevices (void) const -{ - return m_devices.size (); -} -Ptr -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 () - .AddConstructor () - ; - return tid; -} - -BinaryErrorModel::BinaryErrorModel () -{ - counter = 0; -} - -BinaryErrorModel::~BinaryErrorModel () -{ -} - - -bool -BinaryErrorModel::DoCorrupt (Ptr 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 diff --git a/src/internet/test/error-channel.h b/src/internet/test/error-channel.h deleted file mode 100644 index 92c39685d..000000000 --- a/src/internet/test/error-channel.h +++ /dev/null @@ -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 - */ -#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 - -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 p, uint16_t protocol, Mac48Address to, Mac48Address from, - Ptr sender); - - virtual void Add (Ptr device); - - // inherited from ns3::Channel - virtual uint32_t GetNDevices (void) const; - virtual Ptr 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 > 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 p); - virtual void DoReset (void); - - uint8_t counter; - -}; - -} // namespace ns3 - -#endif /* ERROR_CHANNEL_H */ diff --git a/src/internet/test/ipv4-fragmentation-test.cc b/src/internet/test/ipv4-fragmentation-test.cc index e8ac4e1a8..db4b2b5ea 100644 --- a/src/internet/test/ipv4-fragmentation-test.cc +++ b/src/internet/test/ipv4-fragmentation-test.cc @@ -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 #include diff --git a/src/internet/test/ipv6-fragmentation-test.cc b/src/internet/test/ipv6-fragmentation-test.cc index 23407c337..4083af50c 100644 --- a/src/internet/test/ipv6-fragmentation-test.cc +++ b/src/internet/test/ipv6-fragmentation-test.cc @@ -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 #include diff --git a/src/internet/wscript b/src/internet/wscript index 2b25f6d05..1d3ad2a4c 100644 --- a/src/internet/wscript +++ b/src/internet/wscript @@ -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', diff --git a/src/sixlowpan/test/error-channel-sixlow.cc b/src/network/utils/error-channel.cc similarity index 70% rename from src/sixlowpan/test/error-channel-sixlow.cc rename to src/network/utils/error-channel.cc index e647ec37b..12b9f68a3 100644 --- a/src/sixlowpan/test/error-channel-sixlow.cc +++ b/src/network/utils/error-channel.cc @@ -17,68 +17,69 @@ * * Author: Tommaso Pecorella */ -#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 () - .SetGroupName ("SixLowPan") - .AddConstructor () + .SetGroupName ("Network") + .AddConstructor () ; 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 p, uint16_t protocol, +ErrorChannel::Send (Ptr p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr sender) { @@ -129,67 +130,22 @@ ErrorChannelSixlow::Send (Ptr p, uint16_t protocol, } void -ErrorChannelSixlow::Add (Ptr device) +ErrorChannel::Add (Ptr device) { m_devices.push_back (device); } uint32_t -ErrorChannelSixlow::GetNDevices (void) const +ErrorChannel::GetNDevices (void) const { return m_devices.size (); } + Ptr -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 () - .SetGroupName ("SixLowPan") - .AddConstructor () - ; - return tid; -} - -BinaryErrorSixlowModel::BinaryErrorSixlowModel () -{ - m_counter = 0; -} - -BinaryErrorSixlowModel::~BinaryErrorSixlowModel () -{ -} - - -bool -BinaryErrorSixlowModel::DoCorrupt (Ptr 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 diff --git a/src/sixlowpan/test/error-channel-sixlow.h b/src/network/utils/error-channel.h similarity index 75% rename from src/sixlowpan/test/error-channel-sixlow.h rename to src/network/utils/error-channel.h index 2a5f13ca0..22466b60a 100644 --- a/src/sixlowpan/test/error-channel-sixlow.h +++ b/src/network/utils/error-channel.h @@ -17,8 +17,8 @@ * * Author: Tommaso Pecorella */ -#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 p, uint16_t protocol, Mac48Address to, Mac48Address from, Ptr sender); @@ -76,32 +81,15 @@ public: void SetDuplicateMode (bool mode); private: - std::vector > 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 p); - virtual void DoReset (void); - - uint8_t m_counter; - + std::vector > 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 */ diff --git a/src/network/utils/error-model.cc b/src/network/utils/error-model.cc index 30190aca1..d7ab10ec3 100644 --- a/src/network/utils/error-model.cc +++ b/src/network/utils/error-model.cc @@ -551,5 +551,50 @@ ReceiveListErrorModel::DoReset (void) } +NS_OBJECT_ENSURE_REGISTERED (BinaryErrorModel); + +TypeId BinaryErrorModel::GetTypeId (void) +{ + static TypeId tid = TypeId ("ns3::BinaryErrorModel") + .SetParent () + .AddConstructor () + ; + return tid; +} + +BinaryErrorModel::BinaryErrorModel () +{ + NS_LOG_FUNCTION (this); + m_counter = 0; +} + +BinaryErrorModel::~BinaryErrorModel () +{ + NS_LOG_FUNCTION (this); +} + +bool +BinaryErrorModel::DoCorrupt (Ptr 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 diff --git a/src/network/utils/error-model.h b/src/network/utils/error-model.h index 5a8a3ae7d..fb4b21d71 100644 --- a/src/network/utils/error-model.h +++ b/src/network/utils/error-model.h @@ -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 p); + virtual void DoReset (void); + + uint8_t m_counter; //!< internal state counter. + +}; } // namespace ns3 #endif diff --git a/src/network/wscript b/src/network/wscript index b9c40198d..c7776959b 100644 --- a/src/network/wscript +++ b/src/network/wscript @@ -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', diff --git a/src/sixlowpan/test/sixlowpan-fragmentation-test.cc b/src/sixlowpan/test/sixlowpan-fragmentation-test.cc index bc2cd5344..766a3b166 100644 --- a/src/sixlowpan/test/sixlowpan-fragmentation-test.cc +++ b/src/sixlowpan/test/sixlowpan-fragmentation-test.cc @@ -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 #include @@ -271,7 +271,7 @@ SixlowpanFragmentationTest::DoRun (void) Ptr serverNode = CreateObject (); internet.Install (serverNode); Ptr serverDev; - Ptr serverDevErrorModel = CreateObject (); + Ptr serverDevErrorModel = CreateObject (); { Ptr icmpv6l4 = serverNode->GetObject (); icmpv6l4->SetAttribute ("DAD", BooleanValue (false)); @@ -302,7 +302,7 @@ SixlowpanFragmentationTest::DoRun (void) Ptr clientNode = CreateObject (); internet.Install (clientNode); Ptr clientDev; - Ptr clientDevErrorModel = CreateObject (); + Ptr clientDevErrorModel = CreateObject (); { Ptr icmpv6l4 = clientNode->GetObject (); icmpv6l4->SetAttribute ("DAD", BooleanValue (false)); @@ -329,7 +329,7 @@ SixlowpanFragmentationTest::DoRun (void) StartClient (clientNode); // link the two nodes - Ptr channel = CreateObject (); + Ptr channel = CreateObject (); serverDev->SetChannel (channel); clientDev->SetChannel (channel); diff --git a/src/sixlowpan/wscript b/src/sixlowpan/wscript index 30a97fb21..b1d4b706d 100644 --- a/src/sixlowpan/wscript +++ b/src/sixlowpan/wscript @@ -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', ]