<?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:HR/>
	<HTML:BR/>
	<item><HTML:FONT color="blue">set ns [new Simulator]</HTML:FONT></item>
	<item>目的在創造一個 NS2 模擬的物件, 只要的功能在</item>
		<HTML:OL>
		<item>1. 初使化封包格式( packet format)</item>
		<item>2. 創造一個Scheduler</item>
		</HTML:OL>
	<HTML:BR/>
	<item><HTML:FONT color="blue">set node_ [$ns node]</HTML:FONT></item>
	<item>
	建立一個名稱叫做 node_ 的 Node, 當然如果你想要創造很多個點的時候, 一行一行打似乎太慢了, 
	你可以利用TCL迴圈方式產生大量的 Nodes, 如下 :
	</item>
		<HTML:OL>
		<item><HTML:FONT color="green"># 建立30個Nodes</HTML:FONT></item>
		<item><HTML:B>for {set i 0} {$i &lt; 30} {incr i} {</HTML:B></item>
			<HTML:OL>
			<item><HTML:B>set n($i) [$ns node]</HTML:B></item>
			</HTML:OL>
		<item><HTML:B>}</HTML:B></item>
		</HTML:OL>
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns simplex-link &lt; n0 &gt; &lt; n1 &gt; &lt; bandwidth &gt; &lt; delay &gt; &lt; queue_type &gt;	
	</HTML:FONT></item>
	建立一條 Node n0 到 n1 的一條實體連結, 並設定頻寬、delay 時間和 queue 的 type, queue 的 type
	有 DropTail(a FIFO queue)、FQ、SFQ、DRR、RED、CBQ、CBQ/WRR 等 type. 範例如下 :
		<HTML:OL>
		<item><HTML:FONT color="green">
		# 在 n0 及 n1 間建立一個頻寬為2Mb, DropTail Queue 的 Link
		</HTML:FONT></item>
		<item><HTML:B>$ns duplex-link $n0 $n1 2Mb 20ms DropTail </HTML:B></item>
		</HTML:OL>
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns duplex-link &lt; n0 &gt; &lt; n1 &gt; &lt; bandwidth &gt; &lt; delay &gt; &lt; queue_type &gt;
	</HTML:FONT></item>
	同上, 不過是建立一條 duplex link 的連線.
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns attache-agent &lt; node &gt; &lt; agent &gt;
	</HTML:FONT></item>
	將一個 agent 結合到一個 node 上, agent 簡單來說也就表示一個 node 上所用的 protocol, 而一開始建立一個
   	Node 預設的 agent 是 Null. 範例如下 :
		<HTML:OL>
		<item><HTML:FONT color="green">
		# 創造一個TCP的Agent
		</HTML:FONT></item>
		<item><HTML:B>set tcp [new Agent/TCP]</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# TCP agent 結合到 node(n0)
		</HTML:FONT></item>
		<item><HTML:B>$ns attach-agent $n0 $tcp</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green">
		# 但就此範例光是 TCP 無法產生任何 Traffic, 所以通常我們都會再建立一些
		</HTML:FONT></item>
		<item><HTML:FONT color="green">
		# Application 的 Protocol 於 TCP 上(如 FTP、Telnet)
		</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:OL>
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns  connect &lt; agent1 &gt; &lt; agent2 &gt;
	</HTML:FONT></item>
	在兩個 agent 中建立一條 logical 的連結, 不同於 Simplex-link 等方式所建立的實體連結, 如 agent1 和 agent2 
	之間可能相隔好幾個點.
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns trace-all &lt; tracefile &gt;
	</HTML:FONT></item>
	將 ns2 模擬的內容寫回到在 &lt; tracefile &gt; 檔案中. 範例如下 :
		<HTML:OL>
		<item><HTML:B>set nf [open out.tr w]</HTML:B></item>
		<item><HTML:B>$ns trace-all $nf</HTML:B></item>
		</HTML:OL>
	ps. 建議此指令最好放在程式的前面 (在建立 node 和 link 之前), 以免模擬結果無法完整寫回檔案.	
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns  namtrace-all &lt; tracefile &gt;
	</HTML:FONT></item>
	同樣是將 ns2 模擬的內容寫回到在 &lt; tracefile &gt; 檔案中, 不過可以放在 nam 上去顯示模擬畫面, 
	格式也不太一樣.	
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns  at &lt; time &gt; &lt; event &gt;
	</HTML:FONT></item>
	在特定的時間 &lt; time &gt; 讓這個事件 &lt; event &gt; 被執行. 範例如下 : 
		<HTML:OL>
		<item><HTML:FONT color="green">
		# 在4.5秒的時候執行 ftp
		</HTML:FONT></item>
		<item><HTML:B>$ns at 4.5 "$ftp start"</HTML:B></item>
		<item><HTML:FONT color="green">
		# 在5秒時候執行我們自己所定義的 finish 函式
		</HTML:FONT></item>
		<item><HTML:B>$ns at 5.0 "finish"</HTML:B></item>
		</HTML:OL>
	<HTML:BR/>
	<item><HTML:FONT color="blue">
	$ns run
	</HTML:FONT></item>
	開始執行 scheduler.	
	</content>
</document>

