2020-07-03 11:04:08 -07:00
|
|
|
package com.rusefi.io;
|
2020-06-25 22:33:44 -07:00
|
|
|
|
|
|
|
import com.opensr5.ConfigurationImage;
|
|
|
|
import com.opensr5.ini.field.ScalarIniField;
|
2020-07-12 21:44:43 -07:00
|
|
|
import com.rusefi.TestHelper;
|
2020-07-07 18:34:24 -07:00
|
|
|
import com.rusefi.binaryprotocol.BinaryProtocol;
|
2020-06-25 22:33:44 -07:00
|
|
|
import com.rusefi.config.generated.Fields;
|
2020-07-07 21:19:25 -07:00
|
|
|
import com.rusefi.io.tcp.BinaryProtocolProxy;
|
2020-06-25 22:33:44 -07:00
|
|
|
import com.rusefi.io.tcp.BinaryProtocolServer;
|
2021-12-06 14:10:13 -08:00
|
|
|
import com.rusefi.io.tcp.TcpConnector;
|
2022-08-11 09:18:55 -07:00
|
|
|
import com.rusefi.ui.StatusConsumer;
|
2023-12-15 08:35:33 -08:00
|
|
|
import org.junit.jupiter.api.Test;
|
2020-06-25 22:33:44 -07:00
|
|
|
|
2020-07-27 18:23:03 -07:00
|
|
|
import java.io.IOException;
|
2020-07-07 18:34:24 -07:00
|
|
|
import java.util.Objects;
|
2020-06-25 22:33:44 -07:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2020-07-25 21:16:43 -07:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2020-06-25 22:33:44 -07:00
|
|
|
|
2020-07-26 11:27:07 -07:00
|
|
|
import static com.rusefi.TestHelper.assertLatch;
|
2024-02-14 11:43:20 -08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
2020-06-25 22:33:44 -07:00
|
|
|
|
|
|
|
public class TcpCommunicationIntegrationTest {
|
2020-07-03 11:22:42 -07:00
|
|
|
// todo: implement & test TCP connector restart!
|
|
|
|
@Test
|
|
|
|
public void testConnectionFailed() throws InterruptedException {
|
|
|
|
int port = 6101;
|
|
|
|
|
|
|
|
CountDownLatch failedCountDownLatch = new CountDownLatch(1);
|
|
|
|
|
2020-07-24 09:44:29 -07:00
|
|
|
LinkManager clientManager = new LinkManager();
|
2020-07-03 11:22:42 -07:00
|
|
|
clientManager.startAndConnect(Integer.toString(port), new ConnectionStateListener() {
|
|
|
|
@Override
|
|
|
|
public void onConnectionEstablished() {
|
|
|
|
System.out.println("Established");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-01 08:07:37 -07:00
|
|
|
public void onConnectionFailed(String s) {
|
2020-07-03 11:22:42 -07:00
|
|
|
System.out.println("onConnectionFailed");
|
|
|
|
failedCountDownLatch.countDown();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-26 11:27:07 -07:00
|
|
|
assertLatch(failedCountDownLatch);
|
2020-07-03 11:22:42 -07:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:33:44 -07:00
|
|
|
@Test
|
2020-07-03 11:22:42 -07:00
|
|
|
public void testConnectAndTransmitImageOverTcpIp() throws InterruptedException {
|
2020-07-12 21:44:43 -07:00
|
|
|
ScalarIniField iniField = TestHelper.createIniField(Fields.CYLINDERSCOUNT);
|
2020-07-07 18:34:24 -07:00
|
|
|
int value = 239;
|
2020-07-12 21:44:43 -07:00
|
|
|
ConfigurationImage serverImage = TestHelper.prepareImage(value, iniField);
|
2020-06-25 22:33:44 -07:00
|
|
|
int port = 6100;
|
2020-07-07 22:11:04 -07:00
|
|
|
|
2020-07-26 13:52:58 -07:00
|
|
|
BinaryProtocolServer server = TestHelper.createVirtualController(port, serverImage, new BinaryProtocolServer.Context());
|
2020-06-25 22:33:44 -07:00
|
|
|
|
2020-07-03 11:22:42 -07:00
|
|
|
CountDownLatch connectionEstablishedCountDownLatch = new CountDownLatch(1);
|
2020-06-25 22:33:44 -07:00
|
|
|
|
|
|
|
// todo: remove CONFIGURATION_RUSEFI_BINARY or nicer API to disable local file load
|
|
|
|
|
2020-07-24 09:44:29 -07:00
|
|
|
LinkManager clientManager = new LinkManager();
|
2021-12-06 14:10:13 -08:00
|
|
|
clientManager.startAndConnect(TcpConnector.LOCALHOST + ":" + port, new ConnectionStateListener() {
|
2020-06-25 22:33:44 -07:00
|
|
|
@Override
|
|
|
|
public void onConnectionEstablished() {
|
2020-07-03 11:22:42 -07:00
|
|
|
connectionEstablishedCountDownLatch.countDown();
|
2020-06-25 22:33:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-01 08:07:37 -07:00
|
|
|
public void onConnectionFailed(String s) {
|
2020-06-25 22:33:44 -07:00
|
|
|
System.out.println("Failed");
|
|
|
|
}
|
|
|
|
});
|
2020-07-26 11:27:07 -07:00
|
|
|
assertLatch("Connection established", connectionEstablishedCountDownLatch);
|
2020-07-07 18:34:24 -07:00
|
|
|
|
2020-06-25 22:33:44 -07:00
|
|
|
assertEquals(0, server.unknownCommands.get());
|
2020-07-04 07:06:22 -07:00
|
|
|
|
2020-07-07 18:34:24 -07:00
|
|
|
BinaryProtocol clientStreamState = clientManager.getCurrentStreamState();
|
|
|
|
Objects.requireNonNull(clientStreamState, "clientStreamState");
|
|
|
|
ConfigurationImage clientImage = clientStreamState.getControllerConfiguration();
|
|
|
|
String clientValue = iniField.getValue(clientImage);
|
|
|
|
assertEquals(Double.toString(value), clientValue);
|
|
|
|
|
2020-07-18 19:50:32 -07:00
|
|
|
clientManager.close();
|
2020-06-25 22:33:44 -07:00
|
|
|
}
|
|
|
|
|
2020-07-07 18:34:24 -07:00
|
|
|
@Test
|
2020-07-27 18:23:03 -07:00
|
|
|
public void testProxy() throws InterruptedException, IOException {
|
2020-07-12 21:44:43 -07:00
|
|
|
ConfigurationImage serverImage = TestHelper.prepareImage(239, TestHelper.createIniField(Fields.CYLINDERSCOUNT));
|
2020-07-07 21:19:25 -07:00
|
|
|
int controllerPort = 6102;
|
2020-07-07 22:07:50 -07:00
|
|
|
|
2020-07-12 21:44:43 -07:00
|
|
|
// create virtual controller
|
2020-07-26 13:52:58 -07:00
|
|
|
TestHelper.createVirtualController(controllerPort, serverImage, new BinaryProtocolServer.Context());
|
2020-07-07 21:19:25 -07:00
|
|
|
int proxyPort = 6103;
|
|
|
|
|
2020-07-09 20:07:49 -07:00
|
|
|
|
2020-07-12 21:44:43 -07:00
|
|
|
// connect proxy to virtual controller
|
2021-12-06 14:10:13 -08:00
|
|
|
IoStream targetEcuSocket = TestHelper.connectToLocalhost(controllerPort);
|
2020-10-08 21:30:13 -07:00
|
|
|
final AtomicInteger relayCommandCounter = new AtomicInteger();
|
2022-08-11 09:18:55 -07:00
|
|
|
BinaryProtocolProxy.createProxy(targetEcuSocket, proxyPort, () -> relayCommandCounter.incrementAndGet(),
|
|
|
|
StatusConsumer.ANONYMOUS);
|
2020-07-07 21:19:25 -07:00
|
|
|
|
|
|
|
CountDownLatch connectionEstablishedCountDownLatch = new CountDownLatch(1);
|
|
|
|
|
2020-07-12 21:44:43 -07:00
|
|
|
// connect to proxy and read virtual controller through it
|
2020-07-24 09:44:29 -07:00
|
|
|
LinkManager clientManager = new LinkManager();
|
2021-12-06 14:10:13 -08:00
|
|
|
clientManager.startAndConnect(TcpConnector.LOCALHOST + ":" + proxyPort, new ConnectionStateListener() {
|
2020-07-07 21:19:25 -07:00
|
|
|
@Override
|
|
|
|
public void onConnectionEstablished() {
|
|
|
|
connectionEstablishedCountDownLatch.countDown();
|
|
|
|
}
|
2020-07-07 18:34:24 -07:00
|
|
|
|
2020-07-07 21:19:25 -07:00
|
|
|
@Override
|
2022-06-01 08:07:37 -07:00
|
|
|
public void onConnectionFailed(String s) {
|
2020-07-07 21:19:25 -07:00
|
|
|
System.out.println("Failed");
|
|
|
|
}
|
|
|
|
});
|
2020-07-26 11:27:07 -07:00
|
|
|
assertLatch("Connection established", connectionEstablishedCountDownLatch);
|
2020-07-07 21:19:25 -07:00
|
|
|
|
2020-07-18 19:50:32 -07:00
|
|
|
clientManager.close();
|
2020-07-07 18:34:24 -07:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:33:44 -07:00
|
|
|
}
|