A Discrete-Event Network Simulator
API
bulk-send-application-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Tom Henderson (tomh@tomh.org)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 #include "ns3/application-container.h"
20 #include "ns3/boolean.h"
21 #include "ns3/bulk-send-application.h"
22 #include "ns3/bulk-send-helper.h"
23 #include "ns3/inet-socket-address.h"
24 #include "ns3/internet-stack-helper.h"
25 #include "ns3/ipv4-address-helper.h"
26 #include "ns3/ipv4-address.h"
27 #include "ns3/ipv4-interface-container.h"
28 #include "ns3/node-container.h"
29 #include "ns3/node.h"
30 #include "ns3/nstime.h"
31 #include "ns3/packet-sink-helper.h"
32 #include "ns3/packet-sink.h"
33 #include "ns3/simple-net-device-helper.h"
34 #include "ns3/string.h"
35 #include "ns3/test.h"
36 #include "ns3/traced-callback.h"
37 #include "ns3/uinteger.h"
38 
39 using namespace ns3;
40 
48 {
49  public:
51  ~BulkSendBasicTestCase() override;
52 
53  private:
54  void DoRun() override;
59  void SendTx(Ptr<const Packet> p);
65  void ReceiveRx(Ptr<const Packet> p, const Address& addr);
66  uint64_t m_sent{0};
67  uint64_t m_received{0};
68 };
69 
71  : TestCase("Check a basic 300KB transfer")
72 {
73 }
74 
76 {
77 }
78 
79 void
81 {
82  m_sent += p->GetSize();
83 }
84 
85 void
87 {
88  m_received += p->GetSize();
89 }
90 
91 void
93 {
94  Ptr<Node> sender = CreateObject<Node>();
95  Ptr<Node> receiver = CreateObject<Node>();
97  nodes.Add(sender);
98  nodes.Add(receiver);
99  SimpleNetDeviceHelper simpleHelper;
100  simpleHelper.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
101  simpleHelper.SetChannelAttribute("Delay", StringValue("10ms"));
103  devices = simpleHelper.Install(nodes);
105  internet.Install(nodes);
107  ipv4.SetBase("10.1.1.0", "255.255.255.0");
108  Ipv4InterfaceContainer i = ipv4.Assign(devices);
109  uint16_t port = 9;
110  BulkSendHelper sourceHelper("ns3::TcpSocketFactory", InetSocketAddress(i.GetAddress(1), port));
111  sourceHelper.SetAttribute("MaxBytes", UintegerValue(300000));
112  ApplicationContainer sourceApp = sourceHelper.Install(nodes.Get(0));
113  sourceApp.Start(Seconds(0.0));
114  sourceApp.Stop(Seconds(10.0));
115  PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",
116  InetSocketAddress(Ipv4Address::GetAny(), port));
117  ApplicationContainer sinkApp = sinkHelper.Install(nodes.Get(1));
118  sinkApp.Start(Seconds(0.0));
119  sinkApp.Stop(Seconds(10.0));
120 
121  Ptr<BulkSendApplication> source = DynamicCast<BulkSendApplication>(sourceApp.Get(0));
122  Ptr<PacketSink> sink = DynamicCast<PacketSink>(sinkApp.Get(0));
123 
124  source->TraceConnectWithoutContext("Tx", MakeCallback(&BulkSendBasicTestCase::SendTx, this));
126 
127  Simulator::Run();
128  Simulator::Destroy();
129 
130  NS_TEST_ASSERT_MSG_EQ(m_sent, 300000, "Sent the full 300000 bytes");
131  NS_TEST_ASSERT_MSG_EQ(m_received, 300000, "Received the full 300000 bytes");
132 }
133 
143 {
144  public:
146  ~BulkSendSeqTsSizeTestCase() override;
147 
148  private:
149  void DoRun() override;
157  void SendTx(Ptr<const Packet> p,
158  const Address& from,
159  const Address& to,
160  const SeqTsSizeHeader& header);
169  const Address& from,
170  const Address& to,
171  const SeqTsSizeHeader& header);
172  uint64_t m_sent{0};
173  uint64_t m_received{0};
174  uint64_t m_seqTxCounter{0};
175  uint64_t m_seqRxCounter{0};
178 };
179 
181  : TestCase("Check a 300KB transfer with SeqTsSize header enabled")
182 {
183 }
184 
186 {
187 }
188 
189 void
191  const Address& from,
192  const Address& to,
193  const SeqTsSizeHeader& header)
194 {
195  // The header is not serialized onto the packet in this trace
196  m_sent += p->GetSize() + header.GetSerializedSize();
197  NS_TEST_ASSERT_MSG_EQ(header.GetSeq(), m_seqTxCounter, "Missing sequence number");
198  m_seqTxCounter++;
199  NS_TEST_ASSERT_MSG_GT_OR_EQ(header.GetTs(), m_lastTxTs, "Timestamp less than last time");
200  m_lastTxTs = header.GetTs();
201 }
202 
203 void
205  const Address& from,
206  const Address& to,
207  const SeqTsSizeHeader& header)
208 {
209  // The header is not serialized onto the packet in this trace
210  m_received += p->GetSize() + header.GetSerializedSize();
211  NS_TEST_ASSERT_MSG_EQ(header.GetSeq(), m_seqRxCounter, "Missing sequence number");
212  m_seqRxCounter++;
213  NS_TEST_ASSERT_MSG_GT_OR_EQ(header.GetTs(), m_lastRxTs, "Timestamp less than last time");
214  m_lastRxTs = header.GetTs();
215 }
216 
217 void
219 {
220  Ptr<Node> sender = CreateObject<Node>();
221  Ptr<Node> receiver = CreateObject<Node>();
223  nodes.Add(sender);
224  nodes.Add(receiver);
225  SimpleNetDeviceHelper simpleHelper;
226  simpleHelper.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
227  simpleHelper.SetChannelAttribute("Delay", StringValue("10ms"));
229  devices = simpleHelper.Install(nodes);
231  internet.Install(nodes);
233  ipv4.SetBase("10.1.1.0", "255.255.255.0");
234  Ipv4InterfaceContainer i = ipv4.Assign(devices);
235  uint16_t port = 9;
236  BulkSendHelper sourceHelper("ns3::TcpSocketFactory", InetSocketAddress(i.GetAddress(1), port));
237  sourceHelper.SetAttribute("MaxBytes", UintegerValue(300000));
238  sourceHelper.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
239  ApplicationContainer sourceApp = sourceHelper.Install(nodes.Get(0));
240  sourceApp.Start(Seconds(0.0));
241  sourceApp.Stop(Seconds(10.0));
242  PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",
243  InetSocketAddress(Ipv4Address::GetAny(), port));
244  sinkHelper.SetAttribute("EnableSeqTsSizeHeader", BooleanValue(true));
245  ApplicationContainer sinkApp = sinkHelper.Install(nodes.Get(1));
246  sinkApp.Start(Seconds(0.0));
247  sinkApp.Stop(Seconds(10.0));
248 
249  Ptr<BulkSendApplication> source = DynamicCast<BulkSendApplication>(sourceApp.Get(0));
250  Ptr<PacketSink> sink = DynamicCast<PacketSink>(sinkApp.Get(0));
251 
252  source->TraceConnectWithoutContext("TxWithSeqTsSize",
254  sink->TraceConnectWithoutContext("RxWithSeqTsSize",
256 
257  Simulator::Run();
258  Simulator::Destroy();
259 
260  NS_TEST_ASSERT_MSG_EQ(m_sent, 300000, "Sent the full 300000 bytes");
261  NS_TEST_ASSERT_MSG_EQ(m_received, 300000, "Received the full 300000 bytes");
262 }
263 
271 {
272  public:
274 };
275 
277  : TestSuite("bulk-send-application", UNIT)
278 {
279  AddTestCase(new BulkSendBasicTestCase, TestCase::QUICK);
280  AddTestCase(new BulkSendSeqTsSizeTestCase, TestCase::QUICK);
281 }
282 
static BulkSendTestSuite g_bulkSendTestSuite
Static variable for test initialization.
Basic test, checks that the right quantity of packets are sent and received.
void DoRun() override
Implementation to actually run this TestCase.
uint64_t m_sent
number of bytes sent
void ReceiveRx(Ptr< const Packet > p, const Address &addr)
Record a packet successfully received.
uint64_t m_received
number of bytes received
void SendTx(Ptr< const Packet > p)
Record a packet successfully sent.
This test checks that the sequence number is sent and received in sequence despite the sending applic...
Time m_lastTxTs
Last recorded timestamp on Tx.
void ReceiveRx(Ptr< const Packet > p, const Address &from, const Address &to, const SeqTsSizeHeader &header)
Record a packet successfully received.
Time m_lastRxTs
Last recorded timestamp on Rx.
uint64_t m_seqRxCounter
Counter for Sequences on Rx.
uint64_t m_seqTxCounter
Counter for Sequences on Tx.
void SendTx(Ptr< const Packet > p, const Address &from, const Address &to, const SeqTsSizeHeader &header)
Record a packet successfully sent.
void DoRun() override
Implementation to actually run this TestCase.
uint64_t m_received
number of bytes received
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes, not the socket attributes.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::BulkSendApplication on each node of the input container configured with all the attri...
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
Time GetTs() const
uint32_t GetSeq() const
Header with a sequence, a timestamp, and a "size" attribute.
uint32_t GetSerializedSize() const override
build a set of SimpleNetDevice objects
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Hold variables of type string.
Definition: string.h:56
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:144
#define NS_TEST_ASSERT_MSG_GT_OR_EQ(actual, limit, msg)
Test that an actual value is greater than or equal to a limit and report and abort if not.
Definition: test.h:915
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55