NS2相关学习——创建Xgraph的输出文件

来源:互联网 发布:烈焰功勋心法数据库端 编辑:程序博客网 时间:2024/06/05 16:32

经过前面学习代码的编写,这一部分,我们要学会如何进行分析,一个很直观的方式就是将结果图形化表示出来。

ns-allinone包的一部分是“xgraph”,一个绘图程序,可用于创建模拟结果的图形表示。 在本节中,将向您展示如何在Tcl脚本中创建可用作xgraph数据集的输出文件的简单方法。 在途中,还将向您展示如何使用流量生成器。(这里介绍的技术是创建适合xgraph的输出文件的许多可能方式之一)

1、创建拓扑图

首先,我们需要创建一个如下图的拓扑图。


代码如下所示:

set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]$ns duplex-link $n0 $n3 1Mb 100ms DropTail$ns duplex-link $n1 $n3 1Mb 100ms DropTail$ns duplex-link $n2 $n3 1Mb 100ms DropTail$ns duplex-link $n3 $n4 1Mb 100ms DropTail
我们将流量源附加到节点n0,n1和n2,但是首先我们编写一个程序,将流量源附加到节点n0,n1和n2
proc attach-expoo-traffic { node sink size burst idle rate } {#Get an instance of the simulatorset ns [Simulator instance]#Create a UDP agent and attach it to the nodeset source [new Agent/UDP]$ns attach-agent $node $source#Create an Expoo traffic agent and set its configuration parametersset traffic [new Application/Traffic/Exponential]$traffic set packetSize_ $size$traffic set burst_time_ $burst$traffic set idle_time_ $idle$traffic set rate_ $rate                # Attach traffic source to the traffic generator        $traffic attach-agent $source#Connect the source and the sink$ns connect $source $sinkreturn $traffic}
它有六个参数:一个节点,一个先前创建的流量信宿,流量源的数据包大小,突发和空闲时间(指数分布)和峰值速率。
首先,该过程创建流量源并将其附加到节点,然后创建一个Traffic / Expoo对象,设置其配置参数并将其附加到流量源,最后连接源和宿。 最后,该过程返回流量来源的句柄。 这个过程是一个很好的例子,可以处理如何将诸如将流量源附加到多个节点的任务。 现在我们使用这个过程将具有不同峰值速率的流量源连接到n0,n1和n2,并将它们连接到n4上的三个流量接收器,这些流量源必须首先创建:

set sink0 [new Agent/LossMonitor]set sink1 [new Agent/LossMonitor]set sink2 [new Agent/LossMonitor]$ns attach-agent $n4 $sink0$ns attach-agent $n4 $sink1$ns attach-agent $n4 $sink2set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
在这个例子中,我们使用Agent / LossMonitor对象作为流量接收器,因为它们存储接收的字节数,我们可以使用这个来计算带宽。
现在我们可以写出实际将数据写入输出文件的程序。

proc record {} {        global sink0 sink1 sink2 f0 f1 f2        #Get an instance of the simulator        set ns [Simulator instance]        #Set the time after which the procedure should be called again        set time 0.5        #How many bytes have been received by the traffic sinks?        set bw0 [$sink0 set bytes_]        set bw1 [$sink1 set bytes_]        set bw2 [$sink2 set bytes_]        #Get the current time        set now [$ns now]        #Calculate the bandwidth (in MBit/s) and write it to the files        puts $f0 "$now [expr $bw0/$time*8/1000000]"        puts $f1 "$now [expr $bw1/$time*8/1000000]"        puts $f2 "$now [expr $bw2/$time*8/1000000]"        #Reset the bytes_ values on the traffic sinks        $sink0 set bytes_ 0        $sink1 set bytes_ 0        $sink2 set bytes_ 0        #Re-schedule the procedure        $ns at [expr $now+$time] "record"}
此程序读取从三个流量接收器接收的字节数。 然后,它计算带宽(以MBit / s为单位),并将其与当前时间一起写入三个输出文件,然后重置流量汇点上的bytes_值。

2、运行模拟

首先,调用“record”过程,然后每0.5秒定期重新安排一次。 然后三个流量来源在10秒钟开始,并在50秒钟停止。 在60秒钟后,调用“finish”过程。完整的脚本代码如下:

#Create a simulator objectset ns [new Simulator]#Open the output filesset f0 [open out0.tr w]set f1 [open out1.tr w]set f2 [open out2.tr w]#Create 5 nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]set n4 [$ns node]#Connect the nodes$ns duplex-link $n0 $n3 1Mb 100ms DropTail$ns duplex-link $n1 $n3 1Mb 100ms DropTail$ns duplex-link $n2 $n3 1Mb 100ms DropTail$ns duplex-link $n3 $n4 1Mb 100ms DropTail#Define a 'finish' procedureproc finish {} {global f0 f1 f2#Close the output filesclose $f0close $f1close $f2#Call xgraph to display the resultsexec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &        exit 0}#Define a procedure that attaches a UDP agent to a previously created node#'node' and attaches an Expoo traffic generator to the agent with the#characteristic values 'size' for packet size 'burst' for burst time,#'idle' for idle time and 'rate' for burst peak rate. The procedure connects#the source with the previously defined traffic sink 'sink' and returns the#source object.proc attach-expoo-traffic { node sink size burst idle rate } {#Get an instance of the simulatorset ns [Simulator instance]#Create a UDP agent and attach it to the nodeset source [new Agent/UDP]$ns attach-agent $node $source#Create an Expoo traffic agent and set its configuration parametersset traffic [new Application/Traffic/Exponential]$traffic set packetSize_ $size$traffic set burst_time_ $burst$traffic set idle_time_ $idle$traffic set rate_ $rate                # Attach traffic source to the traffic generator        $traffic attach-agent $source#Connect the source and the sink$ns connect $source $sinkreturn $traffic}#Define a procedure which periodically records the bandwidth received by the#three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.proc record {} {        global sink0 sink1 sink2 f0 f1 f2#Get an instance of the simulatorset ns [Simulator instance]#Set the time after which the procedure should be called again        set time 0.5#How many bytes have been received by the traffic sinks?        set bw0 [$sink0 set bytes_]        set bw1 [$sink1 set bytes_]        set bw2 [$sink2 set bytes_]#Get the current time        set now [$ns now]#Calculate the bandwidth (in MBit/s) and write it to the files        puts $f0 "$now [expr $bw0/$time*8/1000000]"        puts $f1 "$now [expr $bw1/$time*8/1000000]"        puts $f2 "$now [expr $bw2/$time*8/1000000]"#Reset the bytes_ values on the traffic sinks        $sink0 set bytes_ 0        $sink1 set bytes_ 0        $sink2 set bytes_ 0#Re-schedule the procedure        $ns at [expr $now+$time] "record"}#Create three traffic sinks and attach them to the node n4set sink0 [new Agent/LossMonitor]set sink1 [new Agent/LossMonitor]set sink2 [new Agent/LossMonitor]$ns attach-agent $n4 $sink0$ns attach-agent $n4 $sink1$ns attach-agent $n4 $sink2#Create three traffic sourcesset source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]#Start logging the received bandwidth$ns at 0.0 "record"#Start the traffic sources$ns at 10.0 "$source0 start"$ns at 10.0 "$source1 start"$ns at 10.0 "$source2 start"#Stop the traffic sources$ns at 50.0 "$source0 stop"$ns at 50.0 "$source1 stop"$ns at 50.0 "$source2 stop"#Call the finish procedure after 60 seconds simulation time$ns at 60.0 "finish"#Run the simulation$ns run

链接:http://www.isi.edu/nsnam/ns/tutorial/



0 0