#Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 0 blue $ns color 1 red $ns color 2 white #Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] # set trace file(預先定義) set f [open out.tr w] $ns trace-all $f set nf [open out.nam w] #Open the NAM trace file $ns namtrace-all $nf #Create links between the nodes (雙向連接) $ns duplex-link $n0 $n2 5Mb 2ms DropTail $ns duplex-link $n1 $n2 5Mb 2ms DropTail $ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
它有點相似於FIFO(先入先出)的存儲方式。Drop Tail最大的優勢是原理簡單。node
#Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10 #設置n2與n3之間鏈路上隊列緩衝區的最大值爲10
#Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-up # n0在n2的下方45° $ns duplex-link-op $n1 $n2 orient right-down # n1在n2的上方45° $ns duplex-link-op $n2 $n3 orient right # n2的右邊是n3 #隊列位置定義了隊列與水平的夾角(水平線如下度數爲正) #right-up 45 #right-down -45 #right 0 右
#Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5 #duplex-link-op 設置雙工鏈路屬性
1. **queuePos 0.5表示包從上到下進入隊列** (上圖即爲從上到下) 2. queuePos 0表示包從右到左進入隊列 3. queuePos 1表示包從左到右進入隊列 4. queuePos 1.5表示包從下到上進入隊列 5. queuePos 2==queuePos 0
所以queuePos N表示包進入隊列時的角度,角度 =(N×π) % 2π緩存
set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp0 set udp1 [new Agent/UDP] $ns attach-agent $n3 $udp1 $udp1 set class_ 1 #udp1的流量是紅色 set cbr1 [new Application/Traffic/CBR] $cbr1 attach-agent $udp1 set null0 [new Agent/Null] #這是接收器(sink) $ns attach-agent $n3 $null0 set null1 [new Agent/Null] #也是接收器(sink) $ns attach-agent $n1 $null1 $ns connect $udp0 $null0 $ns connect $udp1 $null1 $ns at 1.0 "$cbr0 start" $ns at 1.1 "$cbr1 start"
time=1.0
網絡
time=1.14
tcp
#Setup a TCP connection set tcp [new Agent/TCP] #建立一個tcp代理 $tcp set class_ 2 #設置tcp流的顏色爲白色(n0) set sink [new Agent/TCPSink] #Sink:接收器(n3) $ns attach-agent $n0 $tcp $ns attach-agent $n3 $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 1.2 "$ftp start"
t=1.23
ide
$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#把節點和代理分離開,把代理重置爲空代理 puts [$cbr0 set packetSize_] #在命令行打印cbr0產生包恆定的大小 puts [$cbr0 set interval_] #在命令行打印cbr0包之間的間隔 #這個模擬程序會執行3s $ns at 3.0 "finish" #Define a 'finish' procedure proc finish {} { global ns f nf $ns flush-trace close $f close $nf #Close the NAM trace file puts "running nam..." exec nam out.nam & #Execute NAM on the trace file exit 0 } #即關閉全部nam文件,將out.nam以可視化方式實現