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


    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

    有图有真相:

  • 相关阅读:
    View Controller 生命周期的各个方法的用法
    IOS开发之Post 方式获取服务器数据
    委托代理
    Function
    SKPhysicsContactDelegate协议
    UITouch附加
    Remove Duplicates from Sorted Array II
    4Sum
    [Text Justification
    Count and Say
  • 原文地址:https://www.cnblogs.com/zhangyuhang3/p/6868448.html
Copyright © 2020-2023  润新知