
# # ## ### ##### ######## ############# #####################

proc main {} {
    X %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    set s [socket 127.0.0.1 7777]
    set i [IChan new chan $s]

    if 1 {
	set rc [chan create {read} $i]

	chan configure $rc -blocking 0
	chan event     $rc readable [list getter $rc]
    } else {
	chan configure $s -blocking 0
	chan event     $s readable [list getter $s]
    }

    X %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    vwait forever
    exit
}

proc X {args} {
    puts stderr [join $args]
    return
}

source Chan.tcl

# # ## ### ##### ######## ############# #####################

proc getter {c} {
    Y getter. $c ____________

    if {[eof $c]} {
	chan event $c readable ""
	Y "        " $c got eof
	exit
    }

    set len [gets $c line]
    Y "        " $c got '$line'

    Y \t eof_____ $c is [eof $c] 
    Y \t fblocked $c is [fblocked $c] 

    if {!$len} {
	Y "       " $c switching to -> reader
	chan event $c readable [list reader $c]
    }

    Y getter. $c /done
    return
}

proc reader {c} {
    Y reader. $c ____________

    if {[eof $c]} {
	chan close $c
	Y "       " $c got eof, and closed
	exit
    }

    set buf [read $c 5];#10000]

    Y "       " $c got [string length $buf] bytes
    Y \t eof_____ $c is [eof $c] 
    Y \t fblocked $c is [fblocked $c] 
    foreach l [split $buf \n] { Y --- '$l' }
    Y reader. $c /done
}

proc Y {args} {
    X \t\t {*}$args
}

# # ## ### ##### ######## ############# #####################

# # ## ### ##### ######## ############# #####################

main

 	  	 
