-
Notifications
You must be signed in to change notification settings - Fork 39
/
TelnetConsole.java
269 lines (223 loc) · 8.49 KB
/
TelnetConsole.java
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
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen.cli.telnet;
import com.google.common.base.Preconditions;
import io.permazen.Permazen;
import io.permazen.cli.Console;
import io.permazen.core.Database;
import io.permazen.kv.KVDatabase;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import jline.TerminalFactory;
import org.dellroad.nvt4j.Terminal;
import org.dellroad.nvt4j.impl.TerminalImpl;
/**
* A {@link Console} connected to a telnet client via a {@link Socket}.
*
* <p>
* This class can be used to implement an embedded CLI console within an application, accessed via telnet. For example:
* <pre>
* // Accept a new connection and setup telnet CLI console
* try (final Socket socket = /* accept new TCP connection */;
* final TelnetConsole console = TelnetConsole.create(database, socket)) {
*
* // Install commands and functions
* console.getSession().loadFunctionsFromClasspath();
* console.getSession().loadCommandsFromClasspath();
*
* // Run the console
* console.run();
* }
* </pre>
*/
public final class TelnetConsole extends Console {
/**
* Internal constructor.
*/
private TelnetConsole(KVDatabase kvdb, Database db, Permazen jdb, InputStream input, OutputStream output,
jline.Terminal terminal, String encoding, String appName) throws IOException {
super(kvdb, db, jdb, input, output, terminal, encoding, appName);
}
/**
* Simplified factory method for {@link io.permazen.SessionMode#KEY_VALUE} mode.
*
* @param kvdb key/value {@link KVDatabase}
* @param socket socket connected to telnet client
* @return new telnet console instance
* @throws IOException if an I/O error occurs
*/
public static TelnetConsole create(KVDatabase kvdb, Socket socket) throws IOException {
return TelnetConsole.create(kvdb, null, null, socket.getInputStream(), socket.getOutputStream(), null, null);
}
/**
* Simplified factory method for {@link io.permazen.SessionMode#CORE_API} mode.
*
* @param db core API {@link Database}
* @param socket socket connected to telnet client
* @return new telnet console instance
* @throws IOException if an I/O error occurs
*/
public static TelnetConsole create(Database db, Socket socket) throws IOException {
return TelnetConsole.create(null, db, null, socket.getInputStream(), socket.getOutputStream(), null, null);
}
/**
* Simplified factory method for {@link io.permazen.SessionMode#PERMAZEN} mode.
*
* @param jdb {@link Permazen} database
* @param socket socket connected to telnet client
* @return new telnet console instance
* @throws IOException if an I/O error occurs
*/
public static TelnetConsole create(Permazen jdb, Socket socket) throws IOException {
return TelnetConsole.create(null, null, jdb, socket.getInputStream(), socket.getOutputStream(), null, null);
}
/**
* Generic factory method.
*
* @param kvdb {@link KVDatabase} for {@link io.permazen.SessionMode#KEY_VALUE} (otherwise must be null)
* @param db {@link Database} for {@link io.permazen.SessionMode#CORE_API} (otherwise must be null)
* @param jdb {@link Permazen} for {@link io.permazen.SessionMode#PERMAZEN} (otherwise must be null)
* @param input console input
* @param output console output
* @param encoding character encoding for {@code terminal}, or null for default
* @param appName JLine application name, or null for none
* @return new telnet console instance
* @throws IOException if an I/O error occurs
* @throws IllegalArgumentException if not exactly one of {@code kvdb}, {@code db} or {@code jdb} is not null
*/
public static TelnetConsole create(KVDatabase kvdb, Database db, Permazen jdb, InputStream input, OutputStream output,
String encoding, String appName) throws IOException {
// Set up nvt4j; ignore the initial clear & reposition
final Terminal nvt4jTerminal = new TerminalImpl(input, output) {
private boolean cleared;
private boolean moved;
@Override
public void clear() throws IOException {
if (this.cleared)
super.clear();
this.cleared = true;
}
@Override
public void move(int row, int col) throws IOException {
if (this.moved)
super.move(row, col);
this.moved = true;
}
};
nvt4jTerminal.put(TerminalImpl.AUTO_WRAP_ON);
nvt4jTerminal.setCursor(true);
// Have JLine do input & output through telnet terminal
final InputStream jlineInput = new InputStream() {
@Override
public int read() throws IOException {
return nvt4jTerminal.get();
}
};
final OutputStream jlineOutput = new OutputStream() {
@Override
public void write(int value) throws IOException {
nvt4jTerminal.put(value);
}
};
// Build console
return new TelnetConsole(kvdb, db, jdb, jlineInput, jlineOutput,
new TelnetTerminal(TerminalFactory.get(), nvt4jTerminal), encoding, appName);
}
// ForwardingTerminal
/**
* Wrapper class for a JLine {@link jline.Terminal} that forwards all methods to the wrapped delegate.
*/
public static class ForwardingTerminal implements jline.Terminal {
protected final jline.Terminal terminal;
public ForwardingTerminal(jline.Terminal terminal) {
Preconditions.checkArgument(terminal != null, "null terminal");
this.terminal = terminal;
}
@Override
public void init() throws Exception {
this.terminal.init();
}
@Override
public void restore() throws Exception {
this.terminal.restore();
}
@Override
public void reset() throws Exception {
this.terminal.reset();
}
@Override
public boolean isSupported() {
return this.terminal.isSupported();
}
@Override
public int getWidth() {
return this.terminal.getWidth();
}
@Override
public int getHeight() {
return this.terminal.getHeight();
}
@Override
public boolean isAnsiSupported() {
return this.terminal.isAnsiSupported();
}
@Override
public OutputStream wrapOutIfNeeded(OutputStream out) {
return this.terminal.wrapOutIfNeeded(out);
}
@Override
public InputStream wrapInIfNeeded(InputStream in) throws IOException {
return this.terminal.wrapInIfNeeded(in);
}
@Override
public boolean hasWeirdWrap() {
return this.terminal.hasWeirdWrap();
}
@Override
public boolean isEchoEnabled() {
return this.terminal.isEchoEnabled();
}
@Override
public void setEchoEnabled(boolean enabled) {
this.terminal.setEchoEnabled(enabled);
}
@Override
public String getOutputEncoding() {
return this.terminal.getOutputEncoding();
}
@Override
public void enableInterruptCharacter() {
this.terminal.enableInterruptCharacter();
}
@Override
public void disableInterruptCharacter() {
this.terminal.disableInterruptCharacter();
}
}
// TelnetTerminal
/**
* JLine {@link jline.Terminal} that gets window size information from an associated nvt4j {@link Terminal}.
*/
public static class TelnetTerminal extends ForwardingTerminal {
private final Terminal nvt;
public TelnetTerminal(jline.Terminal terminal, Terminal nvt) {
super(terminal);
Preconditions.checkArgument(nvt != null, "null nvt");
this.nvt = nvt;
}
@Override
public int getWidth() {
final int width = this.nvt.getColumns();
return width > 0 ? width : super.getWidth();
}
@Override
public int getHeight() {
final int height = this.nvt.getRows();
return height > 0 ? height : super.getHeight();
}
// TODO: echoEnabled <-> EchoOptionHandler, possibly others
}
}