# The Agni Project, NIST # $Author: schaal $ # $Date: 1999/02/02 20:44:58 $ # $Revision: 1.1 $ # $Source: /mdvg/CVSRepository/agni/agni/applications/monitor/monitor.etcl,v $ #Contains: # Agent code for the monitor #See Also: monitor.tcl (the gui) # To do : # a resctl_script: to prevent move when there is some connected stuff # Idea: a move_ok proc, than the resctl_on_stream_relocation if {[stream_exists monitor] == 0} { stream_create monitor } stream_open monitor if {[list_agents monitor] != ""} { puts stdout "agent monitor already there !" return } register_agent monitor {} { global monitorGui proc connectToMonitorGui {pMonitorId} { global monitorGui set lIdx [string first ":" $pMonitorId] set lAddr [string range $pMonitorId 0 [expr $lIdx - 1]] set lPort [string range $pMonitorId [expr $lIdx + 1] end] set lIdx [string first ":" $lPort] set lType [string range $lPort [expr $lIdx + 1] end] set lPort [string range $lPort 0 [expr $lIdx - 1]] #WARNING: if a monitor is registering itself it will lost the former connection... #WARNING: currently a same ip adr:port could register several time with a different type catch { close $monitorGui($pMonitorId) } catch { unset monitorGui($pMonitorId) } ## no time out in tcl ... if [catch { set lSock [socket $lAddr $lPort] } err ] { puts stderr "agent-monitor: failed to connect to $lAddr $lPort, $err" #### ???????? puts stderr $errorInfo catch {close $lSock} return 0 } else { set monitorGui($pMonitorId) $lSock return 1 } } # the pUnset business is for a future movable agent monitor .... proc disconnectMonitorGui {pMonitorId {pUnset 1}} { global monitorGui catch { close $monitorGui($pMonitorId) } if $pUnset { catch { unset monitorGui($pMonitorId) } catch { unset isAGwpGui($pMonitorId) } } else { set monitorGui($pMonitorId) "-" } return } # send one command to one (registred) regular monitor GUI. proc send_gui_command { cmdname arg {pType mon} } { global monitorGui if { $arg != "" } { set lStuffToSend "$cmdname $arg" } else { set lStuffToSend $cmdname } #?? Use array startsearch stuff ?? foreach mId [array names monitorGui "\*:$pType" ] { send_to_one_gui_command $mId $lStuffToSend } } proc send_to_one_gui_command { monitorid pStuffToSend } { global monitorGui set monitor_socket $monitorGui($monitorid) if [catch { puts $monitor_socket $pStuffToSend ; flush $monitor_socket }] { set lIdx [string first ":" $monitorId ] if [connectToMonitorGui $monitorid monitorGui] { if [catch { puts $monitor_socket $pStuffToSend ; flush $monitor_socket }] { disconnectMonitorGui $monitorid monitorGui puts stderr "agent-monitor: lost monitor $monitorid" return -1 } } else { return -1 } } puts $monitor_socket "ENDCOMMAND" flush $monitor_socket return 0 } on_stream_append { set lArgc [llength $argv] if {$lArgc == 0} { puts stderr "agent-momitor: empty message!" return } switch [lindex $argv 0] { "REGISTER_MONITORGUI" { if {$lArgc != 3} { puts stderr "agent-monitor: Usage REGISTER_MONITORGUI addr port [-gwp]" return -1 } set lType mon set hostname [lindex $argv 1] set port [lindex $argv 2] connectToMonitorGui "$hostname:$port:$lType" catch {unset lType; unset hostname; unset port } return 0 } "DEREGISTER_MONITORGUI" { if {$lArgc != 3} { puts stderr "agent-monitor: DEREGISTER_MONITORGUI addr port: error protocol" return -1 } set hostname [lindex $argv 1] set port [lindex $argv 2] disconnectMonitorGui "$hostname:$port:lType" catch {unset lType; unset hostname; unset port } return 0 } "ARRIVED" { send_gui_command "MOVED" [lrange $argv 1 end] return 0 } "CREATED" { send_gui_command "CREATED" [lrange $argv 1 end] return 0 } "DESTROYED" { send_gui_command "DESTROYED" [lrange $argv 1 end] return 0 } "CREATEDLOCATION" { send_gui_command "CREATEDLOCATION" [lrange $argv 1 end] return 0 } "DESTROYEDLOCATION" { send_gui_command "DESTROYEDLOCATION" [lrange $argv 1 end] return 0 } "DISPLAYERROR" { set lName [get_sender_name] if {$lName == ""} { set lName "****OUTSIDE****" } send_gui_command "DISPLAYERROR" [format "{%s : %s}" \ $lName \ [lrange $argv 1 end]] return 0 } "DISPLAYMESSAGE" { set lName [get_sender_name] if {$lName == ""} { set lName "****OUTSIDE****" } send_gui_command "DISPLAYMESSAGE" [format "{%s : %s}" \ $lName \ [lrange $argv 1 end]] return 0 } } } }