版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第4章例1#建立一個(gè)模擬 set ns new Simulator#定義不同數(shù)據(jù)流的顏色(NAM顯示時(shí)用到)$ns color 1 Blue$ns color 2 Red#開(kāi)啟Trace跟蹤和NAM跟蹤 set tracefd open wired.tr w $ns trace-all $tracefd set nf open wired.nam w $ns namtrace-all $nf#定義結(jié)束進(jìn)程 proc finish global ns tracefd nf $ns flush-trace close $tracefd close $nf exit 0#定義節(jié)點(diǎn)set n0 $ns
2、 node set n1 $ns node set n2 $ns node set n3 $ns node set n4 $ns node set n5 $ns node #$n5 color black $n5 color red#定義節(jié)點(diǎn)間的鏈路$ns duplex-link $n0 $n1 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n4 2Mb 20ms DropTail建立一個(gè)數(shù)據(jù)發(fā)送弋理 將數(shù)據(jù)發(fā)送弋理綁定到發(fā)送節(jié)點(diǎn) 建立一個(gè)數(shù)據(jù)接收弋理 將數(shù)據(jù)接收弋理綁定到接收節(jié)
3、點(diǎn)連接兩個(gè)弋理 (也就決定了數(shù)據(jù)包的發(fā)送和接收節(jié)點(diǎn))$ns duplex-link $n3 $n4 2Mb 10ms DropTail$ns duplex-link $n4 $n5 2Mb 10ms DropTail #定義鏈路的隊(duì)列長(zhǎng)度$ns queue-limit $n1 $n4 10#指定節(jié)點(diǎn)間的相互位置(NAM顯示用到) $ns duplex-link-op $n0 $n1 orient right-down $ns duplex-link-op $n2 $n1 orient right-up $ns duplex-link-op $n1 $n4 orient right $ns du
4、plex-link-op $n3 $n4 orient left-down $ns duplex-link-op $n5 $n4 orient left-up#監(jiān)視鏈路的隊(duì)列$ns duplex-link-op $n1 $n4 queuePos 0.5#建立一個(gè)TCP連接set tcp new Agent/TCP$tcp set class_ 2$ns attach-agent $n0 $tcp set sink new Agent/TCPSink $ns attach-agent $n5 $sink $ns connect $tcp $sink $tcp set fid_ 1 #在TCP連接
5、上建立FTP流set ftp new Application/FTP$ftp attach-agent $tcp$ftp set type_ FTP#建立一個(gè)UDP弋理set udp new Agent/UDP;#$ns attach-agent $n2 $udp;#set null new Agent/Null ;# $ns attach-agent $n3 $null ;# $ns connect $udp $null ;# $udp set fid_ 2 #在UDP弋理上建立CBR流set cbr new Application/Traffic/CBR $cbr attach-agent
6、 $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb無(wú)線信道信道模型:無(wú)線物理層$cbr set random_ false #啟動(dòng)和結(jié)束流代理$ns at 0.5 $cbr start$ns at 1.0 $ftp start$ns at 9.0 $ftp stop$ns at 9.5 $cbr stop$ns at 9.5 $ns detach-agent $n0 $tcp; $ns detach-agent $n5 $sink#仿真結(jié)束時(shí)調(diào)用結(jié)束進(jìn)程$ns at 10.0 finish#打印CBR數(shù)據(jù)包的大
7、小和間隔puts CBR packet_size_ = $cbr set packet_size_puts CBR interval = $cbr set interval_#執(zhí)行模擬$ns run例2#簡(jiǎn)單無(wú)線網(wǎng)絡(luò)模型模擬#無(wú)線節(jié)點(diǎn)參數(shù)set val(chan) Channel/WirelessChannel ;# channel type信道類(lèi)型:set val(prop) Propagation/TwoRayGround ;# radio-propagation model TwoRayGroundset val(netif) Phy/WirelessPhy ;# network int
8、erface typeset val(mac) Mac/802_11;# MAC type MAC層協(xié)議set val(ifq) Queue/DropTail/PriQueue ;# interface queue typeset val(ll) LL ;# link layer typeset val(ant)Antenna/OmniAntenna ;# antenna modelset val(ifqlen)50;# max packet in ifqset val(rp)AODV;# 路由協(xié)議set val(x)600;# 拓?fù)溟L(zhǎng)度set val(y)200;# 拓?fù)鋵挾萻et val(
9、stop)10.0;# time of simulation end# 建立一個(gè) simulator 實(shí)例 set ns new Simulator#$ns use-newtrace #開(kāi)啟Trace跟蹤和NAM艮蹤set tracefd open wireless.tr wset namtrace open wireless.nam w$ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y)#建立 topology 對(duì)象set topo new Topography$topo load_flatgrid
10、 $val(x) $val(y)#創(chuàng)建 godcreate-god 3set chan_1_ new $val(chan)#配置無(wú)線節(jié)點(diǎn)(包括使用何種路由協(xié)議,何種ma(協(xié)議,無(wú)線信道的模型等等)$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_1_ -topoIns
11、tance $topo -agentTrace ON -routerTrace ON -macTrace ON -movementTrace OFF#建立無(wú)線節(jié)點(diǎn)并設(shè)置節(jié)點(diǎn)的位置(節(jié)點(diǎn)位置決定了拓?fù)浣Y(jié)構(gòu))set n(0) $ns node#$n(0) shape hexagon#$n(0) label n0#$n(0) label-color Red$n(0) random-motion 0$n(0) set X_ 100.0$n(0) set Y_ 100.0$n(0) set Z_ 0.0#建立一個(gè)UDP弋理set udp0 new Agent/UDP;#$ns attach-agent
12、$n(0) $udp0;#set null0 new Agent/Null ;# $ns attach-agent $n(2) $null0 ;# $ns connect $udp0 $null0 ;# 收節(jié)點(diǎn))建立一個(gè)數(shù)據(jù)發(fā)送弋理將數(shù)據(jù)發(fā)送弋理綁定到發(fā)送節(jié)點(diǎn)建立一個(gè)數(shù)據(jù)接收弋理將數(shù)據(jù)接收弋理綁定到接收節(jié)點(diǎn)連接兩個(gè)弋理(也就決定了數(shù)據(jù)包的發(fā)送和接$ns initial_node_pos $n(0) 20 set n(1) $ns node$n(1) random-motion 0$n(1) set X_ 300.0$n(1) set Y_ 100.0$n(1) set Z_ 0.0$ns in
13、itial_node_pos $n(1) 20set n(2) $ns node$n(2) random-motion 0$n(2) set X_ 500.0$n(2) set Y_ 100.0$n(2) set Z_ 0.0$ns initial_node_pos $n(2) 20#在UDP弋理上建立CBR流set cbr new Application/Traffic/CBR $cbr attach-agent $udp0# 仿真結(jié)束時(shí)重置節(jié)點(diǎn)for set i 0 $i 3 incr i $ns at 10.0 $n($i) reset;#啟動(dòng)和結(jié)束流弋理$ns at 0.5 $cbr
14、start$ns at 9.5 $cbr stop#定義結(jié)束進(jìn)程proc finish global ns tracefd namtrace$ns flush-traceclose $tracefdclose $namtrace;# 2 個(gè)網(wǎng)絡(luò);# 每個(gè)網(wǎng)絡(luò)一個(gè)子網(wǎng)個(gè)子網(wǎng)的節(jié)點(diǎn)數(shù)目為 1和 3exit 0#仿真結(jié)束時(shí)調(diào)用結(jié)束進(jìn)程$ns at $val(stop) finish$ns at $val(stop) puts NS EXISTING.; $ns haltputs Start Simulation.# run the simulation$ns run例3#有線無(wú)線網(wǎng)絡(luò)混合模擬#無(wú)線節(jié)
15、點(diǎn)參數(shù)set val(chan) Channel/WirelessChannel ;# channel type 信道類(lèi)型:無(wú)線信道set val(prop) Propagation/TwoRayGround ;# radio-propagation model信道模型:TwoRayGroundset val(netif)Phy/WirelessPhy;# network interface type無(wú)線物理層set val(mac)Mac/802_11;# MAC type MAC層協(xié)議set val(ifq)Queue/DropTail/PriQueue ;# interface queu
16、e typeset val(ll)LL;# link layer typeset val(ant)Antenna/OmniAntenna;# antenna modelset val(ifqlen)50;# max packet in ifqset val(rp)DSDV;# 路由協(xié)議set val(nn)4;# 節(jié)點(diǎn)數(shù)目set val(x)600)set val(y)600)set val(stop)10.0;# time of simulation end# 建立一個(gè) simulator 實(shí)例 set ns new Simulator #設(shè)定分層路由地址$ns node-config -a
17、ddressType hierarchicalAddrParams set domain_num_ 2lappend cluster_num 1 1AddrParams set cluster_num_ $cluster_numlappend eilastlevel 1 3;# 2AddrParams set nodes_num_ $eilastlevel puts Configuration of hierarchical addressing done#$ns use-newtrace#設(shè)置 traceset tracefd open wired_wireless2.tr wset nam
18、trace open wired_wireless2.nam w$ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y)#建立 topology 對(duì)象set topo new Topography$topo load_flatgrid $val(x) $val(y)#創(chuàng)建 godcreate-god $val(nn)set sinkNode $ns node 0.0.0$sinkNode set X_ 500$sinkNode set Y_ 300$sinkNode set Z_ 0$ns initial
19、_node_pos $sinkNode 60set chan_1_ new $val(chan)#無(wú)線節(jié)點(diǎn)配置$ns node-config -wiredRouting ON -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_1_ -topoInstance $topo -agentTrace ON
20、 -routerTrace ON -macTrace ON -movementTrace OFF#新建BS節(jié)點(diǎn)set bs(0) $ns node 1.0.0$bs(0) random-motion 0 #節(jié)點(diǎn)標(biāo)簽與初始位置設(shè)定$bs(0) set X_ 200.0$bs(0) set Y_ 300.0$bs(0) set Z_ 0.0$ns initial_node_pos $bs(0) 60#定義節(jié)點(diǎn)間的鏈路$ns duplex-link $sinkNode $bs(0) 10Mb 1ms DropTail$ns duplex-link-op $sinkNode $bs(0) orient
21、 left#定義鏈路的隊(duì)列長(zhǎng)度#$ns queue-limit $bs(0) $sinkNode 10#監(jiān)視鏈路的隊(duì)列#$ns duplex-link-op $bs(0) $sinkNode queuePos 0.5$ns node-config -wiredRouting OFF -macType Mac/802_11#新建SS節(jié)點(diǎn)set ss(0) $ns node 1.0.1$ss(0) base-station AddrParams addr2id $bs(0) node-addr$ss(0) set X_ 50.0$ss(0) set Y_ 450.0$ss(0) set Z_ 0.
22、0$ns initial_node_pos $ss(0) 60set ss(1) $ns node 1.0.2$ss(1) base-station AddrParams addr2id $bs(0) node-addr$ss(1) set X_ 50.0$ss(1) set Y_ 150.0$ss(1) set Z_ 0.0$ns initial_node_pos $ss(1) 60set udp0 new Agent/UDP$ns attach-agent $ss(0) $udp0set null0 new Agent/Null$ns attach-agent $sinkNode $nul
23、l0 $ns connect $udp0 $null0 set ugs0 new Application/Traffic/UGS $ugs0 attach-agent $udp0set udp1 new Agent/UDP $ns attach-agent $sinkNode $udp1 set null1 new Agent/Null $ns attach-agent $ss(1) $null1 $ns connect $udp1 $null1 set ugs1 new Application/Traffic/UGS $ugs1 attach-agent $udp1$ns at 0.5 $u
24、gs0 start $ns at 1.0 $ugs1 start $ns at 9.0 $ugs1 stop $ns at 9.5 $ugs0 stopproc finish global ns tracefd namtrace $ns flush-trace close $tracefd close $namtrace exit 0#事件安排$ns at $val(stop) finish$ns at $val(stop) puts NS EXISTING.; $ns haltputs Start Simulation.# run the simulation $ns run第 10 章de
25、lay.awkBEGIN highest_uid=0; event = $1; time = $2; node = $3; len = length(node); if(len = 3) node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; else from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; if(len=3) if(event=s & nod
26、e_=2 & trace_type=AGT & pkt_type=cbr) start_timeuid = time; else if(event=r & to_node=0 & pkt_type=cbr) end_timeuid = time;if(highest_uid uid) highest_uid = uid;END id=1;k=0; total_delay=0; avg_delay=0;for(i=0; i 0) total_delay=total_delay+delay; k+;avg_delay=total_delay/k; printf(%f %.9fn,rate,avg_
27、delay);get_perform#!/bin/shi=1while $i -le 300 ; dons w_w.tcl $igawk - delay.awk rate=$i wired_wireless.tr delaygawk - loss.awk rate=$i wired_wireless.tr loss_rategawk -f throughtput.awk rate=$i wired_wireless.tr throughtput if $i -le 225 ; theni=$($i+20)elif $i -le 250 ; theni=$($i+5)elsei=$($i+2)f
28、idoneexit 0jitter.awkBEGIN highest_uid=0;event = $1;time = $2;node = $3;len = length(node);if(len = 3) node_ = substr(node,2,1); trace_type = $4;flag = $5;uid = $7; pkt_type = $8; pkt_size = $9; else from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12;if(len=3) if(event=s
29、 & node_=2 & trace_type=AGT & pkt_type=cbr) start_timeuid = time; else if(event=r & to_node=0 & pkt_type=cbr) end_timeuid = time;if(highest_uid uid) highest_uid = uid;END id=1;for(i=0; i=highest_uid; i+ )start = start_timei;end = end_timei;if(start end) delayi=end-start;last_delay=0;last_seqno=0;for
30、(i=0; i=highest_uid; i+)if(delayi!=0) if(last_delay=0) jitter=0; else jitter=(delayi-last_delay)/(i-last_seqno);printf(%d %.8fn,i,jitter); last_delay=delayi; last_seqno=i;loss.awkBEGIN fsDrops=0;numfs2=0;numfs0=0;event = $1;time = $2;node = $3;len = length(node);if(len = 3) node_ = substr(node,2,1);
31、 trace_type = $4; flag = $5;uid = $6;pkt_type = $7; pkt_size = $8; else from_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12;if(len=3) if(event=s & node_=2 & trace_type=AGT & pkt_type=cbr) numfs2+; else if(event=r & to_node=0 & pkt_type=cbr) numfs0+;END loss_rate=0; fsDrop
32、s=numfs2-numfs0;loss_rate=fsDrops/numfs2;printf(%f %.3fn,rate,loss_rate);plot_dataset multiplotset size 0.5,0.5set origin 0.0,0.0set xrange 225:250set xlabel data_rate(Kbps)set ylabel pkt_delay(s)plot delay with linespoints lw 2 pt 19set origin 0.5,0.0set ylabel loss_rate(100%)plot loss with linespo
33、ints lw 2 pt 19set origin 0.0,0.5set ylabel throughtput(Kbps)plot throughtput with linespoints lw 2 pt 19throughtput.awkBEGIN init=0;i=0;event = $1;time = $2;node = $3;len = length(node);if(len = 3) node_ = substr(node,2,1); trace_type = $4; flag = $5; uid = $6; pkt_type = $7; pkt_size = $8; else fr
34、om_node = $3; to_node =$4; pkt_type = $5; pkt_size = $6; flag = $7; uid = $12; if(len=1) if(event=r & to_node=0 & pkt_type=cbr) pkt_byte_sumi+1=pkt_byte_sumi+(pkt_size-20); if(init=0) start_time=time; init=1; end_timei=time; i+; END ;# 2 個(gè)網(wǎng)絡(luò);# 每個(gè)網(wǎng)絡(luò)一個(gè)子網(wǎng);# 2 個(gè)子網(wǎng)的節(jié)點(diǎn)數(shù)目為 1和 3th = 8*pkt_byte_sumi-1/(end_t
35、imei-1-start_time)/1000; printf(%f %.3fn, rate, th);wire_wireless.tcl# # 有線 +無(wú)線網(wǎng)絡(luò)混合模擬 # #增加全局變量 opt(rate) set opt(rate) 0 proc getopt argc argv global opt和過(guò)程 getopt;# 變量 opt(rate) 保存數(shù)據(jù)速率值; # 過(guò)程 getopt 獲取從命令行傳遞過(guò)來(lái)的數(shù)據(jù)速率值set opt(rate) lindex $argv 0#無(wú)線節(jié)點(diǎn)參數(shù)set val(chan)Channel/WirelessChannelset val(prop
36、)Propagation/TwoRayGroundset val(netif)Phy/WirelessPhyset val(mac)Mac/802_11set val(ifq)Queue/DropTail/PriQueueset val(ll)LLset val(ant)Antenna/OmniAntennaset val(ifqlen)50set val(rp)DSDVset val(nn)4set val(x)600set val(y)600set val(stop)10.0# 建立一個(gè) simulator 實(shí)例set ns new Simulator#設(shè)定分層路由地址$ns node-c
37、onfig -addressType hierarchicalAddrParams set domain_num_ 2lappend cluster_num 1 1AddrParams set cluster_num_ $cluster_num lappend eilastlevel 1 3AddrParams set nodes_num_ $eilastlevel puts Configuration of hierarchical addressing done#$ns use-newtrace#設(shè)置 traceset tracefd open wired_wireless.tr w se
38、t namtrace open wired_wireless.nam w $ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y)#建立 topology 對(duì)象set topo new Topography$topo load_flatgrid $val(x) $val(y)#創(chuàng)建 god create-god $val(nn)set sinkNode $ns node 0.0.0$sinkNode set X_ 500$sinkNode set Y_ 300$sinkNode set Z_ 0$ns i
39、nitial_node_pos $sinkNode 60set chan_1_ new $val(chan)#無(wú)線節(jié)點(diǎn)配置$ns node-config -wiredRouting ON -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_1_ -topoInstance $topo -agentTr
40、ace ON -routerTrace ON -macTrace ON -movementTrace OFF#新建BS節(jié)點(diǎn) set bs(0) $ns node 1.0.0$bs(0) random-motion 0# 節(jié)點(diǎn)標(biāo)簽與初始位置設(shè)定$bs(0) set X_ 200.0$bs(0) set Y_ 300.0$bs(0) set Z_ 0.0$ns initial_node_pos $bs(0) 60#定義節(jié)點(diǎn)間的鏈路$ns duplex-link $sinkNode $bs(0) 10Mb 1ms DropTail$ns duplex-link-op $sinkNode $bs(0)
41、 orient left#定義鏈路的隊(duì)列長(zhǎng)度#$ns queue-limit $bs(0) $sinkNode 10#監(jiān)視鏈路的隊(duì)列#$ns duplex-link-op $bs(0) $sinkNode queuePos 0.5$ns node-config -wiredRouting OFF -macType Mac/802_11#新建SS節(jié)點(diǎn)set ss(0) $ns node 1.0.1$ss(0) base-station AddrParams addr2id $bs(0) node-addr$ss(0) set X_ 50.0$ss(0) set Y_ 450.0$ss(0) se
42、t Z_ 0.0$ns initial_node_pos $ss(0) 60set ss(1) $ns node 1.0.2$ss(1) base-station AddrParams addr2id $bs(0) node-addr$ss(1) set X_ 50.0$ss(1) set Y_ 150.0$ss(1) set Z_ 0.0$ns initial_node_pos $ss(1) 60getopt $argc $argv ;# 調(diào)用過(guò)程 getopt 獲取數(shù)據(jù)發(fā)送速率 puts opt(rate)=$opt(rate)set udp0 new Agent/UDP$ns attac
43、h-agent $ss(0) $udp0set null0 new Agent/Null$ns attach-agent $sinkNode $null0的值的值$ns connect $udp0 $null0set cbr0 new Application/Traffic/CBR$cbr0 attach-agent $udp0$cbr0 set rate_ $opt(rate)Kb ;# 數(shù)據(jù)發(fā)送速率值使用變量 opt(rate)set udp1 new Agent/UDP$ns attach-agent $sinkNode $udp1set null1 new Agent/Null$ns
44、attach-agent $ss(1) $null1$ns connect $udp1 $null1set cbr1 new Application/Traffic/CBR$cbr1 attach-agent $udp1$cbr1 set rate_ $opt(rate)Kb ;# 數(shù)據(jù)發(fā)送速率值使用變量 opt(rate)$ns at 0.5 $cbr0 start$ns at 1.0 $cbr1 start$ns at 9.0 $cbr1 stop$ns at 9.5 $cbr0 stopproc finish global ns tracefd namtrace$ns flush-tra
45、ceclose $tracefdclose $namtraceexit 0#事件安排$ns at $val(stop) finish$ns at $val(stop) puts NS EXISTING.; $ns haltputs Start Simulation.# run the simulation$ns run第 11 章set val(chan)Channel/WirelessChannel ;# channel type信道類(lèi)型:set val(prop) Propagation/TwoRayGround ;# radio-propagation model TwoRayGroun
46、dset val(netif) Phy/WirelessPhy;# network interface type無(wú)線信道信道模型:無(wú)線物理層set val(mac)Mac/802_11;# MAC type MAC層協(xié)議set val(ifq) Queue/DropTail/PriQueue ;# interface queue type set val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna;# antenna modelset val(ifqlen) 50 set val(rp)MFloodset val(x)60
47、0set val(y)200set val(stop) 10.0;# max packet in ifq;#;#;#路由協(xié)議拓?fù)溟L(zhǎng)度拓?fù)鋵挾?# time of simulation end# 建立一個(gè) simulator 實(shí)例 set ns new Simulator #$ns use-newtrace#開(kāi)啟Trace跟蹤和NAM艮蹤set tracefd open wireless.tr wset namtrace open wireless.nam w$ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $va
48、l(y)#建立 topology 對(duì)象 set topo new Topography $topo load_flatgrid $val(x) $val(y)#創(chuàng)建 god create-god 3set chan_1_ new $val(chan)#配置無(wú)線節(jié)點(diǎn)(包括使用何種路由協(xié)議,何種$ns node-config -adhocRouting $val(rp) -llType $val(ll) ma(協(xié)議,無(wú)線信道的模型等等)wireless.tcl#簡(jiǎn)單無(wú)線網(wǎng)絡(luò)模型模擬#無(wú)線節(jié)點(diǎn)參數(shù)建立一個(gè)數(shù)據(jù)發(fā)送弋理將數(shù)據(jù)發(fā)送弋理綁定到發(fā)送節(jié)點(diǎn)建立一個(gè)數(shù)據(jù)接收弋理;#-macType $val(ma
49、c) -ifqType $val(ifq) -ifqLen $val(ifqlen) -antType $val(ant) -propType $val(prop) -phyType $val(netif) -channel $chan_1_ -topoInstance $topo -agentTrace ON -routerTrace ON -macTrace ON -movementTrace OFF#建立無(wú)線節(jié)點(diǎn)并設(shè)置節(jié)點(diǎn)的位置(節(jié)點(diǎn)位置決定了拓?fù)浣Y(jié)構(gòu)) set n(0) $ns node #$n(0) shape hexagon #$n(0) label n0#$n(0) label-
50、color Red$n(0) random-motion 0$n(0) set X_ 100.0$n(0) set Y_ 100.0$n(0) set Z_ 0.0$ns initial_node_pos $n(0) 20set n(1) $ns node$n(1) random-motion 0$n(1) set X_ 300.0$n(1) set Y_ 100.0$n(1) set Z_ 0.0$ns initial_node_pos $n(1) 20set n(2) $ns node$n(2) random-motion 0$n(2) set X_ 500.0$n(2) set Y_ 1
51、00.0$n(2) set Z_ 0.0$ns initial_node_pos $n(2) 20#建立一個(gè)UDP弋理set udp0 new Agent/UDP;#$ns attach-agent $n(0) $udp0;#set null0 new Agent/Null$ns attach-agent $n(2) $null0 ;# 將數(shù)據(jù)接收代理綁定到接收節(jié)點(diǎn)$ns connect $udp0 $null0 ;# 連接兩個(gè)代理(也就決定了數(shù)據(jù)包的發(fā)送和接 收節(jié)點(diǎn))#在UDP弋理上建立CBR流set cbr new Application/Traffic/CBR$cbr attach-ag
52、ent $udp0# 仿真結(jié)束時(shí)重置節(jié)點(diǎn)for set i 0 $i 3 incr i $ns at 10.0 $n($i) reset;#啟動(dòng)和結(jié)束流弋理$ns at 0.5 $cbr start$ns at 9.5 $cbr stop#定義結(jié)束進(jìn)程proc finish global ns tracefd namtrace$ns flush-traceclose $tracefdclose $namtraceexit 0#仿真結(jié)束時(shí)調(diào)用結(jié)束進(jìn)程$ns at $val(stop) finish$ns at $val(stop) puts NS EXISTING.; $ns haltputs
53、Start Simulation.# run the simulation$ns runmflood.cc#include #include #include #include / 添加洪泛包類(lèi)型int hdr_mflood:offset_;static class MFloodHeaderClass : public PacketHeaderClass public:MFloodHeaderClass() : PacketHeaderClass(PacketHeader/MFlood, sizeof(hdr_mflood) bind_offset(&hdr_mflood:offset_);
54、class_mfloodhdr;MFlood:MFlood(nsaddr_tid) : Agent(PT_MFLOOD)/,dmux_(0)/,port_dmux_(0) /構(gòu)造函數(shù)index_ = id;/保存 iplogtarget = 0;myseq_ = 0;/ 保存自己產(chǎn)生的封包的最大序號(hào)值void MFlood:Rt_resolve(Packet *p) struct hdr_cmn *ch = HDR_CMN(p);/普通頭部struct hdr_ip *ih = HDR_IP(p);/IP頭部struct hdr_mflood *fh = HDR_MFLOOD(p);/ 洪泛協(xié)
55、議頭部 MFlood_RTEntry* rt;rt = rtable_.rt_lookup(ih-saddr();/ 獲取包的源地址if(rt = NULL) / 如果包的源地址沒(méi)有被保存,保存該包的源地址,并對(duì)該包進(jìn)行轉(zhuǎn)發(fā) rt = new MFlood_RTEntry(ih-saddr(), fh-seq_); LIST_INSERT_HEAD(&rtable_.rthead,rt,rt_link);forward(rt,p,FORWARD_DELAY); rtable_.rt_print(); else if(rt-isNewSeq(fh-seq_) /如果已經(jīng)保存了該包的源地址,判斷該
56、包的有效性,如果是新的封包,進(jìn)行轉(zhuǎn)發(fā)forward(rt, p, FORWARD_DELAY); rt-addSeq(fh-seq_);/ 更新封包的最大序號(hào) rtable_.rt_print(); else / 否則,丟棄該封包 drop(p, LOWSEQ);轉(zhuǎn)發(fā)封包void MFlood:recv(Packet *p, Handler*) / 接收到一個(gè)封包 struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);struct hdr_mflood *fh = HDR_MFLOOD(p); assert(initiali
57、zed();if(ih-saddr() = index_) & (ch-num_forwards() = 0) / 本節(jié)點(diǎn)產(chǎn)生的封包并 且沒(méi)有經(jīng)過(guò)轉(zhuǎn)發(fā)ch-size() += IP_HDR_LEN; / Add the IP Header ih-ttl_ = NETWORK_DIAMETER; / 允許轉(zhuǎn)發(fā)的最大次數(shù) TTL fh-seq_ = myseq_+;/ 更新自己產(chǎn)生的封包的序號(hào)值forward(MFlood_RTEntry*)1,p,0);/ 轉(zhuǎn)發(fā)return; else if(ih-saddr() = index_) / 如果是本節(jié)點(diǎn)產(chǎn)生的封包但經(jīng)過(guò)轉(zhuǎn)發(fā),說(shuō)明有路 由環(huán)路,丟棄該
58、包drop(p, DROP_RTR_ROUTE_LOOP); return; else / 不是本節(jié)點(diǎn)產(chǎn)生的封包,需要轉(zhuǎn)發(fā)if(-ih-ttl_ = 0) / 檢查 TTL域,如果為 0,丟掉drop(p, DROP_RTR_TTL); return;Rt_resolve(p);void MFlood:forward(MFlood_RTEntry* rt, Packet *p, double delay) / struct hdr_cmn *ch = HDR_CMN(p);struct hdr_ip *ih = HDR_IP(p);assert(ih-ttl_ 0);/判斷 TTL 值是否有效
59、assert(rt != 0);ch-next_hop_ = -1;/ 洪泛協(xié)議將數(shù)據(jù)進(jìn)行廣播ch-addr_type() = NS_AF_INET;ch-direction() = hdr_cmn:DOWN; /將包的方向標(biāo)識(shí)為向下傳輸if(delay 0.0) Scheduler:instance().schedule(target_, p, Random:uniform(delay*2);= 0) else Scheduler:instance().schedule(target_, p, 0.);/ TCL Hooksstatic class MFloodclass : public
60、TclClass public:MFloodclass() : TclClass(Agent/MFlood) TclObject* create(int argc, const char*const* argv) assert(argc = 5);/return (new MFlood(nsaddr_t) atoi(argv4);/ PBO agrv4 is index_return (new MFlood(nsaddr_t) Address:instance().str2addr(argv4); class_rtProtoMFlood;int MFlood:command(int argc,
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學(xué)年文山壯族苗族自治州西疇縣數(shù)學(xué)三年級(jí)第一學(xué)期期末教學(xué)質(zhì)量檢測(cè)模擬試題含解析
- 大學(xué)生軍訓(xùn)心得筆記10篇
- 婚禮新娘答謝詞(15篇)
- 學(xué)生頂崗實(shí)習(xí)報(bào)告
- 標(biāo)準(zhǔn)的辭職報(bào)告模板集合九篇
- 大學(xué)生面試自我介紹集錦15篇
- 藥店疫情應(yīng)急預(yù)案
- 九年級(jí)化學(xué)上冊(cè) 第七單元 燃料及其利用 課題1 燃燒與滅火教學(xué)實(shí)錄 (新版)新人教版
- 小學(xué)語(yǔ)文教師教學(xué)計(jì)劃三篇
- 中專(zhuān)第一學(xué)年自我鑒定集錦7篇
- 2023年杭州聯(lián)合銀行校園招聘筆試歷年高頻考點(diǎn)試題答案詳解
- 灌裝軋蓋機(jī)和供瓶機(jī)設(shè)備驗(yàn)證方案
- 人教版五年級(jí)下冊(cè)數(shù)學(xué)講義
- 瓦楞紙箱工藝流程演示文稿
- 漢語(yǔ)教學(xué) 《成功之路+進(jìn)步篇+2》第16課課件
- 安置房項(xiàng)目二次結(jié)構(gòu)磚砌體工程專(zhuān)項(xiàng)施工方案培訓(xùn)資料
- SB/T 10756-2012泡菜
- GB/T 36393-2018土壤質(zhì)量自然、近自然及耕作土壤調(diào)查程序指南
- GB/T 3045-2017普通磨料碳化硅化學(xué)分析方法
- 新疆維吾爾自治區(qū)公共建筑節(jié)能設(shè)計(jì)標(biāo)準(zhǔn)實(shí)施細(xì)則2023
- 2022年西藏自治區(qū)中考英語(yǔ)真題卷(含答案與解析)
評(píng)論
0/150
提交評(píng)論