发布时间 : 星期二 文章网络模拟和协议仿真课程设计报告更新完毕开始阅读c5b03bfc770bf78a6529549e
杭州电子科技大学
基站接收到Hello 包,丢弃;节点接收到自身广播的Hello 包,丢弃;节点接收到有效Hello 包,更新本节点的跳步数,将上一跳节点信息加入邻居表,并广播自身;丢弃重复邻居Hello 包和非邻居包。
5> HelloTimer定时器
运行一段时间的网络会不稳定(包括网络中有新结点加入,节点寿命结束,能量用尽等),用HelloTimer 定时器定时发送Hello 包,更新邻居信息,建立新的树状网络结构。
sdrad.h 中HelloTimer 的定义:
class SDRAD_HelloTimer : public Handler { public:
SDRAD_HelloTimer(SDRAD* a) : agent(a) {} void handle(Event*); private:
SDRAD *agent; Event intr; };
重写handle 函数,使基站发送Hello 包,建立新的网络结构: void SDRAD_HelloTimer::handle(Event*) { agent->nb_purge(); //清楚邻居表
agent->sendHello(true); //基站发送Hello 包 //该定时器每个50s 调度一次
Scheduler::instance().schedule(this, &intr, 50); }
6> Resolve函数
void SDRAD:resolve(Packet *p)
节点通过第一节点建立好网络结构后,就进入数据收集阶段,此协议通过将有用数据处理成Collect 包在网络之间传送。在recv函数中节点在接收到有用数据后,通过resolve 函数将其处理成collect 包后转发给邻居节点。
7> 下一跳函数
u_int32_t SDRAD::nexthop_random() 随机选择邻居表中节点编号
8> 转发函数forward
void SDRAD::forward(u_int32_t seqno,Packet *p, double delay)
9> 发送collect包函数
void SDRAD::sendCollect(u_int32_t ipdst)
节点根据参数ipdst 确定下一跳目的地,发送collect包。
10> 接收collect包函数
void SDRAD::recvCollect(Packet *p)
9
杭州电子科技大学
节点随机选择下一跳,发送collect 包。 基站处理数据,丢弃废包。
3.5 将SDRAD整合到NS-2.35中
3.5.1 建立协议文件
本协议程序共五个文件,分别是: sdrad_packet.h、sdrad_nbtable.h、sdrad.h 和sdrad.cc。在
需要改五个文件:ns-2.35/common/packet.h
ns-2.35/tcl/lib/ns-lib.tcl
ns-2.35/tcl/lib/ns-packet.tcl
ns-2.35/Makefile
1> ns-2.35/common/packet.h文件中找到如下位置,添加红色标记的一行。 static const packet_t PT_PROTONAME = 73; // insert new packet types here
static packet_t PT_NTYPE = 75; // This MUST be the LAST one 找到如下位置,添加红色标记的一行。 name_[PT_DCCP_RESET]=\ name_[PT_PROTONAME]= \name_[PT_SDRAD]=\
name_[PT_NTYPE]= \
2> 在ns-2.35/tcl/lib/ns-lib.tcl文件中找到如下位置,添加红色标记的一段代码。 AODV {
set ragent [$self create-aodv-agent $node] }
SDRAD {
set ragent [$self create-sdrad-agent $node] }
AOMDV {
set ragent [$self create-aomdv-agent $node] }
在合适的位置加入如下代码:
Simulator instproc create-sdrad-agent { node } { #Create SDRAD routing agent
set ragent [new Agent/SDRAD [$node id]] puts \测试 $self at 0.0 \在0.0s 启动协议
10
杭州电子科技大学
$node set ragent_ $ragent return $ragent
}
3> 在ns-2.35/tcl/lib/ns-packet.tcl 文件中找到如下位置,添加红色标记部分。 MDART # routing protocol for ad-hoc networks # AOMDV patch AOMDV Protoname SDRAD#
4> 在ns-2.35/Makefile文件中找到如下位置,添加红色标记部分
wpan/p802_15_4trace.o wpan/p802_15_4transac.o \\ apps/pbc.o \\
protoname/protoname.o protoname/protoname_rtable.o \\ sdrad/sdrad.o \\ $(OBJ_STL) 3.5.3 编译NS-2.35源文件
在cygwin中运行如下命令完成源文件的编译。
huger@huger-PC ~/ns-allinone-2.35 $ cd ns-2.35
huger@huger-PC ~/ns-allinone-2.35/ns-2.35 $ make clean
huger@huger-PC ~/ns-allinone-2.35/ns-2.35 $ make
11
杭州电子科技大学
第四章 仿真验证
4.1 编写仿真tcl脚本sdrad.tcl
#Agent/UDP set packetSize_ 6000 #Define options
set val(ifqlen) 50 ;#max packet in ifq
set val(nn) 7 ;#number of mobilenodes set val(rp) SDRAD
set val(chan) Channel/WirelessChannel set val(prop) Propagation/TwoRayGround set val(netif) Phy/WirelessPhy set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue set val(ll) LL
set val(ant) Antenna/OmniAntenna set val(stop) 20
#============================================ # Main Program
#============================================
#ns_ random 0
#Initialize Global Variables set ns_ [new Simulator] set tracefd [open sdrad.tr w] $ns_ trace-all $tracefd
set namtrace [open sdrad.nam w]
$ns_ namtrace-all-wireless $namtrace 1000 500
#set up topography
set topo [new Topography] $topo load_flatgrid 1000 500
#Create God
create-god $val(nn)
#Create the specified nunber of mobilenodes[$val(nn)] and \#to the channel.
12