A Discrete-Event Network Simulator
API
fifth.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */
15 
16 #include "tutorial-app.h"
17 
18 #include "ns3/applications-module.h"
19 #include "ns3/core-module.h"
20 #include "ns3/internet-module.h"
21 #include "ns3/network-module.h"
22 #include "ns3/point-to-point-module.h"
23 
24 #include <fstream>
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE("FifthScriptExample");
29 
30 // ===========================================================================
31 //
32 // node 0 node 1
33 // +----------------+ +----------------+
34 // | ns-3 TCP | | ns-3 TCP |
35 // +----------------+ +----------------+
36 // | 10.1.1.1 | | 10.1.1.2 |
37 // +----------------+ +----------------+
38 // | point-to-point | | point-to-point |
39 // +----------------+ +----------------+
40 // | |
41 // +---------------------+
42 // 5 Mbps, 2 ms
43 //
44 //
45 // We want to look at changes in the ns-3 TCP congestion window. We need
46 // to crank up a flow and hook the CongestionWindow attribute on the socket
47 // of the sender. Normally one would use an on-off application to generate a
48 // flow, but this has a couple of problems. First, the socket of the on-off
49 // application is not created until Application Start time, so we wouldn't be
50 // able to hook the socket (now) at configuration time. Second, even if we
51 // could arrange a call after start time, the socket is not public so we
52 // couldn't get at it.
53 //
54 // So, we can cook up a simple version of the on-off application that does what
55 // we want. On the plus side we don't need all of the complexity of the on-off
56 // application. On the minus side, we don't have a helper, so we have to get
57 // a little more involved in the details, but this is trivial.
58 //
59 // So first, we create a socket and do the trace connect on it; then we pass
60 // this socket into the constructor of our simple application which we then
61 // install in the source node.
62 // ===========================================================================
63 //
64 
71 static void
72 CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
73 {
74  NS_LOG_UNCOND(Simulator::Now().GetSeconds() << "\t" << newCwnd);
75 }
76 
82 static void
84 {
85  NS_LOG_UNCOND("RxDrop at " << Simulator::Now().GetSeconds());
86 }
87 
88 int
89 main(int argc, char* argv[])
90 {
91  CommandLine cmd(__FILE__);
92  cmd.Parse(argc, argv);
93 
94  // In the following three lines, TCP NewReno is used as the congestion
95  // control algorithm, the initial congestion window of a TCP connection is
96  // set to 1 packet, and the classic fast recovery algorithm is used. Note
97  // that this configuration is used only to demonstrate how TCP parameters
98  // can be configured in ns-3. Otherwise, it is recommended to use the default
99  // settings of TCP in ns-3.
100  Config::SetDefault("ns3::TcpL4Protocol::SocketType", StringValue("ns3::TcpNewReno"));
101  Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(1));
102  Config::SetDefault("ns3::TcpL4Protocol::RecoveryType",
103  TypeIdValue(TypeId::LookupByName("ns3::TcpClassicRecovery")));
104 
106  nodes.Create(2);
107 
109  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
110  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
111 
113  devices = pointToPoint.Install(nodes);
114 
115  Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
116  em->SetAttribute("ErrorRate", DoubleValue(0.00001));
117  devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
118 
120  stack.Install(nodes);
121 
123  address.SetBase("10.1.1.0", "255.255.255.252");
125 
126  uint16_t sinkPort = 8080;
127  Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), sinkPort));
128  PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory",
130  ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));
131  sinkApps.Start(Seconds(0.));
132  sinkApps.Stop(Seconds(20.));
133 
135  ns3TcpSocket->TraceConnectWithoutContext("CongestionWindow", MakeCallback(&CwndChange));
136 
137  Ptr<TutorialApp> app = CreateObject<TutorialApp>();
138  app->Setup(ns3TcpSocket, sinkAddress, 1040, 1000, DataRate("1Mbps"));
140  app->SetStartTime(Seconds(1.));
141  app->SetStopTime(Seconds(20.));
142 
143  devices.Get(1)->TraceConnectWithoutContext("PhyRxDrop", MakeCallback(&RxDrop));
144 
146  Simulator::Run();
148 
149  return 0;
150 }
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.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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:169
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:315
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:204
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
Hold variables of type string.
Definition: string.h:56
static TypeId GetTypeId()
Get the type ID.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
Hold an unsigned integer type.
Definition: uinteger.h:45
static void RxDrop(Ptr< const Packet > p)
Rx drop callback.
Definition: fifth.cc:83
static void CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
Congestion window change callback.
Definition: fifth.cc:72
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#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:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
address
Definition: first.py:47
pointToPoint
Definition: first.py:38
devices
Definition: first.py:42
stack
Definition: first.py:44
interfaces
Definition: first.py:50
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
cmd
Definition: second.py:40