Thursday, 23 January 2014

Simple MANET topology in ns2

This is a sample Tcl script for simple manet topology creation,


# ======================================================================

# Define options for 

# ======================================================================

 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)       200                      ;# max packet in ifq

 set val(netif)        Phy/WirelessPhy          ;# network interface type

 set val(mac)          Mac/802_11               ;# MAC type

 set val(nn)           7                       ;# number of mobilenodes

 set val(rp)           AODV                     ;# Bio sensor routing protocol

 set val(x)            670

 set val(y)            510
source config        

set n(0) [$ns node]

$ns at 0.0 "$n(0) color red"

$n(0) color green

$n(0) shape "circle"
$ns at 0.0 "$n(0) label sensor"
set n(1) [$ns node]

$ns at 0.0 "$n(1) color red"

$n(1) color green

$n(1) shape "circle"
$ns at 0.0 "$n(1) label sensor"
set n(2) [$ns node]

$ns at 0.0 "$n(2) color red"

$n(2) color green

$n(2) shape "circle"
$ns at 0.0 "$n(2) label sensor"
set n(3) [$ns node]

$ns at 0.0 "$n(3) color darkgreen"

$n(3) color green

$n(3) shape "circle"

$ns at 0.0 "$n(3) label Sink"
set n(4) [$ns node]

$ns at 0.0 "$n(4) color red"

$n(4) color green

$n(4) shape "circle"
$ns at 0.0 "$n(4) label sensor"

set n(5) [$ns node]

$ns at 0.0 "$n(5) color red"

$n(5) color green

$n(5) shape "circle"
$ns at 0.0 "$n(5) label sensor"
set n(6) [$ns node]

$ns at 0.0 "$n(6) color red"

$n(6) color green

$n(6) shape "circle"

$ns at 0.0 "$n(6) label sensor"

for {set i 0} {$i < $val(nn)} {incr i} {

        $ns initial_node_pos $n($i) 30

}

$ns at 0.0 "$n(0) setdest 645.0 378.0 30000.0"

$ns at 0.0 "$n(1) setdest 233.0 371.0 30000.0"

$ns at 0.0 "$n(2) setdest 113.0 366.0 30000.0"

$ns at 0.0 "$n(3) setdest 392.0 474.0 30000.0"
                                                                                         

$ns at 0.0 "$n(4) setdest 415.0 184.0 30000.0"

$ns at 0.0 "$n(5) setdest 410.0 65.0 30000.0"

$ns at 0.0 "$n(6) setdest 413.0 259.0 20000.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]

set sink6 [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 $n(6) $sink6

#==============

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

set tcp6 [new Agent/TCP]

$ns attach-agent $n(6) $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

  }
$ns color 0 brown
proc finish {} {

        global ns f namtrace

        $ns flush-trace

        close $namtrace

        #close $f0

        exec nam log.nam &

        exit 0

}
$ns at 5.0 "$n(1) setdest 323.0 371.0 30.0"

$ns at 5.0 "$n(2) setdest 11.0 366.0 30.0"                                                                              

$ns at 2.0 "$n(4) setdest 195.0 234.0 30.0"

$ns at 3.0 "$n(5) setdest 41.0 135.0 30.0"

$ns at 4.0 "$n(6) setdest 443.0 259.0 20.0"

#===

set data [attach-CBR-traffic $n(5) $sink3 500 .2]



set data1 [attach-CBR-traffic $n(1) $sink3 500 .05]


#==

$ns at 0.3 "$data start"

$ns at 4.0 "$data1 start"


$ns at 15.0 "finish"

#===========

puts "Start of simulation.."

$ns run

#==============

No comments:

Post a Comment