InternalPipeStream.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Jun 2016 18:09:58 +0200
changeset 3934 3fc6968232c1
parent 3291 facd0cfd0781
child 3978 e4c47408edb2
permissions -rw-r--r--
class: exept_expecco_plugin class definition

"{ 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 top terminal|

    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
!

size
    ^ queue size
! !

!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.4 2014-06-02 22:16:14 vrany Exp $'
! !