proxy progress - reading controller on connect

This commit is contained in:
rusefi 2020-07-18 18:50:41 -04:00
parent 9e3233df92
commit 1b651a466b
5 changed files with 50 additions and 25 deletions

View File

@ -59,14 +59,10 @@ public class rusEFISSLContext {
// } // }
} }
public static Socket getSSLSocket(String host, int port) { public static Socket getSSLSocket(String host, int port) throws IOException {
try {
if (isJenkins) if (isJenkins)
return new Socket(host, port); return new Socket(host, port);
return getSSLSocketFactory(null /*key*/, TLS).createSocket(host, port); return getSSLSocketFactory(null /*key*/, TLS).createSocket(host, port);
} catch (Exception e) {
throw new IllegalStateException(e);
}
} }
/* /*

View File

@ -129,7 +129,7 @@ public class AuthTokenPanel {
} }
public boolean hasToken() { public boolean hasToken() {
return textField.getText().trim().length() > 0 && !textField.getText().contains(TOKEN_SUBSTRING); return AutoTokenUtil.isToken(textField.getText());
} }
public String getToken() { public String getToken() {

View File

@ -1,9 +1,7 @@
package com.rusefi.tools; package com.rusefi.tools;
import com.opensr5.Logger; import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.autodetect.PortDetector; import com.rusefi.autodetect.PortDetector;
import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.LinkManager;
import com.rusefi.proxy.NetworkConnector; import com.rusefi.proxy.NetworkConnector;
import com.rusefi.server.Backend; import com.rusefi.server.Backend;
import com.rusefi.ui.AuthTokenPanel; import com.rusefi.ui.AuthTokenPanel;
@ -11,21 +9,20 @@ import com.rusefi.ui.AuthTokenPanel;
import java.io.IOException; import java.io.IOException;
public class NetworkConnectorStartup { public class NetworkConnectorStartup {
public static void start(String[] strings) throws IOException { public static void start(String[] strings) throws IOException, InterruptedException {
String authToken = AuthTokenPanel.getAuthToken();
if (!AutoTokenUtil.isToken(authToken)) {
System.err.println("Please configure authentication token using 'set_auth_token' command");
return;
}
String autoDetectedPort = PortDetector.autoDetectSerial(null); String autoDetectedPort = PortDetector.autoDetectSerial(null);
if (autoDetectedPort == null) { if (autoDetectedPort == null) {
System.err.println(ConsoleTools.RUS_EFI_NOT_DETECTED); System.err.println(ConsoleTools.RUS_EFI_NOT_DETECTED);
return; return;
} }
NetworkConnector.runNetworkConnector(authToken, autoDetectedPort, Backend.SERVER_PORT_FOR_CONTROLLERS);
LinkManager linkManager = new LinkManager(Logger.CONSOLE)
.setCompositeLogicEnabled(false)
.setNeedPullData(false);
linkManager.startAndConnect(autoDetectedPort, ConnectionStateListener.VOID);
String authToken = AuthTokenPanel.getAuthToken();
NetworkConnector.runNetworkConnector(Backend.SERVER_PORT_FOR_CONTROLLERS, linkManager.getConnector().getBinaryProtocol().getStream(), Logger.CONSOLE,
authToken);
} }
} }

View File

@ -190,8 +190,7 @@ public class ServerTest {
// start "rusEFI network connector" to connect controller with backend since in real life controller has only local serial port it does not have network // start "rusEFI network connector" to connect controller with backend since in real life controller has only local serial port it does not have network
IoStream targetEcuSocket = TestHelper.connectToLocalhost(controllerPort, logger); SessionDetails deviceSessionDetails = NetworkConnector.runNetworkConnector(MockRusEfiDevice.TEST_TOKEN_1, ProxyClient.LOCALHOST + ":" + controllerPort, serverPortForControllers);
SessionDetails deviceSessionDetails = NetworkConnector.runNetworkConnector(serverPortForControllers, targetEcuSocket, logger, MockRusEfiDevice.TEST_TOKEN_1);
assertTrue(controllerRegistered.await(READ_IMAGE_TIMEOUT, TimeUnit.MILLISECONDS)); assertTrue(controllerRegistered.await(READ_IMAGE_TIMEOUT, TimeUnit.MILLISECONDS));

View File

@ -1,7 +1,9 @@
package com.rusefi.proxy; package com.rusefi.proxy;
import com.opensr5.Logger; import com.opensr5.Logger;
import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.LinkManager;
import com.rusefi.io.commands.HelloCommand; import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.tcp.BinaryProtocolServer; import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.io.tcp.TcpIoStream; import com.rusefi.io.tcp.TcpIoStream;
@ -12,17 +14,48 @@ import com.rusefi.tools.online.HttpUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/** /**
* Connector between rusEFI ECU and rusEFI server * Connector between rusEFI ECU and rusEFI server
*/ */
public class NetworkConnector { public class NetworkConnector {
public static SessionDetails runNetworkConnector(String authToken, String controllerPort, int serverPortForControllers) throws InterruptedException, IOException {
LinkManager linkManager = new LinkManager(Logger.CONSOLE)
.setCompositeLogicEnabled(false)
.setNeedPullData(false);
CountDownLatch onConnected = new CountDownLatch(1);
linkManager.startAndConnect(controllerPort, new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
onConnected.countDown();
}
@Override
public void onConnectionFailed() {
}
});
System.out.println("Connecting to controller...");
onConnected.await(1, TimeUnit.MINUTES);
if (onConnected.getCount() != 0) {
System.out.println("Connection to controller failed");
return null;
}
return runNetworkConnector(serverPortForControllers, linkManager, Logger.CONSOLE, authToken);
}
@NotNull @NotNull
public static SessionDetails runNetworkConnector(int serverPortForControllers, IoStream targetEcuSocket, final Logger logger, String authToken) throws IOException { private static SessionDetails runNetworkConnector(int serverPortForControllers, LinkManager linkManager, final Logger logger, String authToken) throws IOException {
IoStream targetEcuSocket = linkManager.getConnector().getBinaryProtocol().getStream();
HelloCommand.send(targetEcuSocket, logger); HelloCommand.send(targetEcuSocket, logger);
String controllerSignature = HelloCommand.getHelloResponse(targetEcuSocket.getDataBuffer(), logger); String controllerSignature = HelloCommand.getHelloResponse(targetEcuSocket.getDataBuffer(), logger);
// Fields.VEHICLENAME.getAnyValue()
// todo: request vehicle info from controller // todo: request vehicle info from controller
ControllerInfo ci = new ControllerInfo("vehicle", "make", "code", controllerSignature); ControllerInfo ci = new ControllerInfo("vehicle", "make", "code", controllerSignature);