#!/usr/local/bin/wish -f
set i 0
proc ini {} {
    global i
    set i 0
}

proc nuovo {nm} {
    global i w
    set i [expr $i + 1]
    set w($nm) $i
    set w(p,$nm) 0
    set w(bw,$nm) 0
    frame .f$i
    pack .f$i -fill x
    frame .f$i.l
    pack .f$i.l -side left -fill x -expand 1
    canvas .f$i.c -width 600 -height 50 -background red
    pack .f$i.c -side right -anchor se
    .f$i.c create line 0 0 599 0 -fill white
    .f$i.c create line 0 25 599 25 -fill green
    .f$i.c create line 0 49 599 49 -fill blue
    label .f$i.l.l -text $nm -background black -foreground white
    pack .f$i.l.l -side top -anchor ne
    label .f$i.l.l1 -textvariable w(bw,$nm) -background black -foreground white
    pack .f$i.l.l1 -side top -anchor ne
}

proc processa {a} {
    global w
    set bw [lindex $a 6]
    set deltat [lindex $a 7]
    set nm [lindex $a 3]
    if [expr  $deltat < 1 ] {
	set deltat 1
    }
    set w(bw,$nm) "bw [expr $bw / $deltat] B/s"
    if [catch {set w($nm)}] {
	nuovo $nm
    }
    set c .f$w($nm).c
    set x $w(p,$nm)
    set y [expr 49 - (log ($bw + 1))]
    $c create line $x 49 $x $y
    set w(p,$nm) [expr ($x + 1) % 600]
}

while {[gets stdin a] != -1 } {
    update
    processa "$a"
}
