有线节点与无线节点的混合仿真模拟实验

简介: Author: bakari  Date: 2011.11.23 很久之前自己写的一个NS2 的例子,(一个有线和无线相结合的例子,对于初学很有帮助)欢迎交流! # Define options set val(chan) Channel/WirelessChannel ;# 物理信道类型 ...

Author: bakari  Date: 2011.11.23

很久之前自己写的一个NS2 的例子,(一个有线和无线相结合的例子,对于初学很有帮助)欢迎交流!

# Define options

set val(chan) Channel/WirelessChannel ;# 物理信道类型

set val(prop) Propagation/TwoRayGround ;# 设定无限传输模型

set val(netif) Phy/WirelessPhy ;# 网络接口类型

set val(mac) Mac/802_11 ;# MAC 层类型

set val(ifq) Queue/DropTail/PriQueue ;# 接口队列类型

set val(ll) LL ;# LLC 层类型

set val(ant) Antenna/OmniAntenna ;# 天线模型

set val(ifqlen) 50 ;# 网络接口队列的大小

set val(severnode) 1 ;# servre节点的数目

set val(nn) 10 ;# 移动节点的数目

set val(rp) DSDV ;# 设定无线路由协议

set val(x) 1000 ;# 设定拓扑范围

set val(y) 1000 ;# 设定拓扑范围

set val(stop) 31 ;#模拟的总时间

set AgentTrace ON

set RouterTrace ON

set MacTrace OFF

 

#设定模拟器类型

set ns [new Simulator]


#设定跟踪文件

set tracefd [open simple.tr w]

$ns trace-all $tracefd

set namtrace [open simwrls.nam w]

$ns namtrace-all $namtrace

$ns namtrace-all-wireless $namtrace $val(x) $val(y)

#设定拓扑对象

set topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

set chan [new $val(chan)]

#设定god对象

# 建立一个God对象。God对象主要用来对路由协议做性能评价,它存储了节点总数,

#节点间最短路径等信息。节点的MAC对象会调用God对象,因此即使不使用也仍然要建立此对象

create-god $val(nn)

 

#设定sever节点

set SerNode [$ns node]

$SerNode set X_ 500

$SerNode set Y_ 800

$SerNode set Z_ 0

#$ns initial_node_pos $SerNode 10

 

#设置移动节点的属性

$ns node-config -adhocRouting $val(rp) \

-llType $val(ll) \

-macType $val(mac) \

-ifqType $val(ifq) \

-ifqLen $val(ifqlen) \

-antType $val(ant) \

-propType $val(prop) \

-phyType $val(netif) \

-channel $chan \

-topoInstance $topo \

-agentTrace ON \

-routerTrace ON \

-macTrace OFF \

-movementTrace OFF \

-wiredRouting ON

 

# !如果一个节点放在移动节点属性之前,它就成为了有线链路的节点,如果放在移动节点属性下边,它就成了无线节点。

for {set i 1} {$i < $val(nn) } {incr i} {

set node_($i) [$ns node]

#$ns initial_node_pos $node_($i) 10

}

#设定节点的初始位置

$node_(1) set X_ 100

$node_(1) set Y_ 300

$node_(1) set Z_ 0

 

$node_(2) set X_ 500

$node_(2) set Y_ 300

$node_(2) set Z_ 0

 

#$node_(3) set X_ 120

#$node_(3) set Y_ 80

#$node_(3) set Z_ 0

 

$node_(4) set X_ 900

$node_(4) set Y_ 300

$node_(4) set Z_ 0

 

$node_(5) set X_ 200

$node_(5) set Y_ 100

$node_(5) set Z_ 0

 

$node_(6) set X_ 100

$node_(6) set Y_ 100

$node_(6) set Z_ 0

 

$node_(7) set X_ 800

$node_(7) set Y_ 100

$node_(7) set Z_ 0

 

$node_(8) set X_ 300

$node_(8) set Y_ 300

$node_(8) set Z_ 0

 

$node_(9) set X_ 700

$node_(9) set Y_ 300

$node_(9) set Z_ 0

 

#设定有线链路的带宽,时延,队列类型

$ns duplex-link $SerNode $node_(1) 2Mb 150ms DropTail

$ns duplex-link $SerNode $node_(2) 2Mb 150ms DropTail

$ns duplex-link $SerNode $node_(4) 2Mb 150ms DropTail

$ns duplex-link $SerNode $node_(8) 2Mb 150ms DropTail

$ns duplex-link $SerNode $node_(9) 2Mb 150ms DropTail

#设定链路队列大小

$ns queue-limit $SerNode $node_(1) 20

$ns queue-limit $SerNode $node_(2) 20

$ns queue-limit $SerNode $node_(4) 20

$ns queue-limit $SerNode $node_(8) 20

$ns queue-limit $SerNode $node_(9) 20

 

#监测节点间的队列

#数据包进入队列的方位设置,此表示数据包从上到下进入队列

$ns duplex-link-op $SerNode $node_(1) queuePos 0.5

$ns duplex-link-op $SerNode $node_(2) queuePos 0.5

$ns duplex-link-op $SerNode $node_(4) queuePos 0.5

$ns duplex-link-op $SerNode $node_(8) queuePos 0.5

$ns duplex-link-op $SerNode $node_(9) queuePos 0.5

 

#建立tcp连接

set tcp0 [new Agent/TCP]

$ns color 1 Yellow

$tcp0 set class_ 1

$ns attach-agent $SerNode $tcp0

set sink0 [new Agent/TCPSink]

$ns attach-agent $node_(1) $sink0

$ns connect $tcp0 $sink0

 

set tcp1 [new Agent/TCP]

$ns color 1 Yellow

$tcp1 set class_ 1

$ns attach-agent $SerNode $tcp1

set sink1 [new Agent/TCPSink]

$ns attach-agent $node_(2) $sink1

$ns connect $tcp1 $sink1

 

set tcp2 [new Agent/TCP]

$ns color 1 Yellow

$tcp2 set class_ 1

$ns attach-agent $SerNode $tcp2

set sink2 [new Agent/TCPSink]

$ns attach-agent $node_(4) $sink2

$ns connect $tcp2 $sink2

 

set tcp3 [new Agent/TCP]

$ns color 1 Yellow

$tcp3 set class_ 1

$ns attach-agent $SerNode $tcp3

set sink3 [new Agent/TCPSink]

$ns attach-agent $node_(8) $sink3

$ns connect $tcp3 $sink3

 

set tcp4 [new Agent/TCP]

$ns color 1 Yellow

$tcp4 set class_ 1

$ns attach-agent $SerNode $tcp4

set sink4 [new Agent/TCPSink]

$ns attach-agent $node_(9) $sink4

$ns connect $tcp4 $sink4

#设定ftp应用程序

set ftp0 [new Application/FTP]

$ftp0 attach-agent $tcp0

set ftp1 [new Application/FTP]

$ftp1 attach-agent $tcp1

set ftp2 [new Application/FTP]

$ftp2 attach-agent $tcp2

set ftp3 [new Application/FTP]

$ftp3 attach-agent $tcp3

set ftp4 [new Application/FTP]

$ftp4 attach-agent $tcp4

 

#建立udp连接

set udp0 [new Agent/UDP]

$ns set color 2 Red

$udp0 set class_ 2

$ns attach-agent $node_(2) $udp0

set null0 [new Agent/Null]

$ns attach-agent $node_(5) $null0

$ns connect $udp0 $null0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize0 100

$cbr0 set rate0 2Mb

$cbr0 attach-agent $udp0

 

set udp1 [new Agent/UDP]

$ns set color 2 Red

$udp1 set class_ 2

$ns attach-agent $node_(1) $udp1

set null1 [new Agent/Null]

$ns attach-agent $node_(6) $null1

$ns connect $udp1 $null1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize1 100

$cbr1 set rate1 2Mb

$cbr1 attach-agent $udp1

 

set udp2 [new Agent/UDP]

$ns set color 2 Red

$udp2 set class_ 2

$ns attach-agent $node_(9) $udp2

set null2 [new Agent/Null]

$ns attach-agent $node_(7) $null2

$ns connect $udp2 $null2

set cbr2 [new Application/Traffic/CBR]

$cbr2 set packetSize2 100

$cbr2 set rate2 2Mb

$cbr2 attach-agent $udp2

#模拟节点的运动位置和速度

$ns at 1.0 "$node_(5) setdest 850.0 100.0 50.0"

$ns at 2.5 "$node_(6) setdest 900.0 100.0 80.0"

$ns at 13.0 "$node_(7) setdest 500.0 100.0 30.0"

#时间调度器设置流量发生时间

$ns at 1.0 "$ftp0 start"

$ns at 30.5 "$ftp0 stop"

$ns at 1.0 "$ftp1 start"

$ns at 30.5 "$ftp1 stop"

$ns at 1.0 "$ftp2 start"

$ns at 30.5 "$ftp2 stop"

$ns at 1.0 "$ftp3 start"

$ns at 30.5 "$ftp3 stop"

$ns at 1.0 "$ftp4 start"

$ns at 30.5 "$ftp4 stop"

$ns at 1.0 "$cbr0 start"

$ns at 16.2 "$cbr0 stop"

$ns at 2.0 "$cbr1 start"

$ns at 13.0 "$cbr1 stop"

$ns at 13.0 "$cbr2 start"

$ns at 24.0 "$cbr2 stop"

 

#结束nam和simulator

$ns at $val(stop) "stop"

$ns at $val(stop) "puts \"end simulator\"; $ns halt"

proc stop {} {

global ns tracefd namtrace

$ns flush-trace

close $tracefd

close $namtrace

exec nam simwrls.nam &

exit 0

}

#开始模拟

$ns run

有图有真相:

目录
相关文章
|
1天前
|
云安全 人工智能 自然语言处理
|
8天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
647 56
Meta SAM3开源:让图像分割,听懂你的话
|
6天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
318 116
|
5天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
21天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
436 32
|
4天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
272 132

热门文章

最新文章