Skip to content

Instantly share code, notes, and snippets.

@candh
Created December 4, 2019 10:11
Show Gist options
  • Save candh/f367878b5401c389df30b56e26e96eb0 to your computer and use it in GitHub Desktop.
Save candh/f367878b5401c389df30b56e26e96eb0 to your computer and use it in GitHub Desktop.
# create a new simulator object
set ns [new Simulator]
# define different colors for data flows
# as defined by fid_
$ns color 1 Blue
$ns color 2 Red
$ns color 3 Green
$ns color 4 Black
# open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
# define a finish procedure
proc finish {} {
global ns nf
$ns flush-trace
# close the nam trace file
close $nf
# execute nam on the trace file
exec nam out.nam &
exit 0
}
# setup custom variables
set packet_sizeh 1038
# create 7 nodes
for {set i 1} {$i < 8} {incr i} {
set n$i [$ns node]
}
# set n7 as square
$n7 shape square
# create link between the nodes
$ns duplex-link $n1 $n7 5Mb 10ms DropTail
$ns duplex-link $n7 $n4 10Mb 4ms DropTail
$ns duplex-link $n7 $n5 5Mb 12ms DropTail
$ns duplex-link $n2 $n7 10Mb 5ms DropTail
$ns duplex-link $n7 $n3 2Mb 10ms DropTail
$ns duplex-link $n5 $n3 3Mb 10ms DropTail
$ns duplex-link $n4 $n6 2Mb 10ms DropTail
# give nodes position
$ns duplex-link-op $n1 $n7 orient right-down
$ns duplex-link-op $n2 $n7 orient right-up
$ns duplex-link-op $n7 $n4 orient down
$ns duplex-link-op $n7 $n3 orient up
$ns duplex-link-op $n4 $n6 orient right
$ns duplex-link-op $n3 $n5 orient right-down
$ns duplex-link-op $n7 $n5 orient right
# setup TCP(3) & CBR on N1
# TCP + CBR(3)[N1] <---> Sink(3)[N4]
set tcp3 [new Agent/TCP]
$tcp3 set class_ 2
$ns attach-agent $n1 $tcp3
set sink3 [new Agent/TCPSink]
$ns attach-agent $n4 $sink3
$ns connect $tcp3 $sink3
$tcp3 set fid_ 1
set cbr3 [new Application/Traffic/CBR]
$cbr3 attach-agent $tcp3
$cbr3 set type_ CBR
$cbr3 set packet_size_ $packet_sizeh
$cbr3 set rate_ 1mb
$cbr3 set random_ false
# UDP + CBR(1)[N5] <---> Null(1)[N1]
set udp1 [new Agent/UDP]
$ns attach-agent $n5 $udp1
set null1 [new Agent/Null]
$ns attach-agent $n1 $null1
$ns connect $udp1 $null1
$udp1 set fid_ 2
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set type_ CBR
$cbr1 set packet_size_ $packet_sizeh
$cbr1 set rate_ 1mb
$cbr1 set random_ false
# TCP + FTP(2)[N3] <---> Sink(2)[N2]
set tcp2 [new Agent/TCP]
$tcp2 set class_ 2
$ns attach-agent $n3 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2
$ns connect $tcp2 $sink2
$tcp2 set fid_ 3
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set type_ FTP
# TCP + FTP(1)[N7] <---> Sink(1)[N6]
set tcp1 [new Agent/TCP]
$tcp1 set class_ 2
$ns attach-agent $n7 $tcp1
set sink1 [new Agent/TCPSink]
$ns attach-agent $n6 $sink1
$ns connect $tcp1 $sink1
$tcp1 set fid_ 4
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ftp1 set type_ FTP
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr1 start"
$ns at 0.1 "$cbr3 start"
$ns at 1.0 "$ftp1 start"
$ns at 1.0 "$ftp2 start"
$ns at 4.5 "$cbr1 stop"
$ns at 4.5 "$cbr3 stop"
$ns at 4.9 "$ftp1 stop"
$ns at 4.9 "$ftp2 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr1 set packet_size_]"
puts "CBR interval = [$cbr3 set interval_]"
#Run the simulation
$ns run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment