[转] setdest和cbrgen
http://nikosun520.blog.sohu.com/67395894.html
【一】
setdest工具在ns-2.32/indep-utils/cmu-scen-gen/setdest/目录下。ns早期版本该命令使用前需要进行make。
setdest命令格式如下:
./setdest -n <num_of_nodes> -p <pausetime> -s <maxspeed> -t <simtine> -x <maxx> -y <maxy> > <outdir>/<scenario-file>
参数定义如下:
-n num_of_nodes 的值指定了场景中总共有多少个节点。
-p pausetime 指定了节点运动到一个目的地后停留的时间,如果设为0,则节点不停留。
-s maxspeed 指定了节点随机运动速度的最大值,单位是(m/s),节点的运动速度将在[0,maxspeed]中随机选择。
-t simtine 指定了模拟场景持续的时间(单位为s)
-x maxx 指定节点运动区域的长度(x轴方向,单位为m)
-y maxy 指定节点运动区域的宽度(y轴方向,单位为m)
> <outdir>/<scenario-file> 指定了生成场景的输出文件。
例1:./setdest -n 3 -p 0 -s 2 -t 10 -x 300 -y 500 > scen-3n-0p-10t-2s-300-500
例2:./setdest -n 50 -p 1000 -s 0 -t 400 -x 1200 -y 1200 > scen/scen-50n-1000p-400t-1200-1200
场景文件最后有一些统计数据,包括了链路的改变数量(Link Changes)和路由的改变数量(Link Changes),通过这些变量也可以大致了解该场景的拓扑变化频繁程度。此外,场景文件中很多诸如“$god_ set-dist 48 49 2”这样的语句,它表明了当前节点48和节点49的最短跳数为2,也就是说48节点发出的包最短经过2跳转发后能到达49节点。这里计算跳数是根据当前的拓扑,并采用了默认的无线传输的范围参数计算出来的。
使用setdest生成的场景,可以在NS控制台或者运行脚本中使用source命令调入。此外,由于生成的命令中用到了node_数组变量和god_变量,需要在tcl脚本文件中提前建立这些对象。
需要注意的是:生成的场景文件和调用它的tcl文件应该在相同目录,这样source命令才能找到场景文件,否则需要指定相对或绝对路径。
我犯的低级错误:没有仔细看setdest命令格式。使用setdest命令时,没有指定的输出的文件,导致找不到生成的场景文件。郁闷了半天!
setdest命令格式如下:
ns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections] [-rate rate] > <outdir>/<scenario-file>
参数定义如下:
-type 该参数指定产生的是cbr流还是tcp流
-nn 通过nodes指定了有多少个节点
-seed 指定了随机种子数
-mc 这些节点间最大连接数
-rate 通过rate值指定了每个连接之间的流的负载量。如果产生的是cbr流,则包长固定为512Byte,rate值指定的是每秒发送多少个包
> <outdir>/<cbr-file> 指定了输出文件。
例:ns cbrgen.tcl -type cbr -nn 50 -seed 1 -mc 30 -rate 1.0 >cbr/cbr-50n-30c-1p
使用cbrgen生成的数据流文件,可以在NS控制台或者运行脚本中使用source命令调入。此外,由于生成的命令中用到了node_数组变量和god_变量,需要在tcl脚本文件中提前建立这些对象。
需要注意的是:生成的数据流文件和调用它的tcl文件应该在相同目录,这样source命令才能找到场景文件,否则需要指定相对或绝对路径。
cbr-50n-30c-1p文件内容如下:
#
# nodes: 50, max conn: 30, send rate: 1.0, seed: 1
#
#
# 1 connecting to 2 at time 2.5568388786897245
#
set udp_(0) [new Agent/UDP]
$ns_ attach-agent $node_(1) $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_ 1.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 2.5568388786897245 "$cbr_(0) start"
#
# 4 connecting to 5 at time 56.333118917575632
#
set udp_(1) [new Agent/UDP]
$ns_ attach-agent $node_(4) $udp_(1)
set null_(1) [new Agent/Null]
$ns_ attach-agent $node_(5) $null_(1)
set cbr_(1) [new Application/Traffic/CBR]
$cbr_(1) set packetSize_ 512
$cbr_(1) set interval_ 1.0
$cbr_(1) set random_ 1
$cbr_(1) set maxpkts_ 10000
$cbr_(1) attach-agent $udp_(1)
$ns_ connect $udp_(1) $null_(1)
$ns_ at 56.333118917575632 "$cbr_(1) start"
。。。。。。。。。省略
#
# 36 connecting to 37 at time 16.444244867397586
#
set udp_(29) [new Agent/UDP]
$ns_ attach-agent $node_(36) $udp_(29)
set null_(29) [new Agent/Null]
$ns_ attach-agent $node_(37) $null_(29)
set cbr_(29) [new Application/Traffic/CBR]
$cbr_(29) set packetSize_ 512
$cbr_(29) set interval_ 1.0
$cbr_(29) set random_ 1
$cbr_(29) set maxpkts_ 10000
$cbr_(29) attach-agent $udp_(29)
$ns_ connect $udp_(29) $null_(29)
$ns_ at 16.444244867397586 "$cbr_(29) start"
#
#Total sources/connections: 20/30
#
#mflood-scene.tcl代码
#Agent/UDP set packetSize_ 6000
# ======================================================================
# Define options
# ======================================================================
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(x) 1200 ;# X dimension of the topography
set val(y) 1200 ;# Y dimension of the topography
set val(ifqlen) 50 ;# max packet in ifq
#set val(seed) 0.0
set val(rp) MFlood
set val(nn) 50 ;# how many nodes are simulated
set val(cp) "cbr-50n-30c-1p"
set val(sc) "scen-50n-1000p-400t-1200-1200"
set val(stop) 200
# ======================================================================
# Main Program
# ======================================================================
#ns-random 0
# Initialize Global Variables
set ns_ [new Simulator]
set tracefd [open mflood.tr w]
$ns_ trace-all $tracefd
set namtrace [open mflood.nam w]
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
# set up topography
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
#
# Create God
#
set god_ [create-god $val(nn)]
# Create the specified number of mobilenodes [$val(nn)] and "attach" them
# to the channel.
# configure node
set channel [new Channel/WirelessChannel]
$channel set errorProbability_ 0.0
$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 $channel \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON\
-macTrace OFF \
-movementTrace OFF
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
$node_($i) random-motion 0;
}
# Define traffic model
#
puts "Loading scenario file..."
source $val(sc)
#
# Define node movement model
#
puts "Loading connection pattern..."
source $val(cp)
#
# Define node initial position in nam
for {set i 0} {$i < $val(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 < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
$ns_ at $val(stop).0 "stop"
$ns_ at $val(stop).01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
global ns_ tracefd namtrace
$ns_ flush-trace
close $tracefd
close $namtrace
exec nam mflood.nam &
exit 0
}
puts "Starting Simulation..."
$ns_ run
某一时刻截取的运行场景图:。。。。。没图。。。