Hi all. I tried to schedule the UdpEchoClient that send the message to the server at a particular time, eg: every 1 second. But I can't get the results I want. Here is my code and output:
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
import ns.flow_monitor
import random
import xml.dom.minidom
import sys
class MyModel(object):
pass
# Enable Logging
ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
# Set Nodes
nodes = ns.network.NodeContainer()
nodes.Create(2)
# Set devices and channel
pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
devices = pointToPoint.Install(nodes)
stack = ns.internet.InternetStackHelper()
stack.Install(nodes)
# Set IP address
address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"),
ns.network.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign(devices)
# Set application
echoServer = ns.applications.UdpEchoServerHelper(9)
serverApps = echoServer.Install(nodes.Get(1))
serverApps.Start(ns.core.Seconds(0.0))
serverApps.Stop(ns.core.Seconds(10.0))
PacketSize = random.randint(46,1500)
echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.MilliSeconds(1000)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(PacketSize))
clientApps = echoClient.Install(nodes.Get(0))
def ClientStart(model):
clientApps.Start(ns.core.Seconds(0.0))
clientApps.Stop(ns.core.Seconds(10.0))
model = MyModel()
for t in range(5):
ns.core.Simulator.Schedule(ns.core.Seconds(t), ClientStart, model)
# Enable tracing
# pointToPoint.EnablePcapAll("first3")
# Enable flow monitor
flowmon_helper = ns.flow_monitor.FlowMonitorHelper()
monitor = flowmon_helper.InstallAll()
monitor = flowmon_helper.GetMonitor()
# anim = ns.netanim.AnimationInterface('first3py.xml')
# Should add stop befort run
ns.core.Simulator.Stop(ns.core.Seconds(10.0))
ns.core.Simulator.Run()
# Print the results
print ('The PacketSize is: ', PacketSize)
def print_stats(os, st):
print (" Lost Packets: ", st.lostPackets, file=os)
print (" Tx Bytes: ", st.txBytes, file=os)
print (" Rx Bytes: ", st.rxBytes, file=os)
print (" Tx Packets: ", st.txPackets, file=os)
print (" Rx Packets: ", st.rxPackets, file=os)
print (" DelaySum: ", st.delaySum.GetSeconds(),"s", file=os)
monitor.CheckForLostPackets()
classifier = flowmon_helper.GetClassifier()
for flow_id, flow_stats in monitor.GetFlowStats():
t = classifier.FindFlow(flow_id)
proto = {6: 'TCP', 17: 'UDP'} [t.protocol]
print ("FlowID: %i (%s %s/%s --> %s/%i)" % \
(flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort))
print_stats(sys.stdout, flow_stats)
print ('The delay Sum of Node', flow_id,'is: ', flow_stats.delaySum.GetSeconds(),'s')
print ('The lost of Packets in Node',flow_id,'are: ', flow_stats.lostPackets)
# get delay and packet lost
# print ('The delay Sum is: ', flow_stats.delaySum.GetSeconds(),'s')
# print ('The lost of Packets are: ', flow_stats.lostPackets)
ns.core.Simulator.Destroy()
I'm sure there must be something wrong in my way to schedule the event because I got the same output when I'd changed the start time of clientApps from 0 to 5 seconds. But I don't know to solve this problem. Can anybody help me? Thank you very much.