有线网络四节点(星型拓扑,UDP连接,带宽测量)

来源:互联网 发布:3dstart软件下载 编辑:程序博客网 时间:2024/06/01 10:51

实现4节点星型网络场景中网络带宽的测量,给出模拟过程中带宽变化情况的曲线图(Xgraph绘制)!

 

链接:http://blog.chinaunix.net/u3/105477/showart_2087772.html

 

学习的知识点:

 
      如何通过统一方式设置同一类型的连接(代码重用:简洁地生成节点,大规模场景就是采用这种生成方式)!
      如何写采样过程record, 通过调用 $ns at [expr $now+$time] "record" 实现间隔采用(每隔 $time 后进行一次采样), 深刻理解NS2模拟的事件调度机制(事件队列:FIFO)!
     
      如何利用Xgraph进行曲线绘制,以及Xgraph源文件格式!
  

#场景描述:
#有线新星结构, 四节点, 三条UDP连接,中心节点公用
#测量模拟过程中,各条UDP连接的带宽!

#以下是一些基本设置,在看过NS系列--NS2入门实例1、2、3之后,阅读肯定没问题的啦!
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

 

 

原创粉丝点击