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