changeset 11:f3b3bbd227b8 tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 18:26:27 -0700
parents e773d0952613
children 6aaefb22d876
files src/com/five_ten_sg/connectbot/transport/AbsTransport.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 src/com/five_ten_sg/connectbot/transport/TransportFactory.java src/org/tn5250j/framework/tn5250/tnvt.java
diffstat 6 files changed, 410 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/five_ten_sg/connectbot/transport/AbsTransport.java	Thu May 22 16:11:14 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/transport/AbsTransport.java	Thu May 22 18:26:27 2014 -0700
@@ -54,13 +54,13 @@
         return "unknown";
     }
 
+
     /**
      * Encode the current transport into a URI that can be passed via intent calls.
      * @return URI to host
      */
-    public static Uri getUri(String input) {
-        return null;
-    }
+    public abstract Uri getUri(String input);
+
 
     /**
      * Causes transport to connect to the target host. After connecting but before a
@@ -282,9 +282,7 @@
      * @param context context containing the correct resources
      * @return string that hints at the format for connection
      */
-    public static String getFormatHint(Context context) {
-        return "???";
-    }
+    public abstract String getFormatHint(Context context);
 
     /**
      * @return
--- a/src/com/five_ten_sg/connectbot/transport/SSH.java	Thu May 22 16:11:14 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/transport/SSH.java	Thu May 22 18:26:27 2014 -0700
@@ -634,6 +634,9 @@
             httpproxy = options.get("httpproxy");
     }
 
+    /**
+     * @return protocol part of the URI
+     */
     public static String getProtocolName() {
         return PROTOCOL;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/five_ten_sg/connectbot/transport/TN5250.java	Thu May 22 18:26:27 2014 -0700
@@ -0,0 +1,387 @@
+/*
+ * 510ConnectBot
+ * Copyright 2014 Carl Byington
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.five_ten_sg.connectbot.transport;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.List;
+import java.util.Map;
+
+import com.five_ten_sg.connectbot.bean.HostBean;
+import com.five_ten_sg.connectbot.bean.PortForwardBean;
+import com.five_ten_sg.connectbot.service.TerminalBridge;
+import com.five_ten_sg.connectbot.service.TerminalManager;
+import com.five_ten_sg.connectbot.util.HostDatabase;
+
+import android.content.Context;
+import android.net.Uri;
+
+import org.tn5250j.framework.tn5250.tnvt;
+
+
+/**
+ * @author Carl Byington
+ *
+ */
+public class TN5250 extends AbsTransport {
+    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 InputStream is;
+    private OutputStream os;
+    private int width;
+    private int height;
+
+    private boolean connected = false;
+
+    public TN5250() {
+       super();
+   }
+
+   public TN5250(HostBean host, TerminalBridge bridge, TerminalManager manager) {
+       super(host, bridge, manager);
+       handler = new tnvt(screen52, true, false, bridge, manager);
+   }
+
+
+   /**
+    * @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 static Uri getUri(String input) {
+       return null;
+   }
+
+   /**
+    * Causes transport to connect to the target host. After connecting but before a
+    * session is started, must call back to {@link TerminalBridge#onConnected()}.
+    * After that call a session may be opened.
+    */
+   @Override
+   public void connect() {
+       try {
+            connected = tnvt.connect(host.getHostname(), host.getPort());
+            is = tnvt.bin;
+            os = tnvt.bout;
+            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);
+        }
+    }
+
+
+    /**
+     * Checks if read() will block. If there are no bytes remaining in
+     * the underlying transport, return true.
+     */
+    @Override
+    public boolean willBlock() {
+        if (is == null) return true;
+        try {
+            return is.available() == 0;
+        } catch (Exception e) {
+            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
+     * <code>length</code> bytes. If the remote host disconnects, throw an
+     * {@link IOException}.
+     * @param buffer byte buffer to store read bytes into
+     * @param offset where to start writing in the buffer
+     * @param length maximum number of bytes to read
+     * @return number of bytes read
+     * @throws IOException when remote host disconnects
+     */
+    public int read(byte[] buffer, int offset, int length) throws IOException {
+        return 0;
+    }
+
+
+    /**
+     * Writes to the transport. If the host is not yet connected, simply return without
+     * doing anything. An {@link IOException} should be thrown if there is an error after
+     * connection.
+     * @param buffer bytes to write to transport
+     * @throws IOException when there is a problem writing after connection
+     */
+    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
+     * @throws IOException when there is a problem writing after connection
+     */
+    public void write(int c) throws IOException {
+    }
+
+    /**
+     * Flushes the write commands to the transport.
+     * @throws IOException when there is a problem writing after connection
+     */
+    public void flush() throws IOException {
+    }
+
+    /**
+     * Closes the connection to the terminal. Note that the resulting failure to read
+     * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
+     */
+    public void close() {
+        tnvt.disconnect();
+        connected = false;
+    }
+
+    /**
+     * Tells the transport what dimensions the display is currently
+     * @param columns columns of text
+     * @param rows rows of text
+     * @param width width in pixels
+     * @param height height in pixels
+     */
+    @Override
+    public void setDimensions(int columns, int rows, int width, int height) {
+        // 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;
+    }
+
+
+    @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();
+        host.setProtocol(PROTOCOL);
+        host.setHostname(uri.getHost());
+        int port = uri.getPort();
+
+        if (port < 0)
+            port = DEFAULT_PORT;
+
+        host.setPort(port);
+        String nickname = uri.getFragment();
+
+        if (nickname == null || nickname.length() == 0) {
+            host.setNickname(getDefaultNickname(host.getUsername(),
+                                                host.getHostname(), host.getPort()));
+        }
+        else {
+            host.setNickname(uri.getFragment());
+        }
+
+        return host;
+    }
+
+
+    public static String getFormatHint(Context context) {
+        return String.format("%s:%s",
+                             context.getString(R.string.format_hostname),
+                             context.getString(R.string.format_port));
+    }
+
+
+    @Override
+    public boolean usesNetwork() {
+        return true;
+    }
+}
--- a/src/com/five_ten_sg/connectbot/transport/Telnet.java	Thu May 22 16:11:14 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/transport/Telnet.java	Thu May 22 18:26:27 2014 -0700
@@ -33,6 +33,7 @@
 import com.five_ten_sg.connectbot.service.TerminalBridge;
 import com.five_ten_sg.connectbot.service.TerminalManager;
 import com.five_ten_sg.connectbot.util.HostDatabase;
+
 import android.content.Context;
 import android.net.Uri;
 import android.util.Log;
@@ -342,9 +343,6 @@
                              context.getString(R.string.format_port));
     }
 
-    /* (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/TransportFactory.java	Thu May 22 16:11:14 2014 -0700
+++ b/src/com/five_ten_sg/connectbot/transport/TransportFactory.java	Thu May 22 18:26:27 2014 -0700
@@ -36,6 +36,7 @@
 
     private static String[] transportNames = {
         SSH.getProtocolName(),
+        TN5250.getProtocolName(),
         Telnet.getProtocolName(),
         Local.getProtocolName(),
     };
@@ -48,6 +49,9 @@
         if (SSH.getProtocolName().equals(protocol)) {
             return new SSH();
         }
+        else if (TN5250.getProtocolName().equals(protocol)) {
+            return new TN5250();
+        }
         else if (Telnet.getProtocolName().equals(protocol)) {
             return new Telnet();
         }
@@ -63,17 +67,9 @@
         Log.d("TransportFactory", String.format(
                   "Attempting to discover URI for scheme=%s on input=%s", scheme,
                   input));
-
-        if (SSH.getProtocolName().equals(scheme))
-            return SSH.getUri(input);
-        else if (Telnet.getProtocolName().equals(scheme))
-            return Telnet.getUri(input);
-        else if (Local.getProtocolName().equals(scheme)) {
-            Log.d("TransportFactory", "Got to the local parsing area");
-            return Local.getUri(input);
-        }
-        else
-            return null;
+        AbsTransport t = getTransport(protocol);
+        if (t == null) return null;
+        return t.getUri(input);
     }
 
     public static String[] getTransportNames() {
@@ -88,13 +84,9 @@
     }
 
     public static boolean canForwardPorts(String protocol) {
-        // TODO uh, make this have less knowledge about its children
-        if (SSH.getProtocolName().equals(protocol)) {
-            return true;
-        }
-        else {
-            return false;
-        }
+        AbsTransport t = getTransport(protocol);
+        if (t == null) return false;
+        return t.canForwardPorts();
     }
 
     /**
@@ -103,18 +95,9 @@
      * @return expanded format hint
      */
     public static String getFormatHint(String protocol, Context context) {
-        if (SSH.getProtocolName().equals(protocol)) {
-            return SSH.getFormatHint(context);
-        }
-        else if (Telnet.getProtocolName().equals(protocol)) {
-            return Telnet.getFormatHint(context);
-        }
-        else if (Local.getProtocolName().equals(protocol)) {
-            return Local.getFormatHint(context);
-        }
-        else {
-            return AbsTransport.getFormatHint(context);
-        }
+        AbsTransport t = getTransport(protocol);
+        if (t == null) return "???";
+        return t.getFormatHint(context);
     }
 
     /**
--- a/src/org/tn5250j/framework/tn5250/tnvt.java	Thu May 22 16:11:14 2014 -0700
+++ b/src/org/tn5250j/framework/tn5250/tnvt.java	Thu May 22 18:26:27 2014 -0700
@@ -118,8 +118,8 @@
 	private static final byte ESC = 0x04; // 04
 
 	private Socket sock;
-	private BufferedInputStream bin;
-	private BufferedOutputStream bout;
+	public  BufferedInputStream bin;
+	public  BufferedOutputStream bout;
 	private final BlockingQueue<Object> dsq = new ArrayBlockingQueue<Object>(25);
 	private Stream5250 bk;
 	private DataStreamProducer producer;
@@ -172,9 +172,6 @@
      * @param manager
 	 */
 	public tnvt(Screen5250 screen52, boolean enhanced, boolean support132, TerminalBridge bridge, TerminalManager manager) {
-		if (log.isInfoEnabled()) {
-			log.info(" new session -> " + controller.getSessionName());
-		}
 		this.screen52   = screen52;
 		this.support132 = support132;
 		this.enhanced   = enhanced;