#!/usr/bin/tixwish4.1.8.0
# BLURB gpl
# 
#                            Coda File System
#                               Release 5
# 
#           Copyright (c) 1987-1999 Carnegie Mellon University
#                   Additional copyrights listed below
# 
# This  code  is  distributed "AS IS" without warranty of any kind under
# the terms of the GNU General Public Licence Version 2, as shown in the
# file  LICENSE.  The  technical and financial  contributors to Coda are
# listed in the file CREDITS.
# 
#                         Additional copyrights
#                            none currently
# 
#*/

#
# initialize chart parameters
#
set strip_value1   0
set strip_value2   0
set strip_value3   0
set strip_interval 1000
set strip_log1     [ list ]
set strip_log2     [ list ]
set strip_log3     [ list ]
set strip_idx      0
set strip_label    ""

#
# create graphical stuff
#
label .strip_label -text ""
pack .strip_label
canvas .strip_graph -height 100 -width 200 \
		    -relief sunken -border 2
pack .strip_graph

for { set x 0 } { $x < 200 } { incr x } {
  lappend strip_log1 0
  lappend strip_log2 0
  lappend strip_log3 0
}

proc strip_draw {} {
  global strip_log1 strip_value1
  global strip_log2 strip_value2
  global strip_log3 strip_value3
  global strip_interval strip_idx strip_label

  set strip_log1 [ lreplace $strip_log1 $strip_idx $strip_idx $strip_value1 ]
  set strip_log2 [ lreplace $strip_log2 $strip_idx $strip_idx $strip_value2 ]
  set strip_log3 [ lreplace $strip_log3 $strip_idx $strip_idx $strip_value3 ]

  .strip_graph delete data

  set i [ expr 202 - $strip_idx ]
  foreach v1 $strip_log1 v2 $strip_log2 v3 $strip_log3 {
    if { $v1 < 100 } { set v1 100 }
    if { $v2 < 100 } { set v2 100 }
    if { $v3 < 100 } { set v3 100 }
    set val1 [ expr ( ( log10($v1) - 3 ) * 20 ) ]
    set val2 [ expr ( ( log10($v2) - 3 ) * 20 ) ]
    set val3 [ expr ( ( log10($v3) - 3 ) * 20 ) ]
    .strip_graph create line $i 103 \
			     $i [ expr 102 - $val1 ] \
			     -tags data -fill LightSlateBlue
    .strip_graph create line $i [ expr 102 - $val1 ] \
			     $i [ expr 102 - $val3 ] \
			     -tags data -fill SkyBlue
    .strip_graph create line $i [ expr 102 - $val2 ] \
			     $i [ expr 103 - $val2 ] \
			     -tags data -fill Red

    incr i
    if { $i >= 202 } { set i 2 }
  }
  # 1,000 Bps (modem 4800-28800)
  .strip_graph create text 4 99 -text "Modem" -tags data -anchor w -font 5x7

  # 10,000 Bps (ISDN (64k-128k))
  .strip_graph create line 2 83 202 83 -tags data -fill DarkSlateGray
  .strip_graph create text 4 79 -text "ISDN" -tags data -anchor w -font 5x7

  # 100,000 Bps
  .strip_graph create line 2 63 202 63 -tags data -fill DarkSlateGray
  .strip_graph create text 4 59 -text "Wavelan" -tags data -anchor w -font 5x7

  # 1,000,000 Bps (10base-T)
  .strip_graph create line 2 43 202 43 -tags data -fill DarkSlateGray
  .strip_graph create text 4 39 -text "Ethernet" -tags data -anchor w -font 5x7

  # 10,000,000 Bps (100base-T)
  .strip_graph create line 2 23 202 23 -tags data -fill DarkSlateGray
  .strip_graph create text 4 19 -text "Fast Ethernet" -tags data -anchor w -font 5x7

  .strip_graph create text 200 10 -text "$strip_label" -tags data -anchor e

  incr strip_idx
  if { $strip_idx >= 200 } { set strip_idx 0 }

  after $strip_interval strip_draw
}

proc strip_title {text} {
  .strip_label configure -text $text
}

#########################################################################
#
# Venus related stuff
#
proc updatestats {} {
  global input
  #
  # Get a line (may be empty or not ready yet)
  #
  set n [ gets $input line ]
  if { $n == -1 } {
    if [ eof $input ] {
      puts "Connection to venus lost"
      exit 0
    }
    return
  }

  checkline $line
}

#
# connect to the venus port
#
set input [ socket localhost venus ]
puts "Connected to venus"
puts $input "set:fetch"
flush $input

#
# set the socket to non-blocking and attach the callback
#
fconfigure $input -blocking off
fileevent $input readable updatestats

#########################################################################
#
# Customized stuff
#
proc checkline {line} {
  global who strip_value1 strip_value2 strip_value3 strip_label

  #
  # match our patterns
  #
  if [ string match "connection::bandwidth $who*" $line ] {
    if { $who == "" } { set who [ lindex $line 1 ] }
    strip_title [ lindex $line 1]
    set strip_value1 [ lindex $line 2 ]
    set strip_value2 [ lindex $line 3 ]
    set strip_value3 [ lindex $line 4 ]
    if { $strip_value1 < 0 } { set strip_value1 0 }
    if { $strip_value2 < 0 } { set strip_value2 0 }
    if { $strip_value3 < 0 } { set strip_value3 0 }
    set strip_label "[ expr $strip_value2 * 8 ] bps"
  }
}

#
# configure and start drawing
#
set who [ lindex $argv 0 ]
set strip_interval 100
strip_draw
