NS应用实例4
2011-10-09 15:57
211 查看
set ns [new Simulator]
set tracefd [open trace.tr w]
$ns trace-all $tracefd
set namtracefd [open namtrace.nam w]
$ns namtrace-all $namtracefd
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n4 $n3 1Mb 100ms DropTail
#参数的意义: node sink size burst idle rate
# node:数据流的发起节点
# sink:数据流对应的接收代理
# size、idle、rate:数据包大小、idle 时间、发包速率
proc attach-expoo-traffic { node sink size burst idle rate } {
#生成 simulator 实例对象,用于整个仿真过程
set ns [Simulator instance]
#创建 UDP 代理,并与 soure 节点相关联
set source [new Agent/UDP]
$ns attach-agent $node $source
#Create an Expoo traffic agent and set its configuration parameters
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst
$traffic set idle_time_ $idle
$traffic set rate_ $rate
# Attach traffic source to the traffic generator
$traffic attach-agent $source
#Connect the source and the sink
$ns connect $source $sink
return $traffic
}
proc record {} {
#注意:自己写采样过程时,把要用到的变量做声明很重要!
global sink0 sink1 sink2 f0 f1 f2
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
#How many bytes have been received by the traffic sinks?
set bw0 [$sink0 set bytes_]
set bw1 [$sink1 set bytes_]
set bw2 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f0 "$now [expr $bw0/$time*8/1000000]"
puts $f1 "$now [expr $bw1/$time*8/1000000]"
puts $f2 "$now [expr $bw2/$time*8/1000000]"
#Reset the bytes_ values on the traffic sinks
#这点很重要,以重新采样时不受前一次的影响,与 NS2 实现机制有关,暂时可忽略!
$sink0 set bytes_ 0
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Re-schedule the procedure
$ns at [expr $now+$time] "record"
}
#LossMonitor 的特点:?
set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1
$ns attach-agent $n4 $sink2
#调用过程 proc attach-expoo-traffic { node sink size burst idle rate }
#可以很方便的设置同一类型的数据流
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
proc finish {} {
global f0 f1 f2 tracefd namtracefd
close $f0
close $f1
close $f2
close $tracefd
close $namtracefd
exec nam namtrace.nam &
#Call xgraph to display the results
exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
exit 0
}
$ns at 0.0 "record"
$ns at 10.0 "$source0 start"
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"
$ns at 60.0 "finish"
$ns run
程序运行效果图:
set tracefd [open trace.tr w]
$ns trace-all $tracefd
set namtracefd [open namtrace.nam w]
$ns namtrace-all $namtracefd
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n4 $n3 1Mb 100ms DropTail
#参数的意义: node sink size burst idle rate
# node:数据流的发起节点
# sink:数据流对应的接收代理
# size、idle、rate:数据包大小、idle 时间、发包速率
proc attach-expoo-traffic { node sink size burst idle rate } {
#生成 simulator 实例对象,用于整个仿真过程
set ns [Simulator instance]
#创建 UDP 代理,并与 soure 节点相关联
set source [new Agent/UDP]
$ns attach-agent $node $source
#Create an Expoo traffic agent and set its configuration parameters
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst
$traffic set idle_time_ $idle
$traffic set rate_ $rate
# Attach traffic source to the traffic generator
$traffic attach-agent $source
#Connect the source and the sink
$ns connect $source $sink
return $traffic
}
proc record {} {
#注意:自己写采样过程时,把要用到的变量做声明很重要!
global sink0 sink1 sink2 f0 f1 f2
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
#How many bytes have been received by the traffic sinks?
set bw0 [$sink0 set bytes_]
set bw1 [$sink1 set bytes_]
set bw2 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f0 "$now [expr $bw0/$time*8/1000000]"
puts $f1 "$now [expr $bw1/$time*8/1000000]"
puts $f2 "$now [expr $bw2/$time*8/1000000]"
#Reset the bytes_ values on the traffic sinks
#这点很重要,以重新采样时不受前一次的影响,与 NS2 实现机制有关,暂时可忽略!
$sink0 set bytes_ 0
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Re-schedule the procedure
$ns at [expr $now+$time] "record"
}
#LossMonitor 的特点:?
set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1
$ns attach-agent $n4 $sink2
#调用过程 proc attach-expoo-traffic { node sink size burst idle rate }
#可以很方便的设置同一类型的数据流
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
proc finish {} {
global f0 f1 f2 tracefd namtracefd
close $f0
close $f1
close $f2
close $tracefd
close $namtracefd
exec nam namtrace.nam &
#Call xgraph to display the results
exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &
exit 0
}
$ns at 0.0 "record"
$ns at 10.0 "$source0 start"
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"
$ns at 60.0 "finish"
$ns run
程序运行效果图:
相关文章推荐
- NS应用实例1
- NS应用实例2
- NS应用实例3
- NS应用实例5
- jQuery.ajax的简单应用实例 附实例下载
- Android JNI 应用实例
- 程序员转型AI,这里有最全的机器学习介绍+应用实例
- Ruby设计模式编程中对外观模式的应用实例分析
- jquery.validate.js的应用实例,自己写的有点简单
- Quartz+spring实例应用
- 前向算法的实例应用
- ZZ:gcc的静态链接库和动态链接库的一个应用实例
- 小型邮局创建:POP3应用实例 推荐
- 一则表驱动法的应用实例
- Maticsoft.Framework for Asp.Net框架应用实例(一)
- webservice 应用实例
- NSString / NSMutableString 字符串处理,常用代码 (实例)
- TensotFlow 应用实例:07-优化器 Optimizer 介绍
- ASP.NET怎么操作DataTable实例应用
- CFileFind类的详解以及应用实例