InternalPipeStream.st
author Claus Gittinger <cg@exept.de>
Mon, 11 Feb 2002 10:57:34 +0100
changeset 1016 47e190c35919
parent 1015 b03679cb8a3f
child 3128 b4d3fba8e356
permissions -rw-r--r--
*** empty log message ***

"{ Package: 'stx:libbasic2' }"

Stream subclass:#InternalPipeStream
	instanceVariableNames:'queue'
	classVariableNames:''
	poolDictionaries:''
	category:'Streams'
!

!InternalPipeStream class methodsFor:'documentation'!

documentation
"
    not useful on its own, but can be used to talk to a vt100
    terminal view ...
    See example.
"
!

examples
"
                                                                [exBegin]
    |p|

    p := InternalPipeStream new.
    [
        10 timesRepeat:[
            p nextPutLine:'hello'
        ].
    ] fork.

    [
        10 timesRepeat:[
            Transcript showCR:p nextLine
        ].
    ] fork.
                                                                [exEnd]

                                                                [exBegin]
    |userInput elizasOutput|

    userInput    := InternalPipeStream new.
    elizasOutput := InternalPipeStream new.

    top := StandardSystemView new.
    terminal := VT100TerminalView openOnInput: userInput output:elizasOutput in:top.

    top extent:(terminal preferredExtent).
    top label:'The doctor is in'.
    top iconLabel:'doctor'.
    top open.
    top waitUntilVisible.

    terminal translateNLToCRNL:true.
    terminal inputTranslateCRToNL:true.
    terminal localEcho:true.

    elizasOutput nextPutLine:'Hi, I am Eliza'.
    elizasOutput nextPutLine:'What is your problem ?'.
    elizasOutput nextPutLine:''.
    elizasOutput nextPutAll:'>'.

    [top realized] whileTrue:[
        |line answer matchingRule|

        line := userInput nextLine.
        (#('quit' 'exit' 'end' 'bye') includes:line) ifTrue:[
            top destroy.
            ^ self
        ].

        answer := 'Tell me more.'
        elizasOutput nextPutLine:answer.
        elizasOutput nextPutAll:'>'.
    ].
                                                                [exEnd]
"
! !

!InternalPipeStream class methodsFor:'instance creation'!

new
    ^ self basicNew initialize
! !

!InternalPipeStream methodsFor:'accessing'!

atEnd
    ^ false . "/ queue notNil
!

close
    queue := nil
!

next
    "return the next element from the stream (might block until something is written)"

    ^ queue next
!

nextAvailableBytes:nMax into:aBuffer startingAt:startIndex
    |n idx ch|

    n := 0.
    idx := startIndex.
    [n <= nMax] whileTrue:[
        ch := queue nextIfEmpty:[^ n ].
        aBuffer at:idx put:ch.
        idx := idx + 1.
        n := n + 1
    ].
    ^ n
!

nextPut:something
    "write an element (might wakeup readers)"

    queue nextPut:something
! !

!InternalPipeStream methodsFor:'initialization'!

initialize
    queue := SharedQueue new.
! !

!InternalPipeStream methodsFor:'synchronization'!

readWait
    queue readSemaphore wait
! !

!InternalPipeStream class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/InternalPipeStream.st,v 1.2 2002-02-11 09:57:34 cg Exp $'
! !