Mercurial > 510Connectbot
changeset 31:139394237973 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 10 Jun 2014 12:00:07 -0700 |
parents | d738f6b876fe |
children | b086dd794dba |
files | src/com/five_ten_sg/connectbot/service/TerminalBridge.java src/com/five_ten_sg/connectbot/transport/AbsTransport.java src/com/five_ten_sg/connectbot/transport/Local.java src/com/five_ten_sg/connectbot/transport/SSH.java src/com/five_ten_sg/connectbot/transport/TN5250.java src/com/five_ten_sg/connectbot/transport/Telnet.java |
diffstat | 6 files changed, 467 insertions(+), 585 deletions(-) [+] |
line wrap: on
line diff
--- a/src/com/five_ten_sg/connectbot/service/TerminalBridge.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/service/TerminalBridge.java Tue Jun 10 12:00:07 2014 -0700 @@ -318,20 +318,18 @@ */ protected void startConnection() { transport = TransportFactory.getTransport(host.getProtocol()); - keyListener = transport.getTerminalKeyListener(manager, this, buffer, host.getEncoding()); + transport.setLinks(manager, this, host, buffer, emulation); + + keyListener = transport.getTerminalKeyListener(); String monitor_init = host.getMonitor(); if ((monitor_init != null) && (monitor_init.length() > 0)) { monitor = new TerminalMonitor(manager, buffer, keyListener, parent, monitor_init); } - transport.setBridge(this); - transport.setManager(manager); - transport.setHost(host); transport.setCompression(host.getCompression()); transport.setHttpproxy(host.getHttpproxy()); transport.setUseAuthAgent(host.getUseAuthAgent()); - transport.setEmulation(emulation); if (transport.canForwardPorts()) { for (PortForwardBean portForward : manager.hostdb.getPortForwardsForHost(host)) @@ -431,8 +429,6 @@ buffer.reset(); // We no longer need our local output. localOutput.clear(); - // previously tried vt100 and xterm for emulation modes - // "screen" works the best for color and escape codes buffer.setAnswerBack(emulation); if (HostDatabase.DELKEY_BACKSPACE.equals(host.getDelKey()))
--- a/src/com/five_ten_sg/connectbot/transport/AbsTransport.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/transport/AbsTransport.java Tue Jun 10 12:00:07 2014 -0700 @@ -35,20 +35,14 @@ * */ public abstract class AbsTransport { - HostBean host; - TerminalBridge bridge; TerminalManager manager; - - String emulation; + TerminalBridge bridge; + HostBean host; + vt320 buffer; + String emulation; public AbsTransport() {} - public AbsTransport(HostBean host, TerminalBridge bridge, TerminalManager manager) { - this.host = host; - this.bridge = bridge; - this.manager = manager; - } - /** * @return protocol part of the URI */ @@ -56,7 +50,6 @@ return "unknown"; } - /** * Encode the current transport into a URI that can be passed via intent calls. * @return URI to host @@ -147,24 +140,16 @@ // do nothing } - public void setEmulation(String emulation) { - this.emulation = emulation; - } - public String getEmulation() { return emulation; } - public void setHost(HostBean host) { - this.host = host; - } - - public void setBridge(TerminalBridge bridge) { - this.bridge = bridge; - } - - public void setManager(TerminalManager manager) { - this.manager = manager; + public void setLinks(TerminalManager manager, TerminalBridge bridge, HostBean host, vt320 buffer, String emulation) { + this.manager = manager; + this.bridge = bridge; + this.host = host; + this.buffer = buffer; + this.emulation = emulation; } /** @@ -250,14 +235,14 @@ return false; } - public abstract boolean isConnected(); - public abstract boolean isSessionOpen(); - public abstract boolean isAuthenticated(); /** * @return int default port for protocol */ public abstract int getDefaultPort(); + public abstract boolean isConnected(); + public abstract boolean isSessionOpen(); + public abstract boolean isAuthenticated(); /** * @param username @@ -302,8 +287,8 @@ /** * @return a key listener */ - public TerminalKeyListener getTerminalKeyListener(TerminalManager manager, TerminalBridge bridge, vt320 buffer, String encoding) { - return new TerminalKeyListener(manager, bridge, buffer, encoding); + public TerminalKeyListener getTerminalKeyListener() { + return new TerminalKeyListener(manager, bridge, buffer, host.getEncoding()); } }
--- a/src/com/five_ten_sg/connectbot/transport/Local.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/transport/Local.java Tue Jun 10 12:00:07 2014 -0700 @@ -41,12 +41,10 @@ public class Local extends AbsTransport { private static final String TAG = "ConnectBot.Local"; private static final String PROTOCOL = "local"; - private static final String DEFAULT_URI = "local:#Local"; - private FileDescriptor shellFd; - - private FileInputStream is; + private FileDescriptor shellFd; + private FileInputStream is; private FileOutputStream os; /** @@ -55,37 +53,23 @@ public Local() { } - /** - * @param host - * @param bridge - * @param manager - */ - public Local(HostBean host, TerminalBridge bridge, TerminalManager manager) { - super(host, bridge, manager); - } public static String getProtocolName() { return PROTOCOL; } - @Override - public void close() { - try { - if (os != null) { - os.close(); - os = null; - } + + public Uri getUri(String input) { + Uri uri = Uri.parse(DEFAULT_URI); - if (is != null) { - is.close(); - is = null; - } + if (input != null && input.length() > 0) { + uri = uri.buildUpon().fragment(input).build(); } - catch (IOException e) { - Log.e(TAG, "Couldn't close shell", e); - } + + return uri; } + @Override public void connect() { int[] pids = new int[1]; @@ -115,35 +99,6 @@ bridge.onConnected(); } - @Override - public void flush() throws IOException { - os.flush(); - } - - @Override - public String getDefaultNickname(String username, String hostname, int port) { - return DEFAULT_URI; - } - - @Override - public int getDefaultPort() { - return 0; - } - - @Override - public boolean isConnected() { - return is != null && os != null; - } - - @Override - public boolean isSessionOpen() { - return is != null && os != null; - } - - @Override - public boolean isAuthenticated() { - return isConnected(); - } @Override public boolean willBlock() { @@ -166,16 +121,6 @@ } @Override - public void setDimensions(int columns, int rows, int width, int height) { - try { - Exec.setPtyWindowSize(shellFd, rows, columns, width, height); - } - catch (Exception e) { - Log.e(TAG, "Couldn't resize pty", e); - } - } - - @Override public void write(byte[] buffer) throws IOException { if (os != null) os.write(buffer); @@ -187,14 +132,68 @@ os.write(c); } - public Uri getUri(String input) { - Uri uri = Uri.parse(DEFAULT_URI); + @Override + public void flush() throws IOException { + os.flush(); + } + + @Override + public void close() { + try { + if (os != null) { + os.close(); + os = null; + } + + if (is != null) { + is.close(); + is = null; + } + } + catch (IOException e) { + Log.e(TAG, "Couldn't close shell", e); + } + } - if (input != null && input.length() > 0) { - uri = uri.buildUpon().fragment(input).build(); + @Override + public void setDimensions(int columns, int rows, int width, int height) { + try { + Exec.setPtyWindowSize(shellFd, rows, columns, width, height); + } + catch (Exception e) { + Log.e(TAG, "Couldn't resize pty", e); } + } - return uri; + @Override + public int getDefaultPort() { + return 0; + } + + @Override + public boolean isConnected() { + return is != null && os != null; + } + + @Override + public boolean isSessionOpen() { + return isConnected(); + } + + @Override + public boolean isAuthenticated() { + return isConnected(); + } + + @Override + public String getDefaultNickname(String username, String hostname, int port) { + return DEFAULT_URI; + } + + @Override + public void getSelectionArgs(Uri uri, Map<String, String> selection) { + selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); + selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); } @Override @@ -214,12 +213,6 @@ return host; } - @Override - public void getSelectionArgs(Uri uri, Map<String, String> selection) { - selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); - selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); - } - public String getFormatHint(Context context) { return context.getString(R.string.hostpref_nickname_title); }
--- a/src/com/five_ten_sg/connectbot/transport/SSH.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/transport/SSH.java Tue Jun 10 12:00:07 2014 -0700 @@ -82,19 +82,6 @@ * */ public class SSH extends AbsTransport implements ConnectionMonitor, InteractiveCallback, AuthAgentCallback { - public SSH() { - super(); - } - - /** - * @param host - * @param bridge - * @param manager - */ - public SSH(HostBean host, TerminalBridge bridge, TerminalManager manager) { - super(host, bridge, manager); - } - private static final String PROTOCOL = "ssh"; private static final String TAG = "ConnectBot.SSH"; private static final int DEFAULT_PORT = 22; @@ -212,6 +199,60 @@ } + + public SSH() { + super(); + } + + + /** + * @return protocol part of the URI + */ + public static String getProtocolName() { + return PROTOCOL; + } + + + public Uri getUri(String input) { + Matcher matcher = hostmask.matcher(input); + + if (!matcher.matches()) + return null; + + StringBuilder sb = new StringBuilder(); + sb.append(PROTOCOL) + .append("://") + .append(Uri.encode(matcher.group(1))) + .append('@') + .append(matcher.group(2)); + String portString = matcher.group(4); + int port = DEFAULT_PORT; + + if (portString != null) { + try { + port = Integer.parseInt(portString); + + if (port < 1 || port > 65535) { + port = DEFAULT_PORT; + } + } + catch (NumberFormatException nfe) { + // Keep the default port + } + } + + if (port != DEFAULT_PORT) { + sb.append(':') + .append(port); + } + + sb.append("/#") + .append(Uri.encode(input)); + Uri uri = Uri.parse(sb.toString()); + return uri; + } + + private void authenticate() { try { if (connection.authenticateWithNone(host.getUsername())) { @@ -303,6 +344,7 @@ } } + /** * Attempt connection with database row pointed to by cursor. * @param cursor @@ -371,6 +413,7 @@ return tryPublicKey(host.getUsername(), pubkey.getNickname(), pair); } + private boolean tryPublicKey(String username, String keyNickname, KeyPair pair) throws IOException { //bridge.outputLine(String.format("Attempting 'publickey' with key '%s' [%s]...", keyNickname, trileadKey.toString())); boolean success = connection.authenticateWithPublicKey(username, pair); @@ -381,6 +424,7 @@ return success; } + /** * Internal method to request actual PTY terminal once we've finished * authentication. If called before authenticated, it will just fail. @@ -432,6 +476,7 @@ } } + @Override public void connect() { connection = new Connection(host.getHostname(), host.getPort()); @@ -537,31 +582,6 @@ } } - @Override - public void close() { - connected = false; - - if (session != null) { - session.close(); - session = null; - } - - if (connection != null) { - connection.close(); - connection = null; - } - } - - private void onDisconnect() { - close(); - bridge.dispatchDisconnect(false); - } - - @Override - public void flush() throws IOException { - if (stdin != null) - stdin.flush(); - } @Override public boolean willBlock() { @@ -573,6 +593,7 @@ } } + @Override public int read(byte[] buffer, int start, int len) throws IOException { int bytesRead = 0; @@ -602,18 +623,81 @@ return bytesRead; } + @Override public void write(byte[] buffer) throws IOException { if (stdin != null) stdin.write(buffer); } + @Override public void write(int c) throws IOException { if (stdin != null) stdin.write(c); } + + @Override + public void flush() throws IOException { + if (stdin != null) + stdin.flush(); + } + + + public void connectionLost(Throwable reason) { + onDisconnect(); + } + + + private void onDisconnect() { + close(); + bridge.dispatchDisconnect(false); + } + + + @Override + public void close() { + connected = false; + + if (session != null) { + session.close(); + session = null; + } + + if (connection != null) { + connection.close(); + connection = null; + } + } + + + @Override + public void setDimensions(int columns, int rows, int width, int height) { + this.columns = columns; + this.rows = rows; + + if (sessionOpen) { + try { + session.resizePTY(columns, rows, width, height); + } + catch (IOException e) { + Log.e(TAG, "Couldn't send resize PTY packet", e); + } + } + } + + + @Override + public void setOptions(Map<String, String> options) { + if (options.containsKey("compression")) + compression = Boolean.parseBoolean(options.get("compression")); + + if (options.containsKey("httpproxy")) + httpproxy = options.get("httpproxy"); + } + + @Override public Map<String, String> getOptions() { Map<String, String> options = new HashMap<String, String>(); @@ -625,39 +709,22 @@ return options; } + @Override - public void setOptions(Map<String, String> options) { - if (options.containsKey("compression")) - compression = Boolean.parseBoolean(options.get("compression")); - - if (options.containsKey("httpproxy")) - httpproxy = options.get("httpproxy"); + public void setCompression(boolean compression) { + this.compression = compression; } - /** - * @return protocol part of the URI - */ - public static String getProtocolName() { - return PROTOCOL; - } @Override - public boolean isSessionOpen() { - return sessionOpen; + public void setHttpproxy(String httpproxy) { + this.httpproxy = httpproxy; } + @Override - public boolean isConnected() { - return connected; - } - - @Override - public boolean isAuthenticated() { - return authenticated; - } - - public void connectionLost(Throwable reason) { - onDisconnect(); + public void setUseAuthAgent(String useAuthAgent) { + this.useAuthAgent = useAuthAgent; } @Override @@ -867,26 +934,31 @@ } } - @Override - public void setDimensions(int columns, int rows, int width, int height) { - this.columns = columns; - this.rows = rows; - - if (sessionOpen) { - try { - session.resizePTY(columns, rows, width, height); - } - catch (IOException e) { - Log.e(TAG, "Couldn't send resize PTY packet", e); - } - } - } @Override public int getDefaultPort() { return DEFAULT_PORT; } + + @Override + public boolean isConnected() { + return connected; + } + + + @Override + public boolean isSessionOpen() { + return sessionOpen; + } + + + @Override + public boolean isAuthenticated() { + return authenticated; + } + + @Override public String getDefaultNickname(String username, String hostname, int port) { if (port == DEFAULT_PORT) { @@ -897,59 +969,21 @@ } } - public Uri getUri(String input) { - Matcher matcher = hostmask.matcher(input); - - if (!matcher.matches()) - return null; - - StringBuilder sb = new StringBuilder(); - sb.append(PROTOCOL) - .append("://") - .append(Uri.encode(matcher.group(1))) - .append('@') - .append(matcher.group(2)); - String portString = matcher.group(4); - int port = DEFAULT_PORT; - - if (portString != null) { - try { - port = Integer.parseInt(portString); - if (port < 1 || port > 65535) { - port = DEFAULT_PORT; - } - } - catch (NumberFormatException nfe) { - // Keep the default port - } - } + @Override + public void getSelectionArgs(Uri uri, Map<String, String> selection) { + selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); + selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); + selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); + int port = uri.getPort(); - if (port != DEFAULT_PORT) { - sb.append(':') - .append(port); - } + if (port < 0) + port = DEFAULT_PORT; - sb.append("/#") - .append(Uri.encode(input)); - Uri uri = Uri.parse(sb.toString()); - return uri; + selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); + selection.put(HostDatabase.FIELD_HOST_USERNAME, uri.getUserInfo()); } - /** - * Handle challenges from keyboard-interactive authentication mode. - */ - public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) { - interactiveCanContinue = true; - String[] responses = new String[numPrompts]; - - for (int i = 0; i < numPrompts; i++) { - // request response from user for each prompt - responses[i] = bridge.promptHelper.requestPasswordPrompt(instruction, prompt[i]); - } - - return responses; - } @Override public HostBean createHost(Uri uri) { @@ -976,29 +1010,6 @@ return host; } - @Override - public void getSelectionArgs(Uri uri, Map<String, String> selection) { - selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); - selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); - selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); - int port = uri.getPort(); - - if (port < 0) - port = DEFAULT_PORT; - - selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); - selection.put(HostDatabase.FIELD_HOST_USERNAME, uri.getUserInfo()); - } - - @Override - public void setCompression(boolean compression) { - this.compression = compression; - } - - @Override - public void setHttpproxy(String httpproxy) { - this.httpproxy = httpproxy; - } public String getFormatHint(Context context) { return String.format("%s@%s:%s", @@ -1007,9 +1018,29 @@ context.getString(R.string.format_port)); } + + /** + * @return do we use the network + */ @Override - public void setUseAuthAgent(String useAuthAgent) { - this.useAuthAgent = useAuthAgent; + public boolean usesNetwork() { + return true; + } + + + /** + * Handle challenges from keyboard-interactive authentication mode. + */ + public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) { + interactiveCanContinue = true; + String[] responses = new String[numPrompts]; + + for (int i = 0; i < numPrompts; i++) { + // request response from user for each prompt + responses[i] = bridge.promptHelper.requestPasswordPrompt(instruction, prompt[i]); + } + + return responses; } public Map<String, byte[]> retrieveIdentities() { @@ -1107,11 +1138,4 @@ return true; } - /* (non-Javadoc) - * @see com.five_ten_sg.connectbot.transport.AbsTransport#usesNetwork() - */ - @Override - public boolean usesNetwork() { - return true; - } }
--- a/src/com/five_ten_sg/connectbot/transport/TN5250.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/transport/TN5250.java Tue Jun 10 12:00:07 2014 -0700 @@ -52,15 +52,13 @@ private static final String PROTOCOL = "tn5250"; private static final String TAG = "ConnectBot.tn5250"; private static final int DEFAULT_PORT = 23; + private Screen5250 screen52; - - private tnvt handler = null; - private Socket socket; - - private int width; - private int height; - - private boolean connected = false; + private tnvt handler = null; + private Socket socket; + private int width; + private int height; + private boolean connected = false; static final Pattern hostmask; static { @@ -72,18 +70,19 @@ super(); } - public TN5250(HostBean host, TerminalBridge bridge, TerminalManager manager) { - super(host, bridge, manager); - } - - /** - * @return protocol part of the URI - */ + /** + * @return protocol part of the URI + */ public static String getProtocolName() { return PROTOCOL; } + + /** + * Encode the current transport into a URI that can be passed via intent calls. + * @return URI to host + */ public Uri getUri(String input) { Matcher matcher = hostmask.matcher(input); @@ -121,6 +120,7 @@ return uri; } + /** * Causes transport to connect to the target host. After connecting but before a * session is started, must call back to {@link TerminalBridge#onConnected()}. @@ -145,6 +145,7 @@ return true; } + /** * Reads from the transport. Transport must support reading into a byte array * <code>buffer</code> at the start of <code>offset</code> and a maximum of @@ -172,6 +173,7 @@ public void write(byte[] buffer) throws IOException { } + /** * Writes to the transport. See {@link #write(byte[])} for behavior details. * @param c character to write to the transport @@ -180,6 +182,7 @@ public void write(int c) throws IOException { } + /** * Flushes the write commands to the transport. * @throws IOException when there is a problem writing after connection @@ -187,6 +190,7 @@ public void flush() throws IOException { } + /** * Closes the connection to the terminal. Note that the resulting failure to read * should call {@link TerminalBridge#dispatchDisconnect(boolean)}. !!! @@ -196,6 +200,7 @@ connected = false; } + /** * Tells the transport what dimensions the display is currently * @param columns columns of text @@ -208,144 +213,25 @@ // do nothing } - public void setOptions(Map<String, String> options) { - // do nothing - } - - public Map<String, String> getOptions() { - return null; - } - - public void setCompression(boolean compression) { - // do nothing - } - - public void setHttpproxy(String httpproxy) { - // do nothing - } - - public void setUseAuthAgent(String useAuthAgent) { - // do nothing - } - - public void setEmulation(String emulation) { - this.emulation = emulation; - } - - public String getEmulation() { - return emulation; - } - - public void setHost(HostBean host) { - this.host = host; - } - - public void setBridge(TerminalBridge bridge) { - this.bridge = bridge; - } - - public void setManager(TerminalManager manager) { - this.manager = manager; - } - - /** - * Whether or not this transport type can forward ports. - * @return true on ability to forward ports - */ - public boolean canForwardPorts() { - return false; - } - - /** - * Adds the {@link PortForwardBean} to the list. - * @param portForward the port forward bean to add - * @return true on successful addition - */ - public boolean addPortForward(PortForwardBean portForward) { - return false; - } - - /** - * Enables a port forward member. After calling this method, the port forward should - * be operational iff it could be enabled by the transport. - * @param portForward member of our current port forwards list to enable - * @return true on successful port forward setup - */ - public boolean enablePortForward(PortForwardBean portForward) { - return false; - } - - /** - * Disables a port forward member. After calling this method, the port forward should - * be non-functioning iff it could be disabled by the transport. - * @param portForward member of our current port forwards list to enable - * @return true on successful port forward tear-down - */ - public boolean disablePortForward(PortForwardBean portForward) { - return false; - } - - /** - * Removes the {@link PortForwardBean} from the available port forwards. - * @param portForward the port forward bean to remove - * @return true on successful removal - */ - public boolean removePortForward(PortForwardBean portForward) { - return false; - } - - /** - * Gets a list of the {@link PortForwardBean} currently used by this transport. - * @return the list of port forwards - */ - public List<PortForwardBean> getPortForwards() { - return null; - } - - /** - * Whether or not this transport type can transfer files. - * @return true on ability to transfer files - */ - public boolean canTransferFiles() { - return false; - } - - /** - * Downloads the specified remote file to a local folder. - * @param remoteFile The path to the remote file to be downloaded. Must be non-null. - * @param localFolder The path to local folder. Null = default external storage folder. - * @return true on success, false on failure - */ - public boolean downloadFile(String remoteFile, String localFolder) { - return false; - } - - /** - * Uploads the specified local file to the remote host. - * @param localFile The path to the local file to be uploaded. Must be non-null. - * @param remoteFolder The path to the remote directory. Null == default remote directory. - * @return true on success, false on failure - */ - public boolean uploadFile(String localFile, String remoteFile, - String remoteFolder, String mode) { - return false; - } @Override public int getDefaultPort() { return DEFAULT_PORT; } + @Override public boolean isConnected() { return connected; } + @Override public boolean isSessionOpen() { return connected; } + @Override public boolean isAuthenticated() { return connected; @@ -362,6 +248,7 @@ } } + @Override public void getSelectionArgs(Uri uri, Map<String, String> selection) { selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); @@ -420,10 +307,4 @@ return false; } - - @Override - public TerminalKeyListener getTerminalKeyListener(TerminalManager manager, TerminalBridge bridge, vt320 buffer, String encoding) { - return new TerminalKeyListener(manager, bridge, buffer, encoding); - } - }
--- a/src/com/five_ten_sg/connectbot/transport/Telnet.java Tue Jun 03 16:05:21 2014 -0700 +++ b/src/com/five_ten_sg/connectbot/transport/Telnet.java Tue Jun 10 12:00:07 2014 -0700 @@ -51,18 +51,15 @@ public class Telnet extends AbsTransport { private static final String TAG = "ConnectBot.Telnet"; private static final String PROTOCOL = "telnet"; - private static final int DEFAULT_PORT = 23; private TelnetProtocolHandler handler; - private Socket socket; - - private InputStream is; - private OutputStream os; - private int width; - private int height; - - private boolean connected = false; + private Socket socket; + private InputStream is; + private OutputStream os; + private int width; + private int height; + private boolean connected = false; static final Pattern hostmask; static { @@ -108,162 +105,11 @@ }; } - /** - * @param host - * @param bridge - * @param manager - */ - public Telnet(HostBean host, TerminalBridge bridge, TerminalManager manager) { - super(host, bridge, manager); - } public static String getProtocolName() { return PROTOCOL; } - @Override - public void connect() { - try { - socket = new Socket(host.getHostname(), host.getPort()); - connected = true; - is = socket.getInputStream(); - os = socket.getOutputStream(); - bridge.onConnected(); - } - catch (UnknownHostException e) { - Log.d(TAG, "IO Exception connecting to host", e); - } - catch (IOException e) { - Log.d(TAG, "IO Exception connecting to host", e); - } - } - - @Override - public void close() { - connected = false; - - if (socket != null) - try { - socket.close(); - socket = null; - } - catch (IOException e) { - Log.d(TAG, "Error closing telnet socket.", e); - } - } - - @Override - public void flush() throws IOException { - os.flush(); - } - - @Override - public int getDefaultPort() { - return DEFAULT_PORT; - } - - @Override - public boolean isConnected() { - return connected; - } - - @Override - public boolean isSessionOpen() { - return connected; - } - - @Override - public boolean isAuthenticated() { - return isConnected(); - } - - @Override - public boolean willBlock() { - if (is == null) return true; - try { - return is.available() == 0; - } catch (Exception e) { - return true; - } - } - - @Override - public int read(byte[] buffer, int start, int len) throws IOException { - /* process all already read bytes */ - int n = 0; - - do { - n = handler.negotiate(buffer, start); - - if (n > 0) - return n; - } - while (n == 0); - - while (n <= 0) { - do { - n = handler.negotiate(buffer, start); - - if (n > 0) - return n; - } - while (n == 0); - - n = is.read(buffer, start, len); - - if (n < 0) { - bridge.dispatchDisconnect(false); - throw new IOException("Remote end closed connection."); - } - - handler.inputfeed(buffer, start, n); - n = handler.negotiate(buffer, start); - } - - return n; - } - - @Override - public void write(byte[] buffer) throws IOException { - try { - if (os != null) - os.write(buffer); - } - catch (SocketException e) { - bridge.dispatchDisconnect(false); - } - } - - @Override - public void write(int c) throws IOException { - try { - if (os != null) - os.write(c); - } - catch (SocketException e) { - bridge.dispatchDisconnect(false); - } - } - - @Override - public void setDimensions(int columns, int rows, int width, int height) { - try { - handler.setWindowSize(columns, rows); - } - catch (IOException e) { - Log.e(TAG, "Couldn't resize remote terminal", e); - } - } - - @Override - public String getDefaultNickname(String username, String hostname, int port) { - if (port == DEFAULT_PORT) { - return String.format("%s", hostname); - } - else { - return String.format("%s:%d", hostname, port); - } - } public Uri getUri(String input) { Matcher matcher = hostmask.matcher(input); @@ -302,6 +148,175 @@ return uri; } + + @Override + public void connect() { + try { + socket = new Socket(host.getHostname(), host.getPort()); + connected = true; + is = socket.getInputStream(); + os = socket.getOutputStream(); + bridge.onConnected(); + } + catch (UnknownHostException e) { + Log.d(TAG, "IO Exception connecting to host", e); + } + catch (IOException e) { + Log.d(TAG, "IO Exception connecting to host", e); + } + } + + + @Override + public boolean willBlock() { + if (is == null) return true; + try { + return is.available() == 0; + } catch (Exception e) { + return true; + } + } + + + @Override + public int read(byte[] buffer, int start, int len) throws IOException { + /* process all already read bytes */ + int n = 0; + + do { + n = handler.negotiate(buffer, start); + + if (n > 0) + return n; + } + while (n == 0); + + while (n <= 0) { + do { + n = handler.negotiate(buffer, start); + + if (n > 0) + return n; + } + while (n == 0); + + n = is.read(buffer, start, len); + + if (n < 0) { + bridge.dispatchDisconnect(false); + throw new IOException("Remote end closed connection."); + } + + handler.inputfeed(buffer, start, n); + n = handler.negotiate(buffer, start); + } + + return n; + } + + + @Override + public void write(byte[] buffer) throws IOException { + try { + if (os != null) + os.write(buffer); + } + catch (SocketException e) { + bridge.dispatchDisconnect(false); + } + } + + + @Override + public void write(int c) throws IOException { + try { + if (os != null) + os.write(c); + } + catch (SocketException e) { + bridge.dispatchDisconnect(false); + } + } + + + @Override + public void flush() throws IOException { + os.flush(); + } + + + @Override + public void close() { + connected = false; + + if (socket != null) + try { + socket.close(); + socket = null; + } + catch (IOException e) { + Log.d(TAG, "Error closing telnet socket.", e); + } + } + + + @Override + public void setDimensions(int columns, int rows, int width, int height) { + try { + handler.setWindowSize(columns, rows); + } + catch (IOException e) { + Log.e(TAG, "Couldn't resize remote terminal", e); + } + } + + + @Override + public int getDefaultPort() { + return DEFAULT_PORT; + } + + @Override + public boolean isConnected() { + return connected; + } + + @Override + public boolean isSessionOpen() { + return isConnected(); + } + + @Override + public boolean isAuthenticated() { + return isConnected(); + } + + + @Override + public String getDefaultNickname(String username, String hostname, int port) { + if (port == DEFAULT_PORT) { + return String.format("%s", hostname); + } + else { + return String.format("%s:%d", hostname, port); + } + } + + + @Override + public void getSelectionArgs(Uri uri, Map<String, String> selection) { + selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); + selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); + selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); + int port = uri.getPort(); + + if (port < 0) + port = DEFAULT_PORT; + + selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); + } + + @Override public HostBean createHost(Uri uri) { HostBean host = new HostBean(); @@ -326,18 +341,6 @@ return host; } - @Override - public void getSelectionArgs(Uri uri, Map<String, String> selection) { - selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); - selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); - selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); - int port = uri.getPort(); - - if (port < 0) - port = DEFAULT_PORT; - - selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); - } public String getFormatHint(Context context) { return String.format("%s:%s",