2008年8月26日 星期二

ns2 on Ubuntu - install

因為,之後要改學長姐的行車網路模擬器,
所以,會參考行之有年的ns-2摸擬器。
要學習就要有環境,一開始就先來個安裝吧~

先安裝一下相關套件:

[someone@host ~ ]# sudo apt-get install build-essential
[someone@host ~ ]# sudo apt-get install tcl8.4 tcl8.4-dev tk8.4 tk8.4-dev
[someone@host ~ ]# sduo apt-get install libxmu-dev libxmu-headers


接著到 ns2 的 SourceForge 去下載檔案,這邊我下載到的最新版是 2.33(要用就要用最近滴~)

把ns-2解壓縮到/usr/local資料夾下,因為權限的關係,最好在解壓縮之前,在/usr/local下建立一個資料夾ns-allinone-2.33,並且把它的權限改成你常用的一個普通使用者,避免一直需要root的權限

[someone@host ~ ]# mkdir /usr/local/ns-allinone-2.33
[someone@host ~ ]# tar -zxvf ns-allinone-2.32.tar.gz -C /usr/local
[someone@host ~ ]# cd /usr/local/ns-allinone-2.33
[someone@host ~ ]# ./install


然後,安裝最後會要你在.csbrc或.bashrc中加入一些指令
我是加入在~/.bashrc中
.bashrc

export PATH=$PATH:/usr/local/ns-allinone-2.33/bin:/usr/local/ns-allinone-2.33/tcl8.4.18/unix:/usr/local/ns-allinone-2.33/tk8.4.18/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ns-allinone-2.33/otcl-1.13:/usr/local/ns-allinone-2.33/lib
export TCL_LIBRARY=$TCL_LIBRARY:/usr/local/ns-allinone-2.33/tcl8.4.18/library


安裝完後,就去驗証啦,應該會跑個半個小時吧~

[someone@host ~]# cd /usr/local/ns-allinone-2.33/ns-2.33/
[someone@host ~]# ./validate


若驗証完後,若沒有問題的話,它會說all pass;但是,沒有看到一個介面出來,可能會覺得魂身不對勁,好~
example2.tcl

#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5

#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

#Create a Null agent (a traffic sink) and attach it to node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0

#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run


就執行它吧~

[someone@host ~]# ns example2.tcl

就會出現以下視窗啦~



參考資料:
阿駕零零壹 © 學習筆記
cygwin + NS2 2.28 安裝成功

沒有留言:

張貼留言