simulator TCP data rate #4203

research toolset
This commit is contained in:
rusefillc 2022-10-16 00:36:33 -04:00
parent d487148eac
commit 18aea67f6b
5 changed files with 51 additions and 27 deletions

View File

@ -65,7 +65,7 @@ public class BinaryProtocol {
private final Object ioLock = new Object(); private final Object ioLock = new Object();
BinaryProtocolLogger binaryProtocolLogger; BinaryProtocolLogger binaryProtocolLogger;
public static boolean DISABLE_LOCAL_CACHE; public static boolean DISABLE_LOCAL_CONFIGURATION_CACHE;
public static String findCommand(byte command) { public static String findCommand(byte command) {
switch (command) { switch (command) {
@ -377,7 +377,7 @@ public class BinaryProtocol {
} }
private ConfigurationImage getAndValidateLocallyCached() { private ConfigurationImage getAndValidateLocallyCached() {
if (DISABLE_LOCAL_CACHE) if (DISABLE_LOCAL_CONFIGURATION_CACHE)
return null; return null;
ConfigurationImage localCached; ConfigurationImage localCached;
try { try {

View File

@ -182,19 +182,10 @@ public class BinaryProtocolServer {
// todo: relay command // todo: relay command
stream.sendPacket(TS_OK.getBytes()); stream.sendPacket(TS_OK.getBytes());
} else if (command == Fields.TS_OUTPUT_COMMAND) { } else if (command == Fields.TS_OUTPUT_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort());
int count = swap16(dis.readShort());
log.info("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count);
byte[] response = new byte[1 + count];
response[0] = (byte) TS_OK.charAt(0);
BinaryProtocolState binaryProtocolState = linkManager.getBinaryProtocolState(); BinaryProtocolState binaryProtocolState = linkManager.getBinaryProtocolState();
byte[] currentOutputs = binaryProtocolState.getCurrentOutputs(); byte[] currentOutputs = binaryProtocolState.getCurrentOutputs();
if (MOCK_SD_CARD)
currentOutputs[SD_STATUS_OFFSET] = 1 + 4; byte[] response = getOutputCommandResponse(payload, currentOutputs);
if (currentOutputs != null)
System.arraycopy(currentOutputs, offset, response, 1, count);
stream.sendPacket(response); stream.sendPacket(response);
} else if (command == Fields.TS_GET_TEXT) { } else if (command == Fields.TS_GET_TEXT) {
// todo: relay command // todo: relay command
@ -208,6 +199,23 @@ public class BinaryProtocolServer {
} }
} }
@NotNull
public static byte[] getOutputCommandResponse(byte[] payload, byte[] currentOutputs) throws IOException {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort());
int count = swap16(dis.readShort());
if (log.debugEnabled())
log.debug("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count);
byte[] response = new byte[1 + count];
response[0] = (byte) TS_OK.charAt(0);
if (MOCK_SD_CARD)
currentOutputs[SD_STATUS_OFFSET] = 1 + 4;
if (currentOutputs != null)
System.arraycopy(currentOutputs, offset, response, 1, count);
return response;
}
@NotNull @NotNull
public static byte[] getPacketContent(IncomingDataBuffer in, Integer length) throws IOException { public static byte[] getPacketContent(IncomingDataBuffer in, Integer length) throws IOException {
if (log.debugEnabled()) if (log.debugEnabled())
@ -329,11 +337,19 @@ public class BinaryProtocolServer {
log.info("CRC check"); log.info("CRC check");
BinaryProtocolState bp = linkManager.getBinaryProtocolState(); BinaryProtocolState bp = linkManager.getBinaryProtocolState();
byte[] content = bp.getControllerConfiguration().getContent(); byte[] content = bp.getControllerConfiguration().getContent();
int result = IoHelper.getCrc32(content); byte[] packet = createCrcResponse(content);
stream.sendPacket(packet);
}
@NotNull
public static byte[] createCrcResponse(byte[] content) throws IOException {
int crc32value = IoHelper.getCrc32(content);
ByteArrayOutputStream response = new ByteArrayOutputStream(); ByteArrayOutputStream response = new ByteArrayOutputStream();
// header
response.write(TS_OK.charAt(0)); response.write(TS_OK.charAt(0));
new DataOutputStream(response).writeInt(result); // payload
stream.sendPacket(response.toByteArray()); new DataOutputStream(response).writeInt(crc32value);
return response.toByteArray();
} }
public static class Packet { public static class Packet {

View File

@ -10,9 +10,12 @@ import java.net.Socket;
import static com.rusefi.io.tcp.TcpConnector.DEFAULT_PORT; import static com.rusefi.io.tcp.TcpConnector.DEFAULT_PORT;
import static com.rusefi.io.tcp.TcpConnector.LOCALHOST; import static com.rusefi.io.tcp.TcpConnector.LOCALHOST;
public class SimulatorTcpSandbox { /**
public static void main(String[] args) throws IOException, InterruptedException { * @see TcpServerSandbox
BinaryProtocol.DISABLE_LOCAL_CACHE = true; */
public class TcpClientSandbox {
public static void main(String[] args) throws IOException {
BinaryProtocol.DISABLE_LOCAL_CONFIGURATION_CACHE = true;
Socket s = new Socket(LOCALHOST, DEFAULT_PORT); Socket s = new Socket(LOCALHOST, DEFAULT_PORT);
TcpIoStream tsStream = new TcpIoStream("sandbox", s); TcpIoStream tsStream = new TcpIoStream("sandbox", s);
@ -36,9 +39,6 @@ public class SimulatorTcpSandbox {
long time = System.currentTimeMillis() - startMs; long time = System.currentTimeMillis() - startMs;
double timePerCommand = 1.0 * time / count; double timePerCommand = 1.0 * time / count;
System.out.println("Executed " + count + " getSignature in " + time + "ms\n" + "Per-signature: " + timePerCommand + "ms"); System.out.println("Executed " + count + " getSignature in " + time + "ms\n" + "Per-signature: " + timePerCommand + "ms");
// linkManager.submit(() -> {
} }
{ {
@ -57,8 +57,6 @@ public class SimulatorTcpSandbox {
} }
// ConfigurationImage ci = SandboxCommon.readImage(tsStream, linkManager);
System.exit(0); System.exit(0);
} }

View File

@ -2,6 +2,7 @@ package com.rusefi.binaryprotocol.test;
import com.rusefi.CompatibleFunction; import com.rusefi.CompatibleFunction;
import com.rusefi.Listener; import com.rusefi.Listener;
import com.rusefi.binaryprotocol.BinaryProtocol;
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;
@ -18,12 +19,18 @@ import java.net.Socket;
import static com.rusefi.binaryprotocol.IoHelper.swap16; import static com.rusefi.binaryprotocol.IoHelper.swap16;
import static com.rusefi.config.generated.Fields.TS_PROTOCOL; import static com.rusefi.config.generated.Fields.TS_PROTOCOL;
import static com.rusefi.io.tcp.BinaryProtocolServer.TS_OK; import static com.rusefi.io.tcp.BinaryProtocolServer.TS_OK;
import static com.rusefi.io.tcp.BinaryProtocolServer.getOutputCommandResponse;
/** /**
* Fully self-contained fake TCP-IP 'ECU' side of TS protocol * Fully self-contained fake TCP-IP 'ECU' side of TS protocol
* does not have checkCrc32 command implementation so you have to remove it from .ini if you want to connect to this ECU * does not have checkCrc32 command implementation so you have to remove it from .ini if you want to connect to this ECU
*
* @see TcpClientSandbox
*/ */
public class TcpServerSandbox { public class TcpServerSandbox {
private final static byte [] TOTALLY_EMPTY_CONFIGURATION = new byte[Fields.TOTAL_CONFIG_SIZE];
private final static byte [] TOTALLY_EMPTY_OUTPUTS = new byte[Fields.TS_TOTAL_OUTPUT_SIZE];
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Listener serverSocketCreationCallback = parameter -> System.out.println("serverSocketCreationCallback"); Listener serverSocketCreationCallback = parameter -> System.out.println("serverSocketCreationCallback");
CompatibleFunction<Socket, Runnable> socketRunnableFactory = new CompatibleFunction<Socket, Runnable>() { CompatibleFunction<Socket, Runnable> socketRunnableFactory = new CompatibleFunction<Socket, Runnable>() {
@ -71,7 +78,10 @@ public class TcpServerSandbox {
} else if (command == Fields.TS_PAGE_COMMAND) { } else if (command == Fields.TS_PAGE_COMMAND) {
stream.sendPacket(TS_OK.getBytes()); stream.sendPacket(TS_OK.getBytes());
} else if (command == Fields.TS_CRC_CHECK_COMMAND) { } else if (command == Fields.TS_CRC_CHECK_COMMAND) {
throw new IllegalStateException("Need CRC"); stream.sendPacket(BinaryProtocolServer.createCrcResponse(TOTALLY_EMPTY_CONFIGURATION));
} else if (command == Fields.TS_OUTPUT_COMMAND) {
byte[] response = getOutputCommandResponse(payload, TOTALLY_EMPTY_OUTPUTS);
stream.sendPacket(response);
} else if (command == Fields.TS_READ_COMMAND) { } else if (command == Fields.TS_READ_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1)); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort()); int offset = swap16(dis.readShort());
@ -83,6 +93,6 @@ public class TcpServerSandbox {
} else if (command == Fields.TS_GET_FIRMWARE_VERSION) { } else if (command == Fields.TS_GET_FIRMWARE_VERSION) {
stream.sendPacket((TS_OK + "rusEFI proxy").getBytes()); stream.sendPacket((TS_OK + "rusEFI proxy").getBytes());
} else } else
throw new UnsupportedOperationException("Unsupported command " + command); throw new UnsupportedOperationException("Unsupported command " + BinaryProtocol.findCommand(command));
} }
} }

View File

@ -43,7 +43,7 @@ public class BackendTestHelper {
public static void commonServerTest() throws MalformedURLException { public static void commonServerTest() throws MalformedURLException {
HttpUtil.RUSEFI_PROXY_HOSTNAME = TcpConnector.LOCALHOST; HttpUtil.RUSEFI_PROXY_HOSTNAME = TcpConnector.LOCALHOST;
BinaryProtocol.DISABLE_LOCAL_CACHE = true; BinaryProtocol.DISABLE_LOCAL_CONFIGURATION_CACHE = true;
rusEFISSLContext.init("certificate/test_pkcs12.jks", "password"); rusEFISSLContext.init("certificate/test_pkcs12.jks", "password");
} }