mirror of
https://github.com/RfidResearchGroup/RFIDtools.git
synced 2026-05-12 11:19:59 -07:00
Communication rebuild(worked).
This commit is contained in:
@@ -17,24 +17,27 @@ public abstract class DeviceChecker implements Serializable, Closeable {
|
||||
this.communication = communication;
|
||||
}
|
||||
|
||||
private void initCom() {
|
||||
// Auto init communication to mapper.
|
||||
LocalComBridgeAdapter.getInstance()
|
||||
.setInputStream(communication.getInput())
|
||||
.setOutputStream(communication.getOutput())
|
||||
.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a device is can work!
|
||||
*
|
||||
* @return if can work return true, if no, return false
|
||||
*/
|
||||
public boolean check() throws IOException {
|
||||
initCom();
|
||||
return false;
|
||||
public final boolean check() throws IOException {
|
||||
// Auto init communication to mapper.
|
||||
LocalComBridgeAdapter.getInstance()
|
||||
.setInputStream(communication.getInput())
|
||||
.setOutputStream(communication.getOutput())
|
||||
.startServer();
|
||||
if (!checkDevice()) {
|
||||
// if check failed, we must to close device!
|
||||
LocalComBridgeAdapter.getInstance().stopClient();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract boolean checkDevice() throws IOException;
|
||||
|
||||
/**
|
||||
* Close the device(Should no close communication. only close device!)
|
||||
*/
|
||||
|
||||
@@ -25,9 +25,9 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
* */
|
||||
|
||||
// The namespace of the LocalServerSocket
|
||||
public static final String NAMESPACE = "CN.DXL.ComBridgeAdapter.2020_0413";
|
||||
public static final String NAMESPACE = "LocalComBridgeAdapter";
|
||||
// The tag of the log.
|
||||
public static final String LOG_TAG = "ComBridgeAdapter";
|
||||
public static final String LOG_TAG = "LocalComBridgeAdapter";
|
||||
// 本地套接字服务!
|
||||
private LocalServerSocket serverSocket;
|
||||
// 单例!
|
||||
@@ -38,8 +38,12 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
private OutputStream mOutputStream;
|
||||
// 是否已经连接!
|
||||
private volatile boolean isHasClient = false;
|
||||
// 是否关闭监听!
|
||||
private volatile boolean listenAccept = false;
|
||||
// 暂停转发!
|
||||
private boolean pause = false;
|
||||
// 连接到转发服务的客户端!
|
||||
private LocalSocket socket;
|
||||
|
||||
private LocalComBridgeAdapter() {
|
||||
// No instantiation is required
|
||||
@@ -52,20 +56,30 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
private class WorkThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
while (listenAccept) {
|
||||
try {
|
||||
LocalSocket socket = serverSocket.accept();
|
||||
if (isHasClient) {
|
||||
Log.e(LOG_TAG, "The server only once online supported.");
|
||||
Log.e(LOG_TAG, "please disconnect your previous con.");
|
||||
continue;
|
||||
if (serverSocket != null) {
|
||||
socket = serverSocket.accept();
|
||||
if (isHasClient) {
|
||||
isHasClient = false;
|
||||
Log.e(LOG_TAG, "The server only once online supported.");
|
||||
Log.e(LOG_TAG, "please disconnect your previous con.");
|
||||
continue;
|
||||
}
|
||||
isHasClient = true;
|
||||
Thread socket2Device = new DataThread(socket, mOutputStream, socket.getInputStream());
|
||||
Thread device2Socket = new DataThread(socket, socket.getOutputStream(), mInputStream);
|
||||
// start task!
|
||||
device2Socket.start();
|
||||
socket2Device.start();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
isHasClient = true;
|
||||
new DataThread(socket, mOutputStream, socket.getInputStream()).start();
|
||||
new DataThread(socket, socket.getOutputStream(), mInputStream).start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
isHasClient = false;
|
||||
listenAccept = false;
|
||||
Log.d(LOG_TAG, "链接线程终止!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -73,10 +87,10 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
}
|
||||
|
||||
class DataThread extends Thread {
|
||||
private LocalSocket socket;
|
||||
private OutputStream os;
|
||||
private InputStream is;
|
||||
private byte[] buffer = new byte[1024];
|
||||
private byte[] buffer = new byte[1024 * 500];
|
||||
private LocalSocket socket;
|
||||
|
||||
DataThread(LocalSocket socket, OutputStream os, InputStream is) {
|
||||
this.os = os;
|
||||
@@ -88,15 +102,17 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
public void run() {
|
||||
while (isHasClient) {
|
||||
try {
|
||||
if (socket.getFileDescriptor() == null) {
|
||||
throw new IOException("Socket disconnected!");
|
||||
}
|
||||
if (pause) {
|
||||
Log.e(LOG_TAG, "pausing!");
|
||||
continue;
|
||||
}
|
||||
if (os != null && is != null) {
|
||||
int len = is.read(buffer);
|
||||
if (len != -1) {
|
||||
if (len > 0) {
|
||||
os.write(Arrays.copyOf(buffer, len));
|
||||
os.flush();
|
||||
}
|
||||
} else {
|
||||
throw new IOException("IO closed.");
|
||||
@@ -104,12 +120,6 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
isHasClient = false;
|
||||
try {
|
||||
socket.shutdownInput();
|
||||
socket.shutdownOutput();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -118,8 +128,9 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
stop();
|
||||
protected void finalize() {
|
||||
stopClient();
|
||||
stopServer();
|
||||
}
|
||||
|
||||
public static LocalComBridgeAdapter getInstance() {
|
||||
@@ -149,27 +160,48 @@ public final class LocalComBridgeAdapter implements Serializable {
|
||||
Log.d(LOG_TAG, "ComBridgeAdapter pause!");
|
||||
}
|
||||
|
||||
public LocalComBridgeAdapter start() {
|
||||
public LocalComBridgeAdapter startServer() {
|
||||
synchronized (LocalComBridgeAdapter.class) {
|
||||
try {
|
||||
if (serverSocket != null) stop();
|
||||
serverSocket = new LocalServerSocket(NAMESPACE);
|
||||
new WorkThread().start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(LOG_TAG, "If you see an error message like \"Address already in use\", check that you call the stop function");
|
||||
Log.e(LOG_TAG, "如果你看到了类似Address already in use的错误消息,请检查你是否调用停止函数");
|
||||
if (!listenAccept) {
|
||||
listenAccept = true;
|
||||
try {
|
||||
if (serverSocket == null) {
|
||||
serverSocket = new LocalServerSocket(NAMESPACE);
|
||||
}
|
||||
new WorkThread().start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(LOG_TAG, "If you see an error message like \"Address already in use\", check that you call the stopServer function");
|
||||
Log.e(LOG_TAG, "如果你看到了类似Address already in use的错误消息,请检查你是否调用停止函数");
|
||||
}
|
||||
Log.d(LOG_TAG, "ComBridgeAdapter start!");
|
||||
}
|
||||
}
|
||||
Log.d(LOG_TAG, "ComBridgeAdapter start!");
|
||||
return this;
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
public void stopServer() {
|
||||
listenAccept = false;
|
||||
if (serverSocket != null) {
|
||||
try {
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
serverSocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void stopClient() {
|
||||
isHasClient = false;
|
||||
try {
|
||||
if (serverSocket != null)
|
||||
serverSocket.close();
|
||||
|
||||
if (socket != null) {
|
||||
socket.shutdownInput();
|
||||
socket.shutdownOutput();
|
||||
socket.close();
|
||||
socket = null;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.felhr.usbserial.SerialInputStream;
|
||||
import com.felhr.usbserial.SerialOutputStream;
|
||||
import com.felhr.usbserial.UsbSerialDevice;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -163,15 +165,21 @@ public class UsbSerialControl implements DriverInterface<String, UsbManager> {
|
||||
|
||||
@Override
|
||||
public OutputStream getOutput() {
|
||||
if (mPort != null)
|
||||
return mPort.getOutputStream();
|
||||
if (mPort != null) {
|
||||
SerialOutputStream os = mPort.getOutputStream();
|
||||
os.setTimeout(0);
|
||||
return os;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInput() {
|
||||
if (mPort != null)
|
||||
return mPort.getInputStream();
|
||||
if (mPort != null) {
|
||||
SerialInputStream is = mPort.getInputStream();
|
||||
is.setTimeout(0);
|
||||
return is;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -12,10 +12,11 @@ public class EmptyDeivce extends DeviceChecker {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check() throws IOException {
|
||||
protected boolean checkDevice() throws IOException {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user