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;
|
|
25 import java.util.List;
|
|
26 import java.util.Map;
|
|
27
|
12
|
28 import org.tn5250j.framework.tn5250.Screen5250;
|
|
29 import org.tn5250j.framework.tn5250.tnvt;
|
|
30
|
11
|
31 import com.five_ten_sg.connectbot.bean.HostBean;
|
|
32 import com.five_ten_sg.connectbot.bean.PortForwardBean;
|
|
33 import com.five_ten_sg.connectbot.service.TerminalBridge;
|
|
34 import com.five_ten_sg.connectbot.service.TerminalManager;
|
|
35 import com.five_ten_sg.connectbot.util.HostDatabase;
|
|
36
|
|
37 import android.content.Context;
|
|
38 import android.net.Uri;
|
|
39
|
|
40
|
|
41
|
|
42 /**
|
|
43 * @author Carl Byington
|
|
44 *
|
|
45 */
|
|
46 public class TN5250 extends AbsTransport {
|
|
47 private static final String PROTOCOL = "tn5250";
|
|
48 private static final String TAG = "ConnectBot.tn5250";
|
|
49 private static final int DEFAULT_PORT = 23;
|
|
50 private Screen5250 screen52;
|
|
51
|
|
52 private tnvt handler = null;
|
|
53 private Socket socket;
|
|
54
|
|
55 private InputStream is;
|
|
56 private OutputStream os;
|
|
57 private int width;
|
|
58 private int height;
|
|
59
|
|
60 private boolean connected = false;
|
|
61
|
12
|
62 static final Pattern hostmask;
|
|
63 static {
|
|
64 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE);
|
|
65 }
|
|
66
|
|
67
|
11
|
68 public TN5250() {
|
|
69 super();
|
|
70 }
|
|
71
|
|
72 public TN5250(HostBean host, TerminalBridge bridge, TerminalManager manager) {
|
|
73 super(host, bridge, manager);
|
|
74 handler = new tnvt(screen52, true, false, bridge, manager);
|
|
75 }
|
|
76
|
|
77
|
|
78 /**
|
|
79 * @return protocol part of the URI
|
|
80 */
|
|
81 public static String getProtocolName() {
|
|
82 return PROTOCOL;
|
|
83 }
|
|
84
|
12
|
85 public Uri getUri(String input) {
|
|
86 Matcher matcher = hostmask.matcher(input);
|
|
87
|
|
88 if (!matcher.matches())
|
|
89 return null;
|
|
90
|
|
91 StringBuilder sb = new StringBuilder();
|
|
92 sb.append(PROTOCOL)
|
|
93 .append("://")
|
|
94 .append(matcher.group(1));
|
|
95 String portString = matcher.group(3);
|
|
96 int port = DEFAULT_PORT;
|
|
97
|
|
98 if (portString != null) {
|
|
99 try {
|
|
100 port = Integer.parseInt(portString);
|
|
101
|
|
102 if (port < 1 || port > 65535) {
|
|
103 port = DEFAULT_PORT;
|
|
104 }
|
|
105 }
|
|
106 catch (NumberFormatException nfe) {
|
|
107 // Keep the default port
|
|
108 }
|
|
109 }
|
|
110
|
|
111 if (port != DEFAULT_PORT) {
|
|
112 sb.append(':');
|
|
113 sb.append(port);
|
|
114 }
|
|
115
|
|
116 sb.append("/#")
|
|
117 .append(Uri.encode(input));
|
|
118 Uri uri = Uri.parse(sb.toString());
|
|
119 return uri;
|
11
|
120 }
|
|
121
|
|
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() {
|
|
129 try {
|
|
130 connected = tnvt.connect(host.getHostname(), host.getPort());
|
|
131 is = tnvt.bin;
|
|
132 os = tnvt.bout;
|
|
133 bridge.onConnected();
|
|
134 }
|
|
135 catch (UnknownHostException e) {
|
|
136 Log.d(TAG, "IO Exception connecting to host", e);
|
|
137 }
|
|
138 catch (IOException e) {
|
|
139 Log.d(TAG, "IO Exception connecting to host", e);
|
|
140 }
|
|
141 }
|
|
142
|
|
143
|
|
144 /**
|
|
145 * Checks if read() will block. If there are no bytes remaining in
|
|
146 * the underlying transport, return true.
|
|
147 */
|
|
148 @Override
|
|
149 public boolean willBlock() {
|
|
150 if (is == null) return true;
|
|
151 try {
|
|
152 return is.available() == 0;
|
|
153 } catch (Exception e) {
|
|
154 return true;
|
|
155 }
|
|
156 }
|
|
157
|
|
158 /**
|
|
159 * Reads from the transport. Transport must support reading into a byte array
|
|
160 * <code>buffer</code> at the start of <code>offset</code> and a maximum of
|
|
161 * <code>length</code> bytes. If the remote host disconnects, throw an
|
|
162 * {@link IOException}.
|
|
163 * @param buffer byte buffer to store read bytes into
|
|
164 * @param offset where to start writing in the buffer
|
|
165 * @param length maximum number of bytes to read
|
|
166 * @return number of bytes read
|
|
167 * @throws IOException when remote host disconnects
|
|
168 */
|
|
169 public int read(byte[] buffer, int offset, int length) throws IOException {
|
|
170 return 0;
|
|
171 }
|
|
172
|
|
173
|
|
174 /**
|
|
175 * Writes to the transport. If the host is not yet connected, simply return without
|
|
176 * doing anything. An {@link IOException} should be thrown if there is an error after
|
|
177 * connection.
|
|
178 * @param buffer bytes to write to transport
|
|
179 * @throws IOException when there is a problem writing after connection
|
|
180 */
|
|
181 public void write(byte[] buffer) throws IOException {
|
|
182 }
|
|
183
|
|
184 /**
|
|
185 * Writes to the transport. See {@link #write(byte[])} for behavior details.
|
|
186 * @param c character to write to the transport
|
|
187 * @throws IOException when there is a problem writing after connection
|
|
188 */
|
|
189 public void write(int c) throws IOException {
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Flushes the write commands to the transport.
|
|
194 * @throws IOException when there is a problem writing after connection
|
|
195 */
|
|
196 public void flush() throws IOException {
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Closes the connection to the terminal. Note that the resulting failure to read
|
|
201 * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
|
|
202 */
|
|
203 public void close() {
|
|
204 tnvt.disconnect();
|
|
205 connected = false;
|
|
206 }
|
|
207
|
|
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 public void setOptions(Map<String, String> options) {
|
|
221 // do nothing
|
|
222 }
|
|
223
|
|
224 public Map<String, String> getOptions() {
|
|
225 return null;
|
|
226 }
|
|
227
|
|
228 public void setCompression(boolean compression) {
|
|
229 // do nothing
|
|
230 }
|
|
231
|
|
232 public void setHttpproxy(String httpproxy) {
|
|
233 // do nothing
|
|
234 }
|
|
235
|
|
236 public void setUseAuthAgent(String useAuthAgent) {
|
|
237 // do nothing
|
|
238 }
|
|
239
|
|
240 public void setEmulation(String emulation) {
|
|
241 this.emulation = emulation;
|
|
242 }
|
|
243
|
|
244 public String getEmulation() {
|
|
245 return emulation;
|
|
246 }
|
|
247
|
|
248 public void setHost(HostBean host) {
|
|
249 this.host = host;
|
|
250 }
|
|
251
|
|
252 public void setBridge(TerminalBridge bridge) {
|
|
253 this.bridge = bridge;
|
|
254 }
|
|
255
|
|
256 public void setManager(TerminalManager manager) {
|
|
257 this.manager = manager;
|
|
258 }
|
|
259
|
|
260 /**
|
|
261 * Whether or not this transport type can forward ports.
|
|
262 * @return true on ability to forward ports
|
|
263 */
|
|
264 public boolean canForwardPorts() {
|
|
265 return false;
|
|
266 }
|
|
267
|
|
268 /**
|
|
269 * Adds the {@link PortForwardBean} to the list.
|
|
270 * @param portForward the port forward bean to add
|
|
271 * @return true on successful addition
|
|
272 */
|
|
273 public boolean addPortForward(PortForwardBean portForward) {
|
|
274 return false;
|
|
275 }
|
|
276
|
|
277 /**
|
|
278 * Enables a port forward member. After calling this method, the port forward should
|
|
279 * be operational iff it could be enabled by the transport.
|
|
280 * @param portForward member of our current port forwards list to enable
|
|
281 * @return true on successful port forward setup
|
|
282 */
|
|
283 public boolean enablePortForward(PortForwardBean portForward) {
|
|
284 return false;
|
|
285 }
|
|
286
|
|
287 /**
|
|
288 * Disables a port forward member. After calling this method, the port forward should
|
|
289 * be non-functioning iff it could be disabled by the transport.
|
|
290 * @param portForward member of our current port forwards list to enable
|
|
291 * @return true on successful port forward tear-down
|
|
292 */
|
|
293 public boolean disablePortForward(PortForwardBean portForward) {
|
|
294 return false;
|
|
295 }
|
|
296
|
|
297 /**
|
|
298 * Removes the {@link PortForwardBean} from the available port forwards.
|
|
299 * @param portForward the port forward bean to remove
|
|
300 * @return true on successful removal
|
|
301 */
|
|
302 public boolean removePortForward(PortForwardBean portForward) {
|
|
303 return false;
|
|
304 }
|
|
305
|
|
306 /**
|
|
307 * Gets a list of the {@link PortForwardBean} currently used by this transport.
|
|
308 * @return the list of port forwards
|
|
309 */
|
|
310 public List<PortForwardBean> getPortForwards() {
|
|
311 return null;
|
|
312 }
|
|
313
|
|
314 /**
|
|
315 * Whether or not this transport type can transfer files.
|
|
316 * @return true on ability to transfer files
|
|
317 */
|
|
318 public boolean canTransferFiles() {
|
|
319 return false;
|
|
320 }
|
|
321
|
|
322 /**
|
|
323 * Downloads the specified remote file to a local folder.
|
|
324 * @param remoteFile The path to the remote file to be downloaded. Must be non-null.
|
|
325 * @param localFolder The path to local folder. Null = default external storage folder.
|
|
326 * @return true on success, false on failure
|
|
327 */
|
|
328 public boolean downloadFile(String remoteFile, String localFolder) {
|
|
329 return false;
|
|
330 }
|
|
331
|
|
332 /**
|
|
333 * Uploads the specified local file to the remote host.
|
|
334 * @param localFile The path to the local file to be uploaded. Must be non-null.
|
|
335 * @param remoteFolder The path to the remote directory. Null == default remote directory.
|
|
336 * @return true on success, false on failure
|
|
337 */
|
|
338 public boolean uploadFile(String localFile, String remoteFile,
|
|
339 String remoteFolder, String mode) {
|
|
340 return false;
|
|
341 }
|
|
342
|
|
343 @Override
|
|
344 public int getDefaultPort() {
|
|
345 return DEFAULT_PORT;
|
|
346 }
|
|
347
|
|
348 @Override
|
|
349 public boolean isConnected() {
|
|
350 return connected;
|
|
351 }
|
|
352
|
|
353 @Override
|
|
354 public boolean isSessionOpen() {
|
|
355 return connected;
|
|
356 }
|
|
357
|
|
358 @Override
|
|
359 public boolean isAuthenticated() {
|
|
360 return connected;
|
|
361 }
|
|
362
|
|
363
|
|
364 @Override
|
|
365 public String getDefaultNickname(String username, String hostname, int port) {
|
|
366 if (port == DEFAULT_PORT) {
|
|
367 return String.format("%s", hostname);
|
|
368 }
|
|
369 else {
|
|
370 return String.format("%s:%d", hostname, port);
|
|
371 }
|
|
372 }
|
|
373
|
|
374 @Override
|
|
375 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
|
|
376 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
|
|
377 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
|
|
378 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
|
|
379 int port = uri.getPort();
|
|
380
|
|
381 if (port < 0)
|
|
382 port = DEFAULT_PORT;
|
|
383
|
|
384 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
|
|
385 }
|
|
386
|
|
387
|
|
388 @Override
|
|
389 public HostBean createHost(Uri uri) {
|
|
390 HostBean host = new HostBean();
|
|
391 host.setProtocol(PROTOCOL);
|
|
392 host.setHostname(uri.getHost());
|
|
393 int port = uri.getPort();
|
|
394
|
|
395 if (port < 0)
|
|
396 port = DEFAULT_PORT;
|
|
397
|
|
398 host.setPort(port);
|
|
399 String nickname = uri.getFragment();
|
|
400
|
|
401 if (nickname == null || nickname.length() == 0) {
|
|
402 host.setNickname(getDefaultNickname(host.getUsername(),
|
|
403 host.getHostname(), host.getPort()));
|
|
404 }
|
|
405 else {
|
|
406 host.setNickname(uri.getFragment());
|
|
407 }
|
|
408
|
|
409 return host;
|
|
410 }
|
|
411
|
|
412
|
12
|
413 public String getFormatHint(Context context) {
|
11
|
414 return String.format("%s:%s",
|
|
415 context.getString(R.string.format_hostname),
|
|
416 context.getString(R.string.format_port));
|
|
417 }
|
|
418
|
|
419
|
|
420 @Override
|
|
421 public boolean usesNetwork() {
|
|
422 return true;
|
|
423 }
|
|
424 }
|