author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 12:30:05 +0200 | |
changeset 3886 | b4fe47975cce |
parent 3291 | facd0cfd0781 |
child 3978 | e4c47408edb2 |
permissions | -rw-r--r-- |
1015 | 1 |
"{ Package: 'stx:libbasic2' }" |
2 |
||
3 |
Stream subclass:#InternalPipeStream |
|
4 |
instanceVariableNames:'queue' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
7 |
category:'Streams' |
|
8 |
! |
|
9 |
||
1016 | 10 |
!InternalPipeStream class methodsFor:'documentation'! |
11 |
||
12 |
documentation |
|
13 |
" |
|
14 |
not useful on its own, but can be used to talk to a vt100 |
|
15 |
terminal view ... |
|
16 |
See example. |
|
17 |
" |
|
18 |
! |
|
19 |
||
20 |
examples |
|
21 |
" |
|
22 |
[exBegin] |
|
23 |
|p| |
|
24 |
||
25 |
p := InternalPipeStream new. |
|
26 |
[ |
|
27 |
10 timesRepeat:[ |
|
28 |
p nextPutLine:'hello' |
|
29 |
]. |
|
30 |
] fork. |
|
31 |
||
32 |
[ |
|
33 |
10 timesRepeat:[ |
|
34 |
Transcript showCR:p nextLine |
|
35 |
]. |
|
36 |
] fork. |
|
37 |
[exEnd] |
|
38 |
||
39 |
[exBegin] |
|
3291
facd0cfd0781
Fixed examples (added missing vars, dot at the end of statement)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3128
diff
changeset
|
40 |
|userInput elizasOutput top terminal| |
1016 | 41 |
|
42 |
userInput := InternalPipeStream new. |
|
43 |
elizasOutput := InternalPipeStream new. |
|
44 |
||
45 |
top := StandardSystemView new. |
|
46 |
terminal := VT100TerminalView openOnInput: userInput output:elizasOutput in:top. |
|
47 |
||
48 |
top extent:(terminal preferredExtent). |
|
49 |
top label:'The doctor is in'. |
|
50 |
top iconLabel:'doctor'. |
|
51 |
top open. |
|
52 |
top waitUntilVisible. |
|
53 |
||
54 |
terminal translateNLToCRNL:true. |
|
55 |
terminal inputTranslateCRToNL:true. |
|
56 |
terminal localEcho:true. |
|
57 |
||
58 |
elizasOutput nextPutLine:'Hi, I am Eliza'. |
|
59 |
elizasOutput nextPutLine:'What is your problem ?'. |
|
60 |
elizasOutput nextPutLine:''. |
|
61 |
elizasOutput nextPutAll:'>'. |
|
62 |
||
63 |
[top realized] whileTrue:[ |
|
64 |
|line answer matchingRule| |
|
65 |
||
66 |
line := userInput nextLine. |
|
67 |
(#('quit' 'exit' 'end' 'bye') includes:line) ifTrue:[ |
|
68 |
top destroy. |
|
69 |
^ self |
|
70 |
]. |
|
71 |
||
3291
facd0cfd0781
Fixed examples (added missing vars, dot at the end of statement)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3128
diff
changeset
|
72 |
answer := 'Tell me more.'. |
1016 | 73 |
elizasOutput nextPutLine:answer. |
74 |
elizasOutput nextPutAll:'>'. |
|
75 |
]. |
|
76 |
[exEnd] |
|
77 |
" |
|
78 |
! ! |
|
1015 | 79 |
|
80 |
!InternalPipeStream class methodsFor:'instance creation'! |
|
81 |
||
82 |
new |
|
83 |
^ self basicNew initialize |
|
84 |
! ! |
|
85 |
||
86 |
!InternalPipeStream methodsFor:'accessing'! |
|
87 |
||
88 |
atEnd |
|
89 |
^ false . "/ queue notNil |
|
90 |
! |
|
91 |
||
92 |
close |
|
93 |
queue := nil |
|
94 |
! |
|
95 |
||
96 |
next |
|
97 |
"return the next element from the stream (might block until something is written)" |
|
98 |
||
99 |
^ queue next |
|
100 |
! |
|
101 |
||
102 |
nextAvailableBytes:nMax into:aBuffer startingAt:startIndex |
|
103 |
|n idx ch| |
|
104 |
||
105 |
n := 0. |
|
106 |
idx := startIndex. |
|
107 |
[n <= nMax] whileTrue:[ |
|
108 |
ch := queue nextIfEmpty:[^ n ]. |
|
109 |
aBuffer at:idx put:ch. |
|
110 |
idx := idx + 1. |
|
111 |
n := n + 1 |
|
112 |
]. |
|
113 |
^ n |
|
114 |
! |
|
115 |
||
116 |
nextPut:something |
|
117 |
"write an element (might wakeup readers)" |
|
118 |
||
119 |
queue nextPut:something |
|
3128 | 120 |
! |
121 |
||
122 |
size |
|
123 |
^ queue size |
|
1015 | 124 |
! ! |
125 |
||
126 |
!InternalPipeStream methodsFor:'initialization'! |
|
127 |
||
128 |
initialize |
|
129 |
queue := SharedQueue new. |
|
130 |
! ! |
|
131 |
||
132 |
!InternalPipeStream methodsFor:'synchronization'! |
|
133 |
||
134 |
readWait |
|
135 |
queue readSemaphore wait |
|
136 |
! ! |
|
137 |
||
138 |
!InternalPipeStream class methodsFor:'documentation'! |
|
139 |
||
140 |
version |
|
3291
facd0cfd0781
Fixed examples (added missing vars, dot at the end of statement)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
3128
diff
changeset
|
141 |
^ '$Header: /cvs/stx/stx/libbasic2/InternalPipeStream.st,v 1.4 2014-06-02 22:16:14 vrany Exp $' |
1015 | 142 |
! ! |
3128 | 143 |