ns2相关学习——TCL脚本编写(2)
来源:互联网 发布:免费股票数据接口api 编辑:程序博客网 时间:2024/05/16 00:58
下面来学习更加复杂一点的TCL脚本的编写
简述:建立有4个节点的拓扑,其中一个节点作为路由器,用来将两个节点发出的数据包转发到第四个节点上面。
在这里将解释将两个节点的数据流区分开来的方法,展示如何去检测一个队列是否是满的,以及一个数据包是如何被丢弃的。
1、建立拓扑
Tcl脚本大多是大同的,我们需要做的是在基础上进行自己需要的修改。基础代码如下:
#Create a simulator objectset ns [new Simulator]#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} { global ns nf $ns flush-trace#Close the trace file close $nf#Execute nam on the trace file exec nam out.nam & exit 0}# Insert your own code for topology creation# and agent definitions, etc. here#Call the finish procedure after 5 seconds simulation time$ns at 5.0 "finish"#Run the simulation$ns run
做出修改如下:
① 添加四个节点:
set n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]
② 添加链接
$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms DropTail
其实上面这样就可以了,但是,为了让它更加美观,在跑nam的时候不要太懵逼,我们需要变一下链接的位置,使用的语句如下:
$ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right
在改变的时候我们可以使用right, left, up, down,以及这些单词的结合(结合方式:某-某)
2、添加事件
① 设置代理——建立起两个CBR代理和一个NULL代理。CBR代理给n0和n1,n0、n1用于发送数据,n3用于接收数据。
#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1set udp1 [new Agent/UDP]$ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1set cbr1 [new Application/Traffic/CBR]$cbr1 set packetSize_ 500$cbr1 set interval_ 0.005$cbr1 attach-agent $udp1set null0 [new Agent/Null] $ns attach-agent $n3 $null0
② CBR代理和NULL代理进行连接
$ns connect $udp0 $null0 $ns connect $udp1 $null0
③ 设置数据流开始的时间和结束的时间
$ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"
3、区别流
因为有俩发送端,为了区别数据流的情况,我们来给他们加点颜色;
$udp0 set class_ 1$udp1 set class_ 2$ns color 1 Blue$ns color 2 Red
然后这样就udp0发出的数据流变蓝色,另外一个变红色。
4、查看链接的队列
添加下面的语句来监视链接的情况
$ns duplex-link-op $n2 $n3 queuePos 0.5
为了得到相对公平的情况,我们可以更换使用队列,
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
SQF(随机公平排队)这样最后得到的是相对公平的结果
本节完完整代码如下:
#Create a simulator objectset ns [new Simulator]#Define different colors for data flows$ns color 1 Blue$ns color 2 Red#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} { global ns nf $ns flush-trace#Close the trace file close $nf#Execute nam on the trace file exec nam out.nam & exit 0}#Create four nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]#Create links between the nodes$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms SFQ$ns duplex-link-op $n0 $n2 orient right-down$ns duplex-link-op $n1 $n2 orient right-up$ns duplex-link-op $n2 $n3 orient right#Monitor the queue for the link between node 2 and node 3$ns duplex-link-op $n2 $n3 queuePos 0.5#Create a UDP agent and attach it to node n0set udp0 [new Agent/UDP]$udp0 set class_ 1$ns attach-agent $n0 $udp0# Create a CBR traffic source and attach it to udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0#Create a UDP agent and attach it to node n1set udp1 [new Agent/UDP]$udp1 set class_ 2$ns attach-agent $n1 $udp1# Create a CBR traffic source and attach it to udp1set cbr1 [new Application/Traffic/CBR]$cbr1 set packetSize_ 500$cbr1 set interval_ 0.005$cbr1 attach-agent $udp1#Create a Null agent (a traffic sink) and attach it to node n3set null0 [new Agent/Null]$ns attach-agent $n3 $null0#Connect the traffic sources with the traffic sink$ns connect $udp0 $null0 $ns connect $udp1 $null0#Schedule events for the CBR agents$ns at 0.5 "$cbr0 start"$ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"#Run the simulation$ns run
0 0
- ns2相关学习——TCL脚本编写(2)
- ns2相关学习——tcl脚本编写(1)
- ns2相关学习——TCL脚本编写(3)
- NS2学习(6)——Tcl类
- NS2入门学习(二)之tcl脚本示例
- Tcl&NS2学习笔记
- Tcl&NS2学习笔记
- ns2 Tcl脚本程序解析
- NS2中TCL脚本格式——/bin/bash^M: 没有那个文件或目录
- 3.TCL脚本学习——expect
- LiberoSOC分配管脚——Tcl脚本编写
- spider tcl脚本编写
- NS2相关学习——完成一个新协议(2)
- NS2中tcl脚本文件分析(转)
- NS2入门学习(三)之Tcl知识点
- NS2使用tcl脚本debug工具tcl-debug-2.0
- NS2无线脚本(~ns\tcl\ex\simple-wireless.tcl)
- 1.TCL/TK脚本学习——入门基础
- 此计算机上缺少vc2005_sp1_with_atl_fix_redist
- 恭祝 51编程俱乐部 的博客开通
- Cocos2d添加虚拟按钮Button,引用cocostudiolib库方法
- 一些前端书写规范建议
- Linux基础——vmstat 查看负载
- ns2相关学习——TCL脚本编写(2)
- DRUID连接池的实用 配置详解
- java设计模式之单例模式
- recyclerview的应用分析
- ios开发者账号申请,以及邓白氏码的申请
- iOS/OS X内存管理(二):借助工具解决内存问题
- 其他SSH命令
- 排序算法一:堆排序
- jQuery动态绑定事件