NS2相关学习——创建Xgraph的输出文件

经过前面学习代码的编写,这一部分,我们要学会如何进行分析,一个很直观的方式就是将结果图形化表示出来。

ns-allinone包的一部分是“xgraph”,一个绘图程序,可用于创建模拟结果的图形表示。 在本节中,将向您展示如何在Tcl脚本中创建可用作xgraph数据集的输出文件的简单方法。 在途中,还将向您展示如何使用流量生成器。(这里介绍的技术是创建适合xgraph的输出文件的许多可能方式之一)

1、创建拓扑图

首先,我们需要创建一个如下图的拓扑图。


代码如下所示:

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 $n3 $n4 1Mb 100ms DropTail
我们将流量源附加到节点n0,n1和n2,但是首先我们编写一个程序,将流量源附加到节点n0,n1和n2
proc attach-expoo-traffic { node sink size burst idle rate } {#Get an instance of the simulatorset ns [Simulator instance]#Create a UDP agent and attach it to the nodeset source [new Agent/UDP]$ns attach-agent $node $source#Create an Expoo traffic agent and set its configuration parametersset 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 $sinkreturn $traffic
}
它有六个参数:一个节点,一个先前创建的流量信宿,流量源的数据包大小,突发和空闲时间(指数分布)和峰值速率。
首先,该过程创建流量源并将其附加到节点,然后创建一个Traffic / Expoo对象,设置其配置参数并将其附加到流量源,最后连接源和宿。 最后,该过程返回流量来源的句柄。 这个过程是一个很好的例子,可以处理如何将诸如将流量源附加到多个节点的任务。 现在我们使用这个过程将具有不同峰值速率的流量源连接到n0,n1和n2,并将它们连接到n4上的三个流量接收器,这些流量源必须首先创建:

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 $sink2set 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]
在这个例子中,我们使用Agent / LossMonitor对象作为流量接收器,因为它们存储接收的字节数,我们可以使用这个来计算带宽。
现在我们可以写出实际将数据写入输出文件的程序。

proc record {} {global sink0 sink1 sink2 f0 f1 f2#Get an instance of the simulatorset ns [Simulator instance]#Set the time after which the procedure should be called againset 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 timeset now [$ns now]#Calculate the bandwidth (in MBit/s) and write it to the filesputs $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$sink0 set bytes_ 0$sink1 set bytes_ 0$sink2 set bytes_ 0#Re-schedule the procedure$ns at [expr $now+$time] "record"
}
此程序读取从三个流量接收器接收的字节数。 然后,它计算带宽(以MBit / s为单位),并将其与当前时间一起写入三个输出文件,然后重置流量汇点上的bytes_值。

2、运行模拟

首先,调用“record”过程,然后每0.5秒定期重新安排一次。 然后三个流量来源在10秒钟开始,并在50秒钟停止。 在60秒钟后,调用“finish”过程。完整的脚本代码如下:

#Create a simulator object
set ns [new Simulator]#Open the output files
set f0 [open out0.tr w]
set f1 [open out1.tr w]
set f2 [open out2.tr w]#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]#Connect the nodes
$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 $n3 $n4 1Mb 100ms DropTail#Define a 'finish' procedure
proc finish {} {global f0 f1 f2#Close the output filesclose $f0close $f1close $f2#Call xgraph to display the resultsexec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &exit 0
}#Define a procedure that attaches a UDP agent to a previously created node
#'node' and attaches an Expoo traffic generator to the agent with the
#characteristic values 'size' for packet size 'burst' for burst time,
#'idle' for idle time and 'rate' for burst peak rate. The procedure connects
#the source with the previously defined traffic sink 'sink' and returns the
#source object.
proc attach-expoo-traffic { node sink size burst idle rate } {#Get an instance of the simulatorset ns [Simulator instance]#Create a UDP agent and attach it to the nodeset source [new Agent/UDP]$ns attach-agent $node $source#Create an Expoo traffic agent and set its configuration parametersset 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 $sinkreturn $traffic
}#Define a procedure which periodically records the bandwidth received by the
#three traffic sinks sink0/1/2 and writes it to the three files f0/1/2.
proc record {} {global sink0 sink1 sink2 f0 f1 f2#Get an instance of the simulatorset ns [Simulator instance]#Set the time after which the procedure should be called againset 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 timeset now [$ns now]#Calculate the bandwidth (in MBit/s) and write it to the filesputs $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$sink0 set bytes_ 0$sink1 set bytes_ 0$sink2 set bytes_ 0#Re-schedule the procedure$ns at [expr $now+$time] "record"
}#Create three traffic sinks and attach them to the node n4
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#Create three traffic sources
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]#Start logging the received bandwidth
$ns at 0.0 "record"
#Start the traffic sources
$ns at 10.0 "$source0 start"
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
#Stop the traffic sources
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"
#Call the finish procedure after 60 seconds simulation time
$ns at 60.0 "finish"#Run the simulation
$ns run

链接:http://www.isi.edu/nsnam/ns/tutorial/



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508615.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NS2相关学习——在ns中模拟无线场景

之前学习的都是有线场景下的NS2相关应用,现在开始,终于要学习无线啦!无线是我研究的重点,要好好学习呀!在本节中,我们将学习使用ns中提供的移动无线仿真模型。 该部分由两部分组成。 在第一小节中&#xff…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中一种基于蚁群算法的能量有效路由)

牙说:这篇论文是研究蚁群算法在能量有效路由协议的过程中必读的一篇文章,原是全英文,在这里按照自己的理解大致翻译成中文,好好学习,与君共勉。 论文题目:An Energy-Efficient Ant-Based Routing Algorith…

活在幻梦中的你我

其实仔细想想,人类和地球上的其它物种有什么不同呢?可能仅有的不同是,人类会去相信那本来并不存在的事情. 并且会为了那种虚幻的东西为止拼搏、努力。比如科技的发展,不就是人类在实现自己想象中的事物么,飞机、轮船、家电、计算机等等,无一…

An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中基于蚁群算法的能量有效路由)2

牙说:接着上一篇继续写。论文标题:An Energy-Efficient Ant-Based Routing Algorithm forWireless Sensor Networks作者:Tiago Camilo, Carlos Carreto, Jorge S Silva, Fernando Boavida正文: 2、相关工作可以考虑无线传感器网络…

NS2仿真分析无线网络的攻击防御(1)

这个学期有个选题是NS2仿真分析无线网络的攻击防御,比较有意思的样子,现在来慢慢学一下这个是什么东西。 首先,还是一篇文章(老长老长了),还是全英文的,还是先来分析一下它到底在说什么&#x…

NS2仿真分析无线网络的攻击防御(2)

牙说:继续上一篇博文进行翻译。 4. NS和我们的工作 我们试图评估黑洞攻击在无线Ad-hoc网络中的影响。 为了实现这一点,我们已经使用NS 网络模拟 [14]程序模拟了一个含有黑洞节点的无线自组网络场景。为了模拟无线自组织网络中的黑洞节点,我…

Java集合之HashMap源码分析

以下源码均为jdk1.7 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现. 提供所有可选的映射操作, 并允许使用null值和null健. 此类不保证映射的顺序. 需要注意的是: HashMap不是同步的. 哈希表 哈希表定义: 哈希表是一种根据关键码去寻找值的数据映射结构, 该结构通…

NS2相关学习——可靠的MANET应用程序的Gossip协议分析

好久不写,应该努力啦!老师把这篇论文给了我,现在还不知道它在讲什么,来边翻译边学习吧! 文章链接:https://www.researchgate.net/publication/316844643_Analyzing_Gossip_Protocols_for_Reliable_MANET_Ap…

Java集合之LinkedList源码分析

概述 LinkedLIst和ArrayLIst一样, 都实现了List接口, 但其内部的数据结构不同, LinkedList是基于链表实现的(从名字也能看出来), 随机访问效率要比ArrayList差. 它的插入和删除操作比ArrayList更加高效, 但还是要遍历部分链表的指针才能移动到下标所指的位置, 只有在链表两头的…

lex和yacc环境配置

lex和yacc的使用很简单,但环境配置却是各种问题,本章说明lex和yacc在windows下的环境配置。 软件需求: 系统 win7-64位(win7-32, win8, win10全部通过) c编译器: vs2010(2008,2013,2015也全部通过) lex和yacc编译器&#xff1a…

Java集合之Vector源码分析

概述 Vector与ArrayLIst类似, 内部同样维护一个数组, Vector是线程安全的. 方法与ArrayList大体一致, 只是加上 synchronized 关键字, 保证线程安全, 下面就不具体分析源码了, 具体可以查看ArrayList中的源码分析. Vector源码分析 1.主要字段 2.构造函数 3.增删改查 其他方法…

Gossip协议的P2P会员管理

阅读此论文主要目的在于理解gossip协议及其背后的原理,此部分详细翻译,其余部分看时间 文章标题:Gossip协议的P2P会员管理 作者:Ayalvadi J. Ganesh, Anne-Marie Kermarrec, and Laurent Massoulie Abstract:基于…

Java集合之LinkedHashSet源码分析

概述 LinkedHashSet与HashSet类似, 不同的是LinkedHashSet底层使用LinkedHashMap维护元素插入的顺序. LinkedHashSet继承自HashSet, 只是重写了HashSet的构造方法, 初始化一个LinkedHashMap, 其他均与HashSet相同. LinkedHashSet构造方法 HashSet的构造方法: 以上几乎就是Li…

2016-2017NBU期末考试记录

又是一年期末考 这个学期考的少,就两门 还是来记录一下都考了什么东西吧 首先编译:编译的题目是开始十道判断题,后面全都是大题。大题内容有:画出第二次递归过程中,活动记录中静态链和动态链的情况;给出一…

Java集合之ArrayList源码分析

概述 ArrayList可以理解为动态数组, 根据MSDN的说法, 就是Array的复杂版本. 与数组相比, 它的容量能动态增长. ArrayList是List接口的可变数组的实现. 实现了所有可选列表操作, 允许包括null在内的所有元素. 数组的特点, 查询快增删慢. 每个ArrayList实例都有一个容量, 该容…

视频业务原理

最近要学习如何进行视频的用户体验测量,首先学习最基础的知识,视频业务的原理是什么。 研究的视频的应用层协议是HTTP,使用的传输层协议是TCP。 工作过程如下:客户端向服务器请求相应的视频信息;服务器响应请求发回视…

Java集合之Hashtable源码分析

概述 Hashtable也是基于哈希表实现的, 与map相似, 不过Hashtable是线程安全的, Hashtable不允许 key或value为null. 成员变量 Hashtable的数据结构和HashMap一样, 采用 数组加链表的方式实现. 几个成员变量与HashMap一样: 方法 Hashtable的方法与HashMap基本一样, 只是 Ha…

视频质量检测中的TP、FP、Reacll、Precision

在看论文《Measuring Vedio QoE from Encrypted Traffic》的时候看到TP(True Positives)、FP(False Positives)、Precison、Recall的概念,这属于数据挖掘方面的内容,学习之后特来记录。 首先,下…

Java集合之LinkedHashMap源码分析

概述 HashMap是无序的, 即put的顺序与遍历顺序不保证一样. LinkedHashMap是HashMap的一个子类, 它通过重写父类的相关方法, 实现自己的功能. 它保留插入的顺序. 如果需要输出和输入顺序相同时, 就选用此类. LinkedHashMap原理 LinkedHashMap是如何保证输入输出顺序的呢? L…

视频流传输协议RTP/RTCP/RTSP/HTTP的区别

在转载之前:我研究主要是基于HTTP的视频流,正在研读的论文名:“Modeling and Analyzing the Influence of Chunk Size Variation on Bitrate Adaptation in DASH”,里面有一句话,“Compared with early connection-ori…