view src/com/five_ten_sg/connectbot/transport/AbsTransport.java @ 400:e856ecf87b10

Backed out changeset aeb8c2e6d83a
author Carl Byington <carl@five-ten-sg.com>
date Mon, 20 Oct 2014 19:12:52 -0700
parents 2a7199ad90be
children
line wrap: on
line source

/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2007 Kenny Root, Jeffrey Sharkey
 *
 * 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.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.TerminalKeyListener;
import com.five_ten_sg.connectbot.service.TerminalManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import de.mud.terminal.vt320;

/**
 * @author Kenny Root
 *
 */
public abstract class AbsTransport {
    protected String          TAG;
    protected TerminalManager manager;
    protected TerminalBridge  bridge;
    protected String          homeDirectory;
    protected HostBean        host;
    protected vt320           buffer = null;
    protected String          emulation;

    class vt320Default extends vt320 {
        @Override
        public void debug(String s) {
            Log.d(TAG, s);
        }

        // monitor injecting a field
        //@Override
        //public void setField(int l, int c, char [] data)
        // implementation in the base vt320

        // terminal key listener found special key, send notification to monitor
        @Override
        public void monitorKey(boolean down) {
            if (bridge.monitor != null) bridge.monitor.keyState(down);
        }

        // terminal key listener sending to the host
        @Override
        public void write(byte[] b) {
            try {
                AbsTransport.this.write(b);
            }
            catch (IOException e) {
                Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
            }
        }
        @Override
        public void write(int b) {
            try {
                AbsTransport.this.write(b);
            }
            catch (IOException e) {
                Log.e(TAG, "Problem writing outgoing data in vt320() thread", e);
            }
        }

        // We don't use telnet sequences.
        @Override
        public void sendTelnetCommand(byte cmd) {
        }
        // We don't want remote to resize our window.
        @Override
        public void setWindowSize(int c, int r) {
        }
        // play beep noise
        @Override
        public void beep() {
            if ((bridge.parent != null) && (bridge.parent.isShown()))
                manager.playBeep();
            else
                manager.sendActivityNotification(host);
        }

        // test for changed screen contents
        @Override
        public void testChanged() {
            if (bridge.monitor != null) bridge.monitor.testChanged();
        }
        // relay socket writing to the screen
        // bridge.monitor placement of new characters
        @Override
        public void putChar(int c, int l, char ch, int attributes) {
            if (bridge.monitor != null) bridge.monitor.screenChanged(l, c);

            super.putChar(c, l, ch, attributes);
        }
        @Override
        public void insertChar(int c, int l, char ch, int attributes) {
            if (bridge.monitor != null) bridge.monitor.screenChanged(l, l, c, width - 1);

            super.insertChar(c, l, ch, attributes);
        }
        @Override
        public void insertLine(int l, int n, boolean scrollDown) {
            if (bridge.monitor != null) {
                if (scrollDown) bridge.monitor.screenChanged(l, height - 1, 0, width - 1);
                else            bridge.monitor.screenChanged(0, l, 0, width - 1);
            }

            super.insertLine(l, n, scrollDown);
        }
        @Override
        public void deleteLine(int l) {
            if (bridge.monitor != null) bridge.monitor.screenChanged(l, height - 1, 0, width - 1);

            super.deleteLine(l);
        }
        @Override
        public void deleteChar(int c, int l) {
            if (bridge.monitor != null) bridge.monitor.screenChanged(l, l, c, width - 1);

            super.deleteChar(c, l);
        }
        @Override
        public void setCursorPosition(int c, int l) {
            if (bridge.monitor != null) bridge.monitor.cursorMove(l, c);

            super.setCursorPosition(c, l);
        }

    };


    public AbsTransport() {}

    /**
     * @return protocol part of the URI
     */
    public static String getProtocolName() {
        return "unknown";
    }

    /**
     * Encode the current transport into a URI that can be passed via intent calls.
     * @return URI to host
     */
    public abstract Uri getUri(String input);


    /**
     * 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.
     */
    public abstract void connect();

    /**
     * Checks if read() will block. If there are no bytes remaining in
     * the underlying transport, return true.
     */
    public abstract boolean willBlock();

    /**
     * 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 abstract int read(byte[] buffer, int offset, int length) throws IOException;

    /**
     * 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 abstract 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 abstract void write(int c) throws IOException;

    /**
     * Flushes the write commands to the transport.
     * @throws IOException when there is a problem writing after connection
     */
    public abstract void flush() throws IOException;

    /**
     * Closes the connection to the terminal. Note that the resulting failure to read
     * should call {@link TerminalBridge#dispatchDisconnect(boolean)}.
     */
    public abstract void close();

    /**
     * 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
     */
    public abstract void setDimensions(int columns, int rows, int width, int height);

    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 String getEmulation() {
        return emulation;
    }

    protected vt320 setupTransportBuffer() {
        int scrollback = (host.getWantSession()) ?  manager.getScrollback() : 0;
        buffer.setBufferSize(scrollback);
        buffer.setDisplay(bridge);
        return buffer;
    }

    public vt320 getTransportBuffer() {
        buffer = new vt320Default();
        return setupTransportBuffer();
    }

    public void setLinks(TerminalManager manager, TerminalBridge bridge, String homeDirectory, HostBean host, String emulation) {
        this.manager       = manager;
        this.bridge        = bridge;
        this.homeDirectory = homeDirectory;
        this.host          = host;
        this.emulation     = emulation;
    }

    /**
     * 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;
    }


    /**
     * @return int default port for protocol
     */
    public abstract int getDefaultPort();
    public abstract boolean isConnected();
    public abstract boolean isSessionOpen();
    public abstract boolean isAuthenticated();

    /**
     * @param username
     * @param hostname
     * @param port
     * @return
     */
    public abstract String getDefaultNickname(String username, String hostname, int port);

    /**
     * @param uri
     * @param selectionKeys
     * @param selectionValues
     */
    public abstract void getSelectionArgs(Uri uri, Map<String, String> selection);

    /**
     * @param uri
     * @return
     */
    public abstract HostBean createHost(Uri uri);

    /**
     * @param context context containing the correct resources
     * @return string that hints at the format for connection
     */
    public abstract String getFormatHint(Context context);

    /**
     * @return do we use the network
     */
    public abstract boolean usesNetwork();

    /**
     * @return do we need a relay object to read from the transport
     *         and send the data into the vt320 buffer
     */
    public boolean needsRelay() {
        return true;
    }

    /**
     * @return a key listener
     */
    public TerminalKeyListener getTerminalKeyListener() {
        return new TerminalKeyListener(manager, bridge, buffer, host.getEncoding());
    }

}