1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
4
#include "ns3/simple-net-device.h"
5
#include "ns3/simple-channel.h"
6
#include "ns3/address.h"
7
#include "ns3/mac48-address.h"
8
#include "ns3/packet.h"
9
#include "ns3/callback.h"
11
#include "ns3/simulator.h"
12
#include "ns3/error-model.h"
13
#include "ns3/pointer.h"
14
#include "ns3/double.h"
15
#include "ns3/string.h"
19
static void SendPacket (int num, Ptr<NetDevice> device, Address& addr)
21
for (int i = 0; i < num; i++)
23
Ptr<Packet> pkt = Create<Packet> (1000); // 1000 dummy bytes of data
24
device->Send (pkt, addr, 0);
28
// Two nodes, two devices, one channel
29
static void BuildSimpleTopology (Ptr<Node> a, Ptr<Node> b, Ptr<SimpleNetDevice> input, Ptr<SimpleNetDevice> output, Ptr<SimpleChannel> channel)
32
b->AddDevice (output);
33
input->SetAddress (Mac48Address::Allocate ());
34
input->SetChannel (channel);
36
output->SetChannel (channel);
38
output->SetAddress (Mac48Address::Allocate ());
41
class ErrorModelSimple : public TestCase
45
virtual ~ErrorModelSimple ();
48
virtual void DoRun (void);
49
bool Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr);
50
void DropEvent (Ptr<const Packet> p);
55
// Add some help text to this case to describe what it is intended to test
56
ErrorModelSimple::ErrorModelSimple ()
57
: TestCase ("ErrorModel and PhyRxDrop trace for SimpleNetDevice"), m_count (0), m_drops (0)
61
ErrorModelSimple::~ErrorModelSimple ()
66
ErrorModelSimple::Receive (Ptr<NetDevice> nd, Ptr<const Packet> p, uint16_t protocol, const Address& addr)
73
ErrorModelSimple::DropEvent (Ptr<const Packet> p)
79
ErrorModelSimple::DoRun (void)
81
// Set some arbitrary deterministic values
82
SeedManager::SetSeed (7);
83
SeedManager::SetRun (5);
85
Ptr<Node> a = CreateObject<Node> ();
86
Ptr<Node> b = CreateObject<Node> ();
88
Ptr<SimpleNetDevice> input = CreateObject<SimpleNetDevice> ();
89
Ptr<SimpleNetDevice> output = CreateObject<SimpleNetDevice> ();
90
Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
91
BuildSimpleTopology (a, b, input, output, channel);
93
output->SetReceiveCallback (MakeCallback (&ErrorModelSimple::Receive, this));
95
Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> ("RanVar", RandomVariableValue (UniformVariable (0.0, 1.0)));
96
em->SetAttribute ("ErrorRate", DoubleValue (0.001));
97
em->SetAttribute ("ErrorUnit", StringValue ("EU_PKT"));
99
// The below hooks will cause drops and receptions to be counted
100
output->SetAttribute ("ReceiveErrorModel", PointerValue (em));
101
output->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&ErrorModelSimple::DropEvent, this));
103
// Send 10000 packets
104
Simulator::Schedule (Seconds (0), &SendPacket, 10000, input, output->GetAddress ());
107
Simulator::Destroy ();
109
// For this combination of values, we expect about 1 packet in 1000 to be
110
// dropped. For this specific RNG stream, we see 9992 receptions and 8 drops
111
NS_TEST_ASSERT_MSG_EQ (m_count, 9992, "Wrong number of receptions.");
112
NS_TEST_ASSERT_MSG_EQ (m_drops, 8, "Wrong number of drops.");
115
// This is the start of an error model test suite. For starters, this is
116
// just testing that the SimpleNetDevice is working but this can be
117
// extended to many more test cases in the future
118
class ErrorModelTestSuite : public TestSuite
121
ErrorModelTestSuite ();
124
ErrorModelTestSuite::ErrorModelTestSuite ()
125
: TestSuite ("error-model", UNIT)
127
AddTestCase (new ErrorModelSimple);
130
// Do not forget to allocate an instance of this TestSuite
131
static ErrorModelTestSuite errorModelTestSuite;