zoukankan      html  css  js  c++  java
  • 有线节点与无线节点的混合仿真模拟实验

    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

    有图有真相:

    stay hungry stay foolish ----jobs 希望多多烧香!
  • 相关阅读:
    异地主从双机热备份实战
    基于Crawler4j的WEB爬虫
    【转】TCP拥塞控制
    js定时器setTimeout和setInterval的使用
    制作 macOS Sierra U盘USB启动安装盘方法教程 (亲测)
    VMware 14 安装 macOS10.13 详细图文教程
    解决多线程下@Autowired无法注入
    MySQL 使用自增ID主键和UUID 作为主键的优劣比较详细过程(从百万到千万表记录测试)
    关于MYSQL
    关于Mybaits
  • 原文地址:https://www.cnblogs.com/bakari/p/2632095.html
Copyright © 2011-2022 走看看