Thursday 27 December 2012


NAM 2 Code for Network simulation


#code for changing value of size by using the command line
if {$argc == 1}{
set size [lindex $argv 0]
}
else {
puts "Usage: ns $argv 0 size"
exit 1
}

#Section 1
#Create a simulator object
set ns [new Simulator]

#Section 2
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Section 3
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
#exec nam out.nam &
exit 0
}

#Section 4
#Create nodes named n0-n6 according to cities 1-7
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

#Connect nodes n0-n6 by a duplex links with DropTail queuing
#Bandwidth 2Mb/s
#Delay = Link distance in km * 1000/speed of light(300000000)

#Central node5(City6) links
$ns duplex-link $n5 $n6 2Mb 1.4ms DropTail
$ns duplex-link $n5 $n1 2Mb 0.7ms DropTail
$ns duplex-link $n5 $n4 2Mb 2ms DropTail
$ns duplex-link $n5 $n3 2Mb 0.3ms DropTail

#node2( city 2) links
$ns duplex-link $n2 $n6 2Mb 1.3ms DropTail
$ns duplex-link $n2 $n1 2Mb 1.9ms DropTail

#node0(city 1) links
$ns duplex-link $n0 $n4 2Mb 0.27ms DropTail
$ns duplex-link $n0 $n3 2Mb 2.5ms DropTail

#Set the queue limit for each link using variable $size
$ns queue-limit $n5 $n6 $size
$ns queue-limit $n5 $n1 $size
$ns queue-limit $n5 $n4 $size
$ns queue-limit $n5 $n3 $size

$ns queue-limit $n2 $n6 $size
$ns queue-limit $n2 $n1 $size

$ns queue-limit $n0 $n4 $size
$ns queue-limit $n0 $n3 $size

#Create TCP connections between cities
#HTML server connections for city 3(n2) links
set tcp0 [$ns create-connection TCP $n2 TCPSink $n0 1]
set tcp1 [$ns create-connection TCP $n2 TCPSink $n1 1]
set tcp2 [$ns create-connection TCP $n2 TCPSink $n3 1]
set tcp3 [$ns create-connection TCP $n2 TCPSink $n4 1]
set tcp4 [$ns create-connection TCP $n2 TCPSink $n5 1]
set tcp5 [$ns create-connection TCP $n2 TCPSink $n6 1]

#FTP server connections for city 6(n5) links
set tcp6 [$ns create-connection TCP $n5 TCPSink $n0 1]
set tcp7 [$ns create-connection TCP $n5 TCPSink $n1 1]
set tcp8 [$ns create-connection TCP $n5 TCPSink $n2 1]
set tcp9 [$ns create-connection TCP $n5 TCPSink $n3 1]
set tcp10 [$ns create-connection TCP $n5 TCPSink $n4 1]
set tcp11 [$ns create-connection TCP $n5 TCPSink $n6 1]

#Create Application/Traffic/Exponential sources exp0-exp11
set exp0 [new Application/Traffic/Exponential]
set exp1 [new Application/Traffic/Exponential]
set exp2 [new Application/Traffic/Exponential]
set exp3 [new Application/Traffic/Exponential]
set exp4 [new Application/Traffic/Exponential]
set exp5 [new Application/Traffic/Exponential]
set exp6 [new Application/Traffic/Exponential]
set exp7 [new Application/Traffic/Exponential]
set exp8 [new Application/Traffic/Exponential]
set exp9 [new Application/Traffic/Exponential]
set exp10 [new Application/Traffic/Exponential]
set exp11 [new Application/Traffic/Exponential]

#Set the packet size,burst time,idle time and transmission rate for sources exp0-ex11
#burst time = bandwidth * 0.1
#Idle time =0.1-burst time
$exp0 set packetSize_ 500
$exp0 set burst_time_ 0.01
$exp0 set idle_time_ 0.09
$exp0 set rate_ 1Mb

#exp1 source
$exp1 set packetSize_ 500
$exp1 set burst_time_ 0.01
$exp1 set idle_time_ 0.09
$exp1 set rate_ 1Mb

#exp2 source
$exp2 set packetSize_ 500
$exp2 set burst_time_ 0.05
$exp2 set idle_time_ 0.05
$exp2 set rate_ 1Mb

#exp3 source
$exp3 set packetSize_ 500
$exp3 set burst_time_ 0.07
$exp3 set idle_time_ 0.03
$exp3 set rate_ 1Mb

#exp4 source
$exp4 set packetSize_ 500
$exp4 set burst_time_ 0.02
$exp4 set idle_time_ 0.08
$exp4 set rate_ 1Mb

#exp5 source
$exp5 set packetSize_ 500
$exp5 set burst_time_ 0.05
$exp5 set idle_time_ 0.05
$exp5 set rate_ 1Mb

#exp6 source
$exp6 set packetSize_ 500
$exp6 set burst_time_ 0.03
$exp6 set idle_time_ 0.07
$exp6 set rate_ 1Mb

#exp7 source
$exp7 set packetSize_ 500
$exp7 set burst_time_ 0.06
$exp7 set idle_time_ 0.04
$exp7 set rate_ 1Mb

#exp8 source
$exp8 set packetSize_ 500
$exp8 set burst_time_ 0.09
$exp8 set idle_time_ 0.01
$exp8 set rate_ 1Mb

#exp9 source
$exp9 set packetSize_ 500
$exp9 set burst_time_ 0.08
$exp9 set idle_time_ 0.02
$exp9 set rate_ 1Mb

#exp10 source
$exp10 set packetSize_ 500
$exp10 set burst_time_ 0.04
$exp10 set idle_time_ 0.06
$exp10 set rate_ 1Mb

#exp11 source
$exp11 set packetSize_ 500
$exp11 set burst_time_ 0.07
$exp11 set idle_time_ 0.03
$exp11 set rate_ 1Mb

#Attach the source agents from exp0-11 to tcp0-11 accordingly
$exp0 attach-agent $tcp0
$exp1 attach-agent $tcp1
$exp2 attach-agent $tcp2
$exp3 attach-agent $tcp3
$exp4 attach-agent $tcp4
$exp5 attach-agent $tcp5
$exp6 attach-agent $tcp6
$exp7 attach-agent $tcp7
$exp8 attach-agent $tcp8
$exp9 attach-agent $tcp9
$exp10 attach-agent $tcp10
$exp11 attach-agent $tcp11

#Tell the sources when to start
$ns at 0.1 "$exp0 start"
$ns at 0.1 "$exp1 start"
$ns at 0.1 "$exp2 start"
$ns at 0.1 "$exp3 start"
$ns at 0.1 "$exp4 start"
$ns at 0.1 "$exp5 start"
$ns at 0.1 "$exp6 start"
$ns at 0.1 "$exp7 start"
$ns at 0.1 "$exp8 start"
$ns at 0.1 "$exp9 start"
$ns at 0.1 "$exp10 start"
$ns at 0.1 "$exp11 start"

#Section 5
#Call the finish procedure at the end of the simulation time
$ns at 1.0 "finish"

#Section 6
#Run the simulation
$ns run