diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java index f82f959697..8b98e0ab70 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -112,6 +112,7 @@ public class BinaryProtocol { communicationLoggingListener = linkManager.messageListener::postMessage; binaryProtocolLogger = new BinaryProtocolLogger(linkManager); + stream.addCloseListener(binaryProtocolLogger::close); } public boolean isClosed() { @@ -503,9 +504,6 @@ public class BinaryProtocol { } public void close() { - if (stream.isClosed()) - return; - binaryProtocolLogger.close(); stream.close(); } diff --git a/java_console/io/src/main/java/com/rusefi/io/IoStream.java b/java_console/io/src/main/java/com/rusefi/io/IoStream.java index 892f371c2d..e7522c980a 100644 --- a/java_console/io/src/main/java/com/rusefi/io/IoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/IoStream.java @@ -41,6 +41,8 @@ public interface IoStream extends WriteStream, Closeable, StreamStatistics { long latestActivityTime(); + void addCloseListener(Runnable listener); + Object getIoLock(); void onActivity(); diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/AbstractIoStream.java b/java_console/io/src/main/java/com/rusefi/io/serial/AbstractIoStream.java index c2df79cf5d..801b7429fd 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/AbstractIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/AbstractIoStream.java @@ -4,6 +4,7 @@ import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.io.IoStream; import java.io.IOException; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; public abstract class AbstractIoStream implements IoStream { @@ -15,6 +16,7 @@ public abstract class AbstractIoStream implements IoStream { protected final StreamStats streamStats = new StreamStats(); private final AtomicInteger bytesOut = new AtomicInteger(); private long latestActivity; + private final CopyOnWriteArrayList closeListeners = new CopyOnWriteArrayList<>(); public IncomingDataBuffer createDataBuffer() { IncomingDataBuffer incomingData = new IncomingDataBuffer(getClass().getSimpleName(), getStreamStats()); @@ -29,7 +31,16 @@ public abstract class AbstractIoStream implements IoStream { @Override public void close() { + if (isClosed) + return; isClosed = true; + for (Runnable listener : closeListeners) + listener.run(); + } + + @Override + public void addCloseListener(Runnable listener) { + closeListeners.add(listener); } @Override diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/BufferedSerialIoStream.java b/java_console/io/src/main/java/com/rusefi/io/serial/BufferedSerialIoStream.java index 48f1abddfe..a6326a7c90 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/BufferedSerialIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/BufferedSerialIoStream.java @@ -4,6 +4,7 @@ import com.devexperts.logging.Logging; import com.fazecast.jSerialComm.SerialPort; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.io.IoStream; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import static com.devexperts.logging.Logging.getLogging; @@ -21,7 +22,7 @@ public class BufferedSerialIoStream extends SerialIoStream { /** * @see #openPort(String) */ - private BufferedSerialIoStream(SerialPort sp, String port) { + private BufferedSerialIoStream(@NotNull SerialPort sp, String port) { super(sp, port); dataBuffer = createDataBuffer(); } diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java b/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java index 94609a2608..cfa8d496d9 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/SerialIoStream.java @@ -10,9 +10,11 @@ import com.rusefi.NamedThreadFactory; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.test.Bug3923; import com.rusefi.io.IoStream; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.util.Objects; import static com.devexperts.logging.Logging.getLogging; @@ -30,7 +32,7 @@ public abstract class SerialIoStream extends AbstractIoStream { SerialPortThreadFactory.set(new NamedThreadFactory("ECU SerialIoStream jSerialComm")); } - public SerialIoStream(@Nullable SerialPort sp, String port) { + public SerialIoStream(@NotNull SerialPort sp, String port) { this.sp = sp; this.port = port; } @@ -50,10 +52,11 @@ public abstract class SerialIoStream extends AbstractIoStream { @Override public void close() { + if (isClosed()) + return; log.info(port + ": Closing port..."); super.close(); - if (sp != null) - sp.closePort(); + sp.closePort(); log.info(port + ": Closed port."); }