Repository navigation
Expand file tree
/
Copy pathipv4-simple.cpp
More file actions
178 lines (144 loc) · 6.2 KB
/
Copy pathipv4-simple.cpp
File metadata and controls
178 lines (144 loc) · 6.2 KB
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2011-2015 Regents of the University of California.
*
* This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
* contributors.
*
* ndnSIM is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
**/
// ndn-simple.cpp
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/tcp-westwood.h"
namespace ns3 {
/**
*
* To run scenario and see what is happening, use the following command:
*
* NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple
*/
Ptr<PacketSink> sink;
uint64_t lastTotalRx = 0;
void
CalculateThroughput ()
{
Time now = Simulator::Now ();
double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e5;
std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
lastTotalRx = sink->GetTotalRx ();
Simulator::Schedule (MilliSeconds (100), &CalculateThroughput);
}
int
main(int argc, char* argv[])
{
uint16_t numberOfNodes = 5;
uint16_t distance = 400;
uint16_t simTime = 10;
std::string tcpVariant = "TcpNewReno";
tcpVariant = std::string ("ns3::") + tcpVariant;
// Select TCP variant
if (tcpVariant.compare ("ns3::TcpWestwoodPlus") == 0)
{
// TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
// the default protocol type in ns3::TcpWestwood is WESTWOOD
Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
}
else
{
TypeId tcpTid;
NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (tcpVariant, &tcpTid), "TypeId " << tcpVariant << " not found");
Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (tcpVariant)));
}
/* Configure TCP Options */
Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (64));
// setting default parameters for PointToPoint links and channels
Config::SetDefault("ns3::QueueBase::MaxSize", StringValue("10p"));
// Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
CommandLine cmd;
cmd.Parse(argc, argv);
// Creating nodes
NodeContainer nodes;
nodes.Create(numberOfNodes);
// Connecting nodes using two links
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("100Mb/s")));
p2p.SetDeviceAttribute ("Mtu", UintegerValue (1500));
p2p.SetChannelAttribute ("Delay", TimeValue (Seconds (0.010)));
NetDeviceContainer p2pinterA = p2p.Install(nodes.Get(0), nodes.Get(1));
NetDeviceContainer p2pinterB = p2p.Install(nodes.Get(1), nodes.Get(2));
NetDeviceContainer p2pinterC = p2p.Install(nodes.Get(2), nodes.Get(3));
NetDeviceContainer p2pinterD = p2p.Install(nodes.Get(3), nodes.Get(4));
// Install internet stack on all nodes
InternetStackHelper stack;
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer inetfaceA = address.Assign (p2pinterA);
address.SetBase ("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer inetfaceB = address.Assign (p2pinterB);
address.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer inetfaceC = address.Assign (p2pinterC);
address.SetBase ("10.1.4.0", "255.255.255.0");
Ipv4InterfaceContainer inetfaceD = address.Assign (p2pinterD);
Ipv4Address sinkHostAddr = inetfaceD.GetAddress (1);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
for (uint16_t i = 0; i < numberOfNodes; i++)
{
positionAlloc->Add (Vector(distance * i, 0, 0));
}
MobilityHelper mobility;
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.SetPositionAllocator(positionAlloc);
mobility.Install (nodes);
// Installing applications
uint16_t port = 9;
ApplicationContainer sinkApp;
ApplicationContainer remoteApp;
// Consumer
PacketSinkHelper consumerHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
sinkApp.Add (consumerHelper.Install(nodes.Get(4)));
sink = StaticCast<PacketSink> (sinkApp.Get (0));
// Producer
BulkSendHelper producerHelper ("ns3::TcpSocketFactory", InetSocketAddress (sinkHostAddr, port));
producerHelper.SetAttribute("MaxBytes", UintegerValue (10000000)); //10MB
producerHelper.SetAttribute("SendSize", UintegerValue (1460));
/*OnOffHelper producerHelper ("ns3::TcpSocketFactory", (InetSocketAddress (sinkHostAddr, port)));
producerHelper.SetAttribute ("PacketSize", UintegerValue (1040));
producerHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
producerHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
*/remoteApp.Add (producerHelper.Install(nodes.Get(0)));
//sinkApp.Start (Seconds (0.0));
//remoteApp.Start (Seconds (0.0));
//Ptr<Node> n1 = nodes.Get (3);
//Ptr<Ipv4> ipv4 = n1->GetObject<Ipv4> ();
//uint32_t ipv4Index = 2;
Simulator::Schedule (Seconds(0), &CalculateThroughput);
//Simulator::Schedule (Seconds(2.0), &Ipv4::SetDown, ipv4, ipv4Index);
//Simulator::Schedule (Seconds(5.0), &Ipv4::SetUp, ipv4, ipv4Index);
Simulator::Stop(Seconds(simTime + 1));
Simulator::Run();
Simulator::Destroy();
return 0;
}
} // namespace ns3
int
main(int argc, char* argv[])
{
return ns3::main(argc, argv);
}