From b5058dff2b9675ed84ae7576e0afe2021232d2b3 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Thu, 8 Oct 2020 23:45:51 -0400 Subject: [PATCH] TCP open refactoring --- .../io/src/main/java/com/rusefi/io/LinkManager.java | 10 +--------- .../src/main/java/com/rusefi/io/tcp/TcpConnector.java | 2 +- .../src/main/java/com/rusefi/io/tcp/TcpIoStream.java | 9 +++++++++ 3 files changed, 11 insertions(+), 10 deletions(-) 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 a8b72dc352..920f43f001 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 @@ -7,7 +7,6 @@ import com.rusefi.NamedThreadFactory; import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocolState; import com.rusefi.core.EngineState; -import com.rusefi.core.MessagesCentral; import com.rusefi.io.serial.StreamConnector; import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.tcp.TcpConnector; @@ -15,7 +14,6 @@ import com.rusefi.io.tcp.TcpIoStream; import org.jetbrains.annotations.NotNull; import java.io.Closeable; -import java.net.Socket; import java.util.Arrays; import java.util.Objects; import java.util.concurrent.*; @@ -213,14 +211,8 @@ public class LinkManager implements Closeable { @Override public IoStream call() { messageListener.postMessage(getClass(), "Opening port: " + port); - Socket socket; try { - int portPart = TcpConnector.getTcpPort(port); - String hostname = TcpConnector.getHostname(port); - socket = new Socket(hostname, portPart); - TcpIoStream tcpIoStream = new TcpIoStream("[start] ", socket); - - return tcpIoStream; + return TcpIoStream.open(port); } catch (Throwable e) { stateListener.onConnectionFailed(); return null; diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java index 5428659a06..e1a03dd977 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpConnector.java @@ -42,7 +42,7 @@ public class TcpConnector { return confirmation.substring(0, length); } */ - static class InvalidTcpPort extends Exception { + public static class InvalidTcpPort extends Exception { } public static int getTcpPort(String port) throws InvalidTcpPort { diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java index 058440defc..73d104a56e 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/TcpIoStream.java @@ -39,6 +39,15 @@ public class TcpIoStream extends AbstractIoStream { this.dataBuffer = IncomingDataBuffer.createDataBuffer(loggingPrefix, this); } + @NotNull + public static TcpIoStream open(String port) throws TcpConnector.InvalidTcpPort, IOException { + int portPart = TcpConnector.getTcpPort(port); + String hostname = TcpConnector.getHostname(port); + Socket socket = new Socket(hostname, portPart); + + return new TcpIoStream("[start] ", socket); + } + @Override public void close() { // we need to guarantee only one onDisconnect invocation for retry logic to be healthy