wireless model主要是由MobileNode所組成,MobileNode由Node這個類別繼承而來,因此MobileNode除了有基本的Node能力,還有移動和無線傳送接收的能力。目前ad-hoc routing protocol有支援四種Destination Sequence Distance Vector (DSDV)、 Dynamic Source Routing (DSR) 、 Temporally Ordered Routing Algorithm (TORA)以及 Ad-hoc On-demand Distance Vector (AODV)。
在建立Mobile Node之前,我們必須定義Mobile Node所活動的topology大小。
set topo [ new Topography ]
$topo load_flatgrid $opt(x) $opt(y)
接著我們要建立一個God instance,這是用來建立一個矩陣,用以儲存topology的連線資訊。
create-god $opt(nn)
舊的API建立Mobile Node根據使用的routing protocol,而新的 API則是藉由$ns node-config這個instproc來設定,接著就使用產生一般的node的方式產生Mobile Node。
$ns node-config -adhocRouting $opt(adhocRouting)
-llType $opt(ll)
-macType $opt(mac)
-ifqType $opt(ifq)
-ifqLen $opt(ifqlen)
-antType $opt(ant)
-propInstance [new $opt(prop)]
-phyType $opt(netif)
-channel [new $opt(chan)]
-topoInstance $topo
-wiredRouting OFF
-agentTrace ON
-routerTrace OFF
-macTrace OFF
for { set i 0 } { $i < $opt(nn) } { incr i } {
set node($i) [ $ns node ]
$node($i) random-motion 0 ;# disable random motion
}
雖然Mobile Node被設計成可以在三維空間中移動(X,Y,Z),但是Z尚未被使用,因此我們假設只在平面上移動,也就是Z永遠等於0。我們藉由設定X,Y,Z來初始Mobile Node的初始位置,並且使用setdest來移動Mobile Node,也可以使用$node start來做random的移動,不過不建議使用此方式,如果要做random的scenario可以使用CMU提供的tool -- setdest (放在~ns/indep-utils/cmu-scen-gen/中)。
$node set X_ <x1>
$node set Y_ <x2>
$node set Z_ 0
$ns at <time> $node setdest <x2> <y2> <speed> ;# 在時間time秒, 以速度speed往(x2,y2)移動
example1.tcl # ======================================================================
# Default Script Options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;#Channel Type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/Simple ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna(天線) model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 2 ;# number of mobile nodes
set opt(rp) AODV ;# routing protocol. DSDV, DSR, AODV.
#Destination-Sequenced Distance-Vector Routing(AODV)
#Dynamic Source Routing (DSR)
#destination sequenced distance vector (DSDV)
set opt(x) 670 ;# Topology大小
set opt(y) 670 ;# Topology大小
set opt(trace) wireless_basic.tr ;# trace file
set opt(nam.tr) wireless_basic.nam ;# the nam trace file
# ======================================================================
# Main Program
# ======================================================================
# Initialize Global Variables
set ns [new Simulator] ;#建立一個新的模擬物件,並指向變數ns
# This command should be called before the universal trace command $ns trace-all
$ns use-newtrace ;#建立一個新的trace資料
set namtrace [open $opt(nam.tr) w] ;#開起一個$opt(nam.tr)wireless_basic.nam,並且指向namtrace
#把模擬的過程寫入$namtrace,長為$opt(x),寬為opt(y),因為是wireless,所以,要改用namtrace-all-wireless指令
$ns namtrace-all-wireless $namtrace $opt(x) $opt(y)
;#開起一個$opt(trace)wireless_basic.tr,並且指向tracefd
set tracefd [open $opt(trace) w]
$ns trace-all $tracefd ;#把模擬器的封包過程寫入$tracefd
proc finish { } { ;# new a procedure finish
global ns namtrace tracefd opt ;# set global variable
$ns flush-trace ;#把trace清空,都寫入
close $namtrace ;# close file
close $tracefd ;# close file
exec nam $opt(nam.tr) & ;#在背景執行nam去打開$opt(nam.tr)
exit 0
}
# set up topography object 定義Mobile Node所活動的topology大小
set topo [new Topography] ;#建立一個新的地形
$topo load_flatgrid $opt(x) $opt(y) ;#設定長寬
#An omniscient observer
#Stores smallest number of hops from one node to another
#Optimal case to compare routing protocol performance
#Automatically generated by scenario file
#set god [create-god <no of mnodes>]設定node個數
#$god set-dist <from> <to> <#hops>
# Create God (General Operations Director)
# 加入這一行,會出現"num_nodes is set 2"的訊息
create-god $opt(nn)
# Create channel
set chan_ [new $opt(chan)] ;#set chan_ [new Channel/WirelessChannel] 設定Channel
# Create node(0) and node(1)
# configure node, please note the change below.設定節點的參數
$ns node-config -adhocRouting $opt(rp) \ ;# -adhocRouting AODV
-llType $opt(ll) \ ;# -llType LL
-macType $opt(mac) \ ;# -macType Mac/Simple
-ifqType $opt(ifq) \ ;# -ifqType Queue/DropTail/PriQueue
-ifqLen $opt(ifqlen) \ ;# -ifqLen 50
-antType $opt(ant) \ ;# -antType Antenna/OmniAntenna
-propType $opt(prop) \ ;# -propType Propagation/TwoRayGround
-phyType $opt(netif) \ ;# -phyType Phy/WirelessPhy
-topoInstance $topo \ ;# -topoInstance
-agentTrace ON \ ;# 在無線網路,要設定是否要打開agent Trace
-routerTrace ON \ ;# 在無線網路,要設定是否要打開router Trace
-macTrace ON \ ;# 在無線網路,要設定是否要打開mac Trace
-movementTrace ON \ ;# 在無線網路,要設定是否要打開movement Trace
-channel $chan_ ;# 設定channel
#以下指令打完,會出現"INITIALIZE THE LIST xListHead"的訊息
for {set i 0} {$i < $opt(nn)} {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0 ;# disable random motion
$ns initial_node_pos $node($i) 20 ;# 在nam中定義節點初始所在位置
}
#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
$node(0) set X_ 15.0
$node(0) set Y_ 15.0
$node(0) set Z_ 0.0
$node(1) set X_ 150.0
$node(1) set Y_ 150.0
$node(1) set Z_ 0.0
#
# Now produce some simple node movements
# Node_(1) starts to move towards node_(0)
#
$ns at 0.0 "$node(0) setdest 50.0 50.0 5.0" ;# 以5.0的速度移動到目標(50.0,50.0)
$ns at 0.0 "$node(1) setdest 60.0 40.0 10.0" ;# 以10.0的速度移動到目標(60.0,40.0)
# Node(1) then starts to move away from node_(0)
# 若加入這一行指令,在執行這一個tcl時,會出現"SORTING LISTS ...DONE!"的訊息
$ns at 3.0 "$node(1) setdest 240.0 240.0 30.0" ;#以30.0的速度移動到目標(240.0,240.0)
# Setup traffic flow between nodes
# TCP connections between node(0) and node(1)
set tcp [new Agent/TCP] ;#建立TCP物件
$tcp set class_ 2 ;#設定顏色
set sink [new Agent/TCPSink] ;#建立TCPSink物件
$ns attach-agent $node(0) $tcp ;#把node(0)建立在TCP通訊協定上
$ns attach-agent $node(1) $sink ;#把node(1)建立在TCPSink通訊協定上
$ns connect $tcp $sink ;#把TCP與TCPSink相連
set ftp [new Application/FTP] ;#建立ftp通訊
$ftp attach-agent $tcp ;#把ftp建立在tcp協定上
$ns at 0.5 "$ftp start" ;#在時間0.5ftp開始傳送
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns at 6.0 "$node($i) reset"; ;#會reset node上所有的agents
}
$ns at 6.0 "finish"
$ns at 6.01 "puts \"NS EXITING...\" ; $ns halt" ;#ns停止
puts "Starting Simulation..."
$ns run
純程式碼
example.tcl
set ns [new Simulator]
$ns use-newtrace
set namtrace [open wireless_basic.nam w]
$ns namtrace-all-wireless $namtrace 670 670
set tracefd [open wireless_basic.tr w]
$ns trace-all $tracefd
proc finish {} {
global ns namtrace tracefd
$ns flush-trace
close $namtrace
close $tracefd
exec nam wireless_basic.nam &
exit 0
}
set topo [new Topography]
$topo load_flatgrid 670 670
create-god 2
set chan_ [new Channel/WirelessChannel]
$ns node-config -adhocRouting AODV \
-llType LL \
-macType Mac/Simple \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
-channel $chan_
for {set i 0} {$i<2} {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0
$ns initial_node_pos $node($i) 20
}
$node(0) set X_ 15.0
$node(0) set Y_ 15.0
$node(0) set Z_ 0.0
$node(1) set X_ 150.0
$node(1) set Y_ 150.0
$node(1) set Z_ 0.0
$ns at 0.0 "$node(0) setdest 50.0 50.0 5.0"
$ns at 0.0 "$node(1) setdest 60.0 40.0 10.0"
$ns at 3.0 "$node(1) setdest 240.0 240.0 30.0"
set tcp [new Agent/TCP]
$ns color 2 Red
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $node(0) $tcp
$ns attach-agent $node(1) $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.5 "$ftp start"
for {set i 0} {$i<2} {incr i} {
$ns at 6.0 "$node($i) reset"
}
$ns at 6.0 "finish"
$ns at 6.01 "puts \"NS EXITING...\";$ns half"
puts "Starting Simulator..."
$ns run
沒有留言:
張貼留言