Last active
January 11, 2024 23:02
-
-
Save cappee/ce9e2a90824b0f175fbf6d6a68fdac72 to your computer and use it in GitHub Desktop.
ns3 script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ns3/applications-module.h" | |
#include "ns3/core-module.h" | |
#include "ns3/internet-module.h" | |
#include "ns3/network-module.h" | |
#include "ns3/point-to-point-module.h" | |
#include "ns3/csma-module.h" | |
#include "ns3/point-to-point-star.h" | |
#include "ns3/csma-star-helper.h" | |
#include "ns3/wifi-helper.h" | |
#include "ns3/ssid.h" | |
#include "ns3/yans-wifi-helper.h" | |
#include "ns3/mobility-module.h" | |
using namespace ns3; | |
int | |
main(int argc, char* argv[]) | |
{ | |
std::string studentId; | |
bool enableRtsCts = false; | |
bool tracing = false; | |
CommandLine cmd; | |
cmd.AddValue("studentId", "Student number", studentId); | |
cmd.AddValue("enableRtsCts", "Force RTS/CTS use if true", enableRtsCts); | |
cmd.AddValue("tracing", "Enable pcap tracing", tracing); | |
cmd.Parse(argc, argv); | |
if(studentId.empty()){ | |
NS_ABORT_MSG("Error: missing 'studentId' parameter."); | |
return 1; | |
} | |
if(enableRtsCts){ | |
Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0")); | |
} | |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); | |
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO); | |
//LogComponentEnable("TcpSocketBase", LOG_LEVEL_INFO); | |
//LogComponentEnable("OnOffApplication", LOG_LEVEL_INFO); | |
//LogComponentEnable("PacketSink", LOG_LEVEL_INFO); | |
//CHANNEL HELPER | |
PointToPointHelper pointToPoint100_20; | |
pointToPoint100_20.SetDeviceAttribute("DataRate", StringValue("100Mbps")); | |
pointToPoint100_20.SetChannelAttribute("Delay", TimeValue(MilliSeconds(20))); | |
PointToPointHelper pointToPoint10_200; | |
pointToPoint10_200.SetDeviceAttribute("DataRate", StringValue("10Mbps")); | |
pointToPoint10_200.SetChannelAttribute("Delay", TimeValue(MilliSeconds(200))); | |
CsmaHelper csma5_20; | |
csma5_20.SetChannelAttribute("DataRate", StringValue("5Mbps")); | |
csma5_20.SetChannelAttribute("Delay", TimeValue(MilliSeconds(20))); | |
CsmaHelper csma10_200; | |
csma10_200.SetChannelAttribute("DataRate", StringValue("10Mbps")); | |
csma10_200.SetChannelAttribute("Delay", TimeValue(MilliSeconds(200))); | |
//STAR TOPOLOGY HELPER | |
PointToPointStarHelper pointToPointStar(2, pointToPoint10_200); | |
CsmaStarHelper csmaStar(3, csma5_20); | |
//NODES | |
//0: nodo 4 | |
//1: nodo 3 (server) | |
//2: nodo 9 (wifi ap) | |
NodeContainer nodes; | |
nodes.Create(3); | |
//0..6 nodi 10..16 (wifi station) | |
NodeContainer wirelessNodes; | |
wirelessNodes.Create(7); | |
//0: nodo 4 | |
//1: nodo 3 | |
NodeContainer csmaNodes; | |
csmaNodes.Add(nodes.Get(0)); | |
csmaNodes.Add(nodes.Get(1)); | |
//NET DEVICE | |
NetDeviceContainer devices4_3 = csma10_200.Install(csmaNodes); | |
NetDeviceContainer devices4_5 = pointToPoint100_20.Install(nodes.Get(0), csmaStar.GetHub()); | |
NetDeviceContainer devices4_2 = pointToPoint100_20.Install(nodes.Get(0), pointToPointStar.GetHub()); | |
NetDeviceContainer devices4_9 = pointToPoint100_20.Install(nodes.Get(0), nodes.Get(2)); | |
//WIFI | |
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default(); | |
YansWifiPhyHelper phy; | |
phy.SetChannel(wifiChannel.Create()); | |
WifiHelper wifi; | |
wifi.SetStandard(WIFI_STANDARD_80211g); | |
wifi.SetRemoteStationManager("ns3::AarfWifiManager"); | |
WifiMacHelper mac; | |
Ssid ssid = Ssid("2076570"); | |
NetDeviceContainer staDevices; | |
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false)); | |
staDevices = wifi.Install(phy, mac, wirelessNodes); | |
NetDeviceContainer apDevice; | |
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid)); | |
apDevice = wifi.Install(phy, mac, nodes.Get(2)); | |
MobilityHelper mobility; | |
mobility.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator", | |
"X", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=30.0]"), | |
"Y", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=30.0]")); | |
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel", | |
"Bounds", RectangleValue(Rectangle(0, 30, 0, 30))); | |
mobility.Install(wirelessNodes); | |
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); | |
mobility.Install(nodes.Get(2)); | |
//INTERNET STACK | |
InternetStackHelper stack; | |
stack.Install(nodes); | |
stack.Install(wirelessNodes); | |
pointToPointStar.InstallStack(stack); | |
csmaStar.InstallStack(stack); | |
//ASSIGN IP | |
Ipv4AddressHelper address; | |
address.SetBase("10.0.1.0", "255.255.255.248"); | |
Ipv4InterfaceContainer interface4_2 = address.Assign(devices4_2); | |
Ipv4InterfaceContainer interface4_5 = address.Assign(devices4_5); | |
Ipv4InterfaceContainer interface4_9 = address.Assign(devices4_9); | |
address.SetBase("10.0.2.0", "255.255.255.248"); | |
csmaStar.AssignIpv4Addresses(address); | |
address.SetBase("10.0.3.0", "255.255.255.248"); | |
pointToPointStar.AssignIpv4Addresses(address); | |
address.SetBase("10.0.4.0", "255.255.255.240"); | |
Ipv4InterfaceContainer wifiApInterface = address.Assign(apDevice); | |
Ipv4InterfaceContainer wifiStaInterface = address.Assign(staDevices); | |
address.SetBase("10.0.5.0", "255.255.255.248"); | |
Ipv4InterfaceContainer interface4_3 = address.Assign(devices4_3); | |
Ipv4GlobalRoutingHelper::PopulateRoutingTables(); | |
//UDP ECHO | |
UdpEchoServerHelper echoServer(9); | |
ApplicationContainer serverSender=echoServer.Install(nodes.Get(1)); | |
serverSender.Start(Seconds(0.0)); | |
serverSender.Stop(Seconds(15.0)); | |
UdpEchoClientHelper echoClient(interface4_3.GetAddress(1),9); | |
echoClient.SetAttribute("MaxPackets", UintegerValue(250)); | |
echoClient.SetAttribute("Interval", TimeValue(MilliSeconds(20))); | |
echoClient.SetAttribute("PacketSize", UintegerValue(1871)); | |
ApplicationContainer clientReceiver=echoClient.Install(csmaStar.GetSpokeNode(1)); | |
std::string fillString = "Simone, Gabrielli, 2076570, Francesco, Deciano,2043781, Leonardo, Beraldo, 2069743, Chiara, Cisale, 1983210, Gabriele, Cappellaro, 2044279"; | |
uint32_t paddingSize = 1870 - fillString.size(); | |
std::string paddedString = fillString + std::string(paddingSize, '\0'); | |
echoClient.SetFill(clientReceiver.Get(0), paddedString); | |
clientReceiver.Start(Seconds(0.0)); | |
clientReceiver.Stop(Seconds(15.0)); | |
//TCP1 | |
PacketSinkHelper packetSink1 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(0), 80)); | |
ApplicationContainer serverApps1 = packetSink1.Install(pointToPointStar.GetSpokeNode(0)); | |
serverApps1.Start(Seconds(0.0)); | |
serverApps1.Stop(Seconds(15.0)); | |
OnOffHelper onOff1 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(0), 80)); | |
onOff1.SetAttribute("PacketSize", UintegerValue(1240)); | |
onOff1.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
onOff1.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
ApplicationContainer clientApps1 = onOff1.Install(wirelessNodes.Get(2)); | |
clientApps1.Start(Seconds(0.31)); | |
clientApps1.Stop(Seconds(15.0)); | |
//TCP2 | |
PacketSinkHelper packetSink2 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(1), 80)); | |
ApplicationContainer serverApps2 = packetSink2.Install(pointToPointStar.GetSpokeNode(1)); | |
serverApps2.Start(Seconds(0.0)); | |
serverApps2.Stop(Seconds(15.0)); | |
OnOffHelper onOff2 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(1), 80)); | |
onOff2.SetAttribute("PacketSize", UintegerValue(1726)); | |
onOff2.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
onOff2.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
ApplicationContainer clientApps2 = onOff2.Install(csmaStar.GetSpokeNode(2)); | |
clientApps2.Start(Seconds(3.34)); | |
clientApps2.Stop(Seconds(15.0)); | |
//TCP3 | |
PacketSinkHelper packetSink3 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(0), 81)); | |
ApplicationContainer serverApps3 = packetSink3.Install(pointToPointStar.GetSpokeNode(0)); | |
serverApps3.Start(Seconds(0.0)); | |
serverApps3.Stop(Seconds(15.0)); | |
OnOffHelper onOff3 ("ns3::TcpSocketFactory", InetSocketAddress(pointToPointStar.GetSpokeIpv4Address(0), 81)); | |
onOff3.SetAttribute("PacketSize", UintegerValue(1475)); | |
onOff3.SetAttribute("OnTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
onOff3.SetAttribute("OffTime", StringValue("ns3::ExponentialRandomVariable[Mean=1]")); | |
ApplicationContainer clientApps3 = onOff3.Install(wirelessNodes.Get(1)); | |
clientApps3.Start(Seconds(3.72)); | |
clientApps3.Stop(Seconds(15.0)); | |
//TRACING | |
if (tracing) { | |
//pointToPoint100_20.EnablePcap("task", devices4_2.Get(1), true); | |
pointToPoint100_20.EnablePcap("task", pointToPointStar.GetHub()->GetDevice(1), true); | |
pointToPoint10_200.EnablePcap("task", pointToPointStar.GetHub()->GetDevice(2), true); | |
//nodo 4 | |
pointToPoint100_20.EnablePcap("task", devices4_2.Get(0), true); | |
csma10_200.EnablePcap("task", devices4_3.Get(0), true); | |
pointToPoint100_20.EnablePcap("task", devices4_5.Get(0), true); | |
pointToPoint100_20.EnablePcap("task", devices4_9.Get(0), true); | |
//nodo 5 | |
csma5_20.EnablePcap("task", csmaStar.GetHubDevices(), true); | |
pointToPoint100_20.EnablePcap("task", devices4_5.Get(1), true); | |
//nodo 9 | |
phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO); | |
phy.EnablePcap("task", apDevice.Get(0),true); | |
} | |
//RUN SIMULATION | |
Simulator::Stop(Seconds(15.0)); | |
Simulator::Run(); | |
Simulator::Destroy(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment