NS3 Network Simulation (7): Wifi node, ns3wifi
Happy shrimp
Http://blog.csdn.net/lights_joy/
Reprinted, but keep the author information
In the previous section, we simulate a bus-type network. This section tries to convert the n0 in the previous section into a Wireless AP and then connect several nodes. This is also the third. cc example in NS3, which is implemented in Python.
// Default Network Topology//// Wifi 10.1.3.0// AP// * * * *// | | | | 10.1.1.0// n5 n6 n7 n0 -------------- n1 n2 n3 n4// point-to-point | | | |// ================// LAN 10.1.2.0
As in the previous section, first construct a p2p network and then build a bus network cable:
# Construct a point-to-point connection p2pNodes = ns. network. nodeContainer () p2pNodes. create (2) pointToPoint = ns. point_to_point.PointToPointHelper () pointToPoint. setDeviceAttribute ("DataRate", ns. core. stringValue ("5 Mbps") pointToPoint. setChannelAttribute ("Delay", ns. core. stringValue ("2 ms") p2pDevices = pointToPoint. install (p2pNodes) # Build a bus connection nCsma = 3 csmaNodes = ns. network. nodeContainer () csmaNodes. add (p2pNodes. get (1) csmaNodes. create (nCsma) csma = ns. csma. csmaHelper () csma. setChannelAttribute ("DataRate", ns. core. stringValue ("100 Mbps") csma. setChannelAttribute ("Delay", ns. core. timeValue (ns. core. nanoSeconds (6560) csal evices = csma. install (csmaNodes)
Next we will build a wireless network:
# Build a Wi-Fi connection nWifi = 3 wifiStaNodes = ns. network. nodeContainer () wifiStaNodes. create (nWifi) wifiApNode = p2pNodes. get (0) channel = ns. wifi. yansWifiChannelHelper. default () phy = ns. wifi. yansWifiPhyHelper. default () phy. setChannel (channel. create ())
Configure the AP:
# Configure APwifi = ns. wifi. wifiHelper. default () wifi. setRemoteStationManager ("ns3: javasfwifimanager") mac = ns. wifi. nqosWifiMacHelper. default () ssid = ns. wifi. ssid ("ns-3-ssid") mac. setType ("ns3: StaWifiMac", "Ssid", ns. wifi. ssidValue (ssid), "ActiveProbing", ns. core. booleanValue (False) staDevices = wifi. install (phy, mac, wifiStaNodes) mac. setType ("ns3: ApWifiMac", "Ssid", ns. wifi. ssidValue (ssid) apDevices = wifi. install (phy, mac, wifiApNode );
Configure the location parameters of the wireless node:
# Configure the location of the wireless node mobility = ns. mobility. equalityhelper () mobility. setPositionAllocator ("ns3: GridPositionAllocator", "MinX", ns. core. doublevalues (0.0), "MinY", ns. core. doubleValue (0.0), "DeltaX", ns. core. doubleValue (5.0), "DeltaY", ns. core. doubleValue (10.0), "GridWidth", ns. core. uintegerValue (3), "LayoutType", ns. core. stringValue ("RowFirst") mobility. setequalitymodel ("ns3: random=2dequalitymodel", "Bounds", ns. mobility. rectangleValue (ns. mobility. rectangle (-50, 50,-50, 50) mobility. install (wifiStaNodes) mobility. setequalitymodel ("ns3: constantpositionequalitymodel") mobility. install (wifiApNode)
Then install the Protocol Stack:
# Install protocol stack = ns. internet. InternetStackHelper () stack. Install (csmaNodes) stack. Install (wifiApNode) stack. Install (wifiStaNodes)
Configure the IP address. This is the same as that in the previous section. It only adds the 10.1.3.0 CIDR Block:
# Configure IPaddress = ns. internet. ipv4AddressHelper () address. setBase (ns. network. listen 4address ("10.1.1.0"), ns. network. ipv4Mask ("255.255.255.0") p2pInterfaces = address. assign (p2pDevices) address. setBase (ns. network. listen 4address ("10.1.2.0"), ns. network. ipv4Mask ("255.255.255.0") csmaInterfaces = address. assign (capplies evices) address. setBase (ns. network. listen 4address ("10.1.3.0"), ns. network. ipv4Mask ("255.255.255.0") address. assign (staDevices) address. assign (apDevices)
Next, simulate an Echo service. This is the same as the previous section, but the Client is installed on a wi-fi node.
# Configure the application echoServer = ns. applications. udpEchoServerHelper (9) serverApps = echoServer. install (csmaNodes. get (nCsma) serverApps. start (ns. core. seconds (1.0) serverApps. stop (ns. core. seconds (20.0) echoClient = ns. applications. udpEchoClientHelper (csmaInterfaces. getAddress (nCsma), 9) echoClient. setAttribute ("MaxPackets", ns. core. uintegerValue (5) echoClient. setAttribute ("Interval", ns. core. timeValue (ns. core. seconds (1.0) echoClient. setAttribute ("PacketSize", ns. core. uintegerValue (1024) clientApps = echoClient. install (wifiStaNodes. get (nWifi-1) clientApps. start (ns. core. seconds (2.0) clientApps. stop (ns. core. seconds (20.0 ))
The next part is almost identical to the previous section, but the Simulator. Stop is added, because without this function call, the Simulator. Run will never Stop:
# The global routing manager creates a route table ns for each node based on the link announcement generated by the node. internet. ipv4GlobalRoutingHelper. populateRoutingTables () ns. core. simulator. stop (ns. core. seconds (10.0); pointToPoint. enablePcapAll ("third"); csma. enablePcap ("third", csal evices. get (1), True) phy. enablePcap ("third", apDevices. get (0) anim = ns. netanim. animationInterface ('Third. xml ') anim. setConstantPosition (p2pNodes. get (0), 10, 10) anim. setConstantPosition (csmaNodes. get (0), 30, 10) anim. setConstantPosition (csmaNodes. get (1), 40, 10) anim. setConstantPosition (csmaNodes. get (2), 50, 10) anim. setConstantPosition (csmaNodes. get (3), 60, 10) anim. enablePacketMetadata (True) # Start simulation ns. core. simulator. run () ns. core. simulator. destroy ()
Check the simulation results displayed by NetAnim:
Let's look at the third-0-1.pcap content:
As we wish, the 802.11 protocol, haha ~~~~~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.