#Procedure exponential traffic generator
proc attach-expoo-traffic {node sink size burst idle rate class color} {
        
	#get simulator instance
	set ns [Simulator instance]

        #Create UDP agent and attach it to the node
	set source [new Agent/UDP]
        $ns attach-agent $node $source
        $source set class_ $class
        $ns color $class $color

        #Create Expoo traffic agent and set its configuration parameters
	set traffic [new Application/Traffic/Exponential]
        $traffic set packetSize_ $size
        $traffic set burst_time_ $burst
        $traffic set idle_rate_ $rate
        $traffic set rate_ $rate

        #Attach traffic source to traffic generator
	$traffic attach-agent $source
        #Connect source and sink
	$ns connect $source $sink
        return $traffic
}

#Define a 'finish' procedure
proc finish {} {
        global ns nf tr MAX_TIME sink0
        $ns flush-trace
	#Close the trace file
        close $nf
	close $tr

	#How many bytes have been received by traffic sinks?
	set now [$ns now]
	#Get current time
	puts "Estatisticas:"
	puts "Tempo Simulacao:  $now s"
	puts "Pacotes recebidos no nodo 3:  [$sink0 set npkts_]"
	puts "Bytes recebidos no nodo 3:  [$sink0 set bytes_]"
	puts "Utilizacao do link:  [expr [$sink0 set bytes_]*8./(10000000.*$now)*100.]%"
	
	#Execute nam on the trace file
        exec nam out.nam &
        exit 0
}

#Create Simulator object
set ns [new Simulator]

#Set simulation time
set MAX_TIME 500

#Seed random number generator with current time (for Expoo traffic)
ns-random 0

#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red

#Open the nam trace file
set nf [open out.nam w]
set tr [open out.tr w]
$ns trace-all $tr
$ns namtrace-all $nf

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
$ns duplex-link $n2 $n3 10Mb 10ms SFQ
#$ns queue-limit $n2 $n3 100

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5

#Create null agent (sink)
set sink0 [new Agent/LossMonitor]
$ns attach-agent $n3 $sink0

#Create exponential generators (node sink size burst idle rate class color)
set traffgen0 [attach-expoo-traffic $n0 $sink0 1000 800ms 2ms 5M 1 Green]

#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 1000
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp1

#Connect CBR traffic source to the traffic sink
$ns connect $udp1 $sink0

#Schedule events for the traffic generators
$ns at 0.0 "$traffgen0 start"
$ns at 1.0 "$cbr0 start"
$ns at [expr $MAX_TIME-1] "$traffgen0 stop"
$ns at [expr $MAX_TIME-1] "$cbr0 stop"
#Call the finish procedure after $MAX_TIME seconds of simulation time
$ns at $MAX_TIME "finish"

#Run the simulation
$ns run

