NSSL Home > Scientific Publications > Technical Memorandum NSSL-107

[Table of Contents]

 

Appendix A: NS Simulation Source Code

 

#Create a simulator object
set ns [new Simulator]

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

#Open files for queue and statistics information
set qf [open q.tr w]
set qf1 [open q1.tr w]
set sf [open stat.tr w]

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

#Define packet information
set myPacketSize 1000   ;# in bytes
set myRate 100		;# in kilobits 

#Define queue information
set queuelimit 100	;# in MBytes
set queuesize [expr $queuelimit * 1000000 / $myPacketSize]

#Running time
set interval 30

#Define a 'finish' procedure
proc finish {} {
	global ns nf qf sf qf1 
	$ns flush-trace

	close $nf
	close $qf
	close $qf1
	close $sf

	exec nam out.nam &
	exit 0
}

#Write information into the stat file
proc write-stats-info {node myqmon mysamples} {
	global sf interval 

	puts $sf "Node: $node"
	puts $sf "---------------packet------------------"
	puts $sf "# of packet arrivals = [expr [$myqmon set parrivals_]/$interval]/seconds"
	puts $sf "# of packet departures = [expr [$myqmon set pdepartures_]/$interval]/seconds"
	puts $sf "# of packet drops = [$myqmon set pdrops_]"
	puts $sf "# of packets in queue when ended = [$myqmon set pkts_]"
	set pktint [$myqmon get-pkts-integrator]
	puts $sf "Integral queue size = [$pktint set sum_]"
	
	puts $sf "---------------bytes-------------------"
	puts $sf "# of bytes arrival = [expr [$myqmon set barrivals_]/$interval]/seconds"
	puts $sf "# of bytes departure = [expr [$myqmon set bdepartures_]/$interval]/seconds"
	puts $sf "# of bytes drop = [$myqmon set bdrops_]"
	puts $sf "# of bytes in queue when ended = [$myqmon set size_]"
	set byteint [$myqmon get-bytes-integrator]
	puts $sf "Integral queue size = [$byteint set sum_]"	
	puts $sf ""
	puts $sf "Average queue delay = [$mysamples mean]"
	puts $sf ""
	puts $sf ""
}

proc attach-cbr-traffic {node mysink size rate mycolor} {
	#Get an instance of the simulator
	set ns [Simulator instance]

	#Create a TCP agent and attach it to the node
	set source [new Agent/UDP]
	#$source set packetSize_ $size
	$ns attach-agent $node $source
	$source set class_ $mycolor

	#Create a constant bit rate traffic and set its configuration parameters
	set traffic [new Application/Traffic/CBR]
	$traffic set packetSize_ $size
	$traffic set rate_ [append rate "kb"]

	#Attach traffic source to the traffic generator
	$traffic attach-agent $source

	#Connect the source and the sink
	$ns connect $source $mysink
	return $traffic
}

#Create nodes
set norman [$ns node]
set onenet [$ns node]
set ncdc [$ns node]
set amarillo [$ns node]
set lubbock [$ns node]
set dfdv [$ns node]
set ftsmith [$ns node]
set tulsa [$ns node]
set twinlaks [$ns node]

#Label node
$norman label "Norman"
$onenet label "OneNet node"
$ncdc label "NCDC"
$amarillo label "Amarillo"
$lubbock label "Lubbock"
$dfdv label "DFDV"
$ftsmith label "Ft. Smith"
$tulsa label "Tulsa"
$twinlaks label "TWINLAKS"

#Define special characteristics
$onenet shape "box"

#Create duplex-link
$ns duplex-link $norman $onenet 45Mb 10ms DropTail
$ns duplex-link $onenet $ncdc 2400Mb 10ms DropTail 
$ns duplex-link $amarillo $norman 56kb 10ms DropTail
$ns duplex-link $lubbock $norman 56kb 10ms DropTail
$ns duplex-link $dfdv $norman 56kb 10ms DropTail
$ns duplex-link $ftsmith $tulsa 56kb 10ms DropTail
$ns duplex-link $tulsa $norman 1.54Mb 10ms DropTail
$ns duplex-link $twinlaks $norman 1.54Mb 10ms DropTail

#$ns duplex-link-op $amarillo $norman queuePos 0.5

#Set the queue limit
$ns queue-limit $norman $onenet $queuesize
$ns queue-limit $amarillo $norman $queuesize
$ns queue-limit $lubbock $norman $queuesize
$ns queue-limit $dfdv $norman $queuesize
$ns queue-limit $ftsmith $tulsa $queuesize
$ns queue-limit $tulsa $norman $queuesize
$ns queue-limit $twinlaks $norman $queuesize

#Create queue monitor for the norman station
set qmon [$ns monitor-queue $norman $onenet $qf]
set samples [new Samples]
$qmon set-delay-samples $samples

#Create queue monitor for the amarillo station
set qmon1 [$ns monitor-queue $amarillo $norman $qf1]
set samples1 [new Samples]
$qmon1 set-delay-samples $samples1

for {set i 0} {$i<6} {incr i} {
	set sink($i) [new Agent/Null]
	$ns attach-agent $norman $sink($i)
}

set sink_tulsa [new Agent/Null]
$ns attach-agent $tulsa $sink_tulsa

set sink_ncdc [new Agent/Null]
$ns attach-agent $ncdc $sink_ncdc

set doubleRate [expr $myRate * 2]
set totalRate [expr $myRate * 6]

#Create traffic sources
set cbr0 [attach-cbr-traffic $amarillo $sink(0) $myPacketSize $myRate 0]
set cbr1 [attach-cbr-traffic $lubbock $sink(1) $myPacketSize $myRate 1]
set cbr2 [attach-cbr-traffic $dfdv $sink(2) $myPacketSize $myRate 2]
set cbr3 [attach-cbr-traffic $twinlaks $sink(3) $myPacketSize $myRate 3]
set cbr4 [attach-cbr-traffic $tulsa $sink(4) $myPacketSize $doubleRate 4]
set cbr5 [attach-cbr-traffic $ftsmith $sink_tulsa $myPacketSize $myRate 0]
set cbr6 [attach-cbr-traffic $norman $sink_ncdc $myPacketSize $totalRate 2]

puts $sf "**************************"
puts $sf "Durations = $interval seconds"
puts $sf "Date Rate = $myRate kbits"
puts $sf "Packet Size = $myPacketSize bytes"
puts $sf "**************************"
puts $sf ""

$ns at 0.11 "$cbr0 start"
$ns at 0.12 "$cbr1 start"
$ns at 0.13 "$cbr2 start"
$ns at 0.14 "$cbr3 start"
$ns at 0.15 "$cbr4 start"
$ns at 0.16 "$cbr5 start"
$ns at 0.17 "$cbr6 start"

$ns at [expr $interval + 0.11] "$cbr0 stop"
$ns at [expr $interval + 0.12] "$cbr1 stop"
$ns at [expr $interval + 0.13] "$cbr2 stop"
$ns at [expr $interval + 0.14] "$cbr3 stop"
$ns at [expr $interval + 0.15] "$cbr4 stop"
$ns at [expr $interval + 0.16] "$cbr5 stop"
$ns at [expr $interval + 0.17] "$cbr6 stop"

$ns at [expr $interval + 0.4] "write-stats-info norman $qmon $samples"
$ns at [expr $interval + 0.4] "write-stats-info amarillo $qmon1 $samples1"

$ns at [expr $interval + 0.7] "finish"

#Run the simulation
$ns run 

 

Next: Appendix B: Sample Output Statistics Code