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 78cf63c05c..8951d9055b 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 @@ -467,7 +467,7 @@ public class BinaryProtocol { if (isClosed) return null; try { - LinkManager.assertCommunicationThread(); + linkManager.assertCommunicationThread(); dropPending(); sendPacket(packet); 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 ce5d0b2bd2..ce403561f6 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 @@ -53,8 +53,20 @@ public class LinkManager implements Closeable { System.out.println(source + ": " + message); } }; + private Thread communicationThread; public LinkManager() { + Future future = submit(() -> { + communicationThread = Thread.currentThread(); + System.out.println("communicationThread lookup DONE"); + }); + try { + // let's wait for the above trivial task to finish + future.get(); + } catch (InterruptedException | ExecutionException e) { + throw new IllegalStateException(e); + } + engineState = new EngineState(new EngineState.EngineStateListenerImpl() { @Override public void beforeLine(String fullLine) { @@ -162,32 +174,12 @@ public class LinkManager implements Closeable { COMMUNICATION_QUEUE, new NamedThreadFactory("communication executor")); - static { -/* - Future future = submit(new Runnable() { - @Override - public void run() { - // WAT? this is hanging?! - COMMUNICATION_THREAD = Thread.currentThread(); - System.out.println("Done"); - } - }); - try { - // let's wait for the above trivial task to finish - future.get(); - System.out.println("Done2"); - } catch (InterruptedException | ExecutionException e) { - throw new IllegalStateException(e); - } - */ + public void assertCommunicationThread() { + if (Thread.currentThread() != communicationThread) + throw new IllegalStateException("Communication on wrong thread"); } - public static void assertCommunicationThread() { -// if (Thread.currentThread() != COMMUNICATION_THREAD) -// throw new IllegalStateException("Communication on wrong thread"); - } - - private EngineState engineState; + private final EngineState engineState; public EngineState getEngineState() { return engineState; diff --git a/java_console/shared_io/src/main/java/com/rusefi/rusEFIVersion.java b/java_console/shared_io/src/main/java/com/rusefi/rusEFIVersion.java index 3945fa110c..d2692e536e 100644 --- a/java_console/shared_io/src/main/java/com/rusefi/rusEFIVersion.java +++ b/java_console/shared_io/src/main/java/com/rusefi/rusEFIVersion.java @@ -6,7 +6,7 @@ import java.net.URL; import java.util.concurrent.atomic.AtomicReference; public class rusEFIVersion { - public static final int CONSOLE_VERSION = 20211021; + public static final int CONSOLE_VERSION = 20211022; public static AtomicReference firmwareVersion = new AtomicReference<>("N/A"); public static long classBuildTimeMillis() { diff --git a/java_console/ui/src/main/java/com/rusefi/ui/lua/LuaScriptPanel.java b/java_console/ui/src/main/java/com/rusefi/ui/lua/LuaScriptPanel.java index c2b9b8dbf2..eaf44480f3 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/lua/LuaScriptPanel.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/lua/LuaScriptPanel.java @@ -3,6 +3,7 @@ package com.rusefi.ui.lua; import com.opensr5.ConfigurationImage; import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.config.generated.Fields; +import com.rusefi.io.LinkManager; import com.rusefi.ui.MessagesPanel; import com.rusefi.ui.UIContext; import com.rusefi.ui.storage.Node; @@ -120,32 +121,36 @@ public class LuaScriptPanel { } void write() { - BinaryProtocol bp = this.context.getLinkManager().getCurrentStreamState(); - String script = scriptText.getText(); - byte[] paddedScript = new byte[Fields.LUA_SCRIPT_SIZE]; - byte[] scriptBytes = script.getBytes(StandardCharsets.US_ASCII); - System.arraycopy(scriptBytes, 0, paddedScript, 0, scriptBytes.length); + LinkManager linkManager = context.getLinkManager(); - int idx = 0; - int remaining; + linkManager.submit(() -> { + BinaryProtocol bp = linkManager.getCurrentStreamState(); - do { - remaining = paddedScript.length - idx; - int thisWrite = Math.min(remaining, Fields.BLOCKING_FACTOR); + byte[] paddedScript = new byte[Fields.LUA_SCRIPT_SIZE]; + byte[] scriptBytes = script.getBytes(StandardCharsets.US_ASCII); + System.arraycopy(scriptBytes, 0, paddedScript, 0, scriptBytes.length); - bp.writeData(paddedScript, idx, Fields.luaScript_offset + idx, thisWrite); + int idx = 0; + int remaining; - idx += thisWrite; + do { + remaining = paddedScript.length - idx; + int thisWrite = Math.min(remaining, Fields.BLOCKING_FACTOR); - remaining -= thisWrite; - } while (remaining > 0); + bp.writeData(paddedScript, idx, Fields.luaScript_offset + idx, thisWrite); - bp.burn(); + idx += thisWrite; - // Burning doesn't reload lua script, so we have to do it manually - resetLua(); + remaining -= thisWrite; + } while (remaining > 0); + + bp.burn(); + + // Burning doesn't reload lua script, so we have to do it manually + resetLua(); + }); } void resetLua() {