Tcl Source Code

Artifact [45eea1775a]
Login

Artifact 45eea1775a947b5e43ac23097761f90b86f08167:

Attachment "testRefchan.tcl" to ticket [2827000fff] added by coldstore 2009-07-25 20:52:33.
namespace eval rc {
    proc initialize {c mode} {
	set ::state 0
	puts "$c initialized"
	return [list initialize read watch blocking finalize]
    }

    proc blocking {c mode} {
	puts stderr "$c blocking $mode in state $::state"
	set x [chan postevent $c read]
	puts stderr "$c posts readable event"
    }

    proc read {c count} {
	puts stderr "$c tries to read $count in state $::state"
	switch -- [incr ::state] {
	    1 {
		set result [string repeat [string repeat 1 10]\n 10]\n[string repeat [string repeat 2 10]\n 10]
	    }
	    default {
		set result ""
	    }
	}
	puts stderr "read yields [string length $result] bytes in state $::state"
	return $result
    }

    proc finalize {c} {
	puts stderr "$c finalized in state: $::state"
    }

    namespace export -clear *
    namespace ensemble create -subcommands {}
}

proc getter {c} {
    if {[eof $c]} {
	chan event $c readable ""
	puts stderr "getter got eof in state $::state"
    }

    set len [gets $c line]
    puts stderr "getter got '$line' in state $::state"
    if {!$len} {
	puts stderr "getter switching to reader in state $::state"
	chan event $c readable [list reader $c]
    }
}

proc reader {c} {
    if {[eof $c]} {
	chan close $c
	puts stderr "reader got eof in state $::state and closed"
    }

    set buf [read $c 10000]
    puts stderr "reader got [string length $buf] bytes in state $::state - eof $c? is [eof $c] should be 0"
    incr ::state
}

set rc [chan create read rc]
chan event $rc readable [list getter $rc]
chan configure $rc -blocking 0

vwait forever