proxy progress

This commit is contained in:
rusefi 2020-07-11 16:04:27 -04:00
parent 7735758ec5
commit 15d0e29436
12 changed files with 72 additions and 23 deletions

View File

@ -14,6 +14,7 @@ import com.rusefi.core.Pair;
import com.rusefi.core.Sensor; import com.rusefi.core.Sensor;
import com.rusefi.core.SensorCentral; import com.rusefi.core.SensorCentral;
import com.rusefi.io.*; import com.rusefi.io.*;
import com.rusefi.io.commands.GetOutputsCommand;
import com.rusefi.stream.LogicdataStreamFile; import com.rusefi.stream.LogicdataStreamFile;
import com.rusefi.stream.StreamFile; import com.rusefi.stream.StreamFile;
import com.rusefi.stream.TSHighSpeedLog; import com.rusefi.stream.TSHighSpeedLog;
@ -550,10 +551,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
if (isClosed) if (isClosed)
return false; return false;
byte packet[] = new byte[5]; byte[] packet = GetOutputsCommand.createRequest();
packet[0] = Fields.TS_OUTPUT_COMMAND;
putShort(packet, 1, 0); // offset
putShort(packet, 3, swap16(Fields.TS_OUTPUT_SIZE));
byte[] response = executeCommand(packet, "output channels", false); byte[] response = executeCommand(packet, "output channels", false);
if (response == null || response.length != (Fields.TS_OUTPUT_SIZE + 1) || response[0] != RESPONSE_OK) if (response == null || response.length != (Fields.TS_OUTPUT_SIZE + 1) || response[0] != RESPONSE_OK)

View File

@ -111,6 +111,7 @@ public class IncomingDataBuffer {
} }
public void dropPending() { public void dropPending() {
// todo: when exactly do we need this logic?
synchronized (cbb) { synchronized (cbb) {
int pending = cbb.length(); int pending = cbb.length();
if (pending > 0) { if (pending > 0) {

View File

@ -0,0 +1,16 @@
package com.rusefi.io.commands;
import com.rusefi.config.generated.Fields;
import static com.rusefi.binaryprotocol.IoHelper.putShort;
import static com.rusefi.binaryprotocol.IoHelper.swap16;
public class GetOutputsCommand {
public static byte[] createRequest() {
byte[] packet = new byte[5];
packet[0] = Fields.TS_OUTPUT_COMMAND;
putShort(packet, 1, 0); // offset
putShort(packet, 3, swap16(Fields.TS_OUTPUT_SIZE));
return packet;
}
}

View File

@ -16,6 +16,10 @@ public class HelloCommand implements Command {
this.tsSignature = tsSignature; this.tsSignature = tsSignature;
} }
public static void send(IoStream stream, Logger logger) throws IOException {
stream.sendPacket(new byte[]{Fields.TS_HELLO_COMMAND}, logger);
}
@Override @Override
public byte getCommand() { public byte getCommand() {
return Fields.TS_HELLO_COMMAND; return Fields.TS_HELLO_COMMAND;

View File

@ -43,6 +43,12 @@ public class Backend {
} }
public void register(ClientConnectionState clientConnectionState) {
synchronized (clients) {
clients.add(clientConnectionState);
}
}
private void close(ClientConnectionState inactiveClient) { private void close(ClientConnectionState inactiveClient) {
inactiveClient.close(); inactiveClient.close();
synchronized (clients) { synchronized (clients) {
@ -50,9 +56,9 @@ public class Backend {
} }
} }
public void register(ClientConnectionState clientConnectionState) { public List<ClientConnectionState> getClients() {
synchronized (clients) { synchronized (clients) {
clients.add(clientConnectionState); return new ArrayList<>(clients);
} }
} }

View File

@ -1,10 +1,11 @@
package com.rusefi.server; package com.rusefi.server;
import com.opensr5.Logger; import com.opensr5.Logger;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.binaryprotocol.BinaryProtocolCommands;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.tcp.TcpIoStream; import com.rusefi.io.tcp.TcpIoStream;
import java.io.Closeable; import java.io.Closeable;
@ -44,12 +45,17 @@ public class ClientConnectionState {
public void sayHello() { public void sayHello() {
try { try {
stream.sendPacket(new byte[]{Fields.TS_HELLO_COMMAND}, logger); HelloCommand.send(stream, logger);
byte[] response = incomingData.getPacket(logger, "", false); byte[] response = incomingData.getPacket(logger, "", false);
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK))
return; return;
String signature = new String(response, 1, response.length - 1); String tokenAndSignature = new String(response, 1, response.length - 1);
logger.info("New client: " + signature); String token = tokenAndSignature.length() > AutoTokenUtil.TOKEN_LENGTH ? tokenAndSignature.substring(0, AutoTokenUtil.TOKEN_LENGTH) : null;
if (!AutoTokenUtil.isToken(token))
throw new IOException("Invalid token");
String signature = tokenAndSignature.substring(AutoTokenUtil.TOKEN_LENGTH);
logger.info(token + " New client: " + signature);
} catch (IOException e) { } catch (IOException e) {
close(); close();

View File

@ -1,11 +1,14 @@
package com.rusefi.ui; package com.rusefi.auth;
public class AutoTokenUtil { public class AutoTokenUtil {
public static final int TOKEN_LENGTH = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12;
public static boolean isToken(String content) { public static boolean isToken(String content) {
if (content == null) if (content == null)
return false; return false;
content = content.trim(); content = content.trim();
if (content.length() != 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12) if (content.length() != TOKEN_LENGTH)
return false; return false;
return content.charAt(8) == '-' && content.charAt(8 + 1 + 4) == '-'; return content.charAt(8) == '-' && content.charAt(8 + 1 + 4) == '-';
} }

View File

@ -1,5 +1,6 @@
package com.rusefi.ui; package com.rusefi.ui;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.ui.storage.PersistentConfiguration; import com.rusefi.ui.storage.PersistentConfiguration;
import com.rusefi.ui.util.URLLabel; import com.rusefi.ui.util.URLLabel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View File

@ -1,6 +1,6 @@
package com.rusefi.ui.test; package com.rusefi.ui.test;
import com.rusefi.ui.AutoTokenUtil; import com.rusefi.auth.AutoTokenUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;

View File

@ -6,6 +6,7 @@ import com.rusefi.binaryprotocol.BinaryProtocolCommands;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.serial.SerialIoStreamJSerialComm; import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import java.io.IOException; import java.io.IOException;
@ -41,7 +42,7 @@ public class SerialAutoChecker implements Runnable {
Logger logger = FileLog.LOGGER; Logger logger = FileLog.LOGGER;
IncomingDataBuffer incomingData = stream.getDataBuffer(); IncomingDataBuffer incomingData = stream.getDataBuffer();
try { try {
stream.sendPacket(new byte[]{Fields.TS_HELLO_COMMAND}, logger); HelloCommand.send(stream, logger);
byte[] response = incomingData.getPacket(logger, "", false); byte[] response = incomingData.getPacket(logger, "", false);
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK))
return; return;

View File

@ -15,21 +15,21 @@ import static com.rusefi.io.tcp.BinaryProtocolServer.getPacketLength;
import static com.rusefi.io.tcp.BinaryProtocolServer.readPromisedBytes; import static com.rusefi.io.tcp.BinaryProtocolServer.readPromisedBytes;
public class MockRusEfiDevice { public class MockRusEfiDevice {
private final String authToken;
private final String signature; private final String signature;
private final Logger logger; private final Logger logger;
public MockRusEfiDevice(String signature, Logger logger) { public MockRusEfiDevice(String authToken, String signature, Logger logger) {
this.authToken = authToken;
this.signature = signature; this.signature = signature;
this.logger = logger; this.logger = logger;
} }
public void connect(int serverPort) throws IOException { public void connect(int serverPort) throws IOException {
TcpIoStream stream = new TcpIoStream(logger, new Socket(LOCALHOST, serverPort)); TcpIoStream stream = new TcpIoStream(logger, new Socket(LOCALHOST, serverPort));
IncomingDataBuffer in = stream.getDataBuffer(); IncomingDataBuffer in = stream.getDataBuffer();
new Thread(() -> { new Thread(() -> {
try { try {
while (true) { while (true) {
int length = getPacketLength(in, () -> { int length = getPacketLength(in, () -> {
@ -42,14 +42,17 @@ public class MockRusEfiDevice {
if (command == Fields.TS_HELLO_COMMAND) { if (command == Fields.TS_HELLO_COMMAND) {
new HelloCommand(logger, signature).handle(packet, stream); new HelloCommand(logger, authToken + signature).handle(packet, stream);
} else {
handleCommand();
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}).start(); }).start();
} }
protected void handleCommand() {
}
} }

View File

@ -8,6 +8,7 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.List;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
@ -29,7 +30,7 @@ public class ServerTest {
CountDownLatch serverCreated = new CountDownLatch(1); CountDownLatch serverCreated = new CountDownLatch(1);
Backend backend = new Backend(3 * Timeouts.SECOND); Backend backend = new Backend(5 * Timeouts.SECOND);
BinaryProtocolServer.tcpServerSocket(serverPort, "Server", new Function<Socket, Runnable>() { BinaryProtocolServer.tcpServerSocket(serverPort, "Server", new Function<Socket, Runnable>() {
@ -42,6 +43,10 @@ public class ServerTest {
clientConnectionState.sayHello(); clientConnectionState.sayHello();
backend.register(clientConnectionState); backend.register(clientConnectionState);
while(true) {
}
} }
}; };
} }
@ -51,11 +56,16 @@ public class ServerTest {
assertEquals(0, backend.getCount()); assertEquals(0, backend.getCount());
new MockRusEfiDevice("rusEFI 2020.07.06.frankenso_na6.2468827536", logger).connect(serverPort); new MockRusEfiDevice("00000000-1234-1234-1234-123456789012", "rusEFI 2020.07.06.frankenso_na6.2468827536", logger).connect(serverPort);
new MockRusEfiDevice("rusEFI 2020.07.11.proteus_f4.1986715563", logger).connect(serverPort); new MockRusEfiDevice("12345678-1234-1234-1234-123456789012", "rusEFI 2020.07.11.proteus_f4.1986715563", logger).connect(serverPort);
// todo: technically we should have callbacks for 'connect', will make this better if this would be failing
sleep(Timeouts.SECOND); sleep(Timeouts.SECOND);
List<ClientConnectionState> clients = backend.getClients();
assertEquals(2, clients.size());
} }
} }