<?xml version="1.0" encoding="Big5"?>
<?xml-stylesheet type="text/css" href="document.css"?>
<document xmlns:HTML="http://www.w3.org/1999/xhtml">
	<title>
	NS2 簡單模擬範例
	</title>
	<content>
	此範例的目的為了產生四個點, 網路拓樸如下 : 
	<HTML:BR/>
	<HTML:IMG src="graph/ns_example_1.PNG"/>
	<HTML:BR/>
	Node1 是 CBR, Node0 是 FTP, 分別經過 Node2 送資料給 Node3, 程式碼 
	<HTML:A href="http://nile.wpi.edu/NS/Example/ns-simple.tcl">ns-simple.tcl</HTML:A>
	內容如下 : 
	<HTML:HR width="80%"/>
		<HTML:OL>
		<item><HTML:FONT color="green"># 首先先建立一個NS2模擬的物件</HTML:FONT></item>
		<item><HTML:B>set ns [new Simulator]</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 定義兩種顏色，以便到時候再nam模擬時，能依顏色看的出來封包的流向
		</HTML:FONT></item>
		<item><HTML:B>$ns color 1 Blue</HTML:B></item>
		<item><HTML:B>$ns color 2 Red</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 如同前面語法所介紹的，建立一個nam所能執行的trace-file
		</HTML:FONT></item>
		<item><HTML:B>set nf [open out.nam w]</HTML:B></item>
		<item><HTML:B>$ns namtrace-all $nf</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 定義一個叫做 'finish' 的 procedure
	   	</HTML:FONT></item>	
		<item><HTML:B>proc finish {} {</HTML:B></item>
			<HTML:OL>
				<item><HTML:B>global ns nf</HTML:B></item>
				<item><HTML:B>$ns flush-trace</HTML:B></item>
				<item><HTML:FONT color="green"># Close the NAM trace file</HTML:FONT></item>
				<item><HTML:B>close $nf</HTML:B></item>
				<item><HTML:FONT color="green"># Execute NAM on the trace file</HTML:FONT></item>
				<item><HTML:B>exec nam out.nam &amp; </HTML:B></item>
				<item><HTML:B>exit 0</HTML:B></item>
			</HTML:OL>
		<item><HTML:B>}</HTML:B></item>	
		<HTML:BR/>
		<item><HTML:FONT color="green"># 建立四個點</HTML:FONT></item>
		<item><HTML:B>set n0 [$ns node]</HTML:B></item>
		<item><HTML:B>set n1 [$ns node]</HTML:B></item>
		<item><HTML:B>set n2 [$ns node]</HTML:B></item>
		<item><HTML:B>set n3 [$ns node]</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 創造點和點之間的連結</HTML:FONT></item>
		<item><HTML:B>$ns duplex-link $n0 $n2 2Mb 10ms DropTail</HTML:B></item>
		<item><HTML:B>$ns duplex-link $n1 $n2 2Mb 10ms DropTail</HTML:B></item>
		<item><HTML:B>$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 設定在nam中每個點所在的相關位置</HTML:FONT></item>
		<item><HTML:B>$ns duplex-link-op $n0 $n2 orient right-down</HTML:B></item>
		<item><HTML:B>$ns duplex-link-op $n1 $n2 orient right-up</HTML:B></item>
		<item><HTML:B>$ns duplex-link-op $n2 $n3 orient right</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 產生一個 TCP Agent 以及 TCPSink Agent(接收端)並作連結
		</HTML:FONT></item>
		<item><HTML:B>set tcp [new Agent/TCP]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n0 $tcp</HTML:B></item>
		<item><HTML:B>set sink [new Agent/TCPSink]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n3 $sink</HTML:B></item>
		<item><HTML:B>$ns connect $tcp $sink</HTML:B></item>
		<item><HTML:B>$tcp set fid_ 1</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 因為 TCP agent 不會產生 traffic 所以再加上 FTP Protocol 來產生 traffic
		</HTML:FONT></item>
		<item><HTML:B>set ftp [new Application/FTP]</HTML:B></item>
		<item><HTML:B>$ftp attach-agent $tcp</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 建立一個 UDP agent 和一個 NULL agent 作連結
   		</HTML:FONT></item>
		<item><HTML:B>set udp [new Agent/UDP]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n1 $udp</HTML:B></item>
		<item><HTML:B>set null [new Agent/Null]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n3 $null</HTML:B></item>
		<item><HTML:B>$ns connect $udp $null</HTML:B></item>
		<item><HTML:B>$udp set fid_ 2 </HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 在 UDP agent 加上一個 CBR agent, 其中 CBR(Constant Bits Rate) 只是依固定的速度產生封包流量
		</HTML:FONT></item>
		<item><HTML:B>set cbr [new Application/Traffic/CBR]</HTML:B></item>
		<item><HTML:B>$cbr attach-agent $udp</HTML:B></item>
		<item><HTML:B>$cbr set packet_size_ 1000</HTML:B></item>
		<item><HTML:B>$cbr set rate_ 1mb</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
	    # Schedule events for the CBR and FTP agents
		</HTML:FONT></item>
		<item><HTML:B>$ns at 0.1 "$cbr start"</HTML:B></item>
		<item><HTML:B>$ns at 1.0 "$ftp start"</HTML:B></item>
		<item><HTML:B>$ns at 4.0 "$ftp stop"</HTML:B></item>
		<item><HTML:B>$ns at 4.5 "$cbr stop"</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 在5秒時候呼叫我們自己定義的 procedure 來結束NS2的模擬	
		</HTML:FONT></item>
		<item><HTML:B>$ns at 5.0 "finish"</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 開始執行模擬
		</HTML:FONT></item>
		<item><HTML:B>$ns run</HTML:B></item>	
		</HTML:OL>
	<HTML:HR width="80%"/>
	<HTML:BR/>
	模擬結果 : 
	<HTML:BR/>
	<HTML:IMG src="graph/ns_example_2.PNG"/>	
	</content>
</document>

