2008年8月29日 星期五

ns2 - Link的基本指令



透過ns-2來模擬上面的網路情況
simple.tcl

set ns [new Simulator]
;# 在檔案一開始會先建立一個的模擬
;# step1:先建立一個Simulator物件
;# step2:把ns變數設定為物件:set ns 物件

$ns color 1 Blue
;# 定義color index 1=> Blue
$ns color 2 Red
;# 定義color index 2=> Red

set nf [open out.nam w]
;# 開啟一個可寫入的檔案out.nam
;# step1:先執行open out.nam w,並傳回這一個物件
;# step2:設定nf為記錄這一個物件

$ns namtrace-all $nf
;# 將所有模擬的資料都寫入$nf這個檔案中

;# 新增一個procedure "finish"
proc finish {} {
  global ns nf
  ;# 宣告ns與nf為在外面定義過的變數
  $ns flush-trace
  ;# 把trace的結果寫入檔案中
  close $nf
  ;# 關掉檔案
  exec nam out.nam &
  ;# 在背景中,透過nam去執行out.nam
  exit 0
  ;# 離開
}

#建立4個Node
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
;# 設定n0為一個node
;# step1:$ns node傳回一個node物件
;# step2:設定n0為這一個傳回的物件

#建立三個link
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
#; node 0連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#; node 1連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
#; node 2連到node 3 頻寬為1Mb,delay time為10ms 為SFQ

#設定link的屬性
$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
;# 觀測n2到n3之間queue的變化,這是要給NAM用的
;# 也就是說只查看安置由n2到n3的封包Queue的情況

set udp0 [new Agent/UDP]
;# 新增一個UDP agent
$udp0 set class_ 1
;# 將$upd1顏色設成1 (Blue)
$ns attach-agent $n0 $udp0
;# $n0架構在$udp0上

set cbr0 [new Application/Traffic/CBR]
;# 建立應用層的流量描述
$cbr0 set packetSize_ 500
;# 設定封包大小
$cbr0 set interval_ 0.005
;# 設定封包間隔時間
$cbr0 attach-agent $udp0
;# 依附在UDP agent上

set udp1 [new Agent/UDP]
$udp1 set class_ 2
;# 將$upd1顏色設成2 (Red)
$ns attach-agent $n1 $udp1

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null0 [new Agent/Null]
;# 建立一個Null Agent
$ns attach-agent $n3 $null0
;# 將$n3架構在$null0

$ns connect $udp0 $null0
;# 把agent $udp0和agent $null0連在一起
$ns connect $udp1 $null0
;# 把agent $udp1和agent $null0連在一起

$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"

$ns run


注意在attach-agent的時候,有時候會不確定哪一個在前面,哪一個在後面~
反正,目前就這樣記,udp、tcp的份量比較大,放在後面

沒有留言: