Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/transport/TN5250.java @ 29:017eeed8332c tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 03 Jun 2014 16:02:29 -0700 |
parents | cfcb8d9859a8 |
children | d738f6b876fe |
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; | |
55 private Screen5250 screen52; | |
56 | |
57 private tnvt handler = null; | |
58 private Socket socket; | |
59 | |
60 private int width; | |
61 private int height; | |
62 | |
63 private boolean connected = false; | |
64 | |
12 | 65 static final Pattern hostmask; |
66 static { | |
67 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE); | |
68 } | |
69 | |
70 | |
11 | 71 public TN5250() { |
72 super(); | |
73 } | |
74 | |
75 public TN5250(HostBean host, TerminalBridge bridge, TerminalManager manager) { | |
76 super(host, bridge, manager); | |
77 } | |
78 | |
79 | |
80 /** | |
81 * @return protocol part of the URI | |
82 */ | |
83 public static String getProtocolName() { | |
84 return PROTOCOL; | |
85 } | |
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 | |
124 /** | |
125 * Causes transport to connect to the target host. After connecting but before a | |
126 * session is started, must call back to {@link TerminalBridge#onConnected()}. | |
127 * After that call a session may be opened. | |
128 */ | |
129 @Override | |
130 public void connect() { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
131 screen52 = new Screen5250; |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
132 handler = new tnvt(screen52, true, false, bridge, manager); |
23 | 133 connected = handler.connect(host.getHostname(), host.getPort()); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
134 if (connected) bridge.onConnected(); |
11 | 135 } |
136 | |
137 | |
138 /** | |
139 * Checks if read() will block. If there are no bytes remaining in | |
140 * the underlying transport, return true. | |
141 */ | |
142 @Override | |
143 public boolean willBlock() { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
144 // 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
|
145 return true; |
11 | 146 } |
147 | |
148 /** | |
149 * Reads from the transport. Transport must support reading into a byte array | |
150 * <code>buffer</code> at the start of <code>offset</code> and a maximum of | |
151 * <code>length</code> bytes. If the remote host disconnects, throw an | |
152 * {@link IOException}. | |
153 * @param buffer byte buffer to store read bytes into | |
154 * @param offset where to start writing in the buffer | |
155 * @param length maximum number of bytes to read | |
156 * @return number of bytes read | |
157 * @throws IOException when remote host disconnects | |
158 */ | |
159 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
|
160 // we don't use a relay thread between the transport and the vt320 buffer |
11 | 161 return 0; |
162 } | |
163 | |
164 | |
165 /** | |
166 * Writes to the transport. If the host is not yet connected, simply return without | |
167 * doing anything. An {@link IOException} should be thrown if there is an error after | |
168 * connection. | |
169 * @param buffer bytes to write to transport | |
170 * @throws IOException when there is a problem writing after connection | |
171 */ | |
172 public void write(byte[] buffer) throws IOException { | |
173 } | |
174 | |
175 /** | |
176 * Writes to the transport. See {@link #write(byte[])} for behavior details. | |
177 * @param c character to write to the transport | |
178 * @throws IOException when there is a problem writing after connection | |
179 */ | |
180 public void write(int c) throws IOException { | |
181 } | |
182 | |
183 /** | |
184 * Flushes the write commands to the transport. | |
185 * @throws IOException when there is a problem writing after connection | |
186 */ | |
187 public void flush() throws IOException { | |
188 } | |
189 | |
190 /** | |
191 * Closes the connection to the terminal. Note that the resulting failure to read | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
192 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}. !!! |
11 | 193 */ |
194 public void close() { | |
13 | 195 handler.disconnect(); |
11 | 196 connected = false; |
197 } | |
198 | |
199 /** | |
200 * Tells the transport what dimensions the display is currently | |
201 * @param columns columns of text | |
202 * @param rows rows of text | |
203 * @param width width in pixels | |
204 * @param height height in pixels | |
205 */ | |
206 @Override | |
207 public void setDimensions(int columns, int rows, int width, int height) { | |
208 // do nothing | |
209 } | |
210 | |
211 public void setOptions(Map<String, String> options) { | |
212 // do nothing | |
213 } | |
214 | |
215 public Map<String, String> getOptions() { | |
216 return null; | |
217 } | |
218 | |
219 public void setCompression(boolean compression) { | |
220 // do nothing | |
221 } | |
222 | |
223 public void setHttpproxy(String httpproxy) { | |
224 // do nothing | |
225 } | |
226 | |
227 public void setUseAuthAgent(String useAuthAgent) { | |
228 // do nothing | |
229 } | |
230 | |
231 public void setEmulation(String emulation) { | |
232 this.emulation = emulation; | |
233 } | |
234 | |
235 public String getEmulation() { | |
236 return emulation; | |
237 } | |
238 | |
239 public void setHost(HostBean host) { | |
240 this.host = host; | |
241 } | |
242 | |
243 public void setBridge(TerminalBridge bridge) { | |
244 this.bridge = bridge; | |
245 } | |
246 | |
247 public void setManager(TerminalManager manager) { | |
248 this.manager = manager; | |
249 } | |
250 | |
251 /** | |
252 * Whether or not this transport type can forward ports. | |
253 * @return true on ability to forward ports | |
254 */ | |
255 public boolean canForwardPorts() { | |
256 return false; | |
257 } | |
258 | |
259 /** | |
260 * Adds the {@link PortForwardBean} to the list. | |
261 * @param portForward the port forward bean to add | |
262 * @return true on successful addition | |
263 */ | |
264 public boolean addPortForward(PortForwardBean portForward) { | |
265 return false; | |
266 } | |
267 | |
268 /** | |
269 * Enables a port forward member. After calling this method, the port forward should | |
270 * be operational iff it could be enabled by the transport. | |
271 * @param portForward member of our current port forwards list to enable | |
272 * @return true on successful port forward setup | |
273 */ | |
274 public boolean enablePortForward(PortForwardBean portForward) { | |
275 return false; | |
276 } | |
277 | |
278 /** | |
279 * Disables a port forward member. After calling this method, the port forward should | |
280 * be non-functioning iff it could be disabled by the transport. | |
281 * @param portForward member of our current port forwards list to enable | |
282 * @return true on successful port forward tear-down | |
283 */ | |
284 public boolean disablePortForward(PortForwardBean portForward) { | |
285 return false; | |
286 } | |
287 | |
288 /** | |
289 * Removes the {@link PortForwardBean} from the available port forwards. | |
290 * @param portForward the port forward bean to remove | |
291 * @return true on successful removal | |
292 */ | |
293 public boolean removePortForward(PortForwardBean portForward) { | |
294 return false; | |
295 } | |
296 | |
297 /** | |
298 * Gets a list of the {@link PortForwardBean} currently used by this transport. | |
299 * @return the list of port forwards | |
300 */ | |
301 public List<PortForwardBean> getPortForwards() { | |
302 return null; | |
303 } | |
304 | |
305 /** | |
306 * Whether or not this transport type can transfer files. | |
307 * @return true on ability to transfer files | |
308 */ | |
309 public boolean canTransferFiles() { | |
310 return false; | |
311 } | |
312 | |
313 /** | |
314 * Downloads the specified remote file to a local folder. | |
315 * @param remoteFile The path to the remote file to be downloaded. Must be non-null. | |
316 * @param localFolder The path to local folder. Null = default external storage folder. | |
317 * @return true on success, false on failure | |
318 */ | |
319 public boolean downloadFile(String remoteFile, String localFolder) { | |
320 return false; | |
321 } | |
322 | |
323 /** | |
324 * Uploads the specified local file to the remote host. | |
325 * @param localFile The path to the local file to be uploaded. Must be non-null. | |
326 * @param remoteFolder The path to the remote directory. Null == default remote directory. | |
327 * @return true on success, false on failure | |
328 */ | |
329 public boolean uploadFile(String localFile, String remoteFile, | |
330 String remoteFolder, String mode) { | |
331 return false; | |
332 } | |
333 | |
334 @Override | |
335 public int getDefaultPort() { | |
336 return DEFAULT_PORT; | |
337 } | |
338 | |
339 @Override | |
340 public boolean isConnected() { | |
341 return connected; | |
342 } | |
343 | |
344 @Override | |
345 public boolean isSessionOpen() { | |
346 return connected; | |
347 } | |
348 | |
349 @Override | |
350 public boolean isAuthenticated() { | |
351 return connected; | |
352 } | |
353 | |
354 | |
355 @Override | |
356 public String getDefaultNickname(String username, String hostname, int port) { | |
357 if (port == DEFAULT_PORT) { | |
358 return String.format("%s", hostname); | |
359 } | |
360 else { | |
361 return String.format("%s:%d", hostname, port); | |
362 } | |
363 } | |
364 | |
365 @Override | |
366 public void getSelectionArgs(Uri uri, Map<String, String> selection) { | |
367 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); | |
368 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); | |
369 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); | |
370 int port = uri.getPort(); | |
371 | |
372 if (port < 0) | |
373 port = DEFAULT_PORT; | |
374 | |
375 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); | |
376 } | |
377 | |
378 | |
379 @Override | |
380 public HostBean createHost(Uri uri) { | |
381 HostBean host = new HostBean(); | |
382 host.setProtocol(PROTOCOL); | |
383 host.setHostname(uri.getHost()); | |
384 int port = uri.getPort(); | |
385 | |
386 if (port < 0) | |
387 port = DEFAULT_PORT; | |
388 | |
389 host.setPort(port); | |
390 String nickname = uri.getFragment(); | |
391 | |
392 if (nickname == null || nickname.length() == 0) { | |
393 host.setNickname(getDefaultNickname(host.getUsername(), | |
394 host.getHostname(), host.getPort())); | |
395 } | |
396 else { | |
397 host.setNickname(uri.getFragment()); | |
398 } | |
399 | |
400 return host; | |
401 } | |
402 | |
403 | |
12 | 404 public String getFormatHint(Context context) { |
11 | 405 return String.format("%s:%s", |
406 context.getString(R.string.format_hostname), | |
407 context.getString(R.string.format_port)); | |
408 } | |
409 | |
410 | |
411 @Override | |
412 public boolean usesNetwork() { | |
413 return true; | |
414 } | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
415 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
416 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
417 @Override |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
418 public boolean needsRelay() { |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
419 // 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
|
420 return false; |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
421 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
422 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
423 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
424 @Override |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
425 public TerminalKeyListener(TerminalManager manager, TerminalBridge bridge, vt320 buffer, String encoding) { |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
426 return new TerminalKeyListener(manager, bridge, buffer, encoding); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
427 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
428 |
11 | 429 } |