diff --git a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java index eb82715769..c545bee2e5 100644 --- a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -100,8 +100,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { if (crc != IoHelper.getCrc32(packet)) throw new IllegalStateException("CRC mismatch"); - - TcpIoStream stream = new TcpIoStream(linkManager, clientSocket.getInputStream(), clientSocket.getOutputStream()); + TcpIoStream stream = new TcpIoStream(linkManager, clientSocket); if (command == COMMAND_HELLO) { stream.sendPacket((TS_OK + Fields.TS_SIGNATURE).getBytes(), FileLog.LOGGER); } else if (command == COMMAND_PROTOCOL) { diff --git a/java_console/io/src/com/rusefi/io/tcp/TcpConnector.java b/java_console/io/src/com/rusefi/io/tcp/TcpConnector.java index 21018ddd82..a970dce4eb 100644 --- a/java_console/io/src/com/rusefi/io/tcp/TcpConnector.java +++ b/java_console/io/src/com/rusefi/io/tcp/TcpConnector.java @@ -96,13 +96,11 @@ public class TcpConnector implements LinkConnector { @Override public void connectAndReadConfiguration(ConnectionStateListener listener) { FileLog.MAIN.logLine("Connecting to host=" + hostname + "/port=" + port); - OutputStream os; - BufferedInputStream stream; + TcpIoStream tcpIoStream; + try { Socket socket = new Socket(hostname, port); - os = socket.getOutputStream(); - stream = new BufferedInputStream(socket.getInputStream()); -// ioStream = new TcpIoStream(os, stream); + tcpIoStream = new TcpIoStream(linkManager, socket); } catch (IOException e) { listener.onConnectionFailed(); FileLog.MAIN.logLine("Failed to connect to " + hostname + "/port=" + port); @@ -124,7 +122,7 @@ public class TcpConnector implements LinkConnector { }; // ioStream.setInputListener(listener1); - bp = new BinaryProtocol(linkManager, FileLog.LOGGER, new TcpIoStream(linkManager, stream, os)); + bp = new BinaryProtocol(linkManager, FileLog.LOGGER, tcpIoStream); boolean result = bp.connectAndReadConfiguration(listener1); if (result) { diff --git a/java_console/io/src/com/rusefi/io/tcp/TcpIoStream.java b/java_console/io/src/com/rusefi/io/tcp/TcpIoStream.java index c59f85e074..f4d7079fa6 100644 --- a/java_console/io/src/com/rusefi/io/tcp/TcpIoStream.java +++ b/java_console/io/src/com/rusefi/io/tcp/TcpIoStream.java @@ -5,9 +5,11 @@ import com.opensr5.io.DataListener; import com.rusefi.io.IoStream; import com.rusefi.io.LinkManager; +import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.Socket; import java.util.Arrays; /** @@ -20,6 +22,10 @@ public class TcpIoStream implements IoStream { private final LinkManager linkManager; private boolean isClosed; + public TcpIoStream(LinkManager linkManager, Socket socket) throws IOException { + this(linkManager, new BufferedInputStream(socket.getInputStream()), socket.getOutputStream()); + } + public TcpIoStream(LinkManager linkManager, InputStream input, OutputStream output) { this.linkManager = linkManager; if (input == null) diff --git a/java_console/io/src/com/rusefi/io/tcp/test/BinaryProtocolServerSandbox.java b/java_console/io/src/com/rusefi/io/tcp/test/BinaryProtocolServerSandbox.java index 1d5ab69729..c49e022a53 100644 --- a/java_console/io/src/com/rusefi/io/tcp/test/BinaryProtocolServerSandbox.java +++ b/java_console/io/src/com/rusefi/io/tcp/test/BinaryProtocolServerSandbox.java @@ -18,7 +18,7 @@ class BinaryProtocolServerSandbox { public static void main(String[] args) { LinkManager linkManager = new LinkManager(); TcpIoStream stream = new TcpIoStream(linkManager, new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()); - BinaryProtocol bp = new BinaryProtocol(linkManager, FileLog.LOGGER, (IoStream) stream); + BinaryProtocol bp = new BinaryProtocol(linkManager, FileLog.LOGGER, stream); linkManager.setConnector(new LinkConnector() { @Override public void connectAndReadConfiguration(ConnectionStateListener listener) { diff --git a/java_console/models/src/com/rusefi/io/test/TcpCommunicationIntegrationTest.java b/java_console/models/src/com/rusefi/io/test/TcpCommunicationIntegrationTest.java new file mode 100644 index 0000000000..ec57216843 --- /dev/null +++ b/java_console/models/src/com/rusefi/io/test/TcpCommunicationIntegrationTest.java @@ -0,0 +1,23 @@ +package com.rusefi.io.test; + +import com.opensr5.ConfigurationImage; +import com.opensr5.ini.field.ScalarIniField; +import com.rusefi.config.Field; +import com.rusefi.config.generated.Fields; +import com.rusefi.tune.xml.Constant; +import org.junit.Test; + +public class TcpCommunicationIntegrationTest { + @Test + public void testConnect() { + ConfigurationImage ci = new ConfigurationImage(Fields.TOTAL_CONFIG_SIZE); + + Field field = Fields.CYLINDERSCOUNT; + + ScalarIniField scalarIniField = new ScalarIniField(field.getName(), field.getOffset(), "", field.getType(), 1); + int input = 239; + scalarIniField.setValue(ci, new Constant(field.getName(), "", Integer.toString(input))); + + + } +}