diff src/com/five_ten_sg/connectbot/transport/SSH.java @ 31:139394237973 tn5250

start tn5250 integration
author Carl Byington <carl@five-ten-sg.com>
date Tue, 10 Jun 2014 12:00:07 -0700
parents 6aaefb22d876
children 77ac18bc1b2f
line wrap: on
line diff
--- 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;
-    }
 }