日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/8/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NS2相关学习——创建Xgraph的输出文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

經(jīng)過前面學(xué)習(xí)代碼的編寫,這一部分,我們要學(xué)會(huì)如何進(jìn)行分析,一個(gè)很直觀的方式就是將結(jié)果圖形化表示出來。

ns-allinone包的一部分是“xgraph”,一個(gè)繪圖程序,可用于創(chuàng)建模擬結(jié)果的圖形表示。 在本節(jié)中,將向您展示如何在Tcl腳本中創(chuàng)建可用作xgraph數(shù)據(jù)集的輸出文件的簡單方法。 在途中,還將向您展示如何使用流量生成器。(這里介紹的技術(shù)是創(chuàng)建適合xgraph的輸出文件的許多可能方式之一)

1、創(chuàng)建拓?fù)鋱D

首先,我們需要?jiǎng)?chuàng)建一個(gè)如下圖的拓?fù)鋱D。


代碼如下所示:

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我們將流量源附加到節(jié)點(diǎn)n0,n1和n2,但是首先我們編寫一個(gè)程序,將流量源附加到節(jié)點(diǎn)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 }它有六個(gè)參數(shù):一個(gè)節(jié)點(diǎn),一個(gè)先前創(chuàng)建的流量信宿,流量源的數(shù)據(jù)包大小,突發(fā)和空閑時(shí)間(指數(shù)分布)和峰值速率。
首先,該過程創(chuàng)建流量源并將其附加到節(jié)點(diǎn),然后創(chuàng)建一個(gè)Traffic / Expoo對(duì)象,設(shè)置其配置參數(shù)并將其附加到流量源,最后連接源和宿。 最后,該過程返回流量來源的句柄。 這個(gè)過程是一個(gè)很好的例子,可以處理如何將諸如將流量源附加到多個(gè)節(jié)點(diǎn)的任務(wù)。 現(xiàn)在我們使用這個(gè)過程將具有不同峰值速率的流量源連接到n0,n1和n2,并將它們連接到n4上的三個(gè)流量接收器,這些流量源必須首先創(chuàng)建:

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]在這個(gè)例子中,我們使用Agent / LossMonitor對(duì)象作為流量接收器,因?yàn)樗鼈兇鎯?chǔ)接收的字節(jié)數(shù),我們可以使用這個(gè)來計(jì)算帶寬。
現(xiàn)在我們可以寫出實(shí)際將數(shù)據(jù)寫入輸出文件的程序。

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 againset 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 timeset now [$ns now]#Calculate the bandwidth (in MBit/s) and write it to the filesputs $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" }此程序讀取從三個(gè)流量接收器接收的字節(jié)數(shù)。 然后,它計(jì)算帶寬(以MBit / s為單位),并將其與當(dāng)前時(shí)間一起寫入三個(gè)輸出文件,然后重置流量匯點(diǎn)上的bytes_值。

2、運(yùn)行模擬

首先,調(diào)用“record”過程,然后每0.5秒定期重新安排一次。 然后三個(gè)流量來源在10秒鐘開始,并在50秒鐘停止。 在60秒鐘后,調(diào)用“finish”過程。完整的腳本代碼如下:

#Create a simulator object set ns [new Simulator]#Open the output files set f0 [open out0.tr w] set f1 [open out1.tr w] set f2 [open out2.tr w]#Create 5 nodes set 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' procedure proc 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 againset 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 timeset now [$ns now]#Calculate the bandwidth (in MBit/s) and write it to the filesputs $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 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 $sink2#Create three traffic sources set 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/



總結(jié)

以上是生活随笔為你收集整理的NS2相关学习——创建Xgraph的输出文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。