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 15513c0545..49df1ad78a 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 @@ -41,7 +41,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { public static final Function PLAIN_SOCKET_FACTORY = port -> { try { - return new ServerSocket(port); + ServerSocket serverSocket = new ServerSocket(port); + Logger.CONSOLE.info("ServerSocket " + port + " created"); + return serverSocket; } catch (IOException e) { throw new IllegalStateException("Error binding server socket " + port, e); } diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/proxy/BaseBroadcastingThread.java b/java_tools/proxy_server/src/main/java/com/rusefi/proxy/BaseBroadcastingThread.java index 360d032617..1e28b39392 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/proxy/BaseBroadcastingThread.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/proxy/BaseBroadcastingThread.java @@ -37,7 +37,7 @@ public class BaseBroadcastingThread { if (command == Fields.TS_HELLO_COMMAND) { // respond on hello request with information about session - logger.info("Sending out " + sessionDetails); + logger.info("Sending to controller connector@proxy: " + sessionDetails); new HelloCommand(logger, sessionDetails.toJson()).handle(stream); } else { handleCommand(packet, stream); diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/proxy/NetworkConnector.java b/java_tools/proxy_server/src/main/java/com/rusefi/proxy/NetworkConnector.java index 947d4bff13..96104ef1fc 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/proxy/NetworkConnector.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/proxy/NetworkConnector.java @@ -42,10 +42,10 @@ public class NetworkConnector { } }); - System.out.println("Connecting to controller..."); + Logger.CONSOLE.info("Connecting to controller..."); onConnected.await(1, TimeUnit.MINUTES); if (onConnected.getCount() != 0) { - System.out.println("Connection to controller failed"); + Logger.CONSOLE.info("Connection to controller failed"); return null; } diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java index 22e771219b..933727c54f 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java @@ -7,8 +7,9 @@ import com.rusefi.io.IoStream; import com.rusefi.io.commands.GetOutputsCommand; import com.rusefi.io.commands.HelloCommand; import com.rusefi.io.tcp.TcpIoStream; +import com.rusefi.shared.FileUtil; +import net.jcip.annotations.GuardedBy; -import java.io.Closeable; import java.io.IOException; import java.net.Socket; @@ -29,6 +30,8 @@ public class ControllerConnectionState { */ private UserDetails userDetails; private ControllerKey controllerKey; + @GuardedBy("this") + private boolean isUsed; public ControllerConnectionState(Socket clientSocket, Logger logger, UserDetailsResolver userDetailsResolver) { this.clientSocket = clientSocket; @@ -56,7 +59,7 @@ public class ControllerConnectionState { public void close() { isClosed = true; - close(clientSocket); + FileUtil.close(clientSocket); } public void requestControllerInfo() throws IOException { @@ -85,21 +88,9 @@ public class ControllerConnectionState { return sessionDetails; } - private static void close(Closeable closeable) { - if (closeable != null) { - try { - closeable.close(); - } catch (IOException ignored) { - // ignored - } - } - } - public void runEndlessLoop() throws IOException { - while (true) { getOutputs(); - } } @@ -112,4 +103,24 @@ public class ControllerConnectionState { if (packet == null) throw new IOException("No response"); } + + /** + * @return true if acquired successfully, false if not + */ + public synchronized boolean acquire() { + if (isUsed) + return false; + isUsed = true; + return true; + } + + public synchronized boolean isUsed() { + return isUsed; + } + + public synchronized void release() { + if (!isUsed) + throw new IllegalStateException("Not acquired by anyone"); + isUsed = false; + } }