Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/transport/TN5250.java @ 48:1e931ef5f776 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 11 Jun 2014 10:11:29 -0700 |
parents | a3fd10a8c0de |
children | 8887bff45dee |
rev | line source |
---|---|
11 | 1 /* |
2 * 510ConnectBot | |
3 * Copyright 2014 Carl Byington | |
4 * | |
5 * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 * you may not use this file except in compliance with the License. | |
7 * You may obtain a copy of the License at | |
8 * | |
9 * http://www.apache.org/licenses/LICENSE-2.0 | |
10 * | |
11 * Unless required by applicable law or agreed to in writing, software | |
12 * distributed under the License is distributed on an "AS IS" BASIS, | |
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 * See the License for the specific language governing permissions and | |
15 * limitations under the License. | |
16 */ | |
17 | |
18 package com.five_ten_sg.connectbot.transport; | |
19 | |
20 import java.io.IOException; | |
21 import java.io.InputStream; | |
22 import java.io.OutputStream; | |
23 import java.net.Socket; | |
24 import java.net.SocketException; | |
13 | 25 import java.net.UnknownHostException; |
11 | 26 import java.util.List; |
27 import java.util.Map; | |
13 | 28 import java.util.regex.Matcher; |
29 import java.util.regex.Pattern; | |
11 | 30 |
12 | 31 import org.tn5250j.framework.tn5250.Screen5250; |
32 import org.tn5250j.framework.tn5250.tnvt; | |
33 | |
13 | 34 import com.five_ten_sg.connectbot.R; |
11 | 35 import com.five_ten_sg.connectbot.bean.HostBean; |
36 import com.five_ten_sg.connectbot.bean.PortForwardBean; | |
37 import com.five_ten_sg.connectbot.service.TerminalBridge; | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
38 import com.five_ten_sg.connectbot.service.TerminalKeyListener; |
11 | 39 import com.five_ten_sg.connectbot.service.TerminalManager; |
40 import com.five_ten_sg.connectbot.util.HostDatabase; | |
41 import android.content.Context; | |
42 import android.net.Uri; | |
13 | 43 import android.util.Log; |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
44 import de.mud.terminal.vt320; |
11 | 45 |
46 | |
47 /** | |
48 * @author Carl Byington | |
49 * | |
50 */ | |
51 public class TN5250 extends AbsTransport { | |
52 private static final String PROTOCOL = "tn5250"; | |
53 private static final String TAG = "ConnectBot.tn5250"; | |
54 private static final int DEFAULT_PORT = 23; | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
55 |
11 | 56 private Screen5250 screen52; |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
57 private tnvt handler = null; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
58 private Socket socket; |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
59 private boolean connected = false; |
11 | 60 |
12 | 61 static final Pattern hostmask; |
62 static { | |
63 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE); | |
64 } | |
65 | |
45
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
66 |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
67 class vt320x5250 extends vt320 { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
68 @Override |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
69 public void debug(String s) { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
70 Log.d(TAG, s); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
71 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
72 @Override |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
73 public void write(byte[] b) { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
74 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
75 @Override |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
76 public void write(int b) { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
77 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
78 // bridge.monitor placement of new characters |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
79 @Override |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
80 public void putChar(int c, int l, char ch, int attributes) { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
81 if (bridge.monitor != null) bridge.monitor.screenChanged(l, c); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
82 super.putChar(c, l, ch, attributes); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
83 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
84 @Override |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
85 public void setCursorPosition(int c, int l) { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
86 if (bridge.monitor != null) bridge.monitor.cursorMove(l, c); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
87 super.setCursorPosition(c, l); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
88 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
89 }; |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
90 |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
91 |
43
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
92 class Terminal5250KeyListener extends TerminalKeyListener { |
46
34ce32c4e807
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
45
diff
changeset
|
93 public Terminal5250KeyListener(TerminalManager manager, |
34ce32c4e807
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
45
diff
changeset
|
94 TerminalBridge bridge, |
34ce32c4e807
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
45
diff
changeset
|
95 vt320 buffer, |
34ce32c4e807
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
45
diff
changeset
|
96 String encoding) { |
45
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
97 super(manager, bridge, buffer, encoding); |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
98 } |
43
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
99 }; |
12 | 100 |
11 | 101 public TN5250() { |
102 super(); | |
103 } | |
104 | |
105 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
106 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
107 * @return protocol part of the URI |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
108 */ |
11 | 109 public static String getProtocolName() { |
110 return PROTOCOL; | |
111 } | |
112 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
113 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
114 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
115 * Encode the current transport into a URI that can be passed via intent calls. |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
116 * @return URI to host |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
117 */ |
12 | 118 public Uri getUri(String input) { |
119 Matcher matcher = hostmask.matcher(input); | |
120 | |
121 if (!matcher.matches()) | |
122 return null; | |
123 | |
124 StringBuilder sb = new StringBuilder(); | |
125 sb.append(PROTOCOL) | |
126 .append("://") | |
127 .append(matcher.group(1)); | |
128 String portString = matcher.group(3); | |
129 int port = DEFAULT_PORT; | |
130 | |
131 if (portString != null) { | |
132 try { | |
133 port = Integer.parseInt(portString); | |
134 | |
135 if (port < 1 || port > 65535) { | |
136 port = DEFAULT_PORT; | |
137 } | |
138 } | |
139 catch (NumberFormatException nfe) { | |
140 // Keep the default port | |
141 } | |
142 } | |
143 | |
144 if (port != DEFAULT_PORT) { | |
145 sb.append(':'); | |
146 sb.append(port); | |
147 } | |
148 | |
149 sb.append("/#") | |
150 .append(Uri.encode(input)); | |
151 Uri uri = Uri.parse(sb.toString()); | |
152 return uri; | |
11 | 153 } |
154 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
155 |
11 | 156 /** |
157 * Causes transport to connect to the target host. After connecting but before a | |
158 * session is started, must call back to {@link TerminalBridge#onConnected()}. | |
159 * After that call a session may be opened. | |
160 */ | |
161 @Override | |
162 public void connect() { | |
30
d738f6b876fe
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
29
diff
changeset
|
163 screen52 = new Screen5250(); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
164 handler = new tnvt(screen52, true, false, bridge, manager); |
37
0395ca628303
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
32
diff
changeset
|
165 handler.setSSLType("TLS"); |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
166 screen52.setVT(handler); |
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
167 screen52.setBuffer(buffer); |
23 | 168 connected = handler.connect(host.getHostname(), host.getPort()); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
169 if (connected) bridge.onConnected(); |
11 | 170 } |
171 | |
172 | |
173 /** | |
174 * Checks if read() will block. If there are no bytes remaining in | |
175 * the underlying transport, return true. | |
176 */ | |
177 @Override | |
178 public boolean willBlock() { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
179 // we don't use a relay thread between the transport and the vt320 buffer |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
180 return true; |
11 | 181 } |
182 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
183 |
11 | 184 /** |
185 * Reads from the transport. Transport must support reading into a byte array | |
186 * <code>buffer</code> at the start of <code>offset</code> and a maximum of | |
187 * <code>length</code> bytes. If the remote host disconnects, throw an | |
188 * {@link IOException}. | |
189 * @param buffer byte buffer to store read bytes into | |
190 * @param offset where to start writing in the buffer | |
191 * @param length maximum number of bytes to read | |
192 * @return number of bytes read | |
193 * @throws IOException when remote host disconnects | |
194 */ | |
195 public int read(byte[] buffer, int offset, int length) throws IOException { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
196 // we don't use a relay thread between the transport and the vt320 buffer |
11 | 197 return 0; |
198 } | |
199 | |
200 | |
201 /** | |
202 * Writes to the transport. If the host is not yet connected, simply return without | |
203 * doing anything. An {@link IOException} should be thrown if there is an error after | |
204 * connection. | |
205 * @param buffer bytes to write to transport | |
206 * @throws IOException when there is a problem writing after connection | |
207 */ | |
208 public void write(byte[] buffer) throws IOException { | |
209 } | |
210 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
211 |
11 | 212 /** |
213 * Writes to the transport. See {@link #write(byte[])} for behavior details. | |
214 * @param c character to write to the transport | |
215 * @throws IOException when there is a problem writing after connection | |
216 */ | |
217 public void write(int c) throws IOException { | |
218 } | |
219 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
220 |
11 | 221 /** |
222 * Flushes the write commands to the transport. | |
223 * @throws IOException when there is a problem writing after connection | |
224 */ | |
225 public void flush() throws IOException { | |
226 } | |
227 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
228 |
11 | 229 /** |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
230 * Closes the connection to the terminal. |
11 | 231 */ |
232 public void close() { | |
13 | 233 handler.disconnect(); |
11 | 234 connected = false; |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
235 bridge.dispatchDisconnect(false); |
11 | 236 } |
237 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
238 |
11 | 239 /** |
240 * Tells the transport what dimensions the display is currently | |
241 * @param columns columns of text | |
242 * @param rows rows of text | |
243 * @param width width in pixels | |
244 * @param height height in pixels | |
245 */ | |
246 @Override | |
247 public void setDimensions(int columns, int rows, int width, int height) { | |
248 // do nothing | |
249 } | |
250 | |
251 | |
252 @Override | |
45
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
253 public vt320 getTransportBuffer() { |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
254 buffer = new vt320x5250(); |
47
a3fd10a8c0de
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
46
diff
changeset
|
255 return setupTransportBuffer(); |
45
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
256 } |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
257 |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
258 |
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
259 @Override |
11 | 260 public int getDefaultPort() { |
261 return DEFAULT_PORT; | |
262 } | |
263 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
264 |
11 | 265 @Override |
266 public boolean isConnected() { | |
267 return connected; | |
268 } | |
269 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
270 |
11 | 271 @Override |
272 public boolean isSessionOpen() { | |
273 return connected; | |
274 } | |
275 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
276 |
11 | 277 @Override |
278 public boolean isAuthenticated() { | |
279 return connected; | |
280 } | |
281 | |
282 | |
283 @Override | |
284 public String getDefaultNickname(String username, String hostname, int port) { | |
285 if (port == DEFAULT_PORT) { | |
286 return String.format("%s", hostname); | |
287 } | |
288 else { | |
289 return String.format("%s:%d", hostname, port); | |
290 } | |
291 } | |
292 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
293 |
11 | 294 @Override |
295 public void getSelectionArgs(Uri uri, Map<String, String> selection) { | |
296 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); | |
297 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); | |
298 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); | |
299 int port = uri.getPort(); | |
300 | |
301 if (port < 0) | |
302 port = DEFAULT_PORT; | |
303 | |
304 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); | |
305 } | |
306 | |
307 | |
308 @Override | |
309 public HostBean createHost(Uri uri) { | |
310 HostBean host = new HostBean(); | |
311 host.setProtocol(PROTOCOL); | |
312 host.setHostname(uri.getHost()); | |
313 int port = uri.getPort(); | |
314 | |
315 if (port < 0) | |
316 port = DEFAULT_PORT; | |
317 | |
318 host.setPort(port); | |
319 String nickname = uri.getFragment(); | |
320 | |
321 if (nickname == null || nickname.length() == 0) { | |
322 host.setNickname(getDefaultNickname(host.getUsername(), | |
323 host.getHostname(), host.getPort())); | |
324 } | |
325 else { | |
326 host.setNickname(uri.getFragment()); | |
327 } | |
328 | |
329 return host; | |
330 } | |
331 | |
332 | |
12 | 333 public String getFormatHint(Context context) { |
11 | 334 return String.format("%s:%s", |
335 context.getString(R.string.format_hostname), | |
336 context.getString(R.string.format_port)); | |
337 } | |
338 | |
339 | |
340 @Override | |
341 public boolean usesNetwork() { | |
342 return true; | |
343 } | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
344 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
345 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
346 @Override |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
347 public boolean needsRelay() { |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
348 // we don't use a relay thread between the transport and the vt320 buffer |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
349 return false; |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
350 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
351 |
43
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
352 public TerminalKeyListener getTerminalKeyListener() { |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
353 return new Terminal5250KeyListener(manager, bridge, buffer, host.getEncoding()); |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
354 } |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
355 |
11 | 356 } |