# ns2.29
# Initialize Global Variables
#-------------------------------------------
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) 3 ;# number of mobilenodes
set val(rp) DSDV ;# routing protocol
set val(x) 500 ;# X dimension of the topography
set val(y) 400 ;# Y dimension of the topography
set val(stop) 150
set ns [new Simulator]
## 1.程序进入tcl\lib\ns-lib.tcl执行Simulator的init构造函数
## 2.init函数中执行初始化变量,初始化包格式(函数在\lib\ns-packet.tcl中)、创建Scheduler(缺省是Calendar调度器)、创建接收代理(nullAgent_)
## 3.调用\common\simulator.{cc,h}执行C++中的simlator过程
set tracefd [open smiple.tr w]
$ns trace-all $tracefd
set windowVsTime2 [open win.tr w]
set namtrace [open simwrls.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
# 设置移动节点的移动范围
#-------------------------------------------
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
## 调用\tcl\mobility\com.tcl中的create-god方法创建god,god.{h,cc}在\mobile目录下。
# Configure 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) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
-movementTrace ON
## 1. 调用lib\ns-lib.tcl中的node-config 函数,设置Simulator中的参数为上面各个相应属性。
## 2. -adhocRouting等通过~tclcl/tcl-object.tcl中的init-vars函数,实现了调用Simultor自身的
## adhocRouting等函数来完成属性的设置。
# 创建节点
#-------------------------------------------
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns node]
## 1. 调用lib\ns-lib.tcl中的Simulator的node函数,选择进入wireless node节点
## 2. node函数中调用Simulator的create-wireless-node函数,完成无线节点的创建(这里会执行
## common目录下的C++移动节点MobileNode.{h,cc})和配置。
## 3. 配置是调用\lib\ns-mobilenode.tcl中mobilenode的add-inerface函数完成的,OTCL复合移动
## 节点的结构基本在这里组装好,像C++中实现的Channel,Phy,Mac,ifq_,LL等通信构件都在这里被创建。
}
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#---------------------------------------------------------------
$node_(0) set X_ 5.0
$node_(0) set Y_ 5.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 490.0
$node_(1) set Y_ 285.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 150.0
$node_(2) set Y_ 240.0
$node_(2) set Z_ 0.0
# Now produce some simple node movements
#---------------------------------------------------------------
$ns at 10.0 "$node_(0) setdest 250.0 250.0 3.0"
$ns at 15.0 "$node_(1) setdest 45.0 285.0 5.0"
$ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"
# Setup traffic flow between nodes
#---------------------------------------------------------------
set tcp [new Agent/TCP/Newreno]
$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 10.0 "$ftp start"
## 1.attach-agent函数实际调用node的attach函数,在此函数中完成了地址或者端口分类器的创建和配置工作。
## 2.设置好谁跟谁通信后,就等着启动FTP服务,把包从src的高层向dest的高层传送了。
## 3.ftp start 后,packet就从高层依次进入低层,就是在sendDown,recv,sendUp等函数之间传送,直到到达目标节点的高层被接收或丢掉。
# 收尾工作
#---------------------------------------------------------------
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam simwrls.nam &
exit 0
}
# Printing the window size
#---------------------------------------------------------------
proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "
plotWindow $tcpSource $file"
}
# Define node initial position in nam
#---------------------------------------------------------------
for {set i 0} {$i < $val(nn) } {incr i} {
$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";
}
# Tell nodes when the simulation ends
#---------------------------------------------------------------
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "stop"
$ns at 10.1 "plotWindow $tcp $windowVsTime2"
$ns at 150.01 "puts\"end simulation\";$ns halt"
$ns run
本文介绍了使用NS2进行无线网络模拟的详细配置步骤,包括初始化全局变量、配置节点参数、设定移动轨迹及流量传输等内容。
659

被折叠的 条评论
为什么被折叠?



