Trên Cygwin, vào đường thư mục chứa file .tcl cần chạy, gõ lệnh: ns file_tcl.tcl
Chương trình sẽ thực thi và tạo ra một file .nam, tiếp tục thực hiện lệnh: ns file_nam.nam
để chạy file .nam. 7. Mã code chương trình
7.1. Mã lệnh tạo nút di động
# Thi"t l#p cau trúc l>p liên k"t, l>p Mac, giao di7n m8ng # và l>p v#t lý cho nút m8ng di ñJng.
Node/MobileNode instproc add interface { channel pmodel lltype mactype qtype qlen iftype anttype } {
$self instvar arptable_ nifs_
$self instvar netif_ mac_ ifq_ ll_ global ns_ MacTrace opt
set t $nifs_ incr nifs_
set netif_($t) [new $iftype] ;# net interface set mac_($t) [new $mactype] ;# mac layer
set ifq_($t) [new $qtype] ;# interface queue set ll_($t) [new $lltype] ;# link layer
set ant_($t) [new $anttype] # Các bi"n cgc bJ
set nullAgent_ [$ns_ set nullAgent_] set netif $netif_($t)
set mac $mac_($t) set ifq $ifq_($t) set ll $ll_($t) # Khii t8o bjng
if { $arptable_ == "" } {
set arptable_ [new ARPTable $self $mac] set drpT [cmu trace Drop "IFQ" $self] $arptable_ dropZtarget $drpT
}
# L>p liên k"t
$ll arptable $arptable_ $ll mac $mac
$ll up target [$self entry] $ll down target $ifq
# Hàng ñEi giao di7n $ifq target $mac $ifq set qlim_ $qlen
set drpT [cmu trace Drop "IFQ" $self] $ifq drop target $drpT
Báo cáo Đồ án Tốt nghiệp Phụ lục
$mac netif $netif $mac up target $ll
$mac down target $netif $mac node $opt(nn)
# Giao di7n m8ng
$netif channel $channel $netif up target $mac
$netif propagation $pmodel ;# Mô hình truy6n
$netif node $self ;# K"t n%i nút < > giao di7n $netif antenna $ant_($t) ;# Gán anten
# Kênh v#t l
$channel addif $netif ;# Thêm vào danh sách gdi7n # ======================================================= # Setting up trace objects
if { $MacTrace == "ON" } {
# Các gói Trace RTS/CTS/ACK
set rcvT [cmu trace Recv "MAC" $self] $mac log target $rcvT
# Trace gói goi
set sndT [cmu trace Send "MAC" $self] $sndT target [$mac sendtarget]
$mac sendtarget $sndT # Trace gói nh#n
set rcvT [cmu trace Recv "MAC" $self] $rcvT target [$mac recvtarget]
$mac recvtarget $rcvT # Trace gói r>t
set drpT [cmu trace Drop "MAC" $self] $mac drop target $drpT
} else {
$mac log target [$ns_ set nullAgent_] $mac drop target [$ns_ set nullAgent_] }
# ====================================================== $self addif $netif
} #
# ðUnh nghĩa cau t8o cLa nút m8ng di ñJng #
$ns_ node config adhocRouting $opt(adhocRouting) \ llType $opt(ll) \ macType $opt(mac) \ ifqType $opt(ifq) \ ifqLen $opt(ifqlen) \ antType $opt(ant) \ propType $opt(prop) \ phyType $opt(netif) \ channelType $opt(chan) \ topoInstance $wtopo \ agentTrace ON \ routerTrace ON \ macTrace OFF
7.2.Mã lệnh chương trình mô phỏng thực hiện xây dựng hàm ra quyết định # ví dg file chương trình thrc hi7n v>i giao thTc AODV
# Define options
# ====================================================================== #
set opt(chan) Channel/WirelessChannel
set opt(prop) Propagation/TwoRayGround
set opt(netif) Phy/WirelessPhy
set opt(mac) Mac/802_11
set opt(ifq) Queue/DropTail/PriQueue
set opt(ll) LL
set opt(ant) Antenna/OmniAntenna
set opt(x) 1500 ;# X dimension of the topography
set opt(y) 300 ;# Y dimension of the topography
set opt(ifqlen) 50 ;# max packet in ifq
#set opt(seed) 0.0
set opt(tr) out5.tr ;# trace file
set opt(nam) final_AODV.nam
set opt(adhocRouting) AODV
set opt(nn) 50 ;# how many nodes are simulated
set opt(cp) "scen 1500x300 50 600 1 1"
set opt(sc) "cbr 50 10 4 512"
set opt(stop) 900.0 ;# simulation time
# Main Program
# ====================================================================== # Initialize Global Variables
# create simulator instance
set ns_ [new Simulator]
# set wireless channel, radio model and topography objects
set wtopo [new Topography]
# create trace object for ns and nam set tracefd [open $opt(tr) w]
$ns_ trace all $tracefd set nf [open $opt(nam) w]
$ns namtrace all wireless $nf $opt(x) $opt(y) # use new trace file format
$ns_ use newtrace # define topology
$wtopo load_flatgrid $opt(x) $opt(y) # Create God
set god_ [create god $opt(nn)] # define how node should be created #global node setting
$ns_ node config adhocRouting $opt(adhocRouting) \ llType $opt(ll) \ macType $opt(mac) \ ifqType $opt(ifq) \ ifqLen $opt(ifqlen) \ antType $opt(ant) \ propType $opt(prop) \ phyType $opt(netif) \ channelType $opt(chan) \ topoInstance $wtopo \
Báo cáo Đồ án Tốt nghiệp Phụ lục
agentTrace ON \ routerTrace ON \ macTrace OFF
# Create the specified number of nodes [$opt(nn)] and "attach" them # to the channel.
for {set i 0} {$i <= $opt(nn) } {incr i} { set node_($i) [$ns_ node]
$node_($i) random motion 0 ;# disable random motion
}
# Define node movement model
puts "Loading connection pattern..." source $opt(cp)
# Define traffic model
puts "Loading scenario file..." source $opt(sc)
# Define node initial position in nam for {set i 0} {$i <= $opt(nn)} {incr i} {
# 20 defines the node size in nam, must adjust it according to your scenario
# The function must be called after mobility model is defined $ns_ initial_node_pos $node_($i) 20
}
# Tell nodes when the simulation ends for {set i 0} {$i <= $opt(nn) } {incr i} {
$ns_ at $opt(stop).000000001 "$node_($i) reset"; }
# tell nam the simulation stop time
#$ns_ at $opt(stop) "$ns_ nam end wireless $opt(stop)"
$ns_ at $opt(stop).000000001 "puts \"NS EXITING...\" ; $ns_ halt" puts "Starting Simulation..."
$ns_ run
7.3. Mã lệnh chương trình mô phỏng ứng dụng cho giao thông # #
# mpgt_aodv.tcl #
# 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(ll) LL ;# link layer type set val(ant) Antenna/OmniAntenna ;# antenna model set val(ifqlen) 50 ;# max packet in ifq set val(nn) 15 ;# number of mobilenodes set val(rp) AODV ;# routing protocol
set val(x) 900 ;# X dimension of topography
set val(y) 400 ;# Y dimension of topography
set ns [new Simulator] set tracefd [open simple.tr w]
set namtrace [open simwrls.nam w] $ns trace all $tracefd
$ns namtrace all wireless $namtrace $val(x) $val(y) # set up topography object
set topo [new Topography] $topo load_flatgrid $val(x) $val(y) create god $val(nn)
#
# Create nn mobilenodes [$val(nn)] and attach them to the channel. #
# 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]
}
# Provide initial location of mobilenodes $node_(0) set X_ 150.0 $node_(0) set Y_ 350.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 350.0 $node_(1) set Y_ 350.0 $node_(1) set Z_ 0.0 $node_(2) set X_ 550.0 $node_(2) set Y_ 350.0 $node_(2) set Z_ 0.0 $node_(3) set X_ 750.0 $node_(3) set Y_ 350.0 $node_(3) set Z_ 0.0 $node_(4) set X_ 50.0 $node_(4) set Y_ 200.0 $node_(4) set Z_ 0.0
Báo cáo Đồ án Tốt nghiệp Phụ lục $node_(5) set X_ 250.0 $node_(5) set Y_ 200.0 $node_(5) set Z_ 0.0 $node_(6) set X_ 450.0 $node_(6) set Y_ 200.0 $node_(6) set Z_ 0.0 $node_(7) set X_ 650.0 $node_(7) set Y_ 200.0 $node_(7) set Z_ 0.0 $node_(8) set X_ 850.0 $node_(8) set Y_ 200.0 $node_(8) set Z_ 0.0 $node_(9) set X_ 150.0 $node_(9) set Y_ 50.0 $node_(9) set Z_ 0.0 $node_(10) set X_ 350.0 $node_(10) set Y_ 50.0 $node_(10) set Z_ 0.0 $node_(11) set X_ 550.0 $node_(11) set Y_ 50.0 $node_(11) set Z_ 0.0 $node_(12) set X_ 750.0 $node_(12) set Y_ 50.0 $node_(12) set Z_ 0.0 $node_(13) set X_ 130.0 $node_(13) set Y_ 300.0 $node_(13) set Z_ 0.0 $node_(14) set X_ 650.0 $node_(14) set Y_ 100.0 $node_(14) set Z_ 0.0 # Generation of movements $ns at 0.0 "$node_(13) setdest 850.0 250.0 70.0" $ns at 0.0 "$node_(14) setdest 50.0 135.0 65.0"
# Set a TCP connection between node_(14) and node_(13) set tcp [new Agent/TCP/Newreno]
$tcp set class_ 2
set sink [new Agent/TCPSink] $ns attach agent $node_(13) $tcp $ns attach agent $node_(14) $sink $ns connect $tcp $sink
set ftp [new Application/FTP] $ftp attach agent $tcp
#Setup a UDP connection between node_(13) and node_(5) set udp [new Agent/UDP]
$ns attach agent $node_(13) $udp set null [new Agent/Null]
$ns attach agent $node_(5) $null $ns connect $udp $null
$udp set fid_ red
#Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach agent $udp
#$cbr set type_ CBR $cbr set packetSize_ 512 #$cbr set rate_ 1mb $cbr set interval_ 0.25 $cbr set random_ 1 $cbr set maxpkts_ 10000
#Schedule events for the CBR and FTP agents $ns at 0.70 "$ftp start"
$ns at 3.1 "$cbr start" $ns at 10.0 "$ftp stop" $ns at 15.0 "$cbr stop"
# 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 }
# Telling 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 100.01 "puts \"end simulation\" ; $ns halt" proc stop {} {
global ns tracefd namtrace $ns flush trace
close $tracefd close $namtrace }