ns2.23——ns-simple.tcl样例解析

#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 0 blue
$ns color 1 red
$ns color 2 white

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# set trace file(预先定义)
set f [open out.tr w]
$ns trace-all $f
set nf [open out.nam w]  #Open the NAM trace file
$ns namtrace-all $nf

#Create links between the nodes (双向链接)
$ns duplex-link $n0 $n2 5Mb   2ms  DropTail
$ns duplex-link $n1 $n2 5Mb   2ms  DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
Drop Tail(队列长度管理机制,丢尾)

它有点类似于FIFO(先入先出)的存储方式。Drop Tail最大的优点是原理简单。

  1. 当路由器队列长度达到最大值时,通过丢包来指示拥塞,先到达路由器的分组首先被传输。
  2. 由于路由器缓存有限,如果包到达时缓存已满,那么路由器就丢弃该分组。
  3. 一旦发生丢包,发送端立即被告知网络拥塞,从而调整发送速率。这种做法不考虑被丢弃包的重要程度。
    #Set Queue Size of link (n2-n3) to 10
    $ns queue-limit $n2 $n3 10 #设置n2与n3之间链路上队列缓冲区的最大值为10
    #Give node position (for NAM)
    $ns duplex-link-op $n0 $n2 orient right-up      # n0在n2的下方45°
    $ns duplex-link-op $n1 $n2 orient right-down    # n1在n2的上方45°
    $ns duplex-link-op $n2 $n3 orient right         # n2的右边是n3
    #队列位置定义了队列与水平的夹角(水平线以下度数为正)
    #right-up       45
    #right-down    -45
    #right          0    右

ns2.23——ns-simple.tcl样例解析

#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#duplex-link-op  设置双工链路属性

ns2.23——ns-simple.tcl样例解析

queuePos
 1. **queuePos 0.5表示包从上到下进入队列** (上图即为从上到下)
 2. queuePos 0表示包从右到左进入队列
 3. queuePos 1表示包从左到右进入队列
 4.  queuePos 1.5表示包从下到上进入队列
 5.  queuePos 2==queuePos 0

因此queuePos N表示包进入队列时的角度,角度 =(N×π) % 2π

set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 1  #udp1的流量是红色
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

set null0 [new Agent/Null] #这是接收器(sink)
$ns attach-agent $n3 $null0

set null1 [new Agent/Null] #也是接收器(sink)
$ns attach-agent $n1 $null1

$ns connect $udp0 $null0
$ns connect $udp1 $null1

$ns at 1.0 "$cbr0 start"
$ns at 1.1 "$cbr1 start"

time=1.0
ns2.23——ns-simple.tcl样例解析

time=1.14
ns2.23——ns-simple.tcl样例解析

#Setup a TCP connection
set tcp [new Agent/TCP]        #创建一个tcp代理
$tcp set class_ 2              #设置tcp流的颜色为白色(n0)
set sink [new Agent/TCPSink]   #Sink:接收器(n3)
$ns attach-agent $n0 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"

t=1.23
ns2.23——ns-simple.tcl样例解析

$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#把节点和代理分离开,把代理重置为空代理
puts [$cbr0 set packetSize_]  #在命令行打印cbr0产生包恒定的大小
puts [$cbr0 set interval_]  #在命令行打印cbr0包之间的间隔

#这个模拟程序会执行3s
$ns at 3.0 "finish"
#Define a 'finish' procedure
proc finish {} {
        global ns f nf
        $ns flush-trace
        close $f
        close $nf            #Close the NAM trace file
        puts "running nam..."
        exec nam out.nam &   #Execute NAM on the trace file
        exit 0
}
#即关闭所有nam文件,将out.nam以可视化方式实现

转载于:https://blog.51cto.com/12059878/2063978

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值