A Discrete-Event Network Simulator
API
fifth.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation;
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <fstream>
18#include "ns3/core-module.h"
19#include "ns3/network-module.h"
20#include "ns3/internet-module.h"
21#include "ns3/applications-module.h"
22#include "ns3/point-to-point-module.h"
23#include "tutorial-app.h"
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE ("FifthScriptExample");
28
29// ===========================================================================
30//
31// node 0 node 1
32// +----------------+ +----------------+
33// | ns-3 TCP | | ns-3 TCP |
34// +----------------+ +----------------+
35// | 10.1.1.1 | | 10.1.1.2 |
36// +----------------+ +----------------+
37// | point-to-point | | point-to-point |
38// +----------------+ +----------------+
39// | |
40// +---------------------+
41// 5 Mbps, 2 ms
42//
43//
44// We want to look at changes in the ns-3 TCP congestion window. We need
45// to crank up a flow and hook the CongestionWindow attribute on the socket
46// of the sender. Normally one would use an on-off application to generate a
47// flow, but this has a couple of problems. First, the socket of the on-off
48// application is not created until Application Start time, so we wouldn't be
49// able to hook the socket (now) at configuration time. Second, even if we
50// could arrange a call after start time, the socket is not public so we
51// couldn't get at it.
52//
53// So, we can cook up a simple version of the on-off application that does what
54// we want. On the plus side we don't need all of the complexity of the on-off
55// application. On the minus side, we don't have a helper, so we have to get
56// a little more involved in the details, but this is trivial.
57//
58// So first, we create a socket and do the trace connect on it; then we pass
59// this socket into the constructor of our simple application which we then
60// install in the source node.
61// ===========================================================================
62//
63
64
65static void
66CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
67{
68 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
69}
70
71static void
73{
74 NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
75}
76
77int
78main (int argc, char *argv[])
79{
80 CommandLine cmd (__FILE__);
81 cmd.Parse (argc, argv);
82
83 // In the following three lines, TCP NewReno is used as the congestion
84 // control algorithm, the initial congestion window of a TCP connection is
85 // set to 1 packet, and the classic fast recovery algorithm is used. Note
86 // that this configuration is used only to demonstrate how TCP parameters
87 // can be configured in ns-3. Otherwise, it is recommended to use the default
88 // settings of TCP in ns-3.
89 Config::SetDefault ("ns3::TcpL4Protocol::SocketType", StringValue ("ns3::TcpNewReno"));
90 Config::SetDefault ("ns3::TcpSocket::InitialCwnd", UintegerValue (1));
91 Config::SetDefault ("ns3::TcpL4Protocol::RecoveryType", TypeIdValue (TypeId::LookupByName ("ns3::TcpClassicRecovery")));
92
94 nodes.Create (2);
95
97 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
98 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
99
101 devices = pointToPoint.Install (nodes);
102
103 Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();
104 em->SetAttribute ("ErrorRate", DoubleValue (0.00001));
105 devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
106
108 stack.Install (nodes);
109
111 address.SetBase ("10.1.1.0", "255.255.255.252");
113
114 uint16_t sinkPort = 8080;
115 Address sinkAddress (InetSocketAddress (interfaces.GetAddress (1), sinkPort));
116 PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
117 ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
118 sinkApps.Start (Seconds (0.));
119 sinkApps.Stop (Seconds (20.));
120
121 Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());
122 ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeCallback (&CwndChange));
123
124 Ptr<TutorialApp> app = CreateObject<TutorialApp> ();
125 app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
126 nodes.Get (0)->AddApplication (app);
127 app->SetStartTime (Seconds (1.));
128 app->SetStopTime (Seconds (20.));
129
130 devices.Get (1)->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&RxDrop));
131
132 Simulator::Stop (Seconds (20));
133 Simulator::Run ();
134 Simulator::Destroy ();
135
136 return 0;
137}
138
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:229
Class for representing data rates.
Definition: data-rate.h:89
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:364
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
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
Hold variables of type string.
Definition: string.h:41
AttributeValue implementation for TypeId.
Definition: type-id.h:595
Hold an unsigned integer type.
Definition: uinteger.h:44
static void RxDrop(Ptr< const Packet > p)
Definition: fifth.cc:72
static void CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
Definition: fifth.cc:66
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#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:205
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
pointToPoint
Definition: first.py:35
devices
Definition: first.py:39
stack
Definition: first.py:41
nodes
Definition: first.py:32
interfaces
Definition: first.py:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35