Posted to tcl by crshults at Mon Jun 15 18:46:57 GMT 2015view raw

  1. set port 9000
  2. set sock ""
  3.  
  4. proc read_data {sock} {
  5. puts [read $sock]
  6. if {[chan eof $sock]} {
  7. puts "chan is eof"
  8. chan event $sock readable {}
  9. chan close $sock
  10. }
  11. }
  12.  
  13. proc uniform {str} {
  14. global port
  15. set ::sock [socket localhost $port]
  16. chan configure $::sock -translation binary -buffering none -blocking no
  17. set msg "RCTL\nBEGIN\n${str}\nEND"
  18. chan event $::sock readable "read_data $::sock"
  19. chan puts $::sock $msg
  20. }
  21.  
  22. uniform "some task"
  23.  
  24. set dead no
  25.  
  26. proc heartbeat {} {
  27. if {[catch {puts $::sock .}]} {
  28. set ::dead yes
  29. }
  30. after 1000 heartbeat
  31. }
  32.  
  33. proc check_for_death {args} {
  34. if {$::dead eq "yes"} {
  35. puts "yeah,... we're dead."
  36. }
  37. }
  38.  
  39. trace add variable dead write check_for_death
  40.  
  41. heartbeat
  42.