Posted to tcl by Napier at Sat Feb 28 03:33:42 GMT 2015view raw

  1. namespace eval Net {
  2. # set chan [startping]; after 10000 {set i 0}; after 9000 [list endping $chan]; vwait i
  3. namespace eval Ping {
  4. variable liveHosts [dict create]
  5.  
  6. proc Start {host args} {
  7. set chan [open [list | ping $host]]
  8. chan configure $chan -blocking 0 -buffering line
  9. chan event $chan readable [list ::Net::Ping::Receive $chan]
  10. dict set ::Net::Ping::liveHosts "$host" [dict create chan $chan timestamp [clock seconds] command "$args"]
  11. # Test Output
  12. puts $::Net::Ping::liveHosts
  13. #
  14. return $chan
  15. }
  16.  
  17. proc Receive {chan} {
  18. set ln [gets $chan]
  19. if {$ln eq ""} {
  20. if {[eof $chan]} {
  21. puts "Ping Completed"
  22. close $chan
  23. return
  24. }
  25. }
  26. puts "$ln"
  27. }
  28.  
  29. proc Finish {host} {
  30. if {[dict exists $::Net::Ping::liveHosts $host]} {
  31. set chan [dict get $Net::Ping::liveHosts $host chan]
  32. } else {puts "Host Not Being Pinged"; return 0}
  33. exec kill -INT [pid $chan]
  34. set pstats [lrange [split [read -nonewline $chan] \n] end-1 end]
  35. if {[dict get $::Net::Ping::liveHosts $host command] != ""} {
  36. if {[catch {{*}[dict get $::Net::Ping::liveHosts $host command]} err]} {
  37. puts "Error During Callback"
  38. puts "Error: $err"
  39. }
  40. }
  41. dict unset ::Net::Ping::liveHosts $host
  42. return $pstats
  43. }
  44. }
  45. }
  46. ::Net::Ping::Start www.google.com puts "Finished Ping"; after 10000 {puts [::Net::Ping::Finish www.google.com]}; vwait i