tc-homework2/scratch/task_2001600.cc

228 lines
8.1 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 createP2P(NodeContainer nodes, StringValue DataRate, StringValue Delay);
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay);
int main(int argc, char* argv[])
{
// Hello
NS_LOG_UNCOND("Hello Task_2001600");
/* ### 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() > 18)
{
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(15);
/* Topologia della Rete*/
// Reti PTP
/* SOTTORETE PTP Node 0,2 */
NodeContainer nodes02;
nodes02.Add(allNodes.Get(0));
nodes02.Add(allNodes.Get(2));
NetDeviceContainer ptp02 = createP2P(nodes02, StringValue("10Mbps"), StringValue("200ms"));
/* SOTTORETE PTP Node 1,2 */
NodeContainer nodes12;
nodes12.Add(allNodes.Get(1));
nodes12.Add(allNodes.Get(2));
NetDeviceContainer ptp12 = createP2P(nodes12, StringValue("10Mbps"), StringValue("200ms"));
/* SOTTORETE PTP Node 2,4 */
NodeContainer nodes24;
nodes24.Add(allNodes.Get(2));
nodes24.Add(allNodes.Get(4));
NetDeviceContainer ptp24 = createP2P(nodes24, StringValue("100Mbps"), StringValue("20ms"));
/* SOTTORETE PTP Node 3,5 */
NodeContainer nodes35;
nodes35.Add(allNodes.Get(3));
nodes35.Add(allNodes.Get(5));
NetDeviceContainer ptp35 = createP2P(nodes35, StringValue("100Mbps"), StringValue("20ms"));
/* SOTTORETE PTP Node 4,5 */
NodeContainer nodes45;
nodes45.Add(allNodes.Get(4));
nodes45.Add(allNodes.Get(5));
NetDeviceContainer ptp45 = createP2P(nodes45, StringValue("100Mbps"), StringValue("20ms"));
/* SOTTORETE PTP Node 3,9 */
NodeContainer nodes39;
nodes39.Add(allNodes.Get(3));
nodes39.Add(allNodes.Get(9));
NetDeviceContainer ptp39 = createP2P(nodes39, StringValue("100Mbps"), StringValue("20ms"));
/* SOTTORETE PTP Node 4,9 */
NodeContainer nodes49;
nodes49.Add(allNodes.Get(4));
nodes49.Add(allNodes.Get(9));
NetDeviceContainer ptp49 = createP2P(nodes49, StringValue("100Mbps"), StringValue("20ms"));
// Reti CSMA
/* SOTTORETE CSMA Nodi 3,4 */
NodeContainer nodes34;
nodes34.Add(allNodes.Get(3));
nodes34.Add(allNodes.Get(4));
NetDeviceContainer csma34 = createCSMA(nodes34, StringValue("10Mbps"), StringValue("200ms"));
/* 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));
NetDeviceContainer csma5678 = createCSMA(nodes5678, StringValue("5Mbps"), StringValue("20ms"));
// 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
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
// AP
NodeContainer wifiApNode;
wifiApNode.Add(allNodes.Get(9));
// Station devices
NodeContainer wifiStationNodes;
wifiStationNodes.Add(allNodes.Get(10));
wifiStationNodes.Add(allNodes.Get(11));
wifiStationNodes.Add(allNodes.Get(12));
wifiStationNodes.Add(allNodes.Get(13));
wifiStationNodes.Add(allNodes.Get(14));
wifiStationNodes.Add(allNodes.Get(15));
WifiHelper wifi;
wifi.SetStandard(WIFI_STANDARD_80211g);
wifi.SetRemoteStationManager("ns3::AarfWifiManager");
WifiMacHelper mac;
Ssid ssid = Ssid("2001600"); // Ssid matricola regerent
// Stations
NetDeviceContainer staDevice;
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
staDevice = wifi.Install(phy, mac, wifiStationNodes);
// AccessPoint
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
staDevice = 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(-30, 30, -30, 30)));
mobility.Install(wifiStationNodes);
// AP is stationary
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
/* ######################################################## */
/* IP Addresses*/
// TODO: assign IP address to subnets
/* ######################################################## */
/* Applications*/
// InternetStackHelper for everyone!
InternetStackHelper stack;
stack.Install(allNodes);
// TODO: applications (Tcp burst, udp echo)
// TCP Burst
// UDP Echo server
/* ######################################################## */
/* Pcap tracing*/
// TODO: enable pcap tracing
/* ######################################################## */
Simulator::Run();
Simulator::Destroy();
return 0;
}
NetDeviceContainer createP2P(NodeContainer nodes, StringValue DataRate, StringValue Delay){
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", DataRate);
pointToPoint.SetChannelAttribute("Delay", Delay);
return pointToPoint.Install(nodes);
}
NetDeviceContainer createCSMA(NodeContainer nodes, StringValue DataRate, StringValue Delay){
CsmaHelper csma;
csma.SetDeviceAttribute("DataRate", DataRate);
csma.SetChannelAttribute("Delay", Delay);
return csma.Install(nodes);
}