From cc17df5f450a7aaba9e03952d4000ac1acb575d9 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Tue, 6 Oct 2020 23:37:44 -0400 Subject: [PATCH] remote SD download works once proper thread synchronization added --- .../io/src/main/java/com/rusefi/io/IoStream.java | 8 ++++++++ .../proxy/client/LocalApplicationProxy.java | 7 ++----- .../com/rusefi/ts_plugin/SdCardReaderPanel.java | 16 ++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) 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 02d87479d5..04ba8ebc00 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 @@ -92,4 +92,12 @@ public interface IoStream extends WriteStream, Closeable, StreamStatistics { default short readShort() throws EOFException { return getDataBuffer().readShort(); } + + default byte[] sendAndGetPacket(byte[] packet, String message, boolean allowLongResponse) throws IOException { + // synchronization is needed for example to help SD card download to live with gauge poker + synchronized (this) { + sendPacket(packet); + return getDataBuffer().getPacket(message, allowLongResponse); + } + } } diff --git a/java_console/io/src/main/java/com/rusefi/proxy/client/LocalApplicationProxy.java b/java_console/io/src/main/java/com/rusefi/proxy/client/LocalApplicationProxy.java index e7abe8bbf7..ddef9449fe 100644 --- a/java_console/io/src/main/java/com/rusefi/proxy/client/LocalApplicationProxy.java +++ b/java_console/io/src/main/java/com/rusefi/proxy/client/LocalApplicationProxy.java @@ -113,11 +113,8 @@ public class LocalApplicationProxy implements Closeable { sleep(context.gaugePokingPeriod()); byte[] commandPacket = GetOutputsCommand.createRequest(); - synchronized (authenticatorToProxyStream) { - authenticatorToProxyStream.sendPacket(commandPacket); - // we do not really need the data, we just need to take response from the socket - authenticatorToProxyStream.readPacket(); - } + // we do not really need the data, we just need to take response from the socket + authenticatorToProxyStream.sendAndGetPacket(commandPacket, "Gauge Poker", false); } if (isTimeForApplicationToConnect(context, start) && relayCommandCounter.get() < 4) { diff --git a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/SdCardReaderPanel.java b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/SdCardReaderPanel.java index 71f0490e39..40ac67591e 100644 --- a/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/SdCardReaderPanel.java +++ b/java_tools/ts_plugin/src/main/java/com/rusefi/ts_plugin/SdCardReaderPanel.java @@ -82,8 +82,7 @@ public class SdCardReaderPanel { packet = new byte[3]; packet[0] = Fields.TS_SD_R_COMMAND; packet[2] = Fields.TS_SD_PROTOCOL_RTC; - stream.sendPacket(packet); - response = stream.getDataBuffer().getPacket("RTC status"); + response = stream.sendAndGetPacket(packet, "RTC status", false); log.info("RTC response " + IoStream.printHexBinary(response)); if (response == null) throw new IOException("RTC No packet"); @@ -92,8 +91,7 @@ public class SdCardReaderPanel { packet[0] = Fields.TS_SD_W_COMMAND; packet[2] = TS_SD_PROTOCOL_FETCH_INFO; packet[6] = Fields.TS_SD_PROTOCOL_READ_DIR; - stream.sendPacket(packet); - response = stream.getDataBuffer().getPacket("read dir command"); + response = stream.sendAndGetPacket(packet, "read dir command", false); if (response == null) throw new IOException("Read Dir No packet"); log.info("read dir command " + IoStream.printHexBinary(response)); @@ -104,8 +102,7 @@ public class SdCardReaderPanel { packet[2] = TS_SD_PROTOCOL_FETCH_INFO; packet[5] = 0x02; packet[6] = 0x02; - stream.sendPacket(packet); - response = stream.getDataBuffer().getPacket("read command", true); + response = stream.sendAndGetPacket(packet, "read command", true); if (response == null) throw new IOException("No packet"); log.info("read command " + IoStream.printHexBinary(response)); @@ -130,8 +127,7 @@ public class SdCardReaderPanel { FileOutputStream fos = null; try { - stream.sendPacket(packet); - byte[] response = stream.getDataBuffer().getPacket("Download file"); + byte[] response = stream.sendAndGetPacket(packet, "Download file", false); log.info("Download file " + IoStream.printHexBinary(response)); setStatus("Downloading " + fileName); @@ -146,8 +142,8 @@ public class SdCardReaderPanel { packet[3] = (byte) chunk; packet[4] = (byte) (chunk >> 8); - stream.sendPacket(packet); - response = stream.getDataBuffer().getPacket("Get file", true); + // we are competing with gauge poking thread for instance + response = stream.sendAndGetPacket(packet, "Get file", true); if (response == null) { log.info("No content response");