eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'CollectingSharedQueueStream':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: CollectingSharedQueueStream


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--CollectingSharedQueueStream

Package:
stx:libbasic2
Category:
Streams
Version:
rev: 1.5 date: 2017/11/28 18:23:55
user: cg
file: CollectingSharedQueueStream.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger

Description:


This class provides a buffering mechanism between a reader and a writer
process (i.e. it is much like a sharedQueue), but remembers the data as
written by the writer internally, providing indexed access to elements.

The reader side may read from it using #next, and possibly access
elements via #at:.
Reading/accessing may start immediately, but will block until enough elements
have been added by another process, the writer.

Instances of this class may be useful to start processing on
big document/data collection immediately, while the data is still being
read by another thread; 
A concrete application is the HTMLDocumentReader, which is being changed to
start processing and displaying the document while the rest is still being read.


Related information:

    Stream
    OrderedCollection
    SharedQueue

Class protocol:

instance creation
o  new
(comment from inherited method)
return an instance of myself without indexed variables


Instance protocol:

accessing
o  at: index
synchronized read - possibly wait for elements up to index
being added (by someone else); then return it.

o  next
return the next value in the queue; if there is none,
wait 'til something is put into the receiver.

o  nextPut: anObject
append anObject to the queue; if anyone is waiting, tell him

accessing-special
o  close
signal the end of input; to be used by the writer

o  finalSize: aNumber
can be used by the writer, if the final size is known in
advance.

o  signalChanges: aBoolean
controls if I should send out size-changeMessages when new elements arrive

dummy converting
o  asStringCollection
(comment from inherited method)
return a new string collection containing the elements;
these ought to be strings. (i.e. String or Text instances)

initialization
o  initialize
(comment from inherited method)
just to ignore initialize to objects which do not need it

queries
o  atEnd

o  currentSize

o  size
must wait until closed


Examples:


two processes synchronized much like with a sharedQueue:
  |s reader|

  s := CollectingSharedQueueStream new.
  reader := [
              [s atEnd] whileFalse:[
                  Transcript showCR:s next
              ].
            ] fork.

  1 to:10 do:[:i |
      Delay waitForSeconds:1.
      s nextPut:i
  ].
the writer reads from a (slow) pipe; the reader sends it to the transcript.
  |pipe s reader|

  s := CollectingSharedQueueStream new.
  reader := [
              [s atEnd] whileFalse:[
                  Transcript showCR:s next
              ].
            ] forkAt:9.

  pipe := PipeStream readingFrom:'ls -lR /usr'.
  pipe notNil ifTrue:[
      [pipe atEnd] whileFalse:[
          pipe readWait.
          s nextPut:(pipe nextLine).
      ].
      pipe close.
  ].
  s close
the writer reads from a (slow) pipe; the collection is used in a TextView, which will block whenever lines are to be displayed, which have not yet been read:
  |view pipe buffer reader|

  buffer := CollectingSharedQueueStream new.
  buffer finalSize:100.

  [
      pipe := PipeStream readingFrom:'ls -lR /usr'.
      pipe notNil ifTrue:[
          [pipe atEnd] whileFalse:[
              pipe readWait.
              buffer nextPut:(pipe nextLine).
          ].
          pipe close.
      ].
      buffer changed:#size.
      buffer close.
  ] fork.

  view := ScrollableView for:TextView.
  view model:buffer; listMessage:#value; aspectMessage:#value.
  view open.


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 26 Apr 2024 16:04:12 GMT