Home > Back-end >  NS3 wireless network simulation
NS3 wireless network simulation

Time:09-25

I wrote a mywireless. Cc, the main content is: this is a wifi model, an AP node, a number of the sta node, the channel is the channel for the Shared channel, that is to say, can the competition and mutual influence communication,
The code is as follows:
/* - * - Mode: c + +; C - file - style: "gnu"; Indent - tabs - mode: nil; - * - */

# include "ns3/core - module. H"
# include "ns3/network - module. H"
# include "ns3/applications - module. H"
# include "ns3/wifi - module. H"
# include "ns3/mobility - module. H"
# include "ns3/Internet - module. H"
# include "ns3/netanim - module. H"

//the Default Network Topology
//
//Number of wifi nodes can be increased up to 250
//|
//Rank 0
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Wifi 10.1.1.0

//AP//* * * *
//| | | |
//n3 n2 n1 n0

Using the namespace ns3;

Mywireless NS_LOG_COMPONENT_DEFINE (" ");

Int
The main (int arg c, char * argv [])
{
Bool verbose=true;//if the is true, it is in the terminal output log content
Uint32_t nWifi=3;//the sta nodes is
Bool tracing=false;//if it is true, then generate a trace file (. Pcap)

CommandLine CMD.
CMD. AddValue (" nWifi ", "Number of wifi devices" STA, nWifi);
CMD. AddValue (" verbose ", "Tell the echo applications to log the if true", verbose);
CMD. AddValue (" tracing ", "Enable pcap tracing", tracing);

CMD. Parse (arg c, argv);

//Check for valid number of csma/wifi nodes
//250 should be enough, otherwise IP addresses
//soon become an issue
If (nWifi & gt; 250)
{
STD: : cout & lt; <"Many wifi nodes, no more than 250 each." & lt; return 1;
}

If (verbose)
{
LogComponentEnable (" UdpEchoClientApplication LOG_LEVEL_INFO);
LogComponentEnable (" UdpEchoServerApplication LOG_LEVEL_INFO);
}

/* create wifi AP node and the Sta */
NodeContainer wifiApNode;
WifiApNode. Create (1);//create a AP node
NodeContainer wifiStaNodes;
WifiStaNodes. Create (nWifi);//create nWifi sta node

/* once you create the object, we will create a channel object and its associated to our PHY layer object manager, to ensure that all PHY layer created by YansWifiPhyHelper objects share the same underlying channels, that is, they share the same wireless medium, and can communicate with interference */
YansWifiChannelHelper channel=YansWifiChannelHelper: : Default ();//configuration Channel assistant
YansWifiPhyHelper phy=YansWifiPhyHelper: : Default ();//configuration phy assistant
Phys. SetChannel (channel. The Create ());//to make each phy is associated with the Channel

WifiHelper wifi.//create wifi assistant, help to create WifiNetDevice object
Wifi. SetRemoteStationManager (" ns3: : AarfWifiManager ");//set the wifi rate control algorithm of helper object types: AARF algorithm

//MAC layer Settings,
WifiMacHelper MAC;//create the MAC layer
Ssid Ssid=Ssid (" ns - 3 - Ssid ");//create the IEEE 802.11 SSID information element
MAC. SetType (" ns3: : StaWifiMac ",//set the MAC type to "ns3: : StaWifiMac
""Ssid," SsidValue (Ssid),//set the MAC "Ssid" the value of the attribute to SsidValue (Ssid)
"ActiveProbing BooleanValue (false));//set the MAC "ActiveProbing" the value of the attribute to BooleanValue (false), if it is true, we send the probe request,

NetDeviceContainer staDevices;//create WifiStaNetDevice object
StaDevices=wifi. Install (phy, MAC, wifiStaNodes);//according to the phy, MAC and returns a node set NetDeviceContainer object

MAC. SetType (" ns3: : ApWifiMac ",//set the MAC type to "ns3: : ApWifiMac
""Ssid," SsidValue (Ssid));

NetDeviceContainer apDevices;//create WifiApNetDevice object
ApDevices=wifi. Install (phy, MAC, wifiApNode);

/* add mobile model, sta node in the moving state, the ap nodes in the stationary state */
MobilityHelper mobility;//create MobilityHelper object, the position and moving model assigned to the node

Distributor//set the position, is used to allocate MobilityModel: : initialize the initial position of each node in the Install,
Mobility. SetPositionAllocator (" ns3: : GridPositionAllocator ",//set the use of mobile model type (on a rectangular 2 d grid distribution location)
"MinX," DoubleValue (0.0),//the x coordinate grid start
"MinY DoubleValue (0.0),//grid start y
DeltaX, DoubleValue (5.0),//object space between x
"DeltaY," DoubleValue (10.0),//object y space between
"GridWidth UintegerValue (3),//arranged in a row the number of objects
"LayoutType", StringValue (" RowFirst "));//layout type
//set the mobile model type
Mobility. SetMobilityModel (" ns3: : RandomWalk2dMobilityModel ",//the type: one node in random directions movement around the bounding box in a random speed
"Bounds," RectangleValue (a Rectangle (- 50, 50 to 50, 50))); The scope of limits//attributes (rectangular)
Mobility. Install (wifiStaNodes);//to install the mobile model in wifiStaNodes node

Mobility. SetMobilityModel (" ns3: : ConstantPositionMobilityModel ");//fixed position model
Mobility. Install (wifiApNode);

/* installation protocol stack */
InternetStackHelper stack;//used to IP/TCP/UDP function aggregation to the existing node
Stack. The Install (wifiApNode);
Stack. The Install (wifiStaNodes);

/* assigning device interface IP address */
Ipv4AddressHelper address;

Address. SetBase (" 10.1.1.0 ", "255.255.255.0");
Ipv4InterfaceContainer apInterfaces;//apInterfaces used to get the IP address of the ap node
ApInterfaces=address. Assign (apDevices);
Ipv4InterfaceContainer staInterfaces;//staInterfaces used to get the IP address of the sta node
StaInterfaces=address. Assign (staDevices);

/* create clien and server application */
UdpEchoServerHelper echoServer (9);//create a server port for 9

ApplicationContainer serverApps=echoServer. Install (wifiApNode. Get (0));//the server installed to ap node
ServerApps. Start (Seconds (1.0));//server in the first seconds, started in the program run
ServerApps. Stop (Seconds (10.0));//server end in 10 seconds programs run

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related