diff src/com/five_ten_sg/connectbot/transport/Telnet.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 b39bcf616a6f
children 77ac18bc1b2f
line wrap: on
line diff
--- 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",