view src/com/five_ten_sg/connectbot/service/TerminalMonitor.java @ 39:69bc44954bb5 tn5250

start tn5250 integration
author Carl Byington <carl@five-ten-sg.com>
date Tue, 10 Jun 2014 17:06:53 -0700
parents 0ce5cc452d02
children 1588e359a972 6b0f1ece1d91
line wrap: on
line source

package com.five_ten_sg.connectbot.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import de.mud.terminal.vt320;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.HashMap;

public class TerminalMonitor {
    public final static String TAG = "ConnectBot.TerminalMonitor";

    public  static final char MONITOR_CMD_INIT         = 0;
    public  static final char MONITOR_CMD_ACTIVATE     = 1;
    public  static final char MONITOR_CMD_HOSTDATA     = 2;
    public  static final char MONITOR_CMD_CURSORMOVE   = 3;
    public  static final char MONITOR_CMD_SCREENCHANGE = 4;
    public  static final char MONITOR_CMD_FIELDVALUE   = 5;
    public  static final char MONITOR_CMD_SETFIELD     = 5;
    public  static final char MONITOR_CMD_GETFIELD     = 6;
    public  static final char MONITOR_CMD_SCREENWATCH  = 7;
    public  static final char MONITOR_CMD_DEPRESS      = 8;

    private static final int    MONITORPORT       = 6000;
    private static final String LOCALHOST         = "127.0.0.1";

    private Context             parent       = null;
    private vt320               buffer       = null;
    private TerminalKeyListener keyListener  = null;
    private View                view         = null;
    private String              init         = null;
    private int                 start_line   = 0;       // monitor part of the screen for changes
    private int                 end_line     = 500;     // ""
    private int                 start_column = 0;       // ""
    private int                 end_column   = 500;     // ""
    private boolean             modified     = false;   // ""
    private HashMap<Integer,Integer>  keymap = null;
    private IBinder             bound        = null;
    private Socket              monitor_socket = null;
    private InputStream         monitor_in     = null;
    private OutputStream        monitor_out    = null;
    private MyReader            monitor_reader = null;
    private MyServiceConnection monitor_connection = new MyServiceConnection();

    class MyReader extends Thread {
        private InputStream monitor_in;
        private byte[] b;
        private boolean is_closing = false;

        public MyReader(InputStream monitor_in) {
            this.monitor_in = monitor_in;
            b = new byte[100];
            Log.i(TAG, "MyReader constructor");
        }

        public void closing() {
            is_closing = true;
        }

        private char[] forceRead(int len) throws IOException {
            int len2 = len * 2;
            int off  = 0;

            if (b.length < len2) b = new byte[len2];

            while (off < len2) {
                int l = monitor_in.read(b, off, len2 - off);

                if (l < 0) throw new IOException("eof");

                off += l;
            }

            return bytesToChars(b, len2);
        }

        public void run() {
            while (true) {
                try {
                    char[] len = forceRead(1);
                    char[] packet = forceRead(len[0]);
                    char cmd = packet[0];
                    Log.i(TAG, String.format("received %d command", (int)cmd));

                    switch (cmd) {
                        case MONITOR_CMD_SETFIELD:
                            if (packet.length >= 4)
                                setField(packet[1], packet[2], packet, 3);
                            break;

                        case MONITOR_CMD_GETFIELD:
                            if (packet.length == 4)
                                getField(packet[1], packet[2], packet[3]);
                            break;

                        case MONITOR_CMD_SCREENWATCH:
                            if (packet.length == 4)
                                screenWatch(packet[1], packet[2], packet[3]);
                            break;

                        case MONITOR_CMD_DEPRESS:
                            if (packet.length == 2)
                                depress(packet[1]);
                            break;

                        default:
                            break;
                    }
                }
                catch (IOException e) {
                    if (!is_closing) Log.e(TAG, "exception in MyReader.run()", e);

                    break;
                }
            }
        }
    }

    class MyServiceConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName className, IBinder service) {
            bound = service;
            Log.i(TAG, "bound to service");

            try {
                InetAddress serverAddr = InetAddress.getByName(LOCALHOST);
                monitor_socket = new Socket(serverAddr, MONITORPORT);
                monitor_in     = monitor_socket.getInputStream();
                monitor_out    = monitor_socket.getOutputStream();
                Log.i(TAG, "connected to monitor socket, send init " + init);
                monitor_reader = new MyReader(monitor_in);
                monitor_reader.start();
                String x = "  " + init;
                monitorWrite(MONITOR_CMD_INIT, x.toCharArray());
            }
            catch (IOException e) {
                Log.e(TAG, "exception in onServiceConnected()", e);
            }
        }
        public void onServiceDisconnected(ComponentName classNam) {
            bound = null;
            Log.i(TAG, "unbound from service");
        }
    };


    public TerminalMonitor(Context parent, vt320 buffer, TerminalKeyListener keyListener, View view, String init) {
        this.parent      = parent;
        this.buffer      = buffer;
        this.keyListener = keyListener;
        this.view        = view;
        this.init        = init;

        // setup the windows->android keymapping
        // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
        // http://developer.android.com/reference/android/view/KeyEvent.html
        keymap      = new HashMap<Integer,Integer>();
        keymap.put(0x08, KeyEvent.KEYCODE_BACK);        // vk_back
        keymap.put(0x09, KeyEvent.KEYCODE_TAB);         // vk_tab
        keymap.put(0x0d, KeyEvent.KEYCODE_ENTER);       // vk_return
        keymap.put(0x1b, KeyEvent.KEYCODE_ESCAPE);      // vk_escape
        keymap.put(0x21, KeyEvent.KEYCODE_PAGE_UP);     // vk_prior
        keymap.put(0x22, KeyEvent.KEYCODE_PAGE_DOWN);   // vk_next
        keymap.put(0x23, KeyEvent.KEYCODE_MOVE_END);    // vk_end
        keymap.put(0x24, KeyEvent.KEYCODE_MOVE_HOME);   // vk_home
        keymap.put(0x25, KeyEvent.KEYCODE_DPAD_LEFT);   // vk_left
        keymap.put(0x26, KeyEvent.KEYCODE_DPAD_UP);     // vk_up
        keymap.put(0x27, KeyEvent.KEYCODE_DPAD_RIGHT);  // vk_right
        keymap.put(0x28, KeyEvent.KEYCODE_DPAD_DOWN);   // vk_down
        keymap.put(0x2d, KeyEvent.KEYCODE_INSERT);      // vk_insert
        keymap.put(0x2e, KeyEvent.KEYCODE_DEL);         // vk_delete
        keymap.put(0x70, KeyEvent.KEYCODE_F1);          // vk_f1
        keymap.put(0x71, KeyEvent.KEYCODE_F2);          // vk_f2
        keymap.put(0x72, KeyEvent.KEYCODE_F3);          // vk_f3
        keymap.put(0x73, KeyEvent.KEYCODE_F4);          // vk_f4
        keymap.put(0x74, KeyEvent.KEYCODE_F5);          // vk_f5
        keymap.put(0x75, KeyEvent.KEYCODE_F6);          // vk_f6
        keymap.put(0x76, KeyEvent.KEYCODE_F7);          // vk_f7
        keymap.put(0x77, KeyEvent.KEYCODE_F8);          // vk_f8
        keymap.put(0x78, KeyEvent.KEYCODE_F9);          // vk_f9
        keymap.put(0x79, KeyEvent.KEYCODE_F10);         // vk_f10
        keymap.put(0x7a, KeyEvent.KEYCODE_F11);         // vk_f11
        keymap.put(0x7b, KeyEvent.KEYCODE_F12);         // vk_f12

        // bind to the monitor service
        Intent intent = new Intent("com.five_ten_sg.connectbot.monitor.MonitorService");
        parent.bindService(intent, monitor_connection, Context.BIND_AUTO_CREATE);
        Log.i(TAG, "constructor");
    }


    public void Disconnect() {
        if (monitor_reader != null) monitor_reader.closing();

        try {
            if (monitor_out    != null) monitor_out.close();

            if (monitor_in     != null) monitor_in.close();

            if (monitor_socket != null) monitor_socket.close();

            Log.i(TAG, "disconnected from monitor socket");
        }
        catch (IOException e) {
            Log.e(TAG, "exception in Disconnect() closing sockets", e);
        }

        monitor_reader = null;
        monitor_out    = null;
        monitor_in     = null;
        monitor_socket = null;

        if (bound != null) parent.unbindService(monitor_connection);

        monitor_connection = null;
    }


    public char[] bytesToChars(byte[] b, int len) {
        char[] c = new char[len >> 1];
        int bp = 0;

        for (int i = 0; i < c.length; i++) {
            byte b1 = b[bp++];
            byte b2 = b[bp++];
            c[i] = (char)(((b1 & 0x00FF) << 8) + (b2 & 0x00FF));
        }

        return c;
    }


    public byte[] charsToBytes(char[] c) {
        byte[] b = new byte[c.length << 1];
        int bp = 0;

        for (int i = 0; i < c.length; i++) {
            b[bp++] = (byte)((c[i] & 0xff00) >> 8);
            b[bp++] = (byte)(c[i] & 0x00ff);
        }

        return b;
    }


    public synchronized void monitorWrite(char cmd, char[] c) {
        try {
            if (monitor_out != null) {
                c[0] = (char)(c.length - 1);    // number of chars following
                c[1] = cmd;
                //Log.i(TAG, String.format("sending %d command", (int)cmd));
                monitor_out.write(charsToBytes(c));
            }
        }
        catch (IOException e) {
            Log.i(TAG, "exception in monitorWrite(), monitor died or closed the socket", e);

            try {
                monitor_out.close();
            }
            catch (IOException ee) {
                Log.e(TAG, "exception in monitorWrite() closing output stream", ee);
            }

            monitor_out = null;
        }
    };

    public void sendScreen(char cmd) {
        char lines   = (char)(buffer.height & 0x0000ffff);
        char columns = (char)(buffer.width & 0x0000ffff);
        char[] arg = new char[4 + lines * columns];
        arg[2] = lines;
        arg[3] = columns;
        int base = 4;
        for (int i = 0; i < lines; i++) {
            System.arraycopy(buffer.charArray[buffer.screenBase + i], 0, arg, base, columns);
            base += columns;
        }
        monitorWrite(cmd, arg);
    }

    public synchronized void activate() {
        sendScreen(MONITOR_CMD_ACTIVATE);
    }

    public synchronized void hostData(byte[] b) {
        for (int i = 0; i < b.length; i++) {
            hostData((int)b[i] & 0xff);
        }
    }

    public synchronized void hostData(int b) {
        char[] arg = new char[3];
        arg[2] = (char)(b & 0x0000ffff);
        monitorWrite(MONITOR_CMD_HOSTDATA, arg);
    }

    public synchronized void cursorMove(int l, int c) {
        char[] arg = new char[4];
        arg[2] = (char)(l & 0x0000ffff);
        arg[3] = (char)(c & 0x0000ffff);
        monitorWrite(MONITOR_CMD_CURSORMOVE, arg);
    }

    public synchronized void testChanged() {
        if (modified) {
            modified = false;
            sendScreen(MONITOR_CMD_SCREENCHANGE);
        }
    }

    public synchronized void screenChanged(int llow, int lhigh, int clow, int chigh) {
        if ((start_line <= lhigh) && (llow <= end_line) && (start_column <= chigh) && (clow <= end_column)) {
            modified = true;
        }
    }

    public synchronized void screenChanged(int l, int c) {
        screenChanged(l, l, c, c);
    }

    public synchronized void setField(int l, int c, char[] data, int offset) {
        Log.i(TAG, "setField()");
        byte[] d = charsToBytes(data);
        byte[] b = new byte[d.length - offset];
        System.arraycopy(d, offset, b, 0, d.length - offset);
        buffer.write(b);
    }

    public synchronized void getField(int l, int c, int len) {
        Log.i(TAG, "getField()");
        char[] arg2 = new char[4 + len];
        arg2[2] = (char)(l & 0x0000ffff);
        arg2[3] = (char)(c & 0x0000ffff);
        int base = 4;
        System.arraycopy(buffer.charArray[buffer.screenBase + l], c, arg2, base, len);
        monitorWrite(MONITOR_CMD_FIELDVALUE, arg2);
    }

    public synchronized void screenWatch(int l, int c, int len) {
        Log.i(TAG, "screenWatch()");
        start_line   = l;
        end_line     = l;
        start_column = c;
        end_column   = c + len - 1;
    }

    public synchronized void depress(int vk_key) {
        Log.i(TAG, String.format("depress() %d", vk_key));
        Integer x = keymap.get(new Integer(vk_key));
        if (x != null) {
            KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, x.intValue());
            keyListener.onKey(view, event.getKeyCode(), event);
        }
    }
}