diff --git a/java_console/io/src/main/java/com/rusefi/io/ByteReader.java b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java new file mode 100644 index 0000000000..ecec38c3c8 --- /dev/null +++ b/java_console/io/src/main/java/com/rusefi/io/ByteReader.java @@ -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; +} diff --git a/java_console/io/src/main/java/com/rusefi/io/LinkManager.java b/java_console/io/src/main/java/com/rusefi/io/LinkManager.java index 4acb26b63c..5697e6793f 100644 --- a/java_console/io/src/main/java/com/rusefi/io/LinkManager.java +++ b/java_console/io/src/main/java/com/rusefi/io/LinkManager.java @@ -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 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, diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java index 061ea428c7..e4af4ad379 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java @@ -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