From 20195f55516c3c9ea4ea91d123536e0be4f5cada Mon Sep 17 00:00:00 2001 From: rusefi Date: Mon, 18 Sep 2017 08:00:36 -0400 Subject: [PATCH] better method location --- .../autotest/src/com/rusefi/IoUtil.java | 40 ++-------------- .../autotest/src/com/rusefi/RealHwTest.java | 4 +- .../io/src/com/rusefi/io/LinkManager.java | 47 ++++++++++++++++++- java_console/ui/src/com/rusefi/CmdLine.java | 4 +- 4 files changed, 55 insertions(+), 40 deletions(-) diff --git a/java_console/autotest/src/com/rusefi/IoUtil.java b/java_console/autotest/src/com/rusefi/IoUtil.java index 31a70f01ba..6d6109ff59 100644 --- a/java_console/autotest/src/com/rusefi/IoUtil.java +++ b/java_console/autotest/src/com/rusefi/IoUtil.java @@ -4,13 +4,10 @@ import com.rusefi.core.EngineState; import com.rusefi.core.Sensor; import com.rusefi.core.SensorCentral; import com.rusefi.io.CommandQueue; -import com.rusefi.io.ConnectionStateListener; import com.rusefi.io.InvocationConfirmationListener; import com.rusefi.io.LinkManager; import com.rusefi.io.tcp.TcpConnector; -import jssc.SerialPortList; -import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -151,45 +148,14 @@ public class IoUtil { } } - /** - * @return null if no port located - */ - static String getDefaultPort() { - String[] ports = SerialPortList.getPortNames(); - if (ports.length == 0) { - System.out.println("Port not specified and no ports found"); - return null; - } - String port = ports[ports.length - 1]; - System.out.println("Using last of " + ports.length + " port(s)"); - System.out.println("All ports: " + Arrays.toString(ports)); - return port; - } - static void realHardwareConnect(String port) { - LinkManager.start(port); - final CountDownLatch connected = new CountDownLatch(1); - LinkManager.open(new ConnectionStateListener() { - @Override - public void onConnectionFailed() { - System.out.println("CONNECTION FAILED, did you specify the right port name?"); - System.exit(-1); - } - - @Override - public void onConnectionEstablished() { - connected.countDown(); - } - }); LinkManager.engineState.registerStringValueAction(EngineState.RUS_EFI_VERSION_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(EngineState.OUTPIN_TAG, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); LinkManager.engineState.registerStringValueAction(AverageAnglesUtil.KEY, (EngineState.ValueCallback) EngineState.ValueCallback.VOID); - try { - connected.await(60, TimeUnit.SECONDS); - } catch (InterruptedException e) { - throw new IllegalStateException(e); - } + + final CountDownLatch connected = LinkManager.connect(port); if (connected.getCount() > 0) throw new IllegalStateException("Not connected in time"); } + } diff --git a/java_console/autotest/src/com/rusefi/RealHwTest.java b/java_console/autotest/src/com/rusefi/RealHwTest.java index 6230bf0516..9ecab90305 100644 --- a/java_console/autotest/src/com/rusefi/RealHwTest.java +++ b/java_console/autotest/src/com/rusefi/RealHwTest.java @@ -1,5 +1,7 @@ package com.rusefi; +import com.rusefi.io.LinkManager; + import static com.rusefi.AutoTest.*; /** @@ -46,7 +48,7 @@ public class RealHwTest { if (args.length == 1 || args.length == 2) { port = args[0]; } else if (args.length == 0) { - port = IoUtil.getDefaultPort(); + port = LinkManager.getDefaultPort(); } else { System.out.println("Only one optional argument expected: port number"); port = null; diff --git a/java_console/io/src/com/rusefi/io/LinkManager.java b/java_console/io/src/com/rusefi/io/LinkManager.java index 801f38a7ae..9960e8c77a 100644 --- a/java_console/io/src/com/rusefi/io/LinkManager.java +++ b/java_console/io/src/com/rusefi/io/LinkManager.java @@ -4,8 +4,10 @@ import com.rusefi.FileLog; import com.rusefi.core.EngineState; import com.rusefi.io.serial.SerialConnector; import com.rusefi.io.tcp.TcpConnector; +import jssc.SerialPortList; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; import java.util.concurrent.*; /** @@ -13,6 +15,30 @@ import java.util.concurrent.*; * 3/3/14 */ public class LinkManager { + @NotNull + public static CountDownLatch connect(String port) { + start(port); + final CountDownLatch connected = new CountDownLatch(1); + open(new ConnectionStateListener() { + @Override + public void onConnectionFailed() { + System.out.println("CONNECTION FAILED, did you specify the right port name?"); + System.exit(-1); + } + + @Override + public void onConnectionEstablished() { + connected.countDown(); + } + }); + try { + connected.await(60, TimeUnit.SECONDS); + } catch (InterruptedException e) { + throw new IllegalStateException(e); + } + return connected; + } + public enum LogLevel { INFO, DEBUG, @@ -44,6 +70,9 @@ public class LinkManager { }); public static final String LOG_VIEWER = "log viewer"; public static final LinkedBlockingQueue COMMUNICATION_QUEUE = new LinkedBlockingQueue<>(); + /** + * All request/responses to underlying controller are happening on this single-threaded executor in a FIFO manner + */ public static final ExecutorService COMMUNICATION_EXECUTOR = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, COMMUNICATION_QUEUE, @@ -51,7 +80,8 @@ public class LinkManager { @Override public Thread newThread(Runnable r) { Thread t = Executors.defaultThreadFactory().newThread(r); - t.setName("communication executor");; + t.setName("communication executor"); + ; return t; } }); @@ -126,4 +156,19 @@ public class LinkManager { return message.substring(CommandQueue.CONFIRMATION_PREFIX.length()); return null; } + + /** + * @return null if no port located + */ + public static String getDefaultPort() { + String[] ports = SerialPortList.getPortNames(); + if (ports.length == 0) { + System.out.println("Port not specified and no ports found"); + return null; + } + String port = ports[ports.length - 1]; + System.out.println("Using last of " + ports.length + " port(s)"); + System.out.println("All ports: " + Arrays.toString(ports)); + return port; + } } diff --git a/java_console/ui/src/com/rusefi/CmdLine.java b/java_console/ui/src/com/rusefi/CmdLine.java index b2e8f4338f..a11ac00e3d 100644 --- a/java_console/ui/src/com/rusefi/CmdLine.java +++ b/java_console/ui/src/com/rusefi/CmdLine.java @@ -1,5 +1,7 @@ package com.rusefi; +import com.rusefi.io.LinkManager; + /** * (c) Andrey Belomutskiy 2013-2017 * 2/22/2015 @@ -12,7 +14,7 @@ public class CmdLine { } String command = args[0]; if (args.length == 1) { - String port = IoUtil.getDefaultPort(); + String port = LinkManager.getDefaultPort(); if (port == null) return; executeCommand(command, port);