ns2代码分析(三)-CBR业务的创建与路由的建立

来源:互联网 发布:淘宝开店成功率 编辑:程序博客网 时间:2024/06/10 20:24

以[1]中的一端代码为例,本文试图阐述CBR数据包的产生,发送与路由流程。

#Setup a UDP connectionset udp [new Agent/UDP]$ns attach-agent $n0 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset cbr [new Application/Traffic/CBR]$cbr attach-agent $udp

new Application/Traffic/CBR创建了CBR类的对象(tools/cbr_traffic.cc),类的继承关系BR_Traffic <-TrafficGenerator<-Application,其中Application(app/app.cc)类中的函数Application::command定义了命令attatch-agent,就是将Application中的指针agent_指向创建的UdpAgent对象udp。cbr在发送数据时,向下递交到udpagent处理。

具体的函数调用流程,参考[2]. 那么就自然产生一个疑问,一个包中仅有目的节点的id和port信息。仿真代码,是怎么样在节点上建立路由表,中间节点是怎么样将数据包结构体投送给目的节点呢?节点上的路由表是什么时候建立的呢?

这里就涉及脚本的的调用过程。就是命令$ns run,之后建立路由表。主要的流程如下(括号内表示代码文件,出现的行,以ns2-35版本为准):


(ns-default.tcl?line=196)$ns_compat(route-logic) compute-routes(ns-route.tcl?line=310)Simulator instproc compute-routes (ns-route.tcl?line=321)Simulator instproc compute-flat-routes{} {$self populate-flat-classifiers $n}

这个脚本函数populate-flat-classifiers,在Simulator::command由对应的同名命令,就是接下来的流程处理由C++代码接管。之后通过查表的方式建立路由表,将目的节点的id需要经过的链路与slot联系起来。就是说一个节点(A),需要发送数据包到一个目的节点(B),就是需要知道A发送的数据需要经过从A引出的哪一条链路上。而slot就是负责这个的。

void DestHashClassifier::do_install(char* dst, NsObject *target) {nsaddr_t d = atoi(dst);int slot = getnxt(target);install(slot, target); }void Classifier::install(int slot, NsObject* p){if (slot >= nslot_)alloc(slot);slot_[slot] = p;if (slot >= maxslot_)maxslot_ = slot;}


[1]Simple Simulation Example

[2]NS代码分析

原创粉丝点击