# Define options 定義一些變數
# ======================================================================
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/Ricean ;# 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(x) 1000 ;# X dimension of topology (拓樸範圍:X)
set val(y) 1000 ;# Y dimension of topology (拓樸範圍:Y)
set val(cp) "" ;# node movement model file
set val(sc) "" ;# traffic model file
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 3 ;# number of mobilenodes (節點個數)
set val(seed) 0.0
set val(stop) 100.0 ;# simulation time (模擬總時間)
set val(tr) exp.tr ;# trace file name
set val(rp) DSDV ;# routing protocol (路由協定)
set AgentTrace ON
set RouterTrace ON
set MacTrace ON
# Initialize Global Variables
set ns_ [new Simulator]
$ns_ color 1 Blue
$ns_ color 2 Red
# Open trace file 開啟trace file
$ns_ use-newtrace ;# Use new trace format 使用新的trace format
set namfd [open nam-exp.nam w]
$ns_ namtrace-all-wireless $namfd $val(x) $val(y)
set tracefd [open $val(tr) w]
$ns_ trace-all $tracefd
# set up topography object
#建立一個拓樸物件,以紀錄mobilenodes在拓樸內移動的情況
set topo [new Topography]
# 拓樸的範圍為 1000m x 1000m
$topo load_flatgrid $val(x) $val(y)
# create channel
set chan [new $val(chan)]
# Create God
set god_ [create-god $val(nn)]
# Create the specified number of mobile nodes [$val(nn)] and "attach" them
# to the channel. Three nodes are created : node(0), node(1) and node(2)
# 設置Mobile node的參數
$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) \
-channel $chan \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0 ;# disable random motion
}
set prop_inst [$ns_ set propInstance_]
$prop_inst MaxVelocity 2.5;
$prop_inst RiceanK 0;
$prop_inst LoadRiceFile "./rice_table.txt";
###############################################
# LOG the propagation information
# To disable logging, simply comment this section
# out
## ###########################################
set prop_tracefd [open proptrace.tr w];
set prop_log [new Trace/Generic]
$prop_log target [$ns_ set nullAgent_]
$prop_log attach $prop_tracefd
$prop_log set src_ 0
$prop_inst tracetarget $prop_log
# Provide initial (X,Y, for now Z=0) co-ordinates for mobile nodes
#
# 設定節點0在一開始時,位置在(350.0, 500.0)
$node_(0) set X_ 350.0
$node_(0) set Y_ 500.0
$node_(0) set Z_ 0.0
# 設定節點1在一開始時,位置在(500.0, 500.0)
$node_(1) set X_ 500.0
$node_(1) set Y_ 500.0
$node_(1) set Z_ 0.0
# 設定節點2在一開始時,位置在(650.0, 500.0)
$node_(2) set X_ 650.0
$node_(2) set Y_ 500.0
$node_(2) set Z_ 0.0
# Load the god object with shortest hop information
# 在節點1和節點2之間最短的hop數為1
#$god_ set-dist 1 2 1
# 在節點0和節點2之間最短的hop數為2
#$god_ set-dist 0 2 2
# 在節點0和節點1之間最短的hop數為1
#$god_ set-dist 0 1 1
# Now produce some simple node movements
# Node_(1) starts to move upward and then downward
set god_ [God instance]
# 在模擬時間200秒的時候,節點1開始從位置(500, 500)移動到(500, 900),速度為2.0 m/sec
$ns_ at 20.0 "$node_(1) setdest 500.0 900.0 2.0"
# 然後在500秒的時候,再從位置(500, 900)移動到(500, 100),速度為2.0 m/sec
$ns_ at 40.0 "$node_(1) setdest 500.0 100.0 2.0"
# Setup traffic flow between nodes 0 connecting to 2 at time 10.0
# 在節點0和節點2建立一條CBR/UDP的連線,且在時間為10秒開始傳送
set udp_(0) [new Agent/UDP]
$udp_(0) set fid_ 1
$ns_ attach-agent $node_(0) $udp_(0)
set null_(0) [new Agent/Null]
$ns_ attach-agent $node_(2) $null_(0)
set cbr_(0) [new Application/Traffic/CBR]
$cbr_(0) set packetSize_ 512
#$cbr_(0) set interval_ 2.0
$cbr_(0) set random_ 1
$cbr_(0) set maxpkts_ 10000
$cbr_(0) attach-agent $udp_(0)
$ns_ connect $udp_(0) $null_(0)
$ns_ at 10.0 "$cbr_(0) start"
#Define node initial position in nam, only for nam
# 在nam中定義節點初始所在位置
for {set i 0} {$i < $val(nn)} {incr i} {
# The function must be called after mobility model is defined.
$ns_ initial_node_pos $node_($i) 60
}
# Tell nodes when the simulation ends
# 設定節點模擬結束時間
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop) "$node_($i) reset";
}
$ns_ at $val(stop) "stop"
$ns_ at $val(stop) "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd namfd
$ns_ flush-trace
close $tracefd
close $namfd
}
puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(rp)"
puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"
puts "Starting Simulation..."
$ns_ run
浙公网安备 33010602011771号