From 625b02f9642a3af3b60e13317f812eb5f012333e Mon Sep 17 00:00:00 2001 From: rusefi Date: Thu, 25 Jun 2020 22:48:22 -0400 Subject: [PATCH] refactoring: better dependency control --- .../autotest/src/com/rusefi/AutoTest.java | 4 +- .../src/com/rusefi/EnduranceTest.java | 4 +- .../autotest/src/com/rusefi/TestingUtils.java | 4 +- .../io/src/com/rusefi/io/CommandQueue.java | 11 ++---- .../io/src/com/rusefi/io/LinkManager.java | 38 ++++++++++--------- .../main/java/com/rusefi/ui/UIContext.java | 2 +- 6 files changed, 31 insertions(+), 32 deletions(-) diff --git a/java_console/autotest/src/com/rusefi/AutoTest.java b/java_console/autotest/src/com/rusefi/AutoTest.java index 77b5601222..e8dc3cff7d 100644 --- a/java_console/autotest/src/com/rusefi/AutoTest.java +++ b/java_console/autotest/src/com/rusefi/AutoTest.java @@ -214,8 +214,8 @@ public class AutoTest { assertWave(msg, chart, EngineChart.SPARK_1, 0.1944, x, x + 180, x + 360, x + 540); } - static EngineChart nextChart() { - return TestingUtils.nextChart(); + private EngineChart nextChart() { + return TestingUtils.nextChart(linkManager.getCommandQueue()); } private void test2003DodgeNeon() { diff --git a/java_console/autotest/src/com/rusefi/EnduranceTest.java b/java_console/autotest/src/com/rusefi/EnduranceTest.java index 2abc16b253..a152327ed0 100644 --- a/java_console/autotest/src/com/rusefi/EnduranceTest.java +++ b/java_console/autotest/src/com/rusefi/EnduranceTest.java @@ -13,7 +13,7 @@ public class EnduranceTest { public static void main(String[] args) { LinkManager linkManager = new LinkManager(); - CommandQueue commandQueue = CommandQueue.getInstance(); + CommandQueue commandQueue = linkManager.getCommandQueue(); long start = System.currentTimeMillis(); int count = parseCount(args); try { @@ -31,7 +31,7 @@ public class EnduranceTest { AutoTest.currentEngineType = 3; sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 3, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue); sleepSeconds(2); - sendCommand(getEnableCommand("self_stimulation"), CommandQueue.getInstance()); + sendCommand(getEnableCommand("self_stimulation"), commandQueue); // IoUtil.changeRpm(1200); AutoTest.currentEngineType = 28; sendCommand("set " + Fields.CMD_ENGINE_TYPE + " " + 28, AutoTest.COMPLEX_COMMAND_RETRY, Timeouts.SET_ENGINE_TIMEOUT, commandQueue); diff --git a/java_console/autotest/src/com/rusefi/TestingUtils.java b/java_console/autotest/src/com/rusefi/TestingUtils.java index 567e558c61..44215accdb 100644 --- a/java_console/autotest/src/com/rusefi/TestingUtils.java +++ b/java_console/autotest/src/com/rusefi/TestingUtils.java @@ -105,9 +105,9 @@ public class TestingUtils { assertTrue(msg, value == null); } - static EngineChart nextChart() { + static EngineChart nextChart(CommandQueue commandQueue) { long start = System.currentTimeMillis(); - EngineChart chart = EngineChartParser.unpackToMap(getNextWaveChart(CommandQueue.getInstance())); + EngineChart chart = EngineChartParser.unpackToMap(getNextWaveChart(commandQueue)); FileLog.MAIN.logLine("AUTOTEST nextChart() in " + (System.currentTimeMillis() - start)); return chart; } diff --git a/java_console/io/src/com/rusefi/io/CommandQueue.java b/java_console/io/src/com/rusefi/io/CommandQueue.java index 70c7458d62..ebf7e1a94a 100644 --- a/java_console/io/src/com/rusefi/io/CommandQueue.java +++ b/java_console/io/src/com/rusefi/io/CommandQueue.java @@ -24,6 +24,7 @@ public class CommandQueue { public static final int SLOW_CONFIRMATION_TIMEOUT = 5000; public static final Class COMMAND_QUEUE_CLASS = CommandQueue.class; private final Object lock = new Object(); + private final LinkManager linkManager; /** * One complex use-case is when we send out a bunch of commands and then we need to handle all the configurations * correctly @@ -31,7 +32,6 @@ public class CommandQueue { */ private Set pendingConfirmations = Collections.synchronizedSet(new HashSet()); - public static final CommandQueue instance = new CommandQueue(); private final BlockingQueue pendingCommands = new LinkedBlockingQueue<>(); private final List commandListeners = new ArrayList<>(); @@ -89,7 +89,7 @@ public class CommandQueue { while (!pendingConfirmations.contains(command)) { counter++; // FileLog.MAIN.logLine("templog sending " + command + " " + System.currentTimeMillis() + " " + new Date()); - LinkManager.send(command, commandRequest.isFireEvent()); + linkManager.send(command, commandRequest.isFireEvent()); long now = System.currentTimeMillis(); synchronized (lock) { lock.wait(commandRequest.getTimeout()); @@ -114,7 +114,8 @@ public class CommandQueue { MessagesCentral.getInstance().postMessage(CommandQueue.class, "Took " + counter + " attempts"); } - private CommandQueue() { + public CommandQueue(LinkManager linkManager) { + this.linkManager = linkManager; Thread thread = new Thread(runnable, "Commands Queue"); thread.setDaemon(true); thread.start(); @@ -140,10 +141,6 @@ public class CommandQueue { } } - public static CommandQueue getInstance() { - return instance; - } - public void write(String command) { write(command, DEFAULT_TIMEOUT); } diff --git a/java_console/io/src/com/rusefi/io/LinkManager.java b/java_console/io/src/com/rusefi/io/LinkManager.java index 15126dd64d..d7f24838d6 100644 --- a/java_console/io/src/com/rusefi/io/LinkManager.java +++ b/java_console/io/src/com/rusefi/io/LinkManager.java @@ -18,6 +18,21 @@ import java.util.concurrent.*; * 3/3/14 */ public class LinkManager { + @NotNull + public static LogLevel LOG_LEVEL = LogLevel.INFO; + + public static LinkDecoder ENCODER = new LinkDecoder() { + @Override + public String unpack(String packedLine) { + return packedLine; + } + }; + + public static final String LOG_VIEWER = "log viewer"; + private final CommandQueue commandQueue = new CommandQueue(this); + + private LinkConnector connector; + @NotNull public CountDownLatch connect(String port) { final CountDownLatch connected = new CountDownLatch(1); @@ -63,7 +78,7 @@ public class LinkManager { } public CommandQueue getCommandQueue() { - return CommandQueue.getInstance(); + return commandQueue; } public enum LogLevel { @@ -76,16 +91,6 @@ public class LinkManager { } } - @NotNull - public static LogLevel LOG_LEVEL = LogLevel.INFO; - - public static LinkDecoder ENCODER = new LinkDecoder() { - @Override - public String unpack(String packedLine) { - return packedLine; - } - }; - /** * Threading of the whole input/output does not look healthy at all! * @@ -100,7 +105,6 @@ public class LinkManager { return t; } }); - public static final String LOG_VIEWER = "log viewer"; public final LinkedBlockingQueue COMMUNICATION_QUEUE = new LinkedBlockingQueue<>(); /** * All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner @@ -144,8 +148,6 @@ public class LinkManager { } }); - private static LinkConnector connector; - /** * This flag controls if mock controls are needed */ @@ -171,7 +173,7 @@ public class LinkManager { } public void setConnector(LinkConnector connector) { - LinkManager.connector = connector; + this.connector = connector; } public static boolean isLogViewerMode(String port) { @@ -183,10 +185,10 @@ public class LinkManager { return connector == LinkConnector.VOID; } - public static void send(String command, boolean fireEvent) throws InterruptedException { - if (connector == null) + public void send(String command, boolean fireEvent) throws InterruptedException { + if (this.connector == null) throw new NullPointerException("connector"); - connector.send(command, fireEvent); + this.connector.send(command, fireEvent); } public void restart() { diff --git a/java_console/ui/src/main/java/com/rusefi/ui/UIContext.java b/java_console/ui/src/main/java/com/rusefi/ui/UIContext.java index ed355fb130..2a62febebc 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/UIContext.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/UIContext.java @@ -15,6 +15,6 @@ public class UIContext { } public CommandQueue getCommandQueue() { - return CommandQueue.instance; + return linkManager.getCommandQueue(); } }