ns2相关学习——TCL脚本编写(2)
下面來學習更加復雜一點的TCL腳本的編寫
簡述:建立有4個節(jié)點的拓撲,其中一個節(jié)點作為路由器,用來將兩個節(jié)點發(fā)出的數(shù)據(jù)包轉(zhuǎn)發(fā)到第四個節(jié)點上面。
在這里將解釋將兩個節(jié)點的數(shù)據(jù)流區(qū)分開來的方法,展示如何去檢測一個隊列是否是滿的,以及一個數(shù)據(jù)包是如何被丟棄的。
1、建立拓撲
Tcl腳本大多是大同的,我們需要做的是在基礎上進行自己需要的修改。基礎代碼如下:
#Create a simulator object set ns [new Simulator]#Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf#Define a 'finish' procedure proc finish {} {global ns nf$ns flush-trace #Close the trace fileclose $nf #Execute nam on the trace fileexec nam out.nam &exit 0 } # Insert your own code for topology creation # and agent definitions, etc. here#Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish"#Run the simulation $ns run
做出修改如下:
① 添加四個節(jié)點:
set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]② 添加鏈接
$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail?其實上面這樣就可以了,但是,為了讓它更加美觀,在跑nam的時候不要太懵逼,我們需要變一下鏈接的位置,使用的語句如下: $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right
在改變的時候我們可以使用right, left, up, down,以及這些單詞的結(jié)合(結(jié)合方式:某-某)
2、添加事件
① 設置代理——建立起兩個CBR代理和一個NULL代理。CBR代理給n0和n1,n0、n1用于發(fā)送數(shù)據(jù),n3用于接收數(shù)據(jù)。
#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1set null0 [new Agent/Null] $ns attach-agent $n3 $null0② CBR代理和NULL代理進行連接
$ns connect $udp0 $null0 $ns connect $udp1 $null0③ 設置數(shù)據(jù)流開始的時間和結(jié)束的時間
$ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop"
3、區(qū)別流
因為有倆發(fā)送端,為了區(qū)別數(shù)據(jù)流的情況,我們來給他們加點顏色;
$udp0 set class_ 1 $udp1 set class_ 2 $ns color 1 Blue $ns color 2 Red然后這樣就udp0發(fā)出的數(shù)據(jù)流變藍色,另外一個變紅色。
4、查看鏈接的隊列
添加下面的語句來監(jiān)視鏈接的情況
$ns duplex-link-op $n2 $n3 queuePos 0.5為了得到相對公平的情況,我們可以更換使用隊列,
SQF(隨機公平排隊)這樣最后得到的是相對公平的結(jié)果
本節(jié)完完整代碼如下:
#Create a simulator object set ns [new Simulator]#Define different colors for data flows $ns color 1 Blue $ns color 2 Red#Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf#Define a 'finish' procedure proc finish {} {global ns nf$ns flush-trace #Close the trace fileclose $nf #Execute nam on the trace fileexec nam out.nam &exit 0 }#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]#Create links between the nodes $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms SFQ$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right#Monitor the queue for the link between node 2 and node 3 $ns duplex-link-op $n2 $n3 queuePos 0.5#Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $udp0 set class_ 1 $ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $udp1 set class_ 2 $ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1#Create a Null agent (a traffic sink) and attach it to node n3 set null0 [new Agent/Null] $ns attach-agent $n3 $null0#Connect the traffic sources with the traffic sink $ns connect $udp0 $null0 $ns connect $udp1 $null0#Schedule events for the CBR agents $ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish"#Run the simulation $ns run創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎
總結(jié)
以上是生活随笔為你收集整理的ns2相关学习——TCL脚本编写(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下environ环境变量操作函
- 下一篇: 大端小端模式判断以及数据转换