ns模拟3

发布时间:2019-08-23 08:00:56编辑:auto阅读(1275)

    # Define options
    set val(chan)           Channel/WirelessChannel    ;# channel type
    set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
    set val(netif)          Phy/WirelessPhy            ;# network interface type
    set val(mac)            Mac/802_11                 ;# MAC type
    set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
    set val(ll)             LL                         ;# link layer type
    set val(ant)            Antenna/OmniAntenna        ;# antenna model
    set val(ifqlen)         50                         ;# max packet in ifq
    set val(nn)             8                          ;# number of mobilenodes
    set val(rp)             AODV                       ;# routing protocol
    set val(x)              1200                              ;# X dimension of topography
    set val(y)              1200                              ;# Y dimension of topography  
    set val(stop)            2                           ;# time of simulation end

    set ns                  [new Simulator]
    set tracefd       [open simple.tr w]
    set namtrace      [open simwrls.nam w]   

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

    #set different colors for data flows
    $ns color 0 Blue
    $ns color 1 Red

    # set up topography object
    set topo       [new Topography]

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

    set chan_1 [new $val(chan)]
    create-god $val(nn)

    #
    #  Create nn mobilenodes [$val(nn)] and attach them to the channel.
    #

    # configure the nodes
            $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 \
                             -topoInstance $topo \
                             -agentTrace ON \
                             -routerTrace ON \
                             -macTrace ON \
                             -movementTrace OFF

    set node_(0) [$ns node]
    $node_(0) color black
    set node_(1) [$ns node]
    $node_(1) color black

    $ns at 0.0 "$node_(0) color red"
    $ns at 0.0 "$node_(1) color red"
    $node_(0) set X_ 370.71
    $node_(0) set Y_ 270.71
    $node_(0) set Z_ 0.0

    $node_(1) set X_ 529.29
    $node_(1) set Y_ 429.29
    $node_(1) set Z_ 0.0

    Phy/WirelessPhy set Pt_ 7.214e-3
    set chan_2 [new $val(chan)]
    $ns node-config \
        -channel $chan_2

    for {set i 2} {$i <= 8 } { incr i } {
    set node_($i) [$ns node]       
    }

    # Provide initial location of mobilenodes
     for {set i 2} {$i <= 8 } { incr i } {
        if {$i<=5} {
            $node_($i) set X_ [expr 200.0+($i-1)*100.0]
            $node_($i) set Y_ 200
        } else {
            $node_($i) set X_ 600
            $node_($i) set Y_ [expr 200+($i-5)*100.0]
        }       
     $node_($i) set Z_ 0.0
     }

    # Generation of movements
    $ns at 110.0 "$node_(0) setdest 124.0 272.0 30.0"
    $ns at 110.0 "$node_(1) setdest 45.0 285.0 50.0"
    $ns at 110.0 "$node_(0) setdest 480.0 300.0 5.0"

    # Set a TCP connection between node_(0) and node_(1)
    set tcp [new Agent/TCP/Newreno]
    $tcp set class_ 2
    set sink [new Agent/TCPSink]
    $ns attach-agent $node_(0) $tcp
    $ns attach-agent $node_(1) $sink
    $ns connect $tcp $sink
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp
    $ftp set fid_ 1
    $ns at 0.3 "$ftp start"

    # Define node initial position in nam
    for {set i 0} {$i <= $val(nn)} { incr i } {
    # 30 defines the node size for nam
    $ns initial_node_pos $node_($i) 30
    }

    # Telling nodes when the simulation ends
    for {set i 0} {$i < $val(nn) } { incr i } {
        $ns at $val(stop) "$node_($i) reset";
    }

    # ending nam and the simulation
    $ns at $val(stop) "$ns nam-end-wireless $val(stop)"
    $ns at $val(stop) "stop"
    $ns at 2.01 "puts \"end simulation\" ; $ns halt"
    proc stop {} {
        global ns tracefd namtrace
        $ns flush-trace
        close $tracefd
        close $namtrace
        exec nam simwrls.nam &
    }

    $ns run

关键字