-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcpapplication.cc
206 lines (167 loc) · 5.53 KB
/
tcpapplication.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include <fstream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("TCPApplication");
// ===========================================================================
//
// node 0 node 1
// +----------------+ +----------------+
// | ns-3 TCP | | ns-3 TCP |
// +----------------+ +----------------+
// | 10.1.1.1 | | 10.1.1.2 |
// +----------------+ +----------------+
// | point-to-point | | point-to-point |
// +----------------+ +----------------+
// | |
// +---------------------+
// 5 Mbps, 2 ms
//
// ===========================================================================
// MAIN TAKEAWAY: Cannot hook onto trace sources and sinks during configuration as
// they may be created during run time, and do not exist during configuration time.
// Application Code
class App : public Application
{
public:
App();
virtual ~App();
void setup(Ptr<Socket> socket, Address address, uint32_t packetSize,
uint32_t nPackets, DataRate dataRate);
private:
virtual void StartApplication(void);
virtual void StopApplication(void);
void ScheduleTx(void);
void SendPacket(void);
Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
DataRate m_dataRate;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetsSent;
};
// Constructor for the application
App::App()
: m_socket(0),
m_peer(),
m_packetSize(0),
m_nPackets(0),
m_dataRate(0),
m_sendEvent(),
m_running(false),
m_packetsSent(0)
{}
// Destructor for the application
App::~App()
{
m_socket = 0;
}
void App::setup(Ptr<Socket> socket, Address address, uint32_t packetSize,
uint32_t nPackets, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}
// Application start
void App::StartApplication(void)
{
m_running = true;
m_packetsSent = 0;
m_socket->Bind();
m_socket->Connect(m_peer);
SendPacket();
}
// Application stop
void App::StopApplication(void)
{
m_running = false;
if(m_sendEvent.IsRunning())
{
Simulator::Cancel(m_sendEvent);
}
if(m_socket)
{
m_socket->Close();
}
}
// Send the packet
void App::SendPacket(void)
{
Ptr<Packet> packet = Create<Packet>(m_packetSize);
m_socket->Send(packet);
if(++m_packetsSent < m_nPackets)
{
ScheduleTx();
}
}
void App::ScheduleTx(void)
{
if(m_running)
{
Time tNext(Seconds(m_packetSize * 8 / static_cast<double>(m_dataRate.GetBitRate())));
m_sendEvent = Simulator::Schedule(tNext, &App::SendPacket, this);
}
}
static void CwndChange(uint32_t oldCwnd, uint32_t newCwnd)
{
NS_LOG_INFO(Simulator::Now().GetSeconds()<<"\t"<<newCwnd);
}
static void RxDrop(Ptr<OutputStreamWrapper> stream, Ptr<const Packet>p)
{
NS_LOG_INFO("RxDrop at "<<Simulator::Now().GetSeconds());
*stream->GetStream() << "Rx drop at: "<< Simulator::Now().GetSeconds();
}
// Main program
int main(int argc, char *argv[])
{
NS_LOG_INFO("Create P2P nodes.....");
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
pointToPoint.SetQueue("ns3::DropTailQueue", "MaxSize", StringValue ("50p"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
// Add errors in the channel at a given rate
Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
em->SetAttribute("ErrorRate", DoubleValue(0.00001));
devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue(em));
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.252");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
uint16_t sinkPort = 8080;
Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), sinkPort));
PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory",
InetSocketAddress(Ipv4Address::GetAny(), sinkPort));
ApplicationContainer sinkApps = packetSinkHelper.Install(nodes.Get(1));
sinkApps.Start(Seconds(0.));
sinkApps.Stop(Seconds(20.));
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket(nodes.Get(0),
TcpSocketFactory::GetTypeId());
ns3TcpSocket->TraceConnectWithoutContext("CongestionWindow",
MakeCallback(&CwndChange));
Ptr<App> app = CreateObject<App>();
app->setup(ns3TcpSocket, sinkAddress, 104000, 1, DataRate("1Mbps"));
nodes.Get(0)->AddApplication(app);
app->SetStartTime(Seconds(1.));
app->SetStopTime(Seconds(20.));
AsciiTraceHelper ascii;
Ptr<OutputStreamWrapper> streamRxDrops = ascii.CreateFileStream("outputs/RxDrops_tcpbasic.txt");
devices.Get(1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback(&RxDrop, streamRxDrops));
Simulator::Stop(Seconds(20));
Simulator::Run();
Simulator::Destroy();
return 0;
}