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