Posted to tcl by Napier at Sat Feb 28 03:33:42 GMT 2015view raw
- namespace eval Net {
- # set chan [startping]; after 10000 {set i 0}; after 9000 [list endping $chan]; vwait i
- namespace eval Ping {
- variable liveHosts [dict create]
- proc Start {host args} {
- set chan [open [list | ping $host]]
- chan configure $chan -blocking 0 -buffering line
- chan event $chan readable [list ::Net::Ping::Receive $chan]
- dict set ::Net::Ping::liveHosts "$host" [dict create chan $chan timestamp [clock seconds] command "$args"]
- # Test Output
- puts $::Net::Ping::liveHosts
- #
- return $chan
- }
- proc Receive {chan} {
- set ln [gets $chan]
- if {$ln eq ""} {
- if {[eof $chan]} {
- puts "Ping Completed"
- close $chan
- return
- }
- }
- puts "$ln"
- }
- proc Finish {host} {
- if {[dict exists $::Net::Ping::liveHosts $host]} {
- set chan [dict get $Net::Ping::liveHosts $host chan]
- } else {puts "Host Not Being Pinged"; return 0}
- exec kill -INT [pid $chan]
- set pstats [lrange [split [read -nonewline $chan] \n] end-1 end]
- if {[dict get $::Net::Ping::liveHosts $host command] != ""} {
- if {[catch {{*}[dict get $::Net::Ping::liveHosts $host command]} err]} {
- puts "Error During Callback"
- puts "Error: $err"
- }
- }
- dict unset ::Net::Ping::liveHosts $host
- return $pstats
- }
- }
- }
- ::Net::Ping::Start www.google.com puts "Finished Ping"; after 10000 {puts [::Net::Ping::Finish www.google.com]}; vwait i