Android Open Source - LibGeppa Client Thread






From Project

Back to project page LibGeppa.

License

The source code is released under:

Apache License

If you think the Android project LibGeppa listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.cattaka.libgeppa.thread;
// w  w  w  .j a va 2 s .  c  o m
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import net.cattaka.libgeppa.Constants;
import net.cattaka.libgeppa.data.IPacket;
import net.cattaka.libgeppa.data.IPacketFactory;
import android.util.Log;

public class ClientThread<T extends IPacket> extends Thread {
    public interface IClientThreadListener<T extends IPacket> {
        /**
         * Note : This method is called from ClientReceiveThread, it's not UI
         * thread.
         */
        public void onReceivePacket(ClientThread<T> target, T packet);

        /**
         * Note : This method is called from this thread, it's not UI thread.
         */
        public void onDisconnected(ClientThread<T> target);
    }

    private static class MyEvent {
        private int eventCode;

        private Object data;

        public MyEvent(int eventCode, Object data) {
            super();
            this.eventCode = eventCode;
            this.data = data;
        }
    }

    private static class ClientReceiveThread<T extends IPacket> extends Thread {
        private ClientThread<T> mParent;

        private InputStream mInputStream;

        private IPacketFactory<T> mPacketFactory;

        private IClientThreadListener<T> mListener;

        public ClientReceiveThread(ClientThread<T> parent, InputStream inputStream,
                IPacketFactory<T> packetFactory, IClientThreadListener<T> listener) {
            super("ClientReceiveThread:" + parent);
            mParent = parent;
            mInputStream = inputStream;
            mPacketFactory = packetFactory;
            mListener = listener;
        }

        @Override
        public void run() {
            super.run();
            try {
                while (true) {
                    T packet = mPacketFactory.readPacket(mInputStream);
                    if (packet != null) {
                        mListener.onReceivePacket(mParent, packet);
                    }
                }
            } catch (IOException e) {
                // ignore
            }
            mParent.mEventQueue.add(new MyEvent(0, null));
        }
    }

    private Socket mSocket;

    private IPacketFactory<T> mPacketFactory;

    private IClientThreadListener<T> mListener;

    private BlockingQueue<MyEvent> mEventQueue;

    public ClientThread(Socket socket, IPacketFactory<T> packetFactory,
            IClientThreadListener<T> listener) {
        super("ClientThread:" + socket);
        mSocket = socket;
        mPacketFactory = packetFactory;
        mListener = listener;
        mEventQueue = new LinkedBlockingQueue<ClientThread.MyEvent>();
    }

    @Override
    public void run() {
        super.run();
        ClientReceiveThread<T> receiveThread = null;
        try {
            InputStream in = mSocket.getInputStream();
            OutputStream out = mSocket.getOutputStream();
            { // Creates receiving thread
              // Note: The receiving thread will stop on mSocket.close() in
              // finally block.
                receiveThread = new ClientReceiveThread<T>(this, in, mPacketFactory, mListener);
                receiveThread.start();
            }

            while (true) {
                MyEvent event = mEventQueue.take();
                if (event.eventCode == 0) {
                    break;
                } else if (event.eventCode == 1) {
                    @SuppressWarnings("unchecked")
                    T packet = (T)event.data;
                    mPacketFactory.writePacket(out, packet);
                    out.flush();
                }
            }
        } catch (InterruptedException e) {
            // Impossible
            Log.w(Constants.TAG, e.getMessage(), e);
        } catch (IOException e) {
            // ignore
        } finally {
            mListener.onDisconnected(this);
            try {
                mSocket.close();
            } catch (IOException e) {
                // Impossible
                Log.w(Constants.TAG, e.getMessage(), e);
            }
            if (receiveThread != null) {
                try {
                    receiveThread.join();
                } catch (InterruptedException e) {
                    // Impossible
                    Log.w(Constants.TAG, e.getMessage(), e);
                }
            }
        }
    }

    public void sendPacket(T packet) {
        mEventQueue.add(new MyEvent(1, packet));
    }

    public String getLabel() {
        return String.valueOf(mSocket.getRemoteSocketAddress());
    }

    public void stopThread() {
        mEventQueue.add(new MyEvent(0, null));
        try {
            mSocket.close();
        } catch (IOException e) {
            // Impossible
            Log.w(Constants.TAG, e.getMessage(), e);
        }
        try {
            this.join();
        } catch (InterruptedException e) {
            // Impossible
            Log.w(Constants.TAG, e.getMessage(), e);
        }
    }
}




Java Source Code List

net.cattaka.libgeppa.ActiveGeppaService.java
net.cattaka.libgeppa.AdkPassiveGeppaService.java
net.cattaka.libgeppa.BluetoothPassiveGeppaService.java
net.cattaka.libgeppa.Constants.java
net.cattaka.libgeppa.GeppaService.java
net.cattaka.libgeppa.IRawSocket.java
net.cattaka.libgeppa.PassiveGeppaService.java
net.cattaka.libgeppa.adapter.AbsConnectionAdapter.java
net.cattaka.libgeppa.adapter.DummyDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceAdapterListener.java
net.cattaka.libgeppa.adapter.IDeviceAdapter.java
net.cattaka.libgeppa.adapter.IDeviceCommandAdapter.java
net.cattaka.libgeppa.adapter.LocalDeviceAdapter.java
net.cattaka.libgeppa.adapter.RemoteDeviceAdapter.java
net.cattaka.libgeppa.binder.ActiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.PassiveGeppaServiceFuncs.java
net.cattaka.libgeppa.binder.async.ActiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.binder.async.AsyncInterfaceException.java
net.cattaka.libgeppa.binder.async.PassiveGeppaServiceFuncsAsync.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterFactory.java
net.cattaka.libgeppa.bluetooth.BluetoothAdapterWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothDeviceWrapper.java
net.cattaka.libgeppa.bluetooth.BluetoothSocketWrapper.java
net.cattaka.libgeppa.bluetooth.IBluetoothAdapter.java
net.cattaka.libgeppa.bluetooth.IBluetoothDevice.java
net.cattaka.libgeppa.bluetooth.IBluetoothSocket.java
net.cattaka.libgeppa.data.BaudRate.java
net.cattaka.libgeppa.data.ConnectionCode.java
net.cattaka.libgeppa.data.ConnectionState.java
net.cattaka.libgeppa.data.DeviceEventCode.java
net.cattaka.libgeppa.data.DeviceInfo.java
net.cattaka.libgeppa.data.DeviceState.java
net.cattaka.libgeppa.data.IPacketFactory.java
net.cattaka.libgeppa.data.IPacket.java
net.cattaka.libgeppa.data.PacketWrapper.java
net.cattaka.libgeppa.data.SocketState.java
net.cattaka.libgeppa.exception.NotImplementedException.java
net.cattaka.libgeppa.net.DummySocketPrepareTask.java
net.cattaka.libgeppa.net.DummySocket.java
net.cattaka.libgeppa.net.PhysicaloidSocketPrepareTask.java
net.cattaka.libgeppa.net.PhysicaloidSocket.java
net.cattaka.libgeppa.net.RemoteSocketPrepareTask.java
net.cattaka.libgeppa.net.RemoteSocket.java
net.cattaka.libgeppa.net.UsbClass.java
net.cattaka.libgeppa.passive.AdkPassiveReceiver.java
net.cattaka.libgeppa.passive.BluetoothPassiveReceiver.java
net.cattaka.libgeppa.passive.IPassiveReceiverListener.java
net.cattaka.libgeppa.passive.IPassiveReceiver.java
net.cattaka.libgeppa.socket.AdkRawSocket.java
net.cattaka.libgeppa.socket.BtRawSocket.java
net.cattaka.libgeppa.thread.ClientThread.java
net.cattaka.libgeppa.thread.ConnectionThread.java
net.cattaka.libgeppa.thread.IConnectionThreadListener.java
net.cattaka.libgeppa.thread.ReceiveThread.java
net.cattaka.libgeppa.thread.ServerThread.java
net.cattaka.libgeppa.util.AidlUtil.java
net.cattaka.libgeppa.util.DeviceUtil.java