-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathVt420Client.java
More file actions
393 lines (356 loc) · 14.9 KB
/
Copy pathVt420Client.java
File metadata and controls
393 lines (356 loc) · 14.9 KB
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.blazemeter.jmeter.rte.protocols.vt420;
import com.blazemeter.jmeter.rte.core.AttentionKey;
import com.blazemeter.jmeter.rte.core.BaseProtocolClient;
import com.blazemeter.jmeter.rte.core.CharacterBasedProtocolClient;
import com.blazemeter.jmeter.rte.core.Input;
import com.blazemeter.jmeter.rte.core.NavigationInput;
import com.blazemeter.jmeter.rte.core.Position;
import com.blazemeter.jmeter.rte.core.Screen;
import com.blazemeter.jmeter.rte.core.TerminalType;
import com.blazemeter.jmeter.rte.core.exceptions.RteIOException;
import com.blazemeter.jmeter.rte.core.listener.ExceptionHandler;
import com.blazemeter.jmeter.rte.core.listener.TerminalStateListener;
import com.blazemeter.jmeter.rte.core.ssl.SSLType;
import com.blazemeter.jmeter.rte.core.wait.ConditionWaiter;
import com.blazemeter.jmeter.rte.core.wait.ConnectionEndWaiter;
import com.blazemeter.jmeter.rte.core.wait.CursorWaitCondition;
import com.blazemeter.jmeter.rte.core.wait.DisconnectWaitCondition;
import com.blazemeter.jmeter.rte.core.wait.SilentWaitCondition;
import com.blazemeter.jmeter.rte.core.wait.SyncWaitCondition;
import com.blazemeter.jmeter.rte.core.wait.TextWaitCondition;
import com.blazemeter.jmeter.rte.core.wait.WaitCondition;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.DisconnectListener;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.ScreenTextListener;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.SilenceListener;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.UnlockListener;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.VisibleCursorListener;
import com.blazemeter.jmeter.rte.protocols.vt420.listeners.Vt420TerminalStateListenerProxy;
import com.blazemeter.jmeter.rte.sampler.NavigationType;
import com.blazemeter.jmeter.rte.sampler.RTESampler;
import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import nl.lxtreme.jvt220.terminal.ConnectionException;
import nl.lxtreme.jvt220.terminal.ConnectionListener;
import nl.lxtreme.jvt220.terminal.ScreenChangeListener;
import nl.lxtreme.jvt220.terminal.TerminalClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Vt420Client extends BaseProtocolClient implements CharacterBasedProtocolClient {
public static final Map<NavigationType, String> NAVIGATION_KEYS = buildNavigationKeysMapping();
public static final Map<AttentionKey, String> ATTENTION_KEYS = buildAttKeysMapping();
private static final Logger LOG = LoggerFactory.getLogger(Vt420Client.class);
private TerminalClient client;
private Map<TerminalStateListener, Vt420TerminalStateListenerProxy> listeners =
new ConcurrentHashMap<>();
private static EnumMap<NavigationType, String> buildNavigationKeysMapping() {
return new EnumMap<NavigationType, String>(NavigationType.class) {
{
put(NavigationType.TAB, "\t");
put(NavigationType.UP, "\033[A");
put(NavigationType.DOWN, "\033[B");
put(NavigationType.LEFT, "\033[D");
put(NavigationType.RIGHT, "\033[C");
}
};
}
private static EnumMap<AttentionKey, String> buildAttKeysMapping() {
return new EnumMap<AttentionKey, String>(AttentionKey.class) {
{
put(AttentionKey.ENTER, "\r");
put(AttentionKey.F1, "\033[11~");
put(AttentionKey.F2, "\033[12~");
put(AttentionKey.F3, "\033[13~");
put(AttentionKey.F4, "\033[14~");
put(AttentionKey.F5, "\033[15~");
put(AttentionKey.F6, "\033[17~");
put(AttentionKey.F7, "\033[18~");
put(AttentionKey.F8, "\033[19~");
put(AttentionKey.F9, "\033[20~");
put(AttentionKey.F10, "\033[21~");
put(AttentionKey.F11, "\033[23~");
put(AttentionKey.F12, "\033[24~");
put(AttentionKey.F13, "\033[25~");
put(AttentionKey.F14, "\033[26~");
put(AttentionKey.F15, "\033[28~");
put(AttentionKey.F16, "\033[29~");
put(AttentionKey.F17, "\033[31~");
put(AttentionKey.F18, "\033[32~");
put(AttentionKey.F19, "\033[33~");
put(AttentionKey.F20, "\033[34~");
put(AttentionKey.F21, "\033[35~");
put(AttentionKey.F22, "\033[36~");
put(AttentionKey.F23, "\033[37~");
put(AttentionKey.F24, "\033[38~");
put(AttentionKey.ROLL_UP, "\033[5~");
put(AttentionKey.ROLL_DN, "\033[6~");
}
};
}
@Override
protected void setField(Input input, long echoTimeoutMillis) {
if (input instanceof NavigationInput) {
NavigationInput navigationInput = (NavigationInput) input;
NavigationType navigationType = navigationInput.getNavigationType();
if (NAVIGATION_KEYS.get(navigationType) != null) {
processArrowKey(echoTimeoutMillis, navigationInput);
} else {
LOG.error("Navigation type {} not supported", navigationType);
throw new IllegalArgumentException("Navigation type not supported");
}
} else {
LOG.error("{} not supported for VT420 protocol. Use Navigation inputs instead",
input.getClass().getSimpleName());
throw new IllegalArgumentException("Not supported input: " + input.getClass());
}
}
private List<String> textToList(String text) {
return text.chars().mapToObj(c -> ((char) c)).collect(Collectors.toList()).stream()
.map(String::valueOf)
.collect(Collectors.toList());
}
private void sendCharacterByOneAtATime(List<String> text, long timeout) {
Semaphore semaphore = new Semaphore(0);
ScreenChangeListener listener = s -> semaphore.release();
client.addScreenChangeListener(listener);
try {
for (String character : text) {
client.sendTextByCurrentCursorPosition(character);
if (!semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
exceptionHandler.setPendingError(
new TimeoutException(
"No server response after waiting '" + timeout + "' milliseconds when sending "
+ "'" + character + "' character of '" + text + "'."));
LOG.warn("If you consider that the character timeout is too low "
+ "you can change the value by adding the line `RTEConnectionConfig"
+ ".characterTimeoutMillis=<time_in_millis>` in the jmeter.properties file.");
}
}
} catch (IOException e) {
exceptionHandler.setPendingError(e);
} catch (InterruptedException ex) {
LOG.debug("Send of '{}' has been interrupted", text, ex);
exceptionHandler.setPendingError(ex);
Thread.currentThread().interrupt();
}
client.removeScreenChangeListener(listener);
}
private void processArrowKey(long echoTimeoutMillis, NavigationInput navigationInput) {
List<String> input = new ArrayList<>();
IntStream.range(0, navigationInput.getRepeat())
.forEach(e -> input.add(NAVIGATION_KEYS.get(navigationInput.getNavigationType())));
input.addAll(textToList(navigationInput.getInput()));
send(input, echoTimeoutMillis);
}
protected void sendAttentionKey(AttentionKey attentionKey) {
String input = ATTENTION_KEYS.get(attentionKey);
if (input == null) {
throw new UnsupportedOperationException(
attentionKey.name() + " attentionKey is unsupported for protocol VT420.");
}
try {
client.sendTextByCurrentCursorPosition(input);
} catch (IOException e) {
exceptionHandler.setPendingError(e);
}
}
@Override
protected ConditionWaiter<?> buildWaiter(WaitCondition waitCondition) {
if (waitCondition instanceof SyncWaitCondition) {
return new UnlockListener((SyncWaitCondition) waitCondition, this, stableTimeoutExecutor,
exceptionHandler);
} else if (waitCondition instanceof TextWaitCondition) {
return new ScreenTextListener((TextWaitCondition) waitCondition, this,
stableTimeoutExecutor,
exceptionHandler);
} else if (waitCondition instanceof SilentWaitCondition) {
return new SilenceListener((SilentWaitCondition) waitCondition, this, stableTimeoutExecutor,
exceptionHandler);
} else if (waitCondition instanceof CursorWaitCondition) {
return new VisibleCursorListener((CursorWaitCondition) waitCondition, this,
stableTimeoutExecutor,
exceptionHandler);
} else if (waitCondition instanceof DisconnectWaitCondition) {
return new DisconnectListener((DisconnectWaitCondition) waitCondition, this,
stableTimeoutExecutor, exceptionHandler);
} else {
throw new UnsupportedOperationException("Wait condition not supported yet.");
}
}
@Override
protected void doDisconnect() {
try {
stableTimeoutExecutor.shutdownNow();
stableTimeoutExecutor = null;
client.disconnect();
} catch (IOException e) {
exceptionHandler.setPendingError(new RteIOException(e, ""));
}
}
@Override
public List<TerminalType> getSupportedTerminalTypes() {
return Collections.singletonList(
new TerminalType("VT420-7", new Dimension(80, 24)));
}
@Override
public void connect(String server, int port, SSLType sslType, TerminalType terminalType,
long timeoutMillis) throws RteIOException {
client = new TerminalClient(terminalType.getScreenSize(), terminalType.getId());
stableTimeoutExecutor = Executors.newSingleThreadScheduledExecutor(NAMED_THREAD_FACTORY);
exceptionHandler = new ExceptionHandler(server);
client.setSocketFactory(getSocketFactory(sslType, server));
ConnectionEndWaiter connectionEndWaiter = new ConnectionEndWaiter(timeoutMillis);
client.addConnectionListener(new ConnectionListener() {
@Override
public void onException(Throwable throwable) {
exceptionHandler.setPendingError(throwable);
}
@Override
public void onConnectionClosed() {
handleServerDisconnection();
}
@Override
public void onConnection() {
connectionEndWaiter.stop();
}
});
listeners.forEach((stateListener, listenerProxy) -> client
.addScreenChangeListener(listeners.get(stateListener)));
try {
client.connect(server, port, (int) timeoutMillis);
connectionEndWaiter.await();
} catch (ConnectionException e) {
LOG.error("Connection error: ", e);
throw new RteIOException(new Throwable("Connection error"), server);
} catch (InterruptedException e) {
exceptionHandler.setPendingError(e);
LOG.error("Connection to {} interrupted cause: ", server, e);
} catch (TimeoutException e) {
exceptionHandler.setPendingError(e);
LOG.error("Timeout connection exceeded ", e);
}
exceptionHandler.throwAnyPendingError();
}
@Override
public void connect(String server, int port, SSLType sslType, TerminalType terminalType,
long timeoutMillis, String devName)
throws RteIOException, InterruptedException, TimeoutException {
client = new TerminalClient(terminalType.getScreenSize(), terminalType.getId());
stableTimeoutExecutor = Executors.newSingleThreadScheduledExecutor(NAMED_THREAD_FACTORY);
exceptionHandler = new ExceptionHandler(server);
client.setSocketFactory(getSocketFactory(sslType, server));
ConnectionEndWaiter connectionEndWaiter = new ConnectionEndWaiter(timeoutMillis);
client.addConnectionListener(new ConnectionListener() {
@Override
public void onException(Throwable throwable) {
exceptionHandler.setPendingError(throwable);
}
@Override
public void onConnectionClosed() {
handleServerDisconnection();
}
@Override
public void onConnection() {
connectionEndWaiter.stop();
}
});
listeners.forEach((stateListener, listenerProxy) -> client
.addScreenChangeListener(listeners.get(stateListener)));
try {
client.connect(server, port, (int) timeoutMillis);
connectionEndWaiter.await();
} catch (ConnectionException e) {
LOG.error("Connection error: ", e);
throw new RteIOException(new Throwable("Connection error"), server);
} catch (InterruptedException e) {
exceptionHandler.setPendingError(e);
LOG.error("Connection to {} interrupted cause: ", server, e);
} catch (TimeoutException e) {
exceptionHandler.setPendingError(e);
LOG.error("Timeout connection exceeded ", e);
}
exceptionHandler.throwAnyPendingError();
}
@Override
public void addTerminalStateListener(TerminalStateListener terminalStateListener) {
Vt420TerminalStateListenerProxy listenerProxy = new Vt420TerminalStateListenerProxy(
terminalStateListener);
listeners.put(terminalStateListener, listenerProxy);
if (client != null) {
client.addScreenChangeListener(listenerProxy);
}
}
@Override
public void removeTerminalStateListener(TerminalStateListener terminalStateListener) {
Vt420TerminalStateListenerProxy listenerProxy = listeners.remove(terminalStateListener);
if (client != null) {
client.removeScreenChangeListener(listenerProxy);
}
}
@Override
public Screen getScreen() {
return Screen.buildScreenFromText(client.getScreen(), client.getScreenSize());
}
@Override
public Optional<Boolean> isInputInhibited() {
return Optional.empty();
}
@Override
public Optional<Position> getCursorPosition() {
return client.getCursorPosition().map(point -> new Position(point.y + 1, point.x + 1));
}
@Override
public boolean isAlarmOn() {
return client.isAlarmOn();
}
@Override
public boolean resetAlarm() {
return client.resetAlarm();
}
@Override
public Set<AttentionKey> getSupportedAttentionKeys() {
return ATTENTION_KEYS.keySet();
}
@Override
public void addScreenChangeListener(ScreenChangeListener listener) {
client.addScreenChangeListener(listener);
}
@Override
public void removeScreenChangeListener(ScreenChangeListener listener) {
client.removeScreenChangeListener(listener);
}
@Override
public void send(List<String> input, long echoTimeoutMillis) {
if (!RTESampler.isWaitResponseCharSent()) {
try {
client.sendTextByCurrentCursorPosition(String.join("", input));
} catch (IOException e) {
exceptionHandler.setPendingError(e);
}
return;
}
sendCharacterByOneAtATime(input, echoTimeoutMillis);
}
public void setExceptionHandler(ExceptionHandler exceptionHandler) {
this.exceptionHandler = exceptionHandler;
}
public void addConnectionListener(ConnectionListener connectionListener) {
client.addConnectionListener(connectionListener);
}
public void removeConnectionListener(ConnectionListener listener) {
client.removeConnectionListener(listener);
}
}