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 3de4a74333..c367ba56be 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 @@ -228,7 +228,7 @@ public class BinaryProtocol { * * @return true if everything fine */ - public boolean connectAndReadConfiguration(DataListener listener) { + public boolean connectAndReadConfiguration(Arguments arguments, DataListener listener) { try { signature = getSignature(stream); log.info("Got " + signature + " signature"); @@ -236,7 +236,7 @@ public class BinaryProtocol { } catch (IOException e) { return false; } - readImage(Fields.TOTAL_CONFIG_SIZE); + readImage(arguments, Fields.TOTAL_CONFIG_SIZE); if (isClosed) return false; @@ -340,11 +340,11 @@ public class BinaryProtocol { /** * read complete tune from physical data stream */ - public void readImage(int size) { + public void readImage(Arguments arguments, int size) { ConfigurationImage image = getAndValidateLocallyCached(); if (image == null) { - image = readFullImageFromController(size); + image = readFullImageFromController(arguments, size); if (image == null) return; } @@ -362,7 +362,7 @@ public class BinaryProtocol { } @Nullable - private ConfigurationImage readFullImageFromController(int size) { + private ConfigurationImage readFullImageFromController(Arguments arguments, int size) { ConfigurationImage image; image = new ConfigurationImage(size); @@ -398,12 +398,14 @@ public class BinaryProtocol { offset += requestSize; } - try { - ConfigurationImageFile.saveToFile(image, CONFIGURATION_RUSEFI_BINARY); - Msq tune = MsqFactory.valueOf(image); - tune.writeXmlFile(CONFIGURATION_RUSEFI_XML); - } catch (Exception e) { - System.err.println("Ignoring " + e); + if (arguments.saveFile) { + try { + ConfigurationImageFile.saveToFile(image, CONFIGURATION_RUSEFI_BINARY); + Msq tune = MsqFactory.valueOf(image); + tune.writeXmlFile(CONFIGURATION_RUSEFI_XML); + } catch (Exception e) { + System.err.println("Ignoring " + e); + } } return image; } 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 c6d8cc4406..d62f0d4159 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 @@ -32,7 +32,7 @@ public class BufferedSerialIoStream extends SerialIoStream { /** * Just open physical serial and not much more - * @see PortHolder#connectAndReadConfiguration() + * @see PortHolder#connectAndReadConfiguration(com.rusefi.binaryprotocol.BinaryProtocol.Arguments) */ public static IoStream openPort(String port) { log.info("[BufferedSerialIoStream] openPort " + port); diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/PortHolder.java b/java_console/io/src/main/java/com/rusefi/io/serial/PortHolder.java index ff97fcd265..ca40f97038 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/PortHolder.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/PortHolder.java @@ -39,7 +39,7 @@ public class PortHolder { this.ioStreamFactory = ioStreamFactory; } - boolean connectAndReadConfiguration() { + boolean connectAndReadConfiguration(BinaryProtocol.Arguments arguments) { IoStream stream = ioStreamFactory.call(); if (stream == null) { // error already reported @@ -50,7 +50,7 @@ public class PortHolder { portLock.notifyAll(); } - boolean result = bp.connectAndReadConfiguration(dataListener); + boolean result = bp.connectAndReadConfiguration( arguments, dataListener); if (listener != null) { if (result) { listener.onConnectionEstablished(); diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/StreamConnector.java b/java_console/io/src/main/java/com/rusefi/io/serial/StreamConnector.java index 3e2d3aa228..796c366e92 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/StreamConnector.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/StreamConnector.java @@ -7,7 +7,6 @@ import com.rusefi.io.ConnectionStateListener; import com.rusefi.io.IoStream; import com.rusefi.io.LinkConnector; import com.rusefi.io.LinkManager; -import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; import static com.devexperts.logging.Logging.getLogging; @@ -20,6 +19,7 @@ public class StreamConnector implements LinkConnector { private final PortHolder portHolder; private final LinkManager linkManager; + private BinaryProtocol.Arguments arguments; public StreamConnector(LinkManager linkManager, Callable ioStreamCallable) { this.linkManager = linkManager; @@ -29,12 +29,13 @@ public class StreamConnector implements LinkConnector { @Override public void connectAndReadConfiguration(BinaryProtocol.Arguments arguments, ConnectionStateListener listener) { + this.arguments = arguments; log.info("StreamConnector: connecting"); portHolder.listener = listener; log.info("scheduleOpening"); linkManager.execute(() -> { log.info("scheduleOpening>openPort"); - portHolder.connectAndReadConfiguration(); + portHolder.connectAndReadConfiguration(arguments); }); } @@ -53,7 +54,7 @@ public class StreamConnector implements LinkConnector { linkManager.execute(() -> { linkManager.messageListener.postMessage(StreamConnector.this.getClass(), "Restarting serial IO"); portHolder.close(); - portHolder.connectAndReadConfiguration(); + portHolder.connectAndReadConfiguration(arguments); }); } diff --git a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java index 526e3df1f0..40d7a2035c 100644 --- a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java +++ b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java @@ -13,12 +13,14 @@ import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments; import java.io.IOException; import java.net.Socket; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReference; import static com.rusefi.io.tcp.TcpConnector.DEFAULT_PORT; import static com.rusefi.io.tcp.TcpConnector.LOCALHOST; public class SimulatorTcpSandbox { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException, InterruptedException { BinaryProtocol.DISABLE_LOCAL_CACHE = true; Socket s = new Socket(LOCALHOST, DEFAULT_PORT); @@ -27,10 +29,12 @@ public class SimulatorTcpSandbox { LinkManager linkManager = new LinkManager(); verifyCrcNoPending(tsStream, linkManager); + AtomicReference configurationImageAtomicReference = new AtomicReference<>(); + CountDownLatch imageLatch = new CountDownLatch(1); StreamConnector streamConnector = new StreamConnector(linkManager, () -> tsStream); linkManager.setConnector(streamConnector); - streamConnector.connectAndReadConfiguration(new BinaryProtocol.Arguments( false), new ConnectionStateListener() { + streamConnector.connectAndReadConfiguration(new BinaryProtocol.Arguments(false), new ConnectionStateListener() { @Override public void onConnectionEstablished() { System.out.println("onConnectionEstablished"); @@ -41,8 +45,8 @@ public class SimulatorTcpSandbox { } else { BinaryProtocolState binaryProtocolState = currentStreamState.getBinaryProtocolState(); ConfigurationImage ci = binaryProtocolState.getControllerConfiguration(); - System.out.println("Got ConfigurationImage " + ci); - System.exit(0); + configurationImageAtomicReference.set(ci); + imageLatch.countDown(); } } @@ -52,6 +56,10 @@ public class SimulatorTcpSandbox { } }); + imageLatch.await(); + ConfigurationImage ci = configurationImageAtomicReference.get(); + System.out.println("Got ConfigurationImage " + ci + ", " + ci.getSize()); + System.exit(0); } private static void verifyCrcNoPending(IoStream tsStream, LinkManager linkManager) {