Some questions about using simulator.Schedule fuction in python.

99 views
Skip to first unread message

weilin zhong

unread,
Aug 22, 2018, 3:22:49 PM8/22/18
to ns-3-users
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()

Screenshot from 2018-08-22 20-18-13.png


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.

Best,
Weilin

Tom Henderson

unread,
Aug 22, 2018, 5:07:30 PM8/22/18
to ns-3-...@googlegroups.com
> Screenshot from 2018-08-22 20-18-13.png <about:invalid#zClosurez>
>
>
> 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.
>

Just call 'Start' once when you want it to start sending, and 'Stop'
when you want it to stop. The interval between packets is set by
attributes in the UdpEchoClient class; have a look at the 'Interval'
attribute. There is no need to keep scheduling 'Start' repeatedly.

weilin zhong

unread,
Aug 23, 2018, 12:43:11 PM8/23/18
to ns-3-users
Hi Tom,

Many thanks for your reply. I've tried the way you've mentioned and it can send multiple packets in a special interval time. But if I need to set a variable interval or packetsize (eg. send 1024 Bytes packet start in 1s, send 500 Bytes packet start in 5s, then send 800 Bytes packet start in 6s...), I think I still need to use the schedule function. I've checked the mailing list and find many ways to use schedule, but I don't know how to wrap my code into a function correctly (as you've mentioned that just call 'start' once) and then schedule it. As I want to make us3 co-simulate with another process, can you give me some tips?

Another question is that I want to get the end-to-end delay in any time I want. In other words, just assume ns3 as a black box, I want to input a time and get the output delay in this time. Is that any method to achieve this or which class/attribute I have to read?

Thank you very much again!

Best,
Weilin
Reply all
Reply to author
Forward
0 new messages