NS2のシナリオの書き方 (無線ネットワーク)
雛形
# Define options set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set val(netif) Phy/WirelessPhy ;# network interface type set val(mac) Mac/802_11 ;# MAC type #set val(ifq) Queue/DropTail/PriQueue ;# Interface queue type set val(ifq) CMUPriQueue ;# for dsr set val(ll) LL ;# Link layer type set val(ant) Antenna/OmniAntenna ;# Antenna type set val(ifqlen) 50 ;# max packet in ifq set val(rp) DSR ;# routing protocol set val(nn) 6 ;# number of mobilenodes set val(x) 1000 ;# x dimension of topography set val(y) 1000 ;# y dimension of topography set val(stop) 10.0 ;# time of simulation end #set val(ss) ss-30-100 ; set val(ss) ss-6 ; #set val(cs) tcp-30-100 ; set val(cs) cs-baytcpapp ; # ====================================================================== set AgentTrace ON set RouterTrace ON set MacTrace OFF LL set mindelay_ 50us LL set delay_ 25us LL set bandwidth_ 0 ;# not used Agent/Null set sport_ 0 Agent/Null set dport_ 0 Agent/CBR set sport_ 0 Agent/CBR set dport_ 0 Agent/TCPSink set sport_ 0 Agent/TCPSink set dport_ 0 Agent/TCP set sport_ 0 Agent/TCP set dport_ 0 Agent/TCP set packetSize_ 1460 Queue/DropTail/PriQueue set Prefer_Routing_Protocols 1 # unity gain, omni-directional antennas # set up the antennas to be centered in the node and 1.5 meters above it Antenna/OmniAntenna set X_ 0 Antenna/OmniAntenna set Y_ 0 Antenna/OmniAntenna set Z_ 1.5 Antenna/OmniAntenna set Gt_ 1.0 Antenna/OmniAntenna set Gr_ 1.0 # Initialize the SharedMedia interface with parameters to make # it work like the 914MHz Lucent WaveLAN DSSS radio interface Phy/WirelessPhy set CPThresh_ 10.0 Phy/WirelessPhy set CSThresh_ 1.559e-11 Phy/WirelessPhy set RXThresh_ 3.652e-10 Phy/WirelessPhy set Rb_ 2*1e6 Phy/WirelessPhy set Pt_ 0.2818 Phy/WirelessPhy set freq_ 914e+6 Phy/WirelessPhy set L_ 1.0 # ====================================================================== set ns_ [new Simulator] set tracefd [open simple.tr w] set windowVsTime2 [open win.tr w] set namtrace [open simwrls.nam w] $ns_ trace-all $tracefd $ns_ namtrace-all-wireless $namtrace $val(x) $val(y) $ns_ use-newtrace # set up topography object set topo [new Topography] $topo load_flatgrid $val(x) $val(y) set god_ [create-god $val(nn)] # Create nn mobilenodes [$val(nn)] and attach them to the cannel. # configure the nodes $ns_ node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node ] } # Set node and Generation of movements source $val(ss) # Set a TCP connections source $val(cs) # Define node initial position in nam for {set i 0} {$i < $val(nn)} {incr i} { # 30 defines the node size for nam $ns_ initial_node_pos $node_($i) 30 } # Tell nodes when the simulation ends for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(stop) "$node_($i) reset"; } # ending nam and the simulation $ns_ at $val(stop) "$ns_ nam-end-wireless $val(stop) " $ns_ at $val(stop) "stop" $ns_ at $val(stop).1 "puts \"end simulation\" ; $ns_ halt" proc stop {} { global ns_ tracefd namtrace $ns_ flush-trace close $tracefd close $namtrace } $ns_ run
ノードを配置する
手書きしたシナリオか
NS2で無線ネットワークをシミュレート - WebLab.ota
- /indep-utils/cmu-scen-gen/setdest/setdest
- setdest -v 1 (バージョン) -n 10 -p 2 (待機時間) -M 30 (最大移動速度) -t 50 -x 300 -y 300
で作ったシナリオをval(ss)で指定する.
$node_(0) set X_ 10.0 $node_(0) set Y_ 50.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 50.0 $node_(1) set Y_ 385.0 $node_(1) set Z_ 0.0 $node_(2) set X_ 150.0 $node_(2) set Y_ 200.0 $node_(2) set Z_ 0.0 $node_(3) set X_ 400.0 $node_(3) set Y_ 200.0 $node_(3) set Z_ 0.0 $node_(4) set X_ 600.0 $node_(4) set Y_ 350.0 $node_(4) set Z_ 0.0 $node_(5) set X_ 750.0 $node_(5) set Y_ 200.0 $node_(5) set Z_ 0.0
コネクションを作る
手書きしたシナリオか
で作ったシナリオをval(ss)で指定する.
pingを打つ
#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." } #Create two ping agents and attach them to the nodes n0 and n2 set p0 [new Agent/Ping] $ns attach-agent $node_(0) $p0 set p1 [new Agent/Ping] $ns attach-agent $node_(4) $p1 #Connect the two agents $ns connect $p0 $p1 #Schedule events $ns at 0.2 "$p0 send"
FTP
set tcp [new Agent/TCP] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns_ attach-agent $node_(6) $tcp $ns_ attach-agent $node_(1) $sink $ns_ connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns_ at 3.0 "$ftp start" $ns_ at 38.2 "$ftp stop"
BayTcpAppを使う
上記の「FTP」の方法だとファイルサイズとかを指定できない(?).ので,BayTcpAppを使ってファイルサイズを指定してみる.
set sftp [new Agent/BayTcpApp/FtpServer] $sftp file_size 1000 set cli [new Agent/BayTcpApp/FtpClient] set ctcp [new Agent/TCP/BayFullTcp] $ctcp attach-application $cli $cli tcp $ctcp $ns_ attach-agent $node_(2) $ctcp set stcp [new Agent/TCP/BayFullTcp] $stcp attach-application $sftp $ns_ attach-agent $node_(3) $stcp $ns_ connect $ctcp $stcp $stcp listen $ns_ at 0 "$cli start" $ns_ at 255 "$cli stop" set ftplist($ctcp) $stcp