close

2007.08.05

這也是上禮拜一起報告的內容,Ping 的實做,不過程式碼相當簡單,所以稍微帶過就好,比較需要注意的是中間有個框起來的程式碼,那個還不是很瞭解其指令的用意,不過大致猜測是為計算 RTT ,並且回傳花了多少 RTT的時間。


    RTT 我查到的解釋為:
 (In telecommunications ) The term round-trip delay time or round-trip time (RTT) has the following meanings: 

1.   The elapsed time for transit of a signal over a closed circuit, or time elapsed for a message to a remote place and back again.

2.   In primary or secondary radar systems, the time required for a transmitted pulse to reach a target and for the echo or transponder reply to return to the receiver.

http://en.wikipedia.org/wiki/Round-trip_delay_time
[RTT]

不過在這裡應該是第一個意思比較說得通,兩個解釋的意思大概都是在說明,一個訊號從來源端傳送到目的地,目的地再回傳訊號回來的流逝時間,也就是花費的時間。以 Ping 來說,A 去 Ping B,B 收到訊號後,回覆給 A,這樣所花費的時間就是 RTT。


下列就是 Ping 實做的程式碼

   set ns [new Simulator]
# 產生一個模擬的物件

   $ns color 1 Blue
   $ns color 2 Red
# 設定顏色

   set nf [open out.nam w]
   $ns namtrace-all $nf
# 開啟一個 nam 檔

   proc finish {} {
# 結束的副函式
           global ns nf
           $ns flush-trace
           close $nf
           exec nam out.nam &
        # 以背景模式去執行 NAM
           exit 0
   }

   set n0 [$ns node]
   set n1 [$ns node]
   set n2 [$ns node]
# 建立三個節點

   $ns duplex-link $n0 $n1 1Mb 10ms DropTail
   $ns duplex-link $n1 $n2 1Mb 10ms DropTail
# 建立實體鏈路

   $ns duplex-link-op $n0 $n1 orient right
   $ns duplex-link-op $n1 $n2 orient right
# 設定節點位置

# =========================== RTT ================================
# Define a 'recv' function for the class 'Agent/Ping'
   Agent/Ping instproc recv {from rtt} {
    $self instvar node_
    puts "node [$node_ id] received ping answer from $from with round-trip-time $rtt ms."
   }
# =========================== RTT ================================

   set p0 [new Agent/Ping]
# 建立 Ping0 的 agent

   $p0 set class_ 1
# 設定顏色藍色

   $ns attach-agent $n0 $p0
# n0-node 為 Ping 協定

   set p1 [new Agent/Ping]
# 建立 Ping1 的 agent

   $p1 set class_ 2
# 設定顏色紅色

   $ns attach-agent $n2 $p1
# n2-node 為 Ping 協定

   $ns connect $p0 $p1
# 連結兩個協定

   $ns at 0.2 "$p0 send"
   $ns at 0.4 "$p1 send"
   $ns at 0.6 "$p0 send"
   $ns at 0.6 "$p1 send"
   $ns at 1.0 "finish"
# 建立事件發生時間

   $ns run
# 執行
 

歡迎對於 NS2 有研究或見解的同學、朋友可以一起討論  by Yo AM 01:34

arrow
arrow
    全站熱搜

    NS2lab 發表在 痞客邦 留言(1) 人氣()