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,一经查实,立即删除!

相关文章

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…

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…

Java集合之ArrayList源码分析

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

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…

Java集合之HashSet源码分析

概述 HashSet是基于HashMap来实现的, 底层采用HashMap的key来保存数据, 借此实现元素不重复, 因此HashSet的实现比较简单, 基本上的都是直接调用底层HashMap的相关方法来完成. HashSet的构造方法就是创建HashMap: 基本操作 1.添加操作 2.删除操作 3.迭代器 其他方法基本也是调…

三次握手wireshark抓包分析,成功握手和失败握手

转载之前:基于HTTP的视频流中,客户端有时会打开使用多条TCP与服务器连接,为了验证每一对话的sessionID是否相同,使用wireshark进行了抓包分析(抓到的都是加密的包,无卵用orz....),这…

Java 内部类及其原理

Java中实现内部类 内部类相信大家都用过很多次了,就不说它是怎么用的了。 内部类 1.成员内部类 需要注意的是, 当成员内部类拥有和外部类同名的成员变量或这方法时, 默认情况下访问的是内部类的成员, 如要访问外部类的同名成员&…

Java8 Lambda表达式

概述 lambda表达式, 是Java8中的一个新特性。可以理解为一个匿名函数。 lambda表达式可以理解为将一个函数浓缩为一行代码,使代码更加简洁紧凑。 lambda表达式语法: (parameters) -> statement; 或 (parameters) -> {statements;} 参…

Java8 方法引用

概述 方法引用是用来直接访问类或实例阴茎存在的方法或者构造方法.它需要由兼容的函数式接口(lambda表达式中用到的接口)构成的目标类型上下文. 有时候, 当我们想要实现一个函数式接口的方法, 但是已经由类实现了我们想要的功能, 这时可以使用方法引用来直接使用现有的功能实现…

配置过程中的一些问题

一、 Tomcat相关问题 1、百度经验有设置用户名密码,但是按照步骤进行,到测试的时候发现还是错误的。 解决:在设置的时候应该stop Tomcat,在设置好之后再重新开启Tomcat,发现可以。 2、把web项目加入Tomcat&#xff0…

流媒体通信协议HLS与DASH的对比

简单了解 HLS(HTTP Live Streaming)协议 是由苹果公司实现的基于HTTP的流媒体通信协议,并成为Quick TIme X和IPhone软件系统的一部分。苹果的IPad也有支持HLS的能力。 HLS传出的视频文件为基于MPEG2文件的切片,每个媒体切片在服务器上单独存放。在一个流…