steps towards Android
This commit is contained in:
parent
4f47b26f8f
commit
465d61ba35
|
@ -0,0 +1,45 @@
|
|||
package com.rusefi.io;
|
||||
|
||||
import com.opensr5.Logger;
|
||||
import com.opensr5.io.DataListener;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public interface ByteReader {
|
||||
static void runReaderLoop(DataListener listener, ByteReader reader, Logger logger) {
|
||||
/**
|
||||
* Threading of the whole input/output does not look healthy at all!
|
||||
*
|
||||
* @see #COMMUNICATION_EXECUTOR
|
||||
*/
|
||||
Executor threadExecutor = Executors.newSingleThreadExecutor(r -> {
|
||||
Thread t = new Thread(r);
|
||||
t.setName("IO executor thread");
|
||||
t.setDaemon(true); // need daemon thread so that COM thread is also daemon
|
||||
return t;
|
||||
});
|
||||
|
||||
threadExecutor.execute(() -> {
|
||||
Thread.currentThread().setName("TCP connector loop");
|
||||
logger.info("Running TCP connection loop");
|
||||
|
||||
byte inputBuffer[] = new byte[256];
|
||||
while (true) {
|
||||
try {
|
||||
int result = reader.read(inputBuffer);
|
||||
if (result == -1)
|
||||
throw new IOException("TcpIoStream: End of input?");
|
||||
listener.onDataArrived(Arrays.copyOf(inputBuffer, result));
|
||||
} catch (IOException e) {
|
||||
System.err.println("TcpIoStream: End of connection");
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
int read(byte[] buffer) throws IOException;
|
||||
}
|
|
@ -116,25 +116,10 @@ public class LinkManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Threading of the whole input/output does not look healthy at all!
|
||||
*
|
||||
* @see #COMMUNICATION_EXECUTOR
|
||||
*/
|
||||
public final Executor TCP_READ_EXECUTOR = Executors.newSingleThreadExecutor(new ThreadFactory() {
|
||||
@Override
|
||||
public Thread newThread(@NotNull Runnable r) {
|
||||
Thread t = new Thread(r);
|
||||
t.setName("IO executor thread");
|
||||
t.setDaemon(true); // need daemon thread so that COM thread is also daemon
|
||||
return t;
|
||||
}
|
||||
});
|
||||
public final LinkedBlockingQueue<Runnable> COMMUNICATION_QUEUE = new LinkedBlockingQueue<>();
|
||||
/**
|
||||
* All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner
|
||||
*
|
||||
* @see #TCP_READ_EXECUTOR
|
||||
*/
|
||||
public final ExecutorService COMMUNICATION_EXECUTOR = new ThreadPoolExecutor(1, 1,
|
||||
0L, TimeUnit.MILLISECONDS,
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.rusefi.io.tcp;
|
|||
|
||||
import com.opensr5.Logger;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.io.ByteReader;
|
||||
import com.rusefi.io.IoStream;
|
||||
import com.rusefi.io.LinkManager;
|
||||
|
||||
|
@ -10,7 +11,6 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Andrey Belomutskiy, (c) 2013-2020
|
||||
|
@ -51,27 +51,9 @@ public class TcpIoStream implements IoStream {
|
|||
|
||||
@Override
|
||||
public void setInputListener(final DataListener listener) {
|
||||
linkManager.TCP_READ_EXECUTOR.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Thread.currentThread().setName("TCP connector loop");
|
||||
logger.info("Running TCP connection loop");
|
||||
|
||||
byte inputBuffer[] = new byte[256];
|
||||
while (true) {
|
||||
try {
|
||||
int result = input.read(inputBuffer);
|
||||
if (result == -1)
|
||||
throw new IOException("TcpIoStream: End of input?");
|
||||
listener.onDataArrived(Arrays.copyOf(inputBuffer, result));
|
||||
} catch (IOException e) {
|
||||
System.err.println("TcpIoStream: End of connection");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
ByteReader reader = buffer -> input.read(buffer);
|
||||
|
||||
ByteReader.runReaderLoop(listener, reader, logger);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue