有线节点与无线节点的混合仿真模拟实验
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]
nstrace?allnstrace?alltracefd
set namtrace [open simwrls.nam w]
nsnamtrace?allnsnamtrace?allnamtrace
nsnamtrace?all?wirelessnsnamtrace?all?wirelessnamtrace?val(x)val(x)val(y)
#設定拓撲對象
set topo [new Topography]
topoloadflatgridtopoloadflatgridval(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
#nsinitialnodeposnsinitialnodeposSerNode 10
?
#設置移動節點的屬性
nsnode?config?adhocRoutingnsnode?config?adhocRoutingval(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<i<val(nn) } {incr i} {
set node_(i)[i)[ns node]
#nsinitialnodeposnsinitialnodeposnode_($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
?
#設定有線鏈路的帶寬,時延,隊列類型
nsduplex?linknsduplex?linkSerNode $node_(1) 2Mb 150ms DropTail
nsduplex?linknsduplex?linkSerNode $node_(2) 2Mb 150ms DropTail
nsduplex?linknsduplex?linkSerNode $node_(4) 2Mb 150ms DropTail
nsduplex?linknsduplex?linkSerNode $node_(8) 2Mb 150ms DropTail
nsduplex?linknsduplex?linkSerNode $node_(9) 2Mb 150ms DropTail
#設定鏈路隊列大小
nsqueue?limitnsqueue?limitSerNode $node_(1) 20
nsqueue?limitnsqueue?limitSerNode $node_(2) 20
nsqueue?limitnsqueue?limitSerNode $node_(4) 20
nsqueue?limitnsqueue?limitSerNode $node_(8) 20
nsqueue?limitnsqueue?limitSerNode $node_(9) 20
?
#監測節點間的隊列
#數據包進入隊列的方位設置,此表示數據包從上到下進入隊列
nsduplex?link?opnsduplex?link?opSerNode $node_(1) queuePos 0.5
nsduplex?link?opnsduplex?link?opSerNode $node_(2) queuePos 0.5
nsduplex?link?opnsduplex?link?opSerNode $node_(4) queuePos 0.5
nsduplex?link?opnsduplex?link?opSerNode $node_(8) queuePos 0.5
nsduplex?link?opnsduplex?link?opSerNode $node_(9) queuePos 0.5
?
#建立tcp連接
set tcp0 [new Agent/TCP]
$ns color 1 Yellow
$tcp0 set class_ 1
nsattach?agentnsattach?agentSerNode $tcp0
set sink0 [new Agent/TCPSink]
nsattach?agentnsattach?agentnode_(1) $sink0
nsconnectnsconnecttcp0 $sink0
?
set tcp1 [new Agent/TCP]
$ns color 1 Yellow
$tcp1 set class_ 1
nsattach?agentnsattach?agentSerNode $tcp1
set sink1 [new Agent/TCPSink]
nsattach?agentnsattach?agentnode_(2) $sink1
nsconnectnsconnecttcp1 $sink1
?
set tcp2 [new Agent/TCP]
$ns color 1 Yellow
$tcp2 set class_ 1
nsattach?agentnsattach?agentSerNode $tcp2
set sink2 [new Agent/TCPSink]
nsattach?agentnsattach?agentnode_(4) $sink2
nsconnectnsconnecttcp2 $sink2
?
set tcp3 [new Agent/TCP]
$ns color 1 Yellow
$tcp3 set class_ 1
nsattach?agentnsattach?agentSerNode $tcp3
set sink3 [new Agent/TCPSink]
nsattach?agentnsattach?agentnode_(8) $sink3
nsconnectnsconnecttcp3 $sink3
?
set tcp4 [new Agent/TCP]
$ns color 1 Yellow
$tcp4 set class_ 1
nsattach?agentnsattach?agentSerNode $tcp4
set sink4 [new Agent/TCPSink]
nsattach?agentnsattach?agentnode_(9) $sink4
nsconnectnsconnecttcp4 $sink4
#設定ftp應用程序
set ftp0 [new Application/FTP]
ftp0attach?agentftp0attach?agenttcp0
set ftp1 [new Application/FTP]
ftp1attach?agentftp1attach?agenttcp1
set ftp2 [new Application/FTP]
ftp2attach?agentftp2attach?agenttcp2
set ftp3 [new Application/FTP]
ftp3attach?agentftp3attach?agenttcp3
set ftp4 [new Application/FTP]
ftp4attach?agentftp4attach?agenttcp4
?
#建立udp連接
set udp0 [new Agent/UDP]
$ns set color 2 Red
$udp0 set class_ 2
nsattach?agentnsattach?agentnode_(2) $udp0
set null0 [new Agent/Null]
nsattach?agentnsattach?agentnode_(5) $null0
nsconnectnsconnectudp0 $null0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize0 100
$cbr0 set rate0 2Mb
cbr0attach?agentcbr0attach?agentudp0
?
set udp1 [new Agent/UDP]
$ns set color 2 Red
$udp1 set class_ 2
nsattach?agentnsattach?agentnode_(1) $udp1
set null1 [new Agent/Null]
nsattach?agentnsattach?agentnode_(6) $null1
nsconnectnsconnectudp1 $null1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize1 100
$cbr1 set rate1 2Mb
cbr1attach?agentcbr1attach?agentudp1
?
set udp2 [new Agent/UDP]
$ns set color 2 Red
$udp2 set class_ 2
nsattach?agentnsattach?agentnode_(9) $udp2
set null2 [new Agent/Null]
nsattach?agentnsattach?agentnode_(7) $null2
nsconnectnsconnectudp2 $null2
set cbr2 [new Application/Traffic/CBR]
$cbr2 set packetSize2 100
$cbr2 set rate2 2Mb
cbr2attach?agentcbr2attach?agentudp2
#模擬節點的運動位置和速度
nsat1.0"nsat1.0"node_(5) setdest 850.0 100.0 50.0"
nsat2.5"nsat2.5"node_(6) setdest 900.0 100.0 80.0"
nsat13.0"nsat13.0"node_(7) setdest 500.0 100.0 30.0"
#時間調度器設置流量發生時間
nsat1.0"nsat1.0"ftp0 start"
nsat30.5"nsat30.5"ftp0 stop"
nsat1.0"nsat1.0"ftp1 start"
nsat30.5"nsat30.5"ftp1 stop"
nsat1.0"nsat1.0"ftp2 start"
nsat30.5"nsat30.5"ftp2 stop"
nsat1.0"nsat1.0"ftp3 start"
nsat30.5"nsat30.5"ftp3 stop"
nsat1.0"nsat1.0"ftp4 start"
nsat30.5"nsat30.5"ftp4 stop"
nsat1.0"nsat1.0"cbr0 start"
nsat16.2"nsat16.2"cbr0 stop"
nsat2.0"nsat2.0"cbr1 start"
nsat13.0"nsat13.0"cbr1 stop"
nsat13.0"nsat13.0"cbr2 start"
nsat24.0"nsat24.0"cbr2 stop"
?
#結束nam和simulator
nsatnsatval(stop) "stop"
nsatnsatval(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
有圖有真相:
轉載于:https://www.cnblogs.com/zhangyuhang3/p/6868448.html
總結
以上是生活随笔為你收集整理的有线节点与无线节点的混合仿真模拟实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql union 语句 ca
- 下一篇: Linux磁盘分区(二):删除