Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/transport/TN5250.java @ 44:959ebe0247b5 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 11 Jun 2014 09:30:50 -0700 |
parents | 6b0f1ece1d91 |
children | 80dcebe51af2 |
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 | |
43
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
66 class Terminal5250KeyListener extends TerminalKeyListener { |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
67 |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
68 }; |
12 | 69 |
11 | 70 public TN5250() { |
71 super(); | |
72 } | |
73 | |
74 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
75 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
76 * @return protocol part of the URI |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
77 */ |
11 | 78 public static String getProtocolName() { |
79 return PROTOCOL; | |
80 } | |
81 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
82 |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
83 /** |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
84 * 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
|
85 * @return URI to host |
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
86 */ |
12 | 87 public Uri getUri(String input) { |
88 Matcher matcher = hostmask.matcher(input); | |
89 | |
90 if (!matcher.matches()) | |
91 return null; | |
92 | |
93 StringBuilder sb = new StringBuilder(); | |
94 sb.append(PROTOCOL) | |
95 .append("://") | |
96 .append(matcher.group(1)); | |
97 String portString = matcher.group(3); | |
98 int port = DEFAULT_PORT; | |
99 | |
100 if (portString != null) { | |
101 try { | |
102 port = Integer.parseInt(portString); | |
103 | |
104 if (port < 1 || port > 65535) { | |
105 port = DEFAULT_PORT; | |
106 } | |
107 } | |
108 catch (NumberFormatException nfe) { | |
109 // Keep the default port | |
110 } | |
111 } | |
112 | |
113 if (port != DEFAULT_PORT) { | |
114 sb.append(':'); | |
115 sb.append(port); | |
116 } | |
117 | |
118 sb.append("/#") | |
119 .append(Uri.encode(input)); | |
120 Uri uri = Uri.parse(sb.toString()); | |
121 return uri; | |
11 | 122 } |
123 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
124 |
11 | 125 /** |
126 * Causes transport to connect to the target host. After connecting but before a | |
127 * session is started, must call back to {@link TerminalBridge#onConnected()}. | |
128 * After that call a session may be opened. | |
129 */ | |
130 @Override | |
131 public void connect() { | |
30
d738f6b876fe
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
29
diff
changeset
|
132 screen52 = new Screen5250(); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
133 handler = new tnvt(screen52, true, false, bridge, manager); |
37
0395ca628303
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
32
diff
changeset
|
134 handler.setSSLType("TLS"); |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
135 screen52.setVT(handler); |
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
136 screen52.setBuffer(buffer); |
23 | 137 connected = handler.connect(host.getHostname(), host.getPort()); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
138 if (connected) bridge.onConnected(); |
11 | 139 } |
140 | |
141 | |
142 /** | |
143 * Checks if read() will block. If there are no bytes remaining in | |
144 * the underlying transport, return true. | |
145 */ | |
146 @Override | |
147 public boolean willBlock() { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
148 // 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
|
149 return true; |
11 | 150 } |
151 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
152 |
11 | 153 /** |
154 * Reads from the transport. Transport must support reading into a byte array | |
155 * <code>buffer</code> at the start of <code>offset</code> and a maximum of | |
156 * <code>length</code> bytes. If the remote host disconnects, throw an | |
157 * {@link IOException}. | |
158 * @param buffer byte buffer to store read bytes into | |
159 * @param offset where to start writing in the buffer | |
160 * @param length maximum number of bytes to read | |
161 * @return number of bytes read | |
162 * @throws IOException when remote host disconnects | |
163 */ | |
164 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
|
165 // we don't use a relay thread between the transport and the vt320 buffer |
11 | 166 return 0; |
167 } | |
168 | |
169 | |
170 /** | |
171 * Writes to the transport. If the host is not yet connected, simply return without | |
172 * doing anything. An {@link IOException} should be thrown if there is an error after | |
173 * connection. | |
174 * @param buffer bytes to write to transport | |
175 * @throws IOException when there is a problem writing after connection | |
176 */ | |
177 public void write(byte[] buffer) throws IOException { | |
178 } | |
179 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
180 |
11 | 181 /** |
182 * Writes to the transport. See {@link #write(byte[])} for behavior details. | |
183 * @param c character to write to the transport | |
184 * @throws IOException when there is a problem writing after connection | |
185 */ | |
186 public void write(int c) throws IOException { | |
187 } | |
188 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
189 |
11 | 190 /** |
191 * Flushes the write commands to the transport. | |
192 * @throws IOException when there is a problem writing after connection | |
193 */ | |
194 public void flush() throws IOException { | |
195 } | |
196 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
197 |
11 | 198 /** |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
199 * Closes the connection to the terminal. |
11 | 200 */ |
201 public void close() { | |
13 | 202 handler.disconnect(); |
11 | 203 connected = false; |
32
b086dd794dba
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
31
diff
changeset
|
204 bridge.dispatchDisconnect(false); |
11 | 205 } |
206 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
207 |
11 | 208 /** |
209 * Tells the transport what dimensions the display is currently | |
210 * @param columns columns of text | |
211 * @param rows rows of text | |
212 * @param width width in pixels | |
213 * @param height height in pixels | |
214 */ | |
215 @Override | |
216 public void setDimensions(int columns, int rows, int width, int height) { | |
217 // do nothing | |
218 } | |
219 | |
220 | |
221 @Override | |
222 public int getDefaultPort() { | |
223 return DEFAULT_PORT; | |
224 } | |
225 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
226 |
11 | 227 @Override |
228 public boolean isConnected() { | |
229 return connected; | |
230 } | |
231 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
232 |
11 | 233 @Override |
234 public boolean isSessionOpen() { | |
235 return connected; | |
236 } | |
237 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
238 |
11 | 239 @Override |
240 public boolean isAuthenticated() { | |
241 return connected; | |
242 } | |
243 | |
244 | |
245 @Override | |
246 public String getDefaultNickname(String username, String hostname, int port) { | |
247 if (port == DEFAULT_PORT) { | |
248 return String.format("%s", hostname); | |
249 } | |
250 else { | |
251 return String.format("%s:%d", hostname, port); | |
252 } | |
253 } | |
254 | |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
30
diff
changeset
|
255 |
11 | 256 @Override |
257 public void getSelectionArgs(Uri uri, Map<String, String> selection) { | |
258 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); | |
259 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); | |
260 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); | |
261 int port = uri.getPort(); | |
262 | |
263 if (port < 0) | |
264 port = DEFAULT_PORT; | |
265 | |
266 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); | |
267 } | |
268 | |
269 | |
270 @Override | |
271 public HostBean createHost(Uri uri) { | |
272 HostBean host = new HostBean(); | |
273 host.setProtocol(PROTOCOL); | |
274 host.setHostname(uri.getHost()); | |
275 int port = uri.getPort(); | |
276 | |
277 if (port < 0) | |
278 port = DEFAULT_PORT; | |
279 | |
280 host.setPort(port); | |
281 String nickname = uri.getFragment(); | |
282 | |
283 if (nickname == null || nickname.length() == 0) { | |
284 host.setNickname(getDefaultNickname(host.getUsername(), | |
285 host.getHostname(), host.getPort())); | |
286 } | |
287 else { | |
288 host.setNickname(uri.getFragment()); | |
289 } | |
290 | |
291 return host; | |
292 } | |
293 | |
294 | |
12 | 295 public String getFormatHint(Context context) { |
11 | 296 return String.format("%s:%s", |
297 context.getString(R.string.format_hostname), | |
298 context.getString(R.string.format_port)); | |
299 } | |
300 | |
301 | |
302 @Override | |
303 public boolean usesNetwork() { | |
304 return true; | |
305 } | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
306 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
307 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
308 @Override |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
309 public boolean needsRelay() { |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
310 // 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
|
311 return false; |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
312 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
313 |
43
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
314 public TerminalKeyListener getTerminalKeyListener() { |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
315 return new Terminal5250KeyListener(manager, bridge, buffer, host.getEncoding()); |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
316 } |
6b0f1ece1d91
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
37
diff
changeset
|
317 |
11 | 318 } |