A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixth.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "tutorial-app.h"
6
7#include "ns3/applications-module.h"
8#include "ns3/core-module.h"
9#include "ns3/internet-module.h"
10#include "ns3/network-module.h"
11#include "ns3/point-to-point-module.h"
12
13#include <fstream>
14
15using namespace ns3;
16
17NS_LOG_COMPONENT_DEFINE("SixthScriptExample");
18
19// ===========================================================================
20//
21// node 0 node 1
22// +----------------+ +----------------+
23// | ns-3 TCP | | ns-3 TCP |
24// +----------------+ +----------------+
25// | 10.1.1.1 | | 10.1.1.2 |
26// +----------------+ +----------------+
27// | point-to-point | | point-to-point |
28// +----------------+ +----------------+
29// | |
30// +---------------------+
31// 5 Mbps, 2 ms
32//
33//
34// We want to look at changes in the ns-3 TCP congestion window. We need
35// to crank up a flow and hook the CongestionWindow attribute on the socket
36// of the sender. Normally one would use an on-off application to generate a
37// flow, but this has a couple of problems. First, the socket of the on-off
38// application is not created until Application Start time, so we wouldn't be
39// able to hook the socket (now) at configuration time. Second, even if we
40// could arrange a call after start time, the socket is not public so we
41// couldn't get at it.
42//
43// So, we can cook up a simple version of the on-off application that does what
44// we want. On the plus side we don't need all of the complexity of the on-off
45// application. On the minus side, we don't have a helper, so we have to get
46// a little more involved in the details, but this is trivial.
47//
48// So first, we create a socket and do the trace connect on it; then we pass
49// this socket into the constructor of our simple application which we then
50// install in the source node.
51// ===========================================================================
52//
53
54/**
55 * Congestion window change callback
56 *
57 * \param stream The output stream file.
58 * \param oldCwnd Old congestion window.
59 * \param newCwnd New congestion window.
60 */
61static void
63{
64 NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "\t" << newCwnd);
65 *stream->GetStream() << Simulator::Now().GetSeconds() << "\t" << oldCwnd << "\t" << newCwnd
66 << std::endl;
67}
68
69/**
70 * Rx drop callback
71 *
72 * \param file The output PCAP file.
73 * \param p The dropped packet.
74 */
75static void
77{
78 NS_LOG_UNCOND("RxDrop at " << Simulator::Now().GetSeconds());
79 file->Write(Simulator::Now(), p);
80}
81
82int
83main(int argc, char* argv[])
84{
85 CommandLine cmd(__FILE__);
86 cmd.Parse(argc, argv);
87
89 nodes.Create(2);
90
92 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
93 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
94
96 devices = pointToPoint.Install(nodes);
97
99 em->SetAttribute("ErrorRate", DoubleValue(0.00001));
100 devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
101
103 stack.Install(nodes);
104
106 address.SetBase("10.1.1.0", "255.255.255.252");
107 Ipv4InterfaceContainer interfaces = address.Assign(devices);
108
109 uint16_t sinkPort = 8080;
110 Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), sinkPort));
111 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory",
113 ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));
114 sinkApps.Start(Seconds(0.));
115 sinkApps.Stop(Seconds(20.));
116
118
120 app->Setup(ns3TcpSocket, sinkAddress, 1040, 1000, DataRate("1Mbps"));
121 nodes.Get(0)->AddApplication(app);
122 app->SetStartTime(Seconds(1.));
123 app->SetStopTime(Seconds(20.));
124
125 AsciiTraceHelper asciiTraceHelper;
126 Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream("sixth.cwnd");
127 ns3TcpSocket->TraceConnectWithoutContext("CongestionWindow",
128 MakeBoundCallback(&CwndChange, stream));
129
130 PcapHelper pcapHelper;
132 pcapHelper.CreateFile("sixth.pcap", std::ios::out, PcapHelper::DLT_PPP);
133 devices.Get(1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback(&RxDrop, file));
134
138
139 return 0;
140}
a polymophic address class
Definition address.h:90
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Class for representing data rates.
Definition data-rate.h:78
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Manage pcap files for device models.
Ptr< PcapFileWrapper > CreateFile(std::string filename, std::ios::openmode filemode, DataLinkType dataLinkType, uint32_t snapLen=std::numeric_limits< uint32_t >::max(), int32_t tzCorrection=0)
Create and initialize a pcap file.
Build a set of PointToPointNetDevice objects.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
static TypeId GetTypeId()
Get the type ID.
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
auto MakeBoundCallback(R(*fnPtr)(Args...), BArgs &&... bargs)
Make Callbacks with varying number of bound arguments.
Definition callback.h:745
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
NodeContainer nodes
address
Definition first.py:36
pointToPoint
Definition first.py:27
devices
Definition first.py:31
stack
Definition first.py:33
interfaces
Definition first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static void CwndChange(Ptr< OutputStreamWrapper > stream, uint32_t oldCwnd, uint32_t newCwnd)
Congestion window change callback.
Definition sixth.cc:62
static void RxDrop(Ptr< PcapFileWrapper > file, Ptr< const Packet > p)
Rx drop callback.
Definition sixth.cc:76