<?xml version="1.0" encoding="Big5"?>
<?xml-stylesheet type="text/css" href="document.css"?>
<document xmlns:HTML="http://www.w3.org/1999/xhtml">
	<title>
	Static Routing &amp; Session Routing 模擬範例
	</title>
	<content>
	<subject>→ Static routing vs. Session routing</subject>
	Static routing 和 Session routing 都是使用 Dijkstra's all-pairs SPF algorithm. Static routing 只在模擬開始時執行一次 Dijkstra's all-pairs SPF algorithm. Session routing 除了在模擬開始時執行一次 Dijkstra's all-pairs SPF algorithm 之外, 當在模擬中發生拓樸改變 (ex: 連結斷掉), Session routing 會再執行 Dijkstra's all-pairs SPF algorithm 以重新計算最短路徑. 如此一來, 透過迅速地重新計算就可避免短暫的傳輸中斷.
	</content>	
	<content>
	<subject>→ Dijkstra's all-pairs SPF algorithm 介紹 </subject>
	Dijkstra's all-pairs SPF algorithm 定義 :
		<HTML:OL>
		<item><HTML:FONT color="red">== Definition == </HTML:FONT></item>
			<HTML:OL>
			<item>N = 網路中所有點的集合</item>
			<item>s = source node</item>
			<item>T = 已經經由 Dijkstra's all-pairs SPF algorithm 找到最短路徑的點的集合</item>
			<item>w( i , j ) = node I 到 node j 的 link cost</item>
			<item>w( i , i ) = 0</item>
			<item>w( i , j ) = ∞ 如果兩個 nodes 沒有直接地連結</item>
			<item>w( I , j ) ≧ 0 如果兩個 nodes 有直接地連結</item>
			<item>L(n) = cost of the least-cost path from node s to node n that is currently known to the algorithm; at termination, this is the cost of the least-cost path in the graph from s to n.</item>
			</HTML:OL>
		<HTML:BR/>	
		<item><HTML:FONT color="red">== Step1 - Initialization ==</HTML:FONT></item>
			<HTML:OL>
			<item>T = { s }</item>
			<item>L(n) = w( i , j ) for n ≠ s</item>
			</HTML:OL>
		<HTML:BR/>	
		<item><HTML:FONT color="red">== Step2 - Get Next Node ==</HTML:FONT></item>	
			<HTML:OL>
			<item>Find x which does not belong to T such that L(x) = min L( j ), j does not belong to T</item>
			</HTML:OL>
		<HTML:BR/>
		<item><HTML:FONT color="red">== Step3 - Update Least-Cost Paths ==</HTML:FONT></item>
			<HTML:OL>
			<item>L(n) = min [ L(n), L(x)+w(x,n)] for all n which does not belong to M</item>
			<item>*Steps 2 and 3 are repeated until T=N.</item>
			</HTML:OL>
		</HTML:OL>	
	接下來就以一連串的圖來解釋 Dijkstra's all-pairs SPF algorithm : 	
	<HTML:HR width="80%"/>
	<HTML:IMG src="graph/ns_routing_static_1.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_2.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_3.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_4.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_5.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_6.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_7.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_8.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_9.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_10.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_11.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_12.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:IMG src="graph/ns_routing_static_13.PNG" width="500" height="300"/>
	<HTML:BR/>
	<HTML:HR width="80%"/>
	</content>
	<content>
	<subject>→ Static Routing example 程式碼解說 </subject>
	以下為Static Routing example的程式碼解說. Session Routing example與Static Routing example的寫法類似, 
	不再進行說明. 程式碼下載 <HTML:A href="example/routeStatic.tcl">routeStatic.tcl</HTML:A>, 
	<HTML:A href="example/routeSession.tcl">routeSession.tcl</HTML:A>.	
	<HTML:BR/>
	Static Routing example 的程式碼部分
	<HTML:HR width="80%"/>
		<HTML:OL>
		<item><HTML:FONT color="green"># 開啟一個模擬物件</HTML:FONT></item>
		<item><HTML:B>set ns [new Simulator]</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 開啟nam trace file</HTML:FONT></item>
		<item><HTML:B>set nf [open routeStatic.nam w]</HTML:B></item>
		<item><HTML:B>$ns namtrace-all $nf</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 定義結束的行程</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"># 關閉trace file</HTML:FONT></item>
			<item><HTML:B>close $nf</HTML:B></item>
			<item><HTML:FONT color="green"># 執行nam</HTML:FONT></item>
			<item><HTML:B>exec nam routeStatic.nam &amp; </HTML:B></item>
			<item><HTML:B>exit 0 </HTML:B></item>
			</HTML:OL>
		<item><HTML:B>}</HTML:B></item>
		<item><HTML:FONT color="green"># 產生6個nodes</HTML:FONT></item>
		<item><HTML:B>for {set i 0} {$i &lt; 6} {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:BR/>
		<item><HTML:FONT color="green"># 建立node間的link</HTML:FONT></item>
		<item><HTML:B>for {set i 0} {$i &lt; 6} {incr i} {</HTML:B></item>
			<HTML:OL>
			<item><HTML:B>$ns duplex-link $n($i) $n([expr ($i+1)%6]) 1Mb 10ms DropTail </HTML:B></item>
			</HTML:OL>
		<item><HTML:B>}</HTML:B></item>
		<item><HTML:B>$ns duplex-link $n(0) $n(4) 1Mb 10ms DropTail</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 設每一條link的link cost</HTML:FONT></item>
		<item><HTML:B>$ns cost $n(0) $n(1) 2</HTML:B></item>
		<item><HTML:B>$ns cost $n(1) $n(0) 2</HTML:B></item>
		<item><HTML:B>$ns cost $n(1) $n(2) 1</HTML:B></item>
		<item><HTML:B>$ns cost $n(2) $n(1) 1</HTML:B></item>
		<item><HTML:B>$ns cost $n(2) $n(3) 5</HTML:B></item>
		<item><HTML:B>$ns cost $n(3) $n(2) 5</HTML:B></item>
		<item><HTML:B>$ns cost $n(3) $n(4) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(4) $n(3) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(4) $n(5) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(5) $n(4) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(5) $n(0) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(0) $n(5) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(4) $n(0) 10</HTML:B></item>
		<item><HTML:B>$ns cost $n(0) $n(4) 10</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 在node0上建立UPD Agent</HTML:FONT></item>
		<item><HTML:B>set udp0 [new Agent/UDP]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n(0) $udp0</HTML:B></item>
		<item><HTML:FONT color="green"># 設定flow id為0</HTML:FONT></item>
		<item><HTML:B>$udp0 set fid_ 0</HTML:B></item>
		<item><HTML:FONT color="green"># 設定封包顏色</HTML:FONT></item>
		<item><HTML:B>$ns color 0 darkgreen</HTML:B></item>
		<item><HTML:FONT color="green"># 在node1上建立UPD Agent</HTML:FONT></item>
		<item><HTML:B>set udp1 [new Agent/UDP]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n(1) $udp1</HTML:B></item>
		<item><HTML:FONT color="green"># 設定flow id為1</HTML:FONT></item>
		<item><HTML:B>$udp1 set fid_ 1</HTML:B></item>
		<item><HTML:FONT color="green"># 設定封包顏色</HTML:FONT></item>
		<item><HTML:B>$ns color 1 red</HTML:B></item>
		<item><HTML:FONT color="green"># 在node4上建立NULL Agent</HTML:FONT></item>
		<item><HTML:B>set null4 [new Agent/Null]</HTML:B></item>
		<item><HTML:B>$ns attach-agent $n(4) $null4</HTML:B></item>
		<item><HTML:FONT color="green"># 連結UDP Agent &amp; NULL Agent</HTML:FONT></item>
		<item><HTML:B>$ns connect $udp0 $null4</HTML:B></item>
		<item><HTML:B>$ns connect $udp1 $null4</HTML:B></item>
		<item><HTML:FONT color="green"># 在node0上產生CBR Traffic</HTML:FONT></item>
		<item><HTML:B>set cbr0 [new Application/Traffic/CBR]</HTML:B></item>
		<item><HTML:B>$cbr0 attach-agent $udp0</HTML:B></item>
		<item><HTML:FONT color="green"># 在node1上產生CBR Traffic</HTML:FONT></item>
		<item><HTML:B>set cbr1 [new Application/Traffic/CBR]</HTML:B></item>
		<item><HTML:B>$cbr1 attach-agent $udp1</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 指定使用 Static Routing</HTML:FONT></item>
		<item><HTML:B>$ns rtproto Static</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 為CBR Traffic排行程</HTML:FONT></item>
		<item><HTML:B>$ns at 0.1 "$cbr0 start"</HTML:B></item>
		<item><HTML:B>$ns at 0.3 "$cbr1 start"</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 0.7s時node0到node4的link斷掉</HTML:FONT></item>
		<item><HTML:B>$ns rtmodel-at 0.7 down $n(0) $n(4) </HTML:B></item>
		<item><HTML:FONT color="green"># 1s時node0到node4的link恢復</HTML:FONT></item>
		<item><HTML:B>$ns rtmodel-at 1 up $n(0) $n(4)</HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 模擬結束</HTML:FONT></item>
		<item><HTML:B>$ns at 1.5 "finish" </HTML:B></item>
		<HTML:BR/>
		<item><HTML:FONT color="green"># 開始跑模擬</HTML:FONT></item>
		<item><HTML:B>$ns run</HTML:B></item>
		</HTML:OL>
	</content>
	<content>
	<subject>→ 執行結果說明</subject>
	<item>0.1s時 node0 開始送 packet 到 node4</item>
	<HTML:IMG src="graph/ns_routing_static_14.PNG"/>
	<HTML:BR/>
	<item>0.3s時 node1 開始送 packet 到 node4</item>
	<HTML:IMG src="graph/ns_routing_static_15.PNG"/>
	<HTML:BR/>
	<item>0.7s 時 node0 到 node4 的 link 斷掉. 由於 Static Routing 並不會重新 run Dijkstra's all-pairs SPF algorithm, 所以造成 node0 到 node4 以及 node1 到 node4 的資料傳輸中斷.</item>
	<HTML:IMG src="graph/ns_routing_static_16.PNG"/>
	<HTML:BR/>
	<item>1s 時 node0 到 node4 的 link 恢復. node0 到 node4 以及 node1到 node4 的資料傳輸恢復.</item>
	<HTML:IMG src="graph/ns_routing_static_17.PNG"/>
	<HTML:BR/>
	</content>
</document>

