从Linux学到NS2:(三)de番外

引言

      本篇文章准备介绍一堆花里胡哨的东西,比如让nam运行起来像Android设计Activity那样多一些美感、用一句话实现上篇tcl脚本中的多行代码、将awk和xgraph嵌入到tcl的proc中使用等。既然是上一篇文章的番外当然也有干货,主要是对上篇仿真内容进行一些修正:取消最大缓冲区的设置,不产生丢包;减少一个路由器使包只经过一跳;设置传输时延为零,通过awk组件处理Trace文件得到数据包从发出入队列直到被接收的时间差,与时延约束比较,得到新的队列溢出情况;把上面花里胡哨的东西用进来。

内容
1. 设置链路布局及传输数据的颜色:
#set colors
$ns color 1 blue
$ns color 2 red   //使用颜色2:$udp1 set class_ 2
$ns simplex-link-op $n0 $r1 orient right-down //n0到r1链路方向为右下
$ns simplex-link-op $n1 $r1 orient right-up
$ns simplex-link-op $r1 $n2 orient right


2. 将下面代码与上篇文章中的比较,每行都可以实现上文好几行代码的功能:
#建立UDP单向代理,udpi为发送者,null为接受者,并分别连接到相应节点上,然后建立UDP连接,在UDP上建立FTP应用  
set udp0 [$ns create-connection UDP $n0 Null $n2 1]  //这样建立udp可能会使上面的颜色设置无效
set udp1 [$ns create-connection UDP $n1 Null $n2 2] 
#Create traffic sources
set source0 [attach-expoo-traffic $n0 $udp0 200 2s 1s 100k]//使用attach-expoo-traffic需要先声明
set source1 [attach-expoo-traffic $n1 $udp1 200 0s 1s 10M]

代价是要在开头声明一个过程:

#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 thesource object.

proc attach-expoo-traffic { node sink size burst idle rate } {
	#Get an instance of the simulator
	set ns [Simulator instance]

	#Create a UDP agent and attach it to the node
	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
}
3. awk、xgraph在proc中实现:

(体现了Tcl 是一种可嵌入的命令脚本化语言。“可嵌入”是指把很多应用有效,无缝地集成在一起,“命令”是指每一条 Tcl 语句都可以理解成命令加参数的形式

proc finish {} {
    set awkCode {
    BEGIN{} {} END{}
    }
    exec awk $awkCode all.q
    exec xgraph -bb -tk -x time -y queue temp.queue &
    exit 0
}
4. 顺便介绍NS2随机数的设置:
#设置随机数产生器
set rng [new RNG] 
$rng seed 1
set Rnd [new RandomVariable/Uniform]
$Rnd set min_ 0
$Rnd set max_ 1
$Rnd use-rng $rng
5. 监控一个队列:
# Tracing a queue
set redq [[$ns link $node_(r1) $node_(r2)] queue]
set tchan_ [open all.q w]
$redq trace curq_ //追踪当前队长
$redq trace ave_  //平均队长
$redq attach $tchan_
set startT1 [expr [$RVstart value]]//使用随机数方法
set endT1 [expr ($startT1 + 50)] //$实现引用替换,[]返回命令结果
6. 显示NS2可用的Application(其他如Agent也可以用此方法):
# 功能:本脚本用于显示 NS2 中类继承关系的树形结构; 
# 通过本脚本,我们可以查看 NS 中各种信源 
proc showApplication arg { 
set cl "Application"
foreach cl [$arg info subclass] { 
puts $cl 
if {[$cl info subclass]!=""} { 
showAgent $cl } 
 } 
} 
showApplication "Application" 

 (上面代码的组合及更多代码会放在我的下载里面或者后续更新编辑在此。    计算服务时延#intarval set awkCode {
    BEGIN {i=0;}
    {action=$1;time=$2;from=$3;layer=$4;seq_no=$6;packet_type=$7;
    if(action=="s"&&from=="_4_"&&layer=="AGT"&&packet_type=="exp") {
        timenow[i]=time;i++;}
}
    END {for(j=0;j<i;j++) {timenow[j]=timenow[j+1]-timenow[j];}
            for(j=0;j<i-1;j++) { printf("%ld\n",timenow[j]/0.0001);}  
})
7. 对NS2学习至今,准备在这里简单介绍一下NS2方法学:

(1)离散事件模拟机制和分裂对象模型原理要非常清楚 |                   / 分类器                                             /现成

(2)牢记节点结构                                                                 | 节点组成 |  复用器 (更基本构件)->网络-->建模  

(3)源代码->对象之间接口 编程接口                                |                   |   代   理                                              \自己做

(4)了解主要的的网络构件                                                 |                    \  链  路

(5)新的协议模块->NS本身源代码/其他研究者              |  Agent代表网络层分组的起点和终点

8.  最后在这里说一下NS2与NS3的区别:

      首先,NS3并不是NS2的扩展,而是一个全新的模拟器,NS3不支持NS2的API;在脚本语言的选择上,NS2使用OTcl语言,NS3仿真脚本使用C++或Python来编写;NS2模拟器用C++和Tcl编写,NS3全部是由C++编写,仅仅带有选择性的Python语言绑定;在功能上,NS3现在还没有包含所有的NS2模块,移植帮助文件正在开发之中。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值