diff --git a/java_console/io/src/main/java/com/rusefi/io/ConnectionFailedListener.java b/java_console/io/src/main/java/com/rusefi/io/ConnectionFailedListener.java new file mode 100644 index 0000000000..e3c512b5f4 --- /dev/null +++ b/java_console/io/src/main/java/com/rusefi/io/ConnectionFailedListener.java @@ -0,0 +1,5 @@ +package com.rusefi.io; + +public interface ConnectionFailedListener { + void onConnectionFailed(); +} diff --git a/java_console/io/src/main/java/com/rusefi/io/ConnectionStateListener.java b/java_console/io/src/main/java/com/rusefi/io/ConnectionStateListener.java index 50f1adb174..ab3010882c 100644 --- a/java_console/io/src/main/java/com/rusefi/io/ConnectionStateListener.java +++ b/java_console/io/src/main/java/com/rusefi/io/ConnectionStateListener.java @@ -4,13 +4,11 @@ package com.rusefi.io; * @author Andrey Belomutskiy * 3/1/2017 */ -public interface ConnectionStateListener { +public interface ConnectionStateListener extends ConnectionFailedListener { ConnectionStateListener VOID = new AbstractConnectionStateListener(); /** * This method is invoked once we have connection & configuration from controller */ void onConnectionEstablished(); - - void onConnectionFailed(); } 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 379a214ee6..afba623d37 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 @@ -196,7 +196,7 @@ public class LinkManager implements Closeable { return connector; } - public void start(String port, ConnectionStateListener stateListener) { + public void start(String port, ConnectionFailedListener stateListener) { Objects.requireNonNull(port, "port"); log.info("LinkManager: Starting " + port); if (isLogViewerMode(port)) { diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java index 695599e487..cdcaae1d0f 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java @@ -29,7 +29,7 @@ public class BinaryProtocolProxy { */ public static final int USER_IO_TIMEOUT = 10 * Timeouts.MINUTE; - public static ServerSocketReference createProxy(IoStream targetEcuSocket, int serverProxyPort, AtomicInteger relayCommandCounter) { + public static ServerSocketReference createProxy(IoStream targetEcuSocket, int serverProxyPort, AtomicInteger relayCommandCounter) throws IOException { Function clientSocketRunnableFactory = clientSocket -> () -> { TcpIoStream clientStream = null; try { diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java index 327c519eab..4e211c9317 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -43,25 +43,25 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { public AtomicInteger unknownCommands = new AtomicInteger(); - public static final Function SECURE_SOCKET_FACTORY = rusEFISSLContext::getSSLServerSocket; + public static final ServerSocketFunction SECURE_SOCKET_FACTORY = rusEFISSLContext::getSSLServerSocket; - public static final Function PLAIN_SOCKET_FACTORY = port -> { - try { - ServerSocket serverSocket = new ServerSocket(port); - log.info("ServerSocket " + port + " created"); - return serverSocket; - } catch (IOException e) { - throw new IllegalStateException("Error binding server socket " + port, e); - } + public static final ServerSocketFunction PLAIN_SOCKET_FACTORY = port -> { + ServerSocket serverSocket = new ServerSocket(port); + log.info("ServerSocket " + port + " created"); + return serverSocket; }; private static ConcurrentHashMap THREAD_FACTORIES_BY_NAME = new ConcurrentHashMap<>(); public void start(LinkManager linkManager) { + try { start(linkManager, DEFAULT_PROXY_PORT, Listener.empty(), new Context()); + } catch (IOException e) { + log.error("Error starting local proxy", e); + } } - public void start(LinkManager linkManager, int port, Listener serverSocketCreationCallback, Context context) { + public void start(LinkManager linkManager, int port, Listener serverSocketCreationCallback, Context context) throws IOException { log.info("BinaryProtocolServer on " + port); Function clientSocketRunnableFactory = clientSocket -> () -> { @@ -84,11 +84,11 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { * @param serverSocketCreationCallback this callback is invoked once we open the server socket * @return */ - public static ServerSocketReference tcpServerSocket(int port, String threadName, Function socketRunnableFactory, Listener serverSocketCreationCallback) { + public static ServerSocketReference tcpServerSocket(int port, String threadName, Function socketRunnableFactory, Listener serverSocketCreationCallback) throws IOException { return tcpServerSocket(socketRunnableFactory, port, threadName, serverSocketCreationCallback, PLAIN_SOCKET_FACTORY); } - public static ServerSocketReference tcpServerSocket(Function clientSocketRunnableFactory, int port, String threadName, Listener serverSocketCreationCallback, Function nonSecureSocketFunction) { + public static ServerSocketReference tcpServerSocket(Function clientSocketRunnableFactory, int port, String threadName, Listener serverSocketCreationCallback, ServerSocketFunction nonSecureSocketFunction) throws IOException { ThreadFactory threadFactory = getThreadFactory(threadName); Objects.requireNonNull(serverSocketCreationCallback, "serverSocketCreationCallback"); diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/ServerSocketFunction.java b/java_console/io/src/main/java/com/rusefi/io/tcp/ServerSocketFunction.java new file mode 100644 index 0000000000..9297af2e75 --- /dev/null +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/ServerSocketFunction.java @@ -0,0 +1,8 @@ +package com.rusefi.io.tcp; + +import java.io.IOException; +import java.net.ServerSocket; + +public interface ServerSocketFunction { + ServerSocket apply(int port) throws IOException; +} diff --git a/java_console/io/src/test/java/com/rusefi/TestHelper.java b/java_console/io/src/test/java/com/rusefi/TestHelper.java index d7e2ec0ec8..c6c1d26a43 100644 --- a/java_console/io/src/test/java/com/rusefi/TestHelper.java +++ b/java_console/io/src/test/java/com/rusefi/TestHelper.java @@ -47,7 +47,7 @@ public class TestHelper { } @NotNull - public static BinaryProtocolServer createVirtualController(ConfigurationImage ci, int port, Listener serverSocketCreationCallback, BinaryProtocolServer.Context context) { + public static BinaryProtocolServer createVirtualController(ConfigurationImage ci, int port, Listener serverSocketCreationCallback, BinaryProtocolServer.Context context) throws IOException { BinaryProtocolState state = new BinaryProtocolState(); state.setController(ci); state.setCurrentOutputs(new byte[1 + Fields.TS_OUTPUT_SIZE]); @@ -83,9 +83,13 @@ public class TestHelper { public static BinaryProtocolServer createVirtualController(int controllerPort, ConfigurationImage controllerImage, BinaryProtocolServer.Context context) throws InterruptedException { CountDownLatch controllerCreated = new CountDownLatch(1); - BinaryProtocolServer server = createVirtualController(controllerImage, controllerPort, parameter -> controllerCreated.countDown(), context); - assertLatch(controllerCreated); - return server; + try { + BinaryProtocolServer server = createVirtualController(controllerImage, controllerPort, parameter -> controllerCreated.countDown(), context); + assertLatch(controllerCreated); + return server; + } catch (IOException e) { + throw new IllegalStateException(e); + } } public static SessionDetails createTestSession(String authToken, String signature) { diff --git a/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java b/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java index f4e2462e8e..519fe9476f 100644 --- a/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java +++ b/java_console/ui/src/main/java/com/rusefi/ui/console/MainFrame.java @@ -13,6 +13,7 @@ import com.rusefi.ui.util.UiUtils; import java.util.TimeZone; +import static com.devexperts.logging.Logging.getLogging; import static com.rusefi.ui.storage.PersistentConfiguration.getConfig; public class MainFrame { @@ -21,7 +22,7 @@ public class MainFrame { /** * @see StartupFrame */ - private FrameHelper frame = new FrameHelper() { + private final FrameHelper frame = new FrameHelper() { @Override protected void onWindowOpened() { FileLog.MAIN.logLine("onWindowOpened"); @@ -42,22 +43,13 @@ public class MainFrame { } }; - public ConnectionStateListener listener; + public ConnectionFailedListener listener; public MainFrame(ConsoleUI consoleUI, TabbedPanel tabbedPane) { this.consoleUI = consoleUI; this.tabbedPane = tabbedPane; - listener = new AbstractConnectionStateListener() { - @Override - public void onConnectionEstablished() { - FileLog.MAIN.logLine("onConnectionEstablished"); - // tabbedPane.romEditorPane.showContent(); - tabbedPane.settingsTab.showContent(); - tabbedPane.logsManager.showContent(); - tabbedPane.fuelTunePane.showContent(); - new BinaryProtocolServer().start(consoleUI.uiContext.getLinkManager()); - } + listener = () -> { }; } @@ -87,8 +79,6 @@ public class MainFrame { @Override public void onConnectionEstablished() { - FileLog.MAIN.logLine("onConnectionEstablished"); -// tabbedPane.romEditorPane.showContent(); tabbedPane.settingsTab.showContent(); tabbedPane.logsManager.showContent(); tabbedPane.fuelTunePane.showContent(); diff --git a/java_console/ui/src/test/java/com/rusefi/BackendTestHelper.java b/java_console/ui/src/test/java/com/rusefi/BackendTestHelper.java index 8ca507dc0a..5cbe9a430f 100644 --- a/java_console/ui/src/test/java/com/rusefi/BackendTestHelper.java +++ b/java_console/ui/src/test/java/com/rusefi/BackendTestHelper.java @@ -8,6 +8,7 @@ import com.rusefi.server.rusEFISSLContext; import com.rusefi.tools.online.HttpUtil; import org.jetbrains.annotations.NotNull; +import java.io.IOException; import java.net.MalformedURLException; import java.util.concurrent.CountDownLatch; @@ -16,13 +17,21 @@ import static com.rusefi.TestHelper.assertLatch; public class BackendTestHelper { public static void runApplicationConnectorBlocking(Backend backend, int serverPortForRemoteUsers) throws InterruptedException { CountDownLatch applicationServerCreated = new CountDownLatch(1); - backend.runApplicationConnector(serverPortForRemoteUsers, parameter -> applicationServerCreated.countDown()); + try { + backend.runApplicationConnector(serverPortForRemoteUsers, parameter -> applicationServerCreated.countDown()); + } catch (IOException e) { + throw new IllegalStateException(e); + } assertLatch(applicationServerCreated); } public static void runControllerConnectorBlocking(Backend backend, int serverPortForControllers) throws InterruptedException { CountDownLatch controllerServerCreated = new CountDownLatch(1); - backend.runControllerConnector(serverPortForControllers, parameter -> controllerServerCreated.countDown()); + try { + backend.runControllerConnector(serverPortForControllers, parameter -> controllerServerCreated.countDown()); + } catch (IOException e) { + throw new IllegalStateException(e); + } assertLatch(controllerServerCreated); } diff --git a/java_console/ui/src/test/java/com/rusefi/io/TcpCommunicationIntegrationTest.java b/java_console/ui/src/test/java/com/rusefi/io/TcpCommunicationIntegrationTest.java index 1c34f7e38d..cb1c3a4a19 100644 --- a/java_console/ui/src/test/java/com/rusefi/io/TcpCommunicationIntegrationTest.java +++ b/java_console/ui/src/test/java/com/rusefi/io/TcpCommunicationIntegrationTest.java @@ -10,6 +10,7 @@ import com.rusefi.io.tcp.BinaryProtocolProxy; import com.rusefi.io.tcp.BinaryProtocolServer; import org.junit.Test; +import java.io.IOException; import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @@ -83,7 +84,7 @@ public class TcpCommunicationIntegrationTest { } @Test - public void testProxy() throws InterruptedException { + public void testProxy() throws InterruptedException, IOException { ConfigurationImage serverImage = TestHelper.prepareImage(239, TestHelper.createIniField(Fields.CYLINDERSCOUNT)); int controllerPort = 6102; diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java index baaba549eb..cd83b16067 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/Backend.java @@ -161,7 +161,7 @@ public class Backend implements Closeable { } } - public void runApplicationConnector(int serverPortForApplications, Listener serverSocketCreationCallback) { + public void runApplicationConnector(int serverPortForApplications, Listener serverSocketCreationCallback) throws IOException { this.serverPortForApplications = serverPortForApplications; // connection from authenticator app which proxies for Tuner Studio // authenticator pushed hello packet on connect @@ -246,7 +246,7 @@ public class Backend implements Closeable { log.info("Disconnecting application " + applicationConnectionState); } - public void runControllerConnector(int serverPortForControllers, Listener serverSocketCreationCallback) { + public void runControllerConnector(int serverPortForControllers, Listener serverSocketCreationCallback) throws IOException { this.serverPortForControllers = serverPortForControllers; log.info("Starting controller connector at " + serverPortForControllers); controllerConnector = BinaryProtocolServer.tcpServerSocket(controllerSocket -> () -> { diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java index 36e9958111..7b8f914824 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/BackendLauncher.java @@ -4,8 +4,10 @@ import com.rusefi.proxy.client.LocalApplicationProxy; import com.rusefi.tools.online.HttpUtil; import com.rusefi.tools.online.ProxyClient; +import java.io.IOException; + public class BackendLauncher { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { /* todo rusEFISSLContext.setupCertificates(new File("keystore.jks"), System.getProperty("RUSEFI_KEYSTORE_PASSWORD")); */