258 lines
9.1 KiB
C++
258 lines
9.1 KiB
C++
#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");
|
||
|
||
/* ### 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 l’uso 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*/
|
||
// 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
|
||
// UDP Echo client
|
||
|
||
/* ######################################################## */
|
||
/* PCAP Tracing */
|
||
if(tracing){
|
||
ptph24.EnablePcapAll("ptp24", true);
|
||
ptph45.EnablePcapAll("ptp45", true);
|
||
ptph49.EnablePcapAll("ptp49", true);
|
||
}
|
||
|
||
/* ######################################################## */
|
||
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);
|
||
|
||
} |