Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
##iniciamos
set ns [new Simulator]
#remove-all-packet-headers; # removes all packet headers
#add-packet-header IP TCP; # adds TCP/IP headers
#set ns [new Simulator]; # instantiate the Simulator
#$ns use-scheduler Heap
##declaramos el tipo de enrutamiento
$ns rtproto LS
##archivo para la simulación
set nf [open a.nam w]
set nt [open trace.tr w]
set arc [open out0.tr w]
set by [open out1.tr w]
set loss [open los.tr w]
$ns namtrace-all $nf
$ns trace-all $nt
proc finish {} {
global ns nf nt arc by loss
$ns flush-trace
close $nf
close $nt
close $arc
close $by
close $loss
exec xgraph out0.tr los.tr -geometry 800x400 &
#exec xgraph out1.tr -geometry 800x400 &
exec nam -a a.nam &
exit 0
}
#set x 10;
puts stdout "Dame la cantidad de nodos: "
gets stdin x
#puts stdin “Valor de x”
#gets stdin x
##creamos 7 nodos
for {set i 0} {$i < $x} {incr i} {
set n($i) [$ns node]
$n($i) color blue
}
##un nodos centrales
set n0 [$ns node]
set size 500
$n0 label "servidor"
$n0 color red
##unimos los nodos 0 al 4 con el primer central
for {set i 0} {$i < $x} {incr i} {
$ns duplex-link $n0 $n($i) 1Mb 10ms DropTail
# $ns duplex-link $n($i) $n(0) 5Mb 50ms DropTail
}
###servidor a hijos
$ns color 1 red
$ns color 2 blue
for {set i 0} {$i < $x} {incr i} {
set udp($i) [new Agent/UDP]
$ns attach-agent $n0 $udp($i)
$udp($i) set fid_ 1
}
for {set i 0} {$i < $x} {incr i} {
set cbr($i) [new Application/Traffic/CBR]
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.001
$cbr($i) attach-agent $udp($i)
}
for {set i 0} {$i < $x} {incr i} {
set null($i) [new Agent/LossMonitor]
$ns attach-agent $n($i) $null($i)
$ns connect $udp($i) $null($i)
}
##hijos a servidor
####hijos a servidor
for {set i 0} {$i < $x} {incr i} {
set udp1($i) [new Agent/UDP]
$ns attach-agent $n($i) $udp1($i)
$udp1($i) set fid_ 2
}
for {set i 0} {$i < $x} {incr i} {
set cbr1($i) [new Application/Traffic/CBR]
$cbr1($i) set packetSize_ 500
$cbr1($i) set interval_ 0.001
$cbr1($i) attach-agent $udp1($i)
}
for {set i 0} {$i < $x} {incr i} {
set null1($i) [new Agent/LossMonitor]
$ns attach-agent $n0 $null1($i)
$ns connect $udp1($i) $null1($i)
}
proc veri {} {
##variables globales
global cbr null x cbr1 null1 n arc by loss
set ns [Simulator instance]
##cada cuando correremos esto
set time 0.05
##paquetes enviados exitosamente
set bits [$null(0) set bytes_]
##paquietes perdidos
set nbits [$null(0) set nlost_]
##sacamos la banda ancha
set cosa [expr $bits/$time*8/1000000]
##sacamos el tiempo actual de la simulacion
set now [$ns now]
###guardamos los datos en el archivos
puts $arc "$now [expr $bits/$time*8/1000000]"
puts $by "$now $bits"
puts $loss "$now $nbits"
##guardamos los datos en ela archivo
puts stdout "bytes de entrada: $cosa"
#$ns at [expr $now+$time] "record"
##aqui sabemos si sobre pasa la banda ancha
if {$cosa>1} {
#$null(0) set bytes_ 0
##disminuimos el intervalo de los paquetes y el tamaño de los mismos
for {set i 0} {$i < $x} {incr i} {
$cbr($i) set packetSize_ 100
$cbr($i) set interval_ 0.01
}
##prueba
}
##si la banda esta mui liberada subimos el intervalo de envio y el tamañ de los paquetes
if {$cosa<0.5} {
for {set i 0} {$i < $x} {incr i} {
$cbr($i) set packetSize_ 500
$cbr($i) set interval_ 0.001
}
}
##para el tiempo
$ns at [expr $now+$time] "veri"
}
$ns at 0.0 "veri"
for {set i 0} {$i < $x} {incr i} {
#$ns at 0.5 "$cbr1($i) start"
$ns at 0.1 "$cbr($i) start"
}
$ns at 10.0 "finish"
$ns run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment