tc-homework2/scratch/task_2001600.cc

336 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <iostream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/ssid.h"
#include "ns3/yans-wifi-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("Task_2001600");
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay);
int main(int argc, char* argv[])
{
// Hello
NS_LOG_UNCOND("Hello Task_2001600");
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
/* ### Creazione della Rete ### */
/* ######################################################## */
/* PARAMETRI */
/*
Parametro obbligatorio di nome “studentId” che rappresenta la stringa della matricola referente. Se
questo parametro non viene passato, la simulazione non deve partire
*/
std::string studentId;
/*
Parametro opzionale di nome “enableRtsCts” che rappresenta un valore booleano con valore di
default uguale a Falso. Quando il suo valore è Vero, deve forzare luso del meccanismo RTS/CTS
*/
bool enableRtsCts = false;
/*
Parametro opzionale di nome “tracing” che rappresenta un valore booleano con valore di default
uguale a Falso. Quando il suo valore è Vero, deve abilitare il tracing in modalità promiscua sugli
switch di rete e sul router Wi-Fi.
*/
bool tracing = false;
// Parse command line arguments
// To be passed `./ns3 run task_2001600 -- --studentId=2001600 --tracing=true --enableRtsCts=true`
CommandLine cmd;
cmd.AddValue("studentId", "studentId", studentId);
cmd.AddValue("enableRtsCts", "Enable RTS/CTS", enableRtsCts);
cmd.AddValue("tracing", "Enable tracing", tracing);
cmd.Parse(argc, argv);
// Se studentId vuoto, la simulazione non deve partire
if (studentId.empty())
{
std::cout << "studentId is empty, simulation will not start. exiting." << std::endl;
return 1;
}
// All the nodes, each subnet uses a subset of these.
// A node can belong to multiple subnets
NodeContainer allNodes;
allNodes.Create(16);
/* Topologia della Rete*/
// Reti PTP
/* SOTTORETE PTP Node 0,2 */
NodeContainer nodes02;
nodes02.Add(allNodes.Get(0));
nodes02.Add(allNodes.Get(2));
PointToPointHelper ptph02;
ptph02.SetDeviceAttribute("DataRate", StringValue("00Mbps"));
ptph02.SetChannelAttribute("Delay", StringValue("200ms"));
NetDeviceContainer ptp02 = ptph02.Install(nodes02);
/* SOTTORETE PTP Node 1,2 */
NodeContainer nodes12;
nodes12.Add(allNodes.Get(1));
nodes12.Add(allNodes.Get(2));
PointToPointHelper ptph12;
ptph12.SetDeviceAttribute("DataRate", StringValue("10Mbps"));
ptph12.SetChannelAttribute("Delay", StringValue("200ms"));
NetDeviceContainer ptp12 = ptph12.Install(nodes12);
/* SOTTORETE PTP Node 2,4 */
NodeContainer nodes24;
nodes24.Add(allNodes.Get(2));
nodes24.Add(allNodes.Get(4));
PointToPointHelper ptph24;
ptph24.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph24.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp24 = ptph24.Install(nodes24);
/* SOTTORETE PTP Node 3,5 */
NodeContainer nodes35;
nodes35.Add(allNodes.Get(3));
nodes35.Add(allNodes.Get(5));
PointToPointHelper ptph35;
ptph35.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph35.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp35 = ptph35.Install(nodes35);
/* SOTTORETE PTP Node 4,5 */
NodeContainer nodes45;
nodes45.Add(allNodes.Get(4));
nodes45.Add(allNodes.Get(5));
PointToPointHelper ptph45;
ptph45.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph45.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp45 = ptph45.Install(nodes45);
/* SOTTORETE PTP Node 3,9 */
NodeContainer nodes39;
nodes39.Add(allNodes.Get(3));
nodes39.Add(allNodes.Get(9));
PointToPointHelper ptph39;
ptph39.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph39.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp39 = ptph39.Install(nodes39);
/* SOTTORETE PTP Node 4,9 */
NodeContainer nodes49;
nodes49.Add(allNodes.Get(4));
nodes49.Add(allNodes.Get(9));
PointToPointHelper ptph49;
ptph49.SetDeviceAttribute("DataRate", StringValue("100Mbps"));
ptph49.SetChannelAttribute("Delay", StringValue("20ms"));
NetDeviceContainer ptp49 = ptph49.Install(nodes49);
// Reti CSMA
/* SOTTORETE CSMA Nodi 3,4 */
NodeContainer nodes34;
nodes34.Add(allNodes.Get(3));
nodes34.Add(allNodes.Get(4));
CsmaHelper csmah34;
csmah34.SetChannelAttribute("DataRate", StringValue("10Mbps"));
csmah34.SetChannelAttribute("Delay", TimeValue(MicroSeconds(200)));
NetDeviceContainer csma34 = csmah34.Install(nodes34);
/* SOTTORETE CSMA Nodi 5,6,7,8 */
NodeContainer nodes5678;
nodes5678.Add(allNodes.Get(5));
nodes5678.Add(allNodes.Get(6));
nodes5678.Add(allNodes.Get(7));
nodes5678.Add(allNodes.Get(8));
CsmaHelper csmah5678;
csmah5678.SetChannelAttribute("DataRate", StringValue("5Mbps"));
csmah5678.SetChannelAttribute("Delay", TimeValue(MicroSeconds(20)));
NetDeviceContainer csma5678 = csmah5678.Install(nodes5678);
// Rete Wi-Fi AARF 802.11g
/*
Wi-Fi operating in Infrastructure mode, AP is stationary, Stations nodes move with random walk mobility pattern over a bounded square of 30 meters for each side
*/
// Channel
// AP
NodeContainer wifiApNode;
wifiApNode.Add(allNodes.Get(9));
// Station devices
NodeContainer wifiStaNodes;
wifiStaNodes.Add(allNodes.Get(10));
wifiStaNodes.Add(allNodes.Get(11));
wifiStaNodes.Add(allNodes.Get(12));
wifiStaNodes.Add(allNodes.Get(13));
wifiStaNodes.Add(allNodes.Get(14));
wifiStaNodes.Add(allNodes.Get(15));
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
WifiHelper wifi;
wifi.SetStandard (WIFI_STANDARD_80211g);
wifi.SetRemoteStationManager("ns3::AarfWifiManager");
// you can do in this way if you want to set a constant data rate
//wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager", "DataMode", StringValue("OfdmRate6Mbps"));
WifiMacHelper mac;
// Infrastructure Mode
Ssid ssid = Ssid("2001600");
NetDeviceContainer staDevices;
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
staDevices = wifi.Install(phy, mac, wifiStaNodes);
NetDeviceContainer apDevices;
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
apDevices = wifi.Install(phy, mac, wifiApNode);
// Mobility helper
MobilityHelper mobility;
// Actually needed?
/*mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX",
DoubleValue(0.0),
"MinY",
DoubleValue(0.0),
"DeltaX",
DoubleValue(5.0),
"DeltaY",
DoubleValue(10.0),
"GridWidth",
UintegerValue(3),
"LayoutType",
StringValue("RowFirst"));
*/
// Stations move with random walk over a square of 30 meters each side
mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
"Bounds",
RectangleValue(Rectangle(-15, 15, -15, 15)));
mobility.Install(wifiStaNodes);
// AP is stationary
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
// TODO: enableRtsCts
/* ######################################################## */
/* IP Addresses*/
// InternetStackHelper for everyone!
InternetStackHelper stack;
stack.Install(allNodes);
// Assign IP address to subnets
Ipv4AddressHelper address;
// nodes 2,4
address.SetBase("10.1.0.0", "255.225.255.252"); // 10.1.0.0/30
Ipv4InterfaceContainer ipptp24;
ipptp24 = address.Assign(ptp24);
// nodes 4,5
address.SetBase("10.1.1.0", "255.225.255.252"); // 10.1.1.0/30
Ipv4InterfaceContainer ipptp45;
ipptp45 = address.Assign(ptp45);
// nodes 3,4
address.SetBase("10.1.2.0", "255.225.255.252"); // 10.1.2.0/30
Ipv4InterfaceContainer icsma34;
icsma34 = address.Assign(csma34);
// nodes 4,9
address.SetBase("10.1.3.0", "255.225.255.252"); // 10.1.3.0/30
Ipv4InterfaceContainer ipptp49;
ipptp49 = address.Assign(ptp49);
// nodes 0,2
address.SetBase("10.1.4.0", "255.225.255.252"); // 10.1.4.0/30
Ipv4InterfaceContainer ipptp02;
ipptp02 = address.Assign(ptp02);
// nodes 0,1
address.SetBase("10.1.5.0", "255.225.255.252"); // 10.1.5.0/30
Ipv4InterfaceContainer ipptp12;
ipptp12 = address.Assign(ptp12);
// nodes 3,5
address.SetBase("10.1.8.0", "255.225.255.252"); // 10.1.8.0/30
Ipv4InterfaceContainer ipptp35;
ipptp35 = address.Assign(ptp35);
// nodes 3,9
address.SetBase("10.1.7.0", "255.225.255.252"); // 10.1.7.0/30
Ipv4InterfaceContainer ipptp39;
ipptp39 = address.Assign(ptp39);
// csma nodes
address.SetBase("10.1.9.0", "255.225.255.248"); // 10.1.9.0/29
Ipv4InterfaceContainer icsma5678;
icsma5678 = address.Assign(csma5678);
// wifi nodes
address.SetBase("10.1.6.0", "255.225.255.240"); // 10.1.9.0/28
address.Assign(staDevices);
address.Assign(apDevices);
/* ######################################################## */
/* Applications*/
// TCP Burst
// UDP Echo application with Client 13 and Server 3
// Size of packet: 1477 Bytes Periodicity: 20ms MaxPackets: 250
// UDP Echo server
UdpEchoServerHelper echoServer(9); // UDP server on port 9
ApplicationContainer serverApps = echoServer.Install(allNodes.Get(3));
serverApps.Start(Seconds(0.0));
// UDP Echo client
// UDP client targeting server 3 using the IP of the interface connected to the wifi ap
UdpEchoClientHelper echoClient(ipptp39.GetAddress(0), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(250));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1447));
ApplicationContainer clientApps = echoClient.Install(allNodes.Get(13));
clientApps.Start(Seconds(0.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
/* ######################################################## */
/* PCAP Tracing */
if(tracing){
ptph24.EnablePcapAll("ptp24", true);
ptph35.EnablePcapAll("ptp35", true);
ptph45.EnablePcapAll("ptp45", true);
ptph39.EnablePcapAll("ptp39", true);
ptph49.EnablePcapAll("ptp49", true);
}
/* ######################################################## */
// Max simulation time: 15s
Simulator::Stop(Seconds(15));
Simulator::Run();
Simulator::Destroy();
return 0;
}
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay){
CsmaHelper csma;
csma.SetDeviceAttribute("DataRate", DataRate);
csma.SetChannelAttribute("Delay", Delay);
return csma.Install(nodes);
}