Android Open Source - LibGeppa Receive 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;
//from ww w  . j  a va2  s .com
import java.io.IOException;
import java.io.InputStream;

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

public class ReceiveThread<T extends IPacket> {
    private int mReceiveMessageId;

    private int mErrorMessageId;

    private Handler mHandler;

    private InputStream mInputStream;

    private IPacketFactory<T> mPacketFactory;

    private Thread mThread;

    private boolean mBuggyStream = false;

    public ReceiveThread(int receiveMessageId, int errorMessageId, Handler handler,
            InputStream inputStream, IPacketFactory<T> packetFactory) {
        super();
        mReceiveMessageId = receiveMessageId;
        mErrorMessageId = errorMessageId;
        mHandler = handler;
        mInputStream = inputStream;
        mPacketFactory = packetFactory;
    }

    public void startThread(String label) {
        startThread(label, false);
    }

    public void startThread(String label, boolean buggyStream) {
        mBuggyStream = buggyStream;
        mThread = new Thread("ReceiveThread:" + label) {
            @Override
            public void run() {
                super.run();
                IPacket packet;
                try {
                    while ((packet = mPacketFactory.readPacket(mInputStream)) != null) {
                        mHandler.obtainMessage(mReceiveMessageId, packet).sendToTarget();
                    }
                } catch (IOException e) {
                    mHandler.obtainMessage(mErrorMessageId).sendToTarget();
                } finally {
                    closeStream();
                }
            }
        };
        mThread.start();
    }

    public void stopThread() throws InterruptedException {
        closeStream();
        if (!mBuggyStream) {
            mThread.join();
        }
    }

    private synchronized void closeStream() {
        try {
            mInputStream.close();
        } catch (IOException e) {
            Log.w(Constants.TAG, e.getMessage());
        }
    }
}




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