Monday, 7 January 2013

Examples for CBR traffic transmission over EDCF-based wireless networks




# globals and flags
set ns [new Simulator]

#number of nodes
set num_wired_nodes  1
set num_mobile_nodes 1
set num_bs_nodes     1 ;# number of base stations
set num_nodes [expr $num_wired_nodes + $num_mobile_nodes + $num_bs_nodes]
set bs_id $num_wired_nodes

# Parameter for wireless nodes
set opt(chan)           Channel/WirelessChannel    ;# channel type
set opt(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set opt(netif)          Phy/WirelessPhy            ;# network interface type
set opt(mac)            Mac/802_11e                ;# MAC type
set opt(ifq)            Queue/DTail/PriQ           ;# interface queue type
set opt(ifqlen)         50
set opt(ll)             LL                         ;# link layer type
set opt(ant)            Antenna/OmniAntenna        ;# antenna model
set opt(ifqlen)         50                        ;# max packet in ifq
set opt(adhocRouting)   NOAH                      ;# routing protocol
set opt(x)          670  ;# X dimension of the topography
set opt(y)          670          ;# Y dimension of the topography

#smallko add the following two lines
Mac/802_11e set dataRate_  1Mb
Mac/802_11e set basicRate_ 1Mb
Mac/802_11e set backoff_mode_ 1

#set up for hierarchical routing
#(needed for routing over a basestation)
$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2          ;# domain number
lappend cluster_num 1 1               ;# cluster number for each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel $num_wired_nodes [expr $num_mobile_nodes + $num_bs_nodes] ;# number of nodes for each cluster            
AddrParams set nodes_num_ $eilastlevel

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all-wireless $nf $opt(x) $opt(y)
set ntr [open out.tr w]
$ns trace-all $ntr

set chan    [new $opt(chan)]
set topo    [new Topography]
$topo load_flatgrid $opt(x) $opt(y)

# Create God
create-god [expr $num_mobile_nodes + $num_bs_nodes]

# creating wired nodes
for {set i 0} {$i < $num_wired_nodes} {incr i} {
        set W($i) [$ns node 0.0.$i]
    puts "wired node $i created"
}


# creating base station
$ns node-config -adhocRouting $opt(adhocRouting) \
                 -llType $opt(ll) \
                 -macType $opt(mac) \
                 -ifqType $opt(ifq) \
                 -ifqLen $opt(ifqlen) \
                 -antType $opt(ant) \
                 -propType $opt(prop)    \
                 -phyType $opt(netif) \
                 -channel $chan      \
                 -topoInstance $topo \
                 -wiredRouting ON \
                 -agentTrace OFF \
                 -routerTrace OFF \
                 -macTrace OFF    \
                 -movementTrace OFF

set BS(0) [$ns node 1.0.0]
$BS(0) random-motion 0
puts "Base-Station node $bs_id created"
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 1.0
$BS(0) set Y_ 2.0
$BS(0) set Z_ 0.0

# creating mobile nodes
$ns node-config -wiredRouting OFF
for {set i 0} {$i < $num_mobile_nodes} {incr i} {
                set wl_node_($i) [$ns node 1.0.[expr $i + 1]]  
                $wl_node_($i) random-motion 0               ;# disable random motion
    puts "wireless node $i created ..."
    $wl_node_($i) base-station [AddrParams addr2id [$BS(0) node-addr]]
    $wl_node_($i) set X_ [expr $i * 10]
    $wl_node_($i) set Y_ [expr $i * 10]
    $wl_node_($i) set Z_ 0.0
}

# linking of root to base-station node
$ns duplex-link $W(0) $BS(0) 10Mb 2ms DropTail

# linking of wired nodes to root node
#for {set i 1} {$i < $num_wired_nodes} {incr i} {
#    $ns duplex-link $W($i) $W(0) 10Mb 2ms DropTail
#}

set src_udp0 [new Agent/UDP]
$src_udp0 set class_ 0
# highest priority
$src_udp0 set prio_ 0
set dst_udp0 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp0
$ns attach-agent $W(0) $dst_udp0
set app0 [new Application/Traffic/CBR]
$app0 attach-agent $src_udp0
$ns connect $src_udp0 $dst_udp0
$ns at 0.0 "$app0 start"

set src_udp1 [new Agent/UDP]
$src_udp1 set class_ 1
$src_udp1 set prio_ 1
set dst_udp1 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp1
$ns attach-agent $W(0) $dst_udp1
set app1 [new Application/Traffic/CBR]
$app1 attach-agent $src_udp1
$ns connect $src_udp1 $dst_udp1
$ns at 0.3 "$app1 start"

set src_udp2 [new Agent/UDP]
$src_udp2 set class_ 2
$src_udp2 set prio_ 2
set dst_udp2 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp2
$ns attach-agent $W(0) $dst_udp2
set app2 [new Application/Traffic/CBR]
$app2 attach-agent $src_udp2
$ns connect $src_udp2 $dst_udp2
$ns at 0.1 "$app2 start"

set src_udp3 [new Agent/UDP]
#lowest priority
$src_udp3 set class_ 3
$src_udp3 set prio_ 3
set dst_udp3 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp3
$ns attach-agent $W(0) $dst_udp3
set app3 [new Application/Traffic/CBR]
$app3 attach-agent $src_udp3
$ns connect $src_udp3 $dst_udp3
$ns at 0.2 "$app3 start"

# Define node initial position in nam
for {set i 0} {$i < $num_mobile_nodes} {incr i} {
    $ns initial_node_pos $wl_node_($i) 20
   }

# Tell nodes when the simulation ends
for {set i 0} {$i < $num_mobile_nodes } {incr i} {
    $ns at 10.0 "$wl_node_($i) reset";
}

$ns at 100.0 "$BS(0) reset";
$ns at 100.0 "$app0 stop"
$ns at 100.0 "$app1 stop"
$ns at 100.0 "$app2 stop"
$ns at 100.0 "$app3 stop"
$ns at 110.0 "puts \"NS EXITING...\" ; $ns halt"

proc stop {} {
    global ns ntr nf
    close $ntr
    close $nf
}

# run the simulation
$ns run
Examples for CBR traffic transmission over DCF-based  wireless networks



# globals and flags
set ns [new Simulator]

#number of nodes
set num_wired_nodes  1
set num_mobile_nodes 1
set num_bs_nodes     1 ;# number of base stations
set num_nodes [expr $num_wired_nodes + $num_mobile_nodes + $num_bs_nodes]
set bs_id $num_wired_nodes

# Parameter for wireless nodes
set opt(chan)           Channel/WirelessChannel    ;# channel type
set opt(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set opt(netif)          Phy/WirelessPhy            ;# network interface type
set opt(mac)            Mac/802_11                 ;# MAC type
set opt(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
set opt(ifqlen)         50
set opt(ll)             LL                         ;# link layer type
set opt(ant)            Antenna/OmniAntenna        ;# antenna model
set opt(ifqlen)         50                        ;# max packet in ifq
set opt(adhocRouting)   NOAH                       ;# routing protocol
set opt(x)          670  ;# X dimension of the topography
set opt(y)          670          ;# Y dimension of the topography

#smallko add the following two lines
Mac/802_11 set dataRate_  1Mb
Mac/802_11 set basicRate_  1Mb

#set up for hierarchical routing
#(needed for routing over a basestation)
$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2          ;# domain number
lappend cluster_num 1 1               ;# cluster number for each domain
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel $num_wired_nodes [expr $num_mobile_nodes + $num_bs_nodes] ;# number of nodes for each cluster            
AddrParams set nodes_num_ $eilastlevel

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all-wireless $nf $opt(x) $opt(y)
set ntr [open out.tr w]
$ns trace-all $ntr

set chan    [new $opt(chan)]
set topo    [new Topography]
$topo load_flatgrid $opt(x) $opt(y)

# Create God
create-god [expr $num_mobile_nodes + $num_bs_nodes]

# creating wired nodes
for {set i 0} {$i < $num_wired_nodes} {incr i} {
        set W($i) [$ns node 0.0.$i]
    puts "wired node $i created"
}


# creating base station
$ns node-config -adhocRouting $opt(adhocRouting) \
                 -llType $opt(ll) \
                 -macType $opt(mac) \
                 -ifqType $opt(ifq) \
                 -ifqLen $opt(ifqlen) \
                 -antType $opt(ant) \
                 -propType $opt(prop)    \
                 -phyType $opt(netif) \
                 -channel $chan      \
                 -topoInstance $topo \
                 -wiredRouting ON \
                 -agentTrace OFF \
                 -routerTrace OFF \
                 -macTrace OFF    \
                 -movementTrace OFF

set BS(0) [$ns node 1.0.0]
$BS(0) random-motion 0
puts "Base-Station node $bs_id created"
#provide some co-ord (fixed) to base station node
$BS(0) set X_ 1.0
$BS(0) set Y_ 2.0
$BS(0) set Z_ 0.0

# creating mobile nodes
$ns node-config -wiredRouting OFF
for {set i 0} {$i < $num_mobile_nodes} {incr i} {
    set wl_node_($i) [$ns node 1.0.[expr $i + 1]]  
        $wl_node_($i) random-motion 0               ;# disable random motion
    puts "wireless node $i created ..."
    $wl_node_($i) base-station [AddrParams addr2id [$BS(0) node-addr]]
    $wl_node_($i) set X_ [expr $i * 10]
    $wl_node_($i) set Y_ [expr $i * 10]
    $wl_node_($i) set Z_ 0.0
}

# linking of root to base-station node
$ns duplex-link $W(0) $BS(0) 10Mb 2ms DropTail

# linking of wired nodes to root node
#for {set i 1} {$i < $num_wired_nodes} {incr i} {
#    $ns duplex-link $W($i) $W(0) 10Mb 2ms DropTail
#}

set src_udp0 [new Agent/UDP]
$src_udp0 set class_ 0
#$src_udp0 set prio_ 0
set dst_udp0 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp0
$ns attach-agent $W(0) $dst_udp0
set app0 [new Application/Traffic/CBR]
$app0 attach-agent $src_udp0
$ns connect $src_udp0 $dst_udp0
$ns at 0.0 "$app0 start"

set src_udp1 [new Agent/UDP]
$src_udp1 set class_ 1
#$src_udp1 set prio_ 1
set dst_udp1 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp1
$ns attach-agent $W(0) $dst_udp1
set app1 [new Application/Traffic/CBR]
$app1 attach-agent $src_udp1
$ns connect $src_udp1 $dst_udp1
$ns at 0.3 "$app1 start"

set src_udp2 [new Agent/UDP]
$src_udp2 set class_ 2
#$src_udp2 set prio_ 2
set dst_udp2 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp2
$ns attach-agent $W(0) $dst_udp2
set app2 [new Application/Traffic/CBR]
$app2 attach-agent $src_udp2
$ns connect $src_udp2 $dst_udp2
$ns at 0.1 "$app2 start"

set src_udp3 [new Agent/UDP]
$src_udp3 set class_ 3
#$src_udp3 set prio_ 3
set dst_udp3 [new Agent/Null]
$ns attach-agent $wl_node_(0) $src_udp3
$ns attach-agent $W(0) $dst_udp3
set app3 [new Application/Traffic/CBR]
$app3 attach-agent $src_udp3
$ns connect $src_udp3 $dst_udp3
$ns at 0.2 "$app3 start"

# Define node initial position in nam
for {set i 0} {$i < $num_mobile_nodes} {incr i} {
    $ns initial_node_pos $wl_node_($i) 20
   }

# Tell nodes when the simulation ends
for {set i 0} {$i < $num_mobile_nodes } {incr i} {
    $ns at 10.0 "$wl_node_($i) reset";
}

$ns at 100.0 "$BS(0) reset";
$ns at 100.0 "$app0 stop"
$ns at 100.0 "$app1 stop"
$ns at 100.0 "$app2 stop"
$ns at 100.0 "$app3 stop"
$ns at 110.0 "puts \"NS EXITING...\" ; $ns halt"

proc stop {} {
    global ns ntr nf
    close $ntr
    close $nf
}

# run the simulation
$ns run
AWK paring program --- getRatio.awk

BEGIN {
        sendLine = 0;
        recvLine = 0;
        fowardLine = 0;
}

$0 ~/^s.* AGT/ {
        sendLine ++ ;
}

$0 ~/^r.* AGT/ {
        recvLine ++ ;
}

$0 ~/^f.* RTR/ {
        fowardLine ++ ;
}

END {
        printf "cbr s:%d r:%d, r/s Ratio:%.4f, f:%d \n", sendLine, recvLine, (recvLine/sendLine),fowardLine;
}




Performance Evaluation of DSDV, AODV, and DSR

TCL script --- adhoc.tcl


if {$argc !=3} {
        puts "Usage: ns adhoc.tcl  Routing_Protocol Traffic_Pattern Scene_Pattern "
        puts "Example:ns adhoc.tcl DSDV cbr-50-10-8 scene-50-0-20"
        exit
}

set par1 [lindex $argv 0]
set par2 [lindex $argv 1]
set par3 [lindex $argv 2]

set val(chan)           Channel/WirelessChannel    ;# channel type
set val(prop)           Propagation/TwoRayGround   ;# radio-propagationmodel
set val(netif)            Phy/WirelessPhy            ;# network interface type
set val(mac)            Mac/802_11                 ;# MAC type

if { $par1=="DSR"} {
  set val(ifq)           CMUPriQueue
} else {
  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(rp)             $par1                       ;# routing protocol
set val(x)                   500
set val(y)                500
set val(seed)               0.0
set val(tr)                temp.tr
set val(nn)             50
set val(cp)                $par2
set val(sc)        $par3
set val(stop)              100.0

set ns_              [new Simulator]

set tracefd     [open $val(tr) w]
$ns_ trace-all $tracefd
$ns_ use-newtrace

set topo       [new Topography]
$topo load_flatgrid $val(x) $val(y)

set god_ [create-god $val(nn)]

set chan_1_ [new $val(chan)]

        $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 OFF                                        

        for {set i 0} {$i < $val(nn) } {incr i} {
                set node_($i) [$ns_ node]
                $node_($i) random-motion 0            ;# disable random motion
        }

puts "Loading connection pattern..."
source $val(cp)

puts "Loading scenario file..."
source $val(sc)

for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ initial_node_pos $node_($i) 20
}

for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at $val(stop).000000001 "$node_($i) reset";
}

$ns_ at $val(stop).000000001 "puts \"NS EXITING...\"; $ns_ halt"
puts "Start Simulation..."
$ns_ run




 
Setting Energy Model for Mobile Nodes


## Setting The wireless Channels..
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)         5                          ;# max packet in ifq
set val(nn)             6                           ;# number of mobilenodes
set val(rp)             DSR                        ;# routing protocol
set val(x)              750                        ;# X dimension of topography
set val(y)              550                        ;# Y dimension of topography 
set val(stop)         18.0                       ;# time of simulation end
## Create a simulator object(nothing but, a scheduler's object)..
      set ns [new Simulator]
## Create a trace file and nam file..
      set tracefd [open wireless1.tr w]
      set namtrace [open wireless1.nam w]   
## Trace the nam and trace details from the main simulation..
      $ns trace-all $tracefd
      $ns namtrace-all-wireless $namtrace $val(x) $val(y)
## set up topography object..
      set topo [new Topography]
      $topo load_flatgrid $val(x) $val(y)
      set god_ [create-god $val(nn)]
## Color Descriptions..
      $ns color 1 dodgerblue
      $ns color 2 blue
      $ns color 3 cyan
      $ns color 4 green
      $ns color 5 yellow
      $ns color 6 black
      $ns color 7 magenta
      $ns color 8 gold
      $ns color 9 red
     
# Setting The Distance Variables..
# For model 'TwoRayGround'
      set dist(5m)  7.69113e-06
      set dist(9m)  2.37381e-06
      set dist(10m) 1.92278e-06
      set dist(11m) 1.58908e-06
      set dist(12m) 1.33527e-06
      set dist(13m) 1.13774e-06
      set dist(14m) 9.81011e-07
      set dist(15m) 8.54570e-07
      set dist(16m) 7.51087e-07
      set dist(20m) 4.80696e-07
      set dist(25m) 3.07645e-07
      set dist(30m) 2.13643e-07
      set dist(35m) 1.56962e-07
      set dist(40m) 1.56962e-10
      set dist(45m) 1.56962e-11
      set dist(50m) 1.20174e-13
      #Phy/WirelessPhy set CSThresh_ $dist(50m)
      #Phy/WirelessPhy set RXThresh_ $dist(50m)
## Setting node config event with set of inputs..
      puts "Node Configuration Started here...\n \
                   -channel $val(chan) \n \
                   -adhocRouting $val(rp) \n \
                   -llType $val(ll) \n \
                   -macType $val(mac) \n \
                   -ifqType $val(ifq) \n \
                   -ifqLen $val(ifqlen) \n \
                   -antType $val(ant) \n \
                   -propType $val(prop) \n \
                   -phyType $val(netif) \n"
                      
        $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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
# Energy model
      $ns node-config  -energyModel EnergyModel \
                        -initialEnergy 20 \
                        -txPower 0.9 \
                        -rxPower 0.8 \
                        -idlePower 0.0 \
                        -sensePower 0.0175
## Creating node objects..               
      for {set i 0} {$i < $val(nn) } { incr i } {
            set node_($i) [$ns node]     
      }
for {set i 0} {$i < $val(nn)} {incr i} {
      $node_($i) color darkgreen
      $ns at 0.0 "$node_($i) color darkgreen"
      }
## Provide initial location of mobilenodes..
           
      if {$val(nn) >0} {
            for {set i 1} {$i < $val(nn) } { incr i } {
                  set xx [expr rand()*600]                 
                  set yy [expr rand()*500];
                  $node_($i) set X_ $xx
                  $node_($i) set Y_ $yy
            }
      }
## set god distance..
      $god_ set-dist 0 1 2
      $god_ set-dist 0 2 2
      $god_ set-dist 0 3 2
      $god_ set-dist 0 4 1
      $god_ set-dist 0 5 2
      $god_ set-dist 1 2 3
      $god_ set-dist 1 3 3
     
## 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 16.01 "puts \"end simulation\" " ;# $ns halt

## Stop procedure..
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
    exec nam wireless1.nam &
    exit 0
}
$ns run



output