Make more Python examples use new modular bindings

This commit is contained in:
Mitch Watrous
2011-05-03 14:00:47 -07:00
parent 3a43783963
commit d9f9c171e0
4 changed files with 73 additions and 58 deletions

View File

@@ -23,49 +23,52 @@
# - DropTail queues
# - Tracing of queues and packet receptions to file "udp-echo.tr"
import ns3
import ns.applications
import ns.core
import ns.csma
import ns.internet
import ns.network
def main(argv):
#
# Allow the user to override any of the defaults and the above Bind() at
# run-time, via command-line arguments
#
cmd = ns3.CommandLine()
cmd = ns.core.CommandLine()
cmd.Parse(argv)
#
# But since this is a realtime script, don't allow the user to mess with
# that.
#
ns3.GlobalValue.Bind("SimulatorImplementationType", ns3.StringValue("ns3::RealtimeSimulatorImpl"))
ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))
#
# Explicitly create the nodes required by the topology (shown above).
#
print "Create nodes."
n = ns3.NodeContainer()
n = ns.network.NodeContainer()
n.Create(4)
internet = ns3.InternetStackHelper()
internet = ns.internet.InternetStackHelper()
internet.Install(n)
#
# Explicitly create the channels required by the topology (shown above).
#
print ("Create channels.")
csma = ns3.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(5000000)))
csma.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(2)));
csma.SetDeviceAttribute("Mtu", ns3.UintegerValue(1400))
csma = ns.csma.CsmaHelper()
csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
d = csma.Install(n)
#
# We've got the "hardware" in place. Now we need to add IP addresses.
#
print ("Assign IP Addresses.")
ipv4 = ns3.Ipv4AddressHelper()
ipv4.SetBase(ns3.Ipv4Address("10.1.1.0"), ns3.Ipv4Mask("255.255.255.0"))
ipv4 = ns.internet.Ipv4AddressHelper()
ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
i = ipv4.Assign(d)
print ("Create Applications.")
@@ -74,10 +77,10 @@ def main(argv):
# Create a UdpEchoServer application on node one.
#
port = 9 # well-known echo port number
server = ns3.UdpEchoServerHelper(port)
server = ns.applications.UdpEchoServerHelper(port)
apps = server.Install(n.Get(1))
apps.Start(ns3.Seconds(1.0))
apps.Stop(ns3.Seconds(10.0))
apps.Start(ns.core.Seconds(1.0))
apps.Stop(ns.core.Seconds(10.0))
#
# Create a UdpEchoClient application to send UDP datagrams from node zero to
@@ -85,16 +88,16 @@ def main(argv):
#
packetSize = 1024
maxPacketCount = 500
interPacketInterval = ns3.Seconds(0.01)
client = ns3.UdpEchoClientHelper(i.GetAddress (1), port)
client.SetAttribute("MaxPackets", ns3.UintegerValue(maxPacketCount))
client.SetAttribute("Interval", ns3.TimeValue(interPacketInterval))
client.SetAttribute("PacketSize", ns3.UintegerValue(packetSize))
interPacketInterval = ns.core.Seconds(0.01)
client = ns.applications.UdpEchoClientHelper(i.GetAddress (1), port)
client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
apps = client.Install(n.Get(0))
apps.Start(ns3.Seconds(2.0))
apps.Stop(ns3.Seconds(10.0))
apps.Start(ns.core.Seconds(2.0))
apps.Stop(ns.core.Seconds(10.0))
ascii = ns3.AsciiTraceHelper()
ascii = ns.network.AsciiTraceHelper()
csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
csma.EnablePcapAll("realtime-udp-echo", False)
@@ -102,8 +105,8 @@ def main(argv):
# Now, do the actual simulation.
#
print ("Run Simulation.")
ns3.Simulator.Run()
ns3.Simulator.Destroy()
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
print ("Done.")
if __name__ == '__main__':

View File

@@ -3,3 +3,5 @@
def build(bld):
obj = bld.create_ns3_program('realtime-udp-echo', ['csma', 'internet'])
obj.source = 'realtime-udp-echo.cc'
bld.register_ns3_script('realtime-udp-echo.py', ['csma', 'internet', 'applications'])

View File

@@ -17,7 +17,12 @@
#
import sys
import ns3
import ns.core
import ns.csma
import ns.internet
import ns.network
import ns.tap_bridge
def main(argv):
#
@@ -25,15 +30,15 @@ def main(argv):
# interact in real-time and therefore we have to use the real-time simulator
# and take the time to calculate checksums.
#
ns3.GlobalValue.Bind("SimulatorImplementationType", ns3.StringValue("ns3::RealtimeSimulatorImpl"))
ns3.GlobalValue.Bind("ChecksumEnabled", ns3.BooleanValue("true"))
ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))
ns.core.GlobalValue.Bind("ChecksumEnabled", ns.core.BooleanValue("true"))
#
# Create two ghost nodes. The first will represent the virtual machine host
# on the left side of the network; and the second will represent the VM on
# the right side.
#
nodes = ns3.NodeContainer()
nodes = ns.network.NodeContainer()
nodes.Create (2)
#
@@ -41,7 +46,7 @@ def main(argv):
# devices installed on both of the nodes. The data rate and delay for the
# channel can be set through the command-line parser.
#
csma = ns3.CsmaHelper()
csma = ns.csma.CsmaHelper()
devices = csma.Install(nodes)
#
@@ -52,24 +57,24 @@ def main(argv):
# only see traffic from one other device on that bridge. That is the case
# for this configuration.
#
tapBridge = ns3.TapBridgeHelper()
tapBridge.SetAttribute ("Mode", ns3.StringValue ("UseLocal"))
tapBridge.SetAttribute ("DeviceName", ns3.StringValue ("tap-left"))
tapBridge = ns.tap_bridge.TapBridgeHelper()
tapBridge.SetAttribute ("Mode", ns.core.StringValue ("UseLocal"))
tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-left"))
tapBridge.Install (nodes.Get (0), devices.Get (0))
#
# Connect the right side tap to the right side wifi device on the right-side
# ghost node.
#
tapBridge.SetAttribute ("DeviceName", ns3.StringValue ("tap-right"))
tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-right"))
tapBridge.Install (nodes.Get (1), devices.Get (1))
#
# Run the simulation for ten minutes to give the user time to play around
#
ns3.Simulator.Stop (ns3.Seconds (600))
ns3.Simulator.Run(signal_check_frequency = -1)
ns3.Simulator.Destroy()
ns.core.Simulator.Stop (ns.core.Seconds (600))
ns.core.Simulator.Run(signal_check_frequency = -1)
ns.core.Simulator.Destroy()
return 0
if __name__ == '__main__':

View File

@@ -17,7 +17,12 @@
#
import sys
import ns3
import ns.core
import ns.internet
import ns.mobility
import ns.network
import ns.tap_bridge
import ns.wifi
def main(argv):
#
@@ -25,35 +30,35 @@ def main(argv):
# interact in real-time and therefore we have to use the real-time simulator
# and take the time to calculate checksums.
#
ns3.GlobalValue.Bind("SimulatorImplementationType", ns3.StringValue("ns3::RealtimeSimulatorImpl"))
ns3.GlobalValue.Bind("ChecksumEnabled", ns3.BooleanValue("true"))
ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))
ns.core.GlobalValue.Bind("ChecksumEnabled", ns.core.BooleanValue("true"))
#
# Create two ghost nodes. The first will represent the virtual machine host
# on the left side of the network; and the second will represent the VM on
# the right side.
#
nodes = ns3.NodeContainer()
nodes = ns.network.NodeContainer()
nodes.Create (2);
#
# We're going to use 802.11 A so set up a wifi helper to reflect that.
#
wifi = ns3.WifiHelper.Default()
wifi.SetStandard (ns3.WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", ns3.StringValue ("OfdmRate54Mbps"));
wifi = ns.wifi.WifiHelper.Default()
wifi.SetStandard (ns.wifi.WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", ns.core.StringValue ("OfdmRate54Mbps"));
#
# No reason for pesky access points, so we'll use an ad-hoc network.
#
wifiMac = ns3.NqosWifiMacHelper.Default()
wifiMac = ns.wifi.NqosWifiMacHelper.Default()
wifiMac.SetType ("ns3::AdhocWifiMac");
#
# Configure the physcial layer.
#
wifiChannel = ns3.YansWifiChannelHelper.Default()
wifiPhy = ns3.YansWifiPhyHelper.Default()
wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
wifiPhy.SetChannel(wifiChannel.Create())
#
@@ -65,10 +70,10 @@ def main(argv):
# We need location information since we are talking about wifi, so add a
# constant position to the ghost nodes.
#
mobility = ns3.MobilityHelper()
positionAlloc = ns3.ListPositionAllocator()
positionAlloc.Add(ns3.Vector(0.0, 0.0, 0.0))
positionAlloc.Add(ns3.Vector(5.0, 0.0, 0.0))
mobility = ns.mobility.MobilityHelper()
positionAlloc = ns.mobility.ListPositionAllocator()
positionAlloc.Add(ns.core.Vector(0.0, 0.0, 0.0))
positionAlloc.Add(ns.core.Vector(5.0, 0.0, 0.0))
mobility.SetPositionAllocator(positionAlloc)
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel")
mobility.Install(nodes)
@@ -81,24 +86,24 @@ def main(argv):
# only see traffic from one other device on that bridge. That is the case
# for this configuration.
#
tapBridge = ns3.TapBridgeHelper()
tapBridge.SetAttribute ("Mode", ns3.StringValue ("UseLocal"));
tapBridge.SetAttribute ("DeviceName", ns3.StringValue ("tap-left"));
tapBridge = ns.tap_bridge.TapBridgeHelper()
tapBridge.SetAttribute ("Mode", ns.core.StringValue ("UseLocal"));
tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-left"));
tapBridge.Install (nodes.Get (0), devices.Get (0));
#
# Connect the right side tap to the right side wifi device on the right-side
# ghost node.
#
tapBridge.SetAttribute ("DeviceName", ns3.StringValue ("tap-right"));
tapBridge.SetAttribute ("DeviceName", ns.core.StringValue ("tap-right"));
tapBridge.Install (nodes.Get (1), devices.Get (1));
#
# Run the simulation for ten minutes to give the user time to play around
#
ns3.Simulator.Stop (ns3.Seconds (600));
ns3.Simulator.Run(signal_check_frequency = -1)
ns3.Simulator.Destroy()
ns.core.Simulator.Stop (ns.core.Seconds (600));
ns.core.Simulator.Run(signal_check_frequency = -1)
ns.core.Simulator.Destroy()
return 0
if __name__ == '__main__':