1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
package bjc.utils.cli;
import static bjc.utils.cli.TerminalCodes.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.util.function.Consumer;
import java.util.function.Function;
import bjc.data.Either;
import bjc.utils.funcutils.StringUtils;
import bjc.utils.parserutils.TokenUtils;
/**
* Implementation of {@link Terminal} using {@link Reader} and {@link Writer}
*
* @author Ben Culkin
*
*/
public class StreamTerminal implements Terminal, Runnable {
private SortedSet<Long> pendingRequests;
private ConcurrentMap<Long, String> pendingReplies;
private Lock replyLock;
private Condition replyCondition;
private Queue<String> pendingOutput;
private boolean isRunning;
private boolean isParagraphMode;
private boolean isPromptOnContinue;
private Scanner input;
private Writer output;
private Writer status;
private String prefix;
private String paragraphTerminator = "";
private String prompt = "..> ";
private Consumer<String> mode;
private long currentRequest = -1;
/**
* Create a new stream terminal.
*
* @param input The input source
* @param output The output source
*/
public StreamTerminal(Reader input, Writer output) {
this(input, output, null, null);
}
/**
* Create a new stream terminal which backs an application
*
* @param input The input source
* @param output The output source
* @param prefix The string any input must start with to be directed to the
* terminal
* @param mode The place where all input not directed to the terminal is
* written.
*/
public StreamTerminal(Reader input, Writer output, String prefix, Consumer<String> mode) {
this.input = new Scanner(input);
this.output = output;
this.status = output;
this.pendingRequests = new TreeSet<>();
this.pendingReplies = new ConcurrentHashMap<>();
this.pendingOutput = new ArrayDeque<>();
this.replyLock = new ReentrantLock();
this.replyCondition = replyLock.newCondition();
this.prefix = prefix;
this.mode = mode;
}
@Override
public void run() {
isRunning = true;
try {
status.write(INFO_STARTCOMPROC.toString() + "\n");
status.flush();
writePendingOutput();
output.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
StringBuilder pendingInput = new StringBuilder();
overall: while (isRunning && input.hasNextLine()) {
try {
writePendingOutput();
output.flush();
String ln = input.nextLine();
if (prefix != null && ln.startsWith(prefix)) {
ln = ln.substring(prefix.length());
String com = "";
int spcIdx = ln.indexOf(' ');
if (spcIdx == -1) {
com = ln;
} else {
com = ln.substring(0, spcIdx);
ln = ln.substring(spcIdx + 1);
}
comswt: switch (com) {
case "c":
handleConfigure(ln);
break;
case "r": {
// General command format is 'r <request no.>,<reply>
String subRep = ln.substring(2);
// Process a reply
int comIndex = subRep.indexOf(',');
long repNo = 0;
if (comIndex == -1) {
// Reply to the oldest message by default
repNo = pendingRequests.first();
} else {
String repStr = subRep.substring(0, comIndex);
try {
repNo = Long.parseLong(repStr);
} catch (NumberFormatException nfex) {
status.write(ERROR_INVREPNO.toString() + "\n");
status.flush();
continue overall;
}
// Skip over the comma
subRep = subRep.substring(comIndex + 1);
}
if (!pendingRequests.contains(repNo)) {
status.write(ERROR_UNKREPNO.toString() + "\n");
status.flush();
continue overall;
}
pendingRequests.remove(repNo);
pendingReplies.put(repNo, subRep);
replyLock.lock();
replyCondition.signalAll();
replyLock.unlock();
break comswt;
}
case "q":
isRunning = false;
break comswt;
case "?":
case "h":
handleHelp(ln);
break;
default:
status.write(ERROR_UNRECCOM.toString() + "\n");
status.flush();
}
} else {
if (isParagraphMode) {
if (ln.equals(paragraphTerminator)) {
mode.accept(pendingInput.toString());
pendingInput = new StringBuilder();
writePendingOutput();
output.flush();
continue;
}
// Handle line-continuation
if (ln.endsWith("\\")) {
pendingInput.append(ln.substring(0, ln.length() - 1));
pendingInput.append(" ");
} else {
pendingInput.append(ln);
pendingInput.append("\n");
}
addOutput(prompt);
} else {
mode.accept(ln);
}
}
writePendingOutput();
output.flush();
} catch (IOException ioex) {
throw new RuntimeException(ioex);
}
}
isRunning = false;
try {
writePendingOutput();
output.flush();
status.write(INFO_ENDCOMPROC.toString() + "\n");
status.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void writePendingOutput() throws IOException {
while (!pendingOutput.isEmpty())
output.write(pendingOutput.remove());
}
private void handleHelp(String ln) {
addOutput("Help not yet fully implemented");
}
private void handleConfigure(String ln) {
int spaceIdx = ln.indexOf(" ");
String setting = ln.substring(0, spaceIdx);
String setArgs = ln.substring(spaceIdx).strip();
List<String> args = TokenUtils.processArguments(setArgs);
switch(setting) {
case "paraTerm":
paragraphTerminator = args.get(0);
break;
case "para":
isParagraphMode = !isParagraphMode;
break;
case "prompt?":
isPromptOnContinue = !isPromptOnContinue;
break;
case "prompt":
prompt = args.get(0);
break;
default:
addOutput(ERROR_UNKCONFSET.toString() + "\n");
}
}
public boolean isParagraphMode() {
return isParagraphMode;
}
public void setParagraphMode(boolean isParagraphMode) {
this.isParagraphMode = isParagraphMode;
}
public boolean isPromptOnContinue() {
return isPromptOnContinue;
}
public void setPromptOnContinue(boolean isPromptOnContinue) {
this.isPromptOnContinue = isPromptOnContinue;
}
public String getParagraphTerminator() {
return paragraphTerminator;
}
public void setParagraphTerminator(String paragraphTerminator) {
this.paragraphTerminator = paragraphTerminator;
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
}
@Override
public long submitRequest(String req) {
long reqNo = currentRequest + 1;
currentRequest += 1;
pendingOutput.add(reqNo + " " + req + "\n");
pendingRequests.add(reqNo);
return reqNo;
}
@Override
public String awaitReply(long id) throws InterruptedException {
if (pendingReplies.containsKey(id))
return pendingReplies.get(id);
while (true) {
replyLock.lock();
replyCondition.await();
replyLock.unlock();
// Explanation: Since the reply map is add-only, the lock isn't actually
// protecting anything. We just want to wait until a response is received.
if (pendingReplies.containsKey(id))
return pendingReplies.get(id);
}
}
@Override
public Optional<String> awaitReply(long id, TimeUnit unit, long delay) throws InterruptedException {
if (pendingReplies.containsKey(id))
return Optional.of(pendingReplies.get(id));
while (true) {
replyLock.lock();
boolean stat = replyCondition.await(delay, unit);
replyLock.unlock();
// If we timed out, say so
if (stat == false)
return Optional.empty();
// Explanation: Since the reply map is add-only, the lock isn't actually
// protecting anything. We just want to wait until a response is received.
if (pendingReplies.containsKey(id))
return Optional.of(pendingReplies.get(id));
}
}
@Override
public Optional<String> checkReply(long id) {
return Optional.ofNullable(pendingReplies.get(id));
}
@Override
public String submitRequestSync(String req) throws InterruptedException {
return awaitReply(submitRequest(req));
}
@Override
public Either<String, Long> submitRequestSync(String req, TimeUnit unit, long delay) throws InterruptedException {
long id = submitRequest(req);
Optional<String> rep = awaitReply(id, unit, delay);
return rep.isEmpty() ? Either.right(id) : Either.left(rep.get());
}
/**
* Add a string to be printed next time output is printed.
*
* @param out The output
*/
public void addOutput(String out) {
pendingOutput.add(out);
}
/**
* Set the mode for handling non-terminal input
*
* @param mode The new mode for handling non-terminal input
*/
public void setMode(Consumer<String> mode) {
this.mode = mode;
}
public Writer getStatus() {
return status;
}
public void setStatus(Writer status) {
this.status = status;
}
}
|