Monday 14 January 2013

 # Define options
 # ==============================
========================================
   set val(chan)         Channel/WirelessChannel  ;# channel type
   set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
   set val(ant)          Antenna/OmniAntenna      ;# Antenna type
   set val(ll)           LL                       ;# Link layer type
   set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
   set val(ifqlen)       50                       ;# max packet in ifq
   set val(netif)        Phy/WirelessPhy          ;# network interface type
   set val(mac)          Mac/802_11               ;# MAC type
   set val(nn)           6                        ;# number of mobilenodes
   set val(rp)           DSR                     ;# routing protocol
   set val(x)            800
   set val(y)            800

 set ns [new Simulator]
 #ns-random 0

 set f [open bhapith.tr w]
 $ns trace-all $f
 set namtrace [open 1_out.nam w]
 set runall [open runall.bat w]
 $ns namtrace-all-wireless $namtrace $val(x) $val(y)
 set f0 [open packets_received.tr w]
 set f1 [open packets_lost.tr w]
 set f2 [open proj_out2.tr w]
 set f3 [open proj_out3.tr w]

 set topo [new Topography]
 $topo load_flatgrid 800 800

 create-god $val(nn)

 set chan_1 [new $val(chan)]
 set chan_2 [new $val(chan)]
 set chan_3 [new $val(chan)]
 set chan_4 [new $val(chan)]
 set chan_5 [new $val(chan)]
 set chan_6 [new $val(chan)]

 # CONFIGURE AND CREATE 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) \
                   #-channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace OFF \
                   -routerTrace ON \
                   -macTrace ON \
                   -movementTrace OFF \
                   -channel $chan_1  # \
                   #-channel $chan_2   \
                   #-channel $chan_3   \
                   #-channel $chan_4   \
                   #-channel $chan_5   \
                   #-channel $chan_6


 proc finish {} {
      global ns f f0 f1 f2 f3 namtrace
      $ns flush-trace
          close $namtrace
      close $f0
          close $f1
       close $f2
          close $f3
          exec xgraph packets_received.tr packets_lost.tr -x in_sec
-y recived_packet -geometry 800x400 &
          exec nam -r 5m 1_out.nam &

             exit 0
 }

 proc record {} {
    global sink0 sink1 sink2 sink3 sink4 sink5 f0 f1 f2 f3
     #Get An Instance Of The Simulator
     set ns [Simulator instance]

     #Set The Time After Which The Procedure Should Be Called Again
     set time 0.05
     #How Many Bytes Have Been Received By The Traffic Sinks?
     set bw0 [$sink5 set npkts_]
     set bw1 [$sink5 set nlost_]
     #set bw2 [$sink2 set npkts_]
     #set bw3 [$sink3 set npkts_]

     #Get The Current Time
     set now [$ns now]

     #Save Data To The Files
     puts $f0 "$now [expr $bw0]"
     puts $f1 "$now [expr $bw1]"
     #puts $f2 "$now [expr $bw2]"
     #puts $f3 "$now [expr $bw3]"

     #Re-Schedule The Procedure
     $ns at [expr $now+$time] "record"
    }

 # define color index
 $ns color 0 blue
 $ns color 1 red
 $ns color 2 chocolate
 $ns color 3 red
 $ns color 4 brown
 $ns color 5 tan
 $ns color 6 gold
 $ns color 7 black

 set n(0) [$ns node]
 #$ns at 0.0 "$n(0) color red"
 $n(0) color "0"
 $n(0) shape "box"
 set n(1) [$ns node]
 $n(1) color "blue"
 $n(1) shape "hexagon"
 set n(2) [$ns node]
 $n(2) color "tan"
 $n(2) shape "circle"
 set n(3) [$ns node]
 $n(3) color "red"
 $n(3) shape "circle"
 set n(4) [$ns node]
 $n(4) color "tan"
 $n(4) shape "circle"
 set n(5) [$ns node]
 $n(5) color "red"
 $n(5) shape "circle"






 for {set i 0} {$i < $val(nn)} {incr i} {
      $ns initial_node_pos $n($i) 30+i*100
 }

 $n(0) set X_ 0.0
 $n(0) set Y_ 0.0
 $n(0) set Z_ 0.0

 $n(1) set X_ 0.0
 $n(1) set Y_ 0.0
 $n(1) set Z_ 0.0

 $n(2) set X_ 0.0
 $n(2) set Y_ 0.0
 $n(2) set Z_ 0.0

 $n(3) set X_ 0.0
 $n(3) set Y_ 0.0
 $n(3) set Z_ 0.0

 $n(4) set X_ 0.0
 $n(4) set Y_ 0.0
 $n(4) set Z_ 0.0

 $n(5) set X_ 0.0
 $n(5) set Y_ 0.0
 $n(5) set Z_ 0.0

 $ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
 $ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
 $ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
 $ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
 $ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
 $ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

 $ns at 2.0 "$n(5) setdest 100.0 400.0 500.0"
 #$ns at 1.5 "$n(3) setdest 450.0 150.0 500.0"


 # CONFIGURE AND SET UP A FLOW


 set sink0 [new Agent/LossMonitor]
 set sink1 [new Agent/LossMonitor]
 set sink2 [new Agent/LossMonitor]
 set sink3 [new Agent/LossMonitor]
 set sink4 [new Agent/LossMonitor]
 set sink5 [new Agent/LossMonitor]
 $ns attach-agent $n(0) $sink0
 $ns attach-agent $n(1) $sink1
 $ns attach-agent $n(2) $sink2
 $ns attach-agent $n(3) $sink3
 $ns attach-agent $n(4) $sink4
 $ns attach-agent $n(5) $sink5

 #$ns attach-agent $sink2 $sink3
 set tcp0 [new Agent/TCP]
 $ns attach-agent $n(0) $tcp0
 set tcp1 [new Agent/TCP]
 $ns attach-agent $n(1) $tcp1
 set tcp2 [new Agent/TCP]
 $ns attach-agent $n(2) $tcp2
 set tcp3 [new Agent/TCP]
 $ns attach-agent $n(3) $tcp3
 set tcp4 [new Agent/TCP]
 $ns attach-agent $n(4) $tcp4
 set tcp5 [new Agent/TCP]
 $ns attach-agent $n(5) $tcp5


 proc attach-CBR-traffic { node sink size interval } {
     #Get an instance of the simulator
     set ns [Simulator instance]
     #Create a CBR  agent and attach it to the node
     set cbr [new Agent/CBR]
     $ns attach-agent $node $cbr
     $cbr set packetSize_ $size
     $cbr set interval_ $interval

     #Attach CBR source to sink;
     $ns connect $cbr $sink
     return $cbr
    }

 set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .015]
 #set cbr1 [attach-CBR-traffic $n(1) $sink2 1000 .015]
 #set cbr2 [attach-CBR-traffic $n(2) $sink3 1000 .015]
 #set cbr3 [attach-CBR-traffic $n(3) $sink0 1000 .015]
 #set cbr4 [attach-CBR-traffic $n(4) $sink3 1000 .015]
 #set cbr5 [attach-CBR-traffic $n(5) $sink0 1000 .015]



 $ns at 0.0 "record"
 $ns at 0.5 "$cbr0 start"
 $ns at 0.5 "$cbr2 start"
 $ns at 2.0 "$cbr0 stop"
 $ns at 2.0 "$cbr2 stop"
 $ns at 1.0 "$cbr0 start"
 $ns at 4.0 "$cbr3 stop"

 $ns at 10.0 "finish"

 puts "Start of simulation.."
 $ns run







--
 Regards,
**

       * *

*      *B*hapith***

Tuesday 8 January 2013

multicast.tcl

 

 

#   Video               MyUdpSink
#   W(0) ------ HA------MH(0)
#  sorce -----> HA------MH

set opt(num_FA) 1

#loss_model: 0 for uniform distribution, 1 for GE model
set loss_model  0

#comm_type: 0 for broacdcast, 1 for unicast
set comm_type 0


set ns_ [new Simulator]


$ns_ node-config -addressType hierarchical


AddrParams set domain_num_ 2
lappend cluster_num 1 1
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 2
AddrParams set nodes_num_ $eilastlevel


set tracefd [open bsc_multicast.tr w]
$ns_ trace-all $tracefd


set opt(nnn) 1


set topo [new Topography]
$topo load_flatgrid 100 100

#create god
set god_ [create-god [expr $opt(nnn)+$opt(num_FA)]]

# wired nodes
set W(0) [$ns_ node 0.0.0]

# create channel
set chan_ [new Channel/WirelessChannel]


$ns_ node-config     -mobileIP ON \
                      -adhocRouting NOAH \
                  -llType LL \
                  -macType Mac/802_11 \
                  -ifqType Queue/DropTail/PriQueue \
                  -ifqLen 2000 \
                  -antType Antenna/OmniAntenna \
                          -propType Propagation/TwoRayGround \
                          -phyType Phy/WirelessPhy \
                  -channel $chan_ \
                         -topoInstance $topo \
                  -wiredRouting ON\
                          -agentTrace OFF \
                  -routerTrace OFF \
                  -macTrace OFF


set HA [$ns_ node 1.0.0]
set HAnetif_ [$HA set netif_(0)]
$HAnetif_ set-error-level 0 0 0 0 $loss_model


$ns_ node-config -wiredRouting OFF
set MH(0) [$ns_ node 1.0.1]
set MHnetif_(0) [$MH(0) set netif_(0)]

#Initially, set the error rate to 0
$MHnetif_(0) set-error-level 0 0 0 0 $loss_model

#At 10.0 second, set the error rate to 0.5
$ns_ at 10.0 "$MHnetif_(0) set-error-level 0 0 0.5 0 $loss_model"

#At 40.0 second, set the error rate to 0
$ns_ at 40.0 "$MHnetif_(0) set-error-level 0 0 0 0 $loss_model"


[$MH(0)  set regagent_] set home_agent_ [AddrParams addr2id [$HA node-addr]]


$HA set X_ 100.0
$HA set Y_ 100.0
$HA set Z_ 0.0

$MH(0) set X_ 80.0
$MH(0) set Y_ 80.0
$MH(0) set Z_ 0.0

$ns_ duplex-link $W(0) $HA 10Mb 10ms myfifo
set q1       [[$ns_ link $W(0) $HA] queue]

set udp [new Agent/mUDP]
#set the sender trace file name to sd
$udp set_filename sd
$ns_ attach-agent $W(0) $udp

set forwarder_ [$HA  set forwarder_]
puts [$forwarder_ port]
$ns_ connect $udp $forwarder_
$forwarder_ dst-addr [AddrParams addr2id [$MH(0) node-addr]]
$forwarder_ comm-type $comm_type

set null [new Agent/mUdpSink]
#set the receiver filename to rd
$null set_filename rd
$ns_ attach-agent $MH(0) $null
$ns_ connect $udp $null

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1Mb
$cbr set random_ false

$ns_ at 0.0 "$cbr start"
$ns_ at 50.0 "$cbr stop"
$ns_ at 50.1 "$MH(0) reset";
$ns_ at 50.0001 "$W(0) reset"
$ns_ at 50.0002 "stop "
$ns_ at 50.0003  "$ns_  halt"

proc stop {} {
    global ns_
    global tracefd
   
    close $tracefd
}

$ns_ run

 


ns-simple.tcl

Otcl coding

 

 

 

# Add a member function call”greet”
Class Teacher
Teacher instproc init {subject} {
$self instvar subject_
set subject_ $subject
}


# Creating a child class of teacher and student
Teacher instproc greet {} {
$self instvar subject_
puts "$subject_ teacher ask
which subject u studied?"
}


#creating a teacher and student object
Class student -superclass Teacher
student instproc greet {} {
$self instvar subject_
puts "$subject_ say
i studied maths"
}


# calling member function “greet” of each node
set a [new Teacher maths]
set b [new student Jey]
$a greet
$b greet

Tcl script to generate graph taking more than two parameter files as output 

 

 

 

# Creating simulation
set ns  [new Simulator]
#Creating nam and trace file
set tracefd       [open Graph3.tr w]
set namtrace      [open Graph3.nam w]   
$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)]

# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
# 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 black
            $ns at 0.0 "$node_($i) color black"
      }
# Provide initial location of mobilenodes
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 250.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 300.0
$node_(2) set Y_ 300.0
$node_(2) set Z_ 0.0
# 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 10.01 "puts \"end simulation\" ; $ns halt"
#Graph procedure..
$ns at 1.0 "Graph"
set g [open graph.tr w]
set g1 [open graph1.tr w]
set g2 [open graph2.tr w]
proc Graph {} {
global ns g g1
set time 1.0
set now [$ns now]
puts $g "[expr rand()*8] [expr rand()*6]"
puts $g1 "[expr rand()*8] [expr rand()*6]"
puts $g2 "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
#Stop proceture
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec xgraph -M -bb -geometry 700X800 graph.tr graph1.tr graph2.tr &
exec nam Graph3.nam &
exit 0
}
$ns run

Tcl script to generate graph taking two parameter files as output

 

set tracefd       [open Graph2.tr w]
set namtrace      [open Graph2.nam w]   

$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)]
# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
## 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 black
            $ns at 0.0 "$node_($i) color black"
      }
# Provide initial location of mobilenodes
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 250.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 300.0
$node_(2) set Y_ 300.0
$node_(2) set Z_ 0.0
# 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 10.01 "puts \"end simulation\" ; $ns halt"
#Graph procedure..
#procedure..
$ns at 1.0 "Graph"
set g [open graph.tr w]
set g1 [open graph1.tr w]
proc Graph {} {
global ns g g1
set time 1.0
set now [$ns now]
puts $g "[expr rand()*8] [expr rand()*6]"
puts $g1 "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
#stop procedure:
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec xgraph -P -bb -geometry 700X800 graph.tr graph1.tr &
exec nam Graph2.nam &
exit 0
}
$ns run

Tcl script to generate graph taking two parameter files as output

 

set tracefd       [open Graph2.tr w]
set namtrace      [open Graph2.nam w]   

$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)]
# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
## 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 black
            $ns at 0.0 "$node_($i) color black"
      }
# Provide initial location of mobilenodes
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 250.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 300.0
$node_(2) set Y_ 300.0
$node_(2) set Z_ 0.0
# 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 10.01 "puts \"end simulation\" ; $ns halt"
#Graph procedure..
#procedure..
$ns at 1.0 "Graph"
set g [open graph.tr w]
set g1 [open graph1.tr w]
proc Graph {} {
global ns g g1
set time 1.0
set now [$ns now]
puts $g "[expr rand()*8] [expr rand()*6]"
puts $g1 "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
#stop procedure:
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec xgraph -P -bb -geometry 700X800 graph.tr graph1.tr &
exec nam Graph2.nam &
exit 0
}
$ns run



 

 

 

 

 

 

 

 

 

 

 

Tcl script to draw the graph

 

set tracefd       [open Graph1.tr w]
set namtrace      [open Graph1.nam w]   
$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)]
# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
## 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 black
            $ns at 0.0 "$node_($i) color black"
      }
# Provide initial location of mobilenodes
$node_(0) set X_ 50.0
$node_(0) set Y_ 50.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 200.0
$node_(1) set Y_ 250.0
$node_(1) set Z_ 0.0
$node_(2) set X_ 300.0
$node_(2) set Y_ 300.0
$node_(2) set Z_ 0.0
# 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 10.01 "puts \"end simulation\" ; $ns halt"
#Graph procedure..
$ns at 1.0 "Graph"
set g [open graph.tr w]
proc Graph {} {
global ns g
set time 1.0
set now [$ns now]
puts $g "[expr rand()*8] [expr rand()*6]"
$ns at [expr $now+$time] "Graph"
}
#Stop proceture
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec xgraph -M -bb -geometry 700X800 graph.tr &
exec nam Graph1.nam &
exit 0
}
$ns run


 

 

 

 

 

 

 

 

 

 

 

 

To set nodes initial and destination position randomly in TCL Script

 

 

# 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)                500                                                     ;# X dimension of topography
set val(y)                400                                                     ;# Y dimension of topography 
set val(stop)           10                                                       ;# time of simulation end
#Creating simulation:
set ns              [new Simulator]
#Creating nam and trace file:
set tracefd       [open wireless3.tr w]
set namtrace      [open wireless3.nam w]   
$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)]
# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
## Creating node objects..         
for {set i 0} {$i < 3 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 0} {$i < 3  } {incr i } {
            $node_($i) color blue
            $ns at 0.0 "$node_($i) color blue"
      }
for {set i 3} {$i < 6 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 3} {$i < 6  } {incr i } {
            $node_($i) color cyan
            $ns at 1.0 "$node_($i) color cyan"
      }
for {set i 6} {$i < 8 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 5} {$i < 8  } {incr i } {
            $node_($i) color red
            $ns at 2.0 "$node_($i) color red"
      }
## Provide initial location of mobilenodes..
            for {set i 0} {$i < $val(nn) } { incr i } {
                  set xx [expr rand()*500]
                  set yy [expr rand()*400]
                  $node_($i) set X_ $xx
                  $node_($i) set Y_ $yy
                 
            }
# 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";
}
# dynamic destination setting procedure..
$ns at 0.0 "destination"
proc destination {} {
      global ns val node_
      set time 1.0
      set now [$ns now]
      for {set i 0} {$i<$val(nn)} {incr i} {
            set xx [expr rand()*500]
            set yy [expr rand()*400]
            $ns at $now "$node_($i) setdest $xx $yy 10.0"
      }
      $ns at [expr $now+$time] "destination"
}
#stop procedure..
$ns at $val(stop) "stop"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec nam wireless3.nam &
}
$ns run

 

 

 

To set nodes destination and color randomly


# Define setting 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)              500                        ;# X dimension of topography
set val(y)              400                        ;# Y dimension of topography 
set val(stop)           10                   ;# time of simulation end
set ns              [new Simulator]
#Creating nam and trace file:
set tracefd       [open wireless2.tr w]
set namtrace      [open wireless2.nam w]   
$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)]
# 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) \
                   -channelType $val(chan) \
                   -topoInstance $topo \
                   -agentTrace ON \
                   -routerTrace ON \
                   -macTrace OFF \
                   -movementTrace ON
                 
## Creating node objects..         
for {set i 0} {$i < 3 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 0} {$i < 3  } {incr i } {
            $node_($i) color blue
            $ns at 0.0 "$node_($i) color blue"
      }
for {set i 3} {$i < 6 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 3} {$i < 5  } {incr i } {
            $node_($i) color cyan
            $ns at 0.0 "$node_($i) color cyan"
      }
for {set i 5} {$i < 8 } { incr i } {
            set node_($i) [$ns node]     
      }
      for {set i 5} {$i < 8  } {incr i } {
            $node_($i) color red
            $ns at 0.0 "$node_($i) color red"
      }
## Provide initial location of mobilenodes..
            for {set i 0} {$i < $val(nn) } { incr i } {
                  set xx [expr rand()*500]
                  set yy [expr rand()*400]
                  $node_($i) set X_ $xx
                  $node_($i) set Y_ $yy
                 
            }
# 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";
}
# dynamic destination setting procedure..
$ns at 0.0 "destination"
proc destination {} {
      global ns val node_
      set time 1.0
      set now [$ns now]
      for {set i 0} {$i<$val(nn)} {incr i} {
            set xx [expr rand()*500]
            set yy [expr rand()*400]
            $ns at $now "$node_($i) setdest $xx $yy 10.0"
      }
      $ns at [expr $now+$time] "destination"
}
#stop procedure..
$ns at $val(stop) "stop"
proc stop {} {
    global ns tracefd namtrace
    $ns flush-trace
    close $tracefd
    close $namtrace
exec nam wireless2.nam &
}
$ns run

Monday 7 January 2013

How to install Cygwin and NS2 in windows?


[Cygwin]
1.      Download the cygwin.rar.

2.      Decompress the cygwin.rar
 

1.      Click the setup.exe to install cygwin.

2.      Because the language of operating system is Traditional Chinese. The text of button is shown in Chinese. But you don’t need to worry about this. I think it is shown in English in your computer. Just click “Next”.

3.      Then choose “Install from Local Directory”

4.      Click “Next”.
(Just keep the settings as they are.)

5.      Click “Next”

6.      Click “Browse” to choose where the software is. (Please choose “ftp%3a%2f%2fftp.nctu.edu.tw%2fWindows%2fcygwin”)

7.      Click “OK”.

8.      Click “Next” and you will see the figure shown as follows. In this window, the cygwin setup program let you choose what software you want to install.

9.      Click “View” first to make the word “Category “change to “Full”.

10.   XFree86-base, XFree86-bin, XFree86-prog, XFree86-lib, XFree86-etc, make, patch, perl, gcc, gcc-g++, gawk, gnuplot, tar and gzip must be chosen. For example, if I want to install XFree-86 base (upper figure), click the “Skip” of “New” column. The “Skip” will be changed to “4.3.0-1” (lower figure).

11.  Click “Next”.
(Please be patient. It may take a long time to finish the installation.)

12.   When setup is done, it will be shown as following figure. Click “Finish”.
 

13.   Click “OK” to finish the cygwin setup program.

[myNS2 setup]
1.      Click the icon on the desktop.

2.      For the first time execution, it will generate some environment parameter setting files. In this example, smallko is my login name to windows system. Therefore, the cygwin will create a folder named “smallko” under home directory. (The actual path for smallko folder is: c:\cygwin\home\smallko) It should be noticed that the login name can not have any space in your name. For example, “A  B” may cause errors when you install NS2.

3.      Download myNS2.

4.      Decompress the ns-allinone-2.28.rar.

5.      Move this folder under c:\cygwin\home\smallko. (P.S. smallko is my login name)

6.      Open a cygwin window

7.      Change the path to ns-allinone-2.28/ns-2.28

8.      Run the command “./configure; make clean; make

9.      Please be patient. It will take some time to finish the compilation.

10.  When it is done, it should look like as follows.

11.  To make sure that you have successfully installed myNS2, you need to check whether you can find ns.exe under ns-allinone-2.28/ns-2.28

12.  Copy the .bashrc to c:/cygwin/home/smallko
13.  Run the example script to test whether you have setup the path or not.
 

14.   If you see the error message like “ns command not found”, no worry about this. Sometimes even you have setup the path, but it does not work. You can copy the ns.exe (nam.exe) to the same place as the simulation script. Run the simulation with “./ns.exe” and it will be ok.