TcpCommunicationIntegrationTest

This commit is contained in:
rusefi 2020-06-25 23:44:49 -04:00
parent 6008159238
commit 8a91ca0e29
4 changed files with 59 additions and 22 deletions

View File

@ -67,10 +67,10 @@ public class BinaryProtocol implements BinaryProtocolCommands {
private final IncomingDataBuffer incomingData;
private boolean isBurnPending;
private BinaryProtocolState state = new BinaryProtocolState();
// todo: this ioLock needs better documentation!
private final Object ioLock = new Object();
private final Object imageLock = new Object();
private ConfigurationImage controller;
private static final int COMPOSITE_OFF_RPM = 300;
@ -94,11 +94,15 @@ public class BinaryProtocol implements BinaryProtocolCommands {
}
public boolean isClosed;
/**
* Snapshot of current gauges status
* @see Fields#TS_OUTPUT_COMMAND
*/
public byte[] currentOutputs;
public byte[] getCurrentOutputs() {
return state.getCurrentOutputs();
}
public void setCurrentOutputs(byte[] currentOutputs) {
state.setCurrentOutputs(currentOutputs);
}
private SensorCentral.SensorListener rpmListener = value -> {
if (value <= COMPOSITE_OFF_RPM) {
needCompositeLogger = true;
@ -467,20 +471,14 @@ public class BinaryProtocol implements BinaryProtocolCommands {
}
public void setController(ConfigurationImage controller) {
synchronized (imageLock) {
this.controller = controller.clone();
}
state.setController(controller);
}
/**
* Configuration as it is in the controller to the best of our knowledge
*/
public ConfigurationImage getControllerConfiguration() {
synchronized (imageLock) {
if (controller == null)
return null;
return controller.clone();
}
return state.getControllerConfiguration();
}
private void sendPacket(byte[] command) throws IOException {
@ -561,7 +559,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
if (response == null || response.length != (Fields.TS_OUTPUT_SIZE + 1) || response[0] != RESPONSE_OK)
return false;
currentOutputs = response;
state.setCurrentOutputs(response);
for (Sensor sensor : Sensor.values()) {
if (sensor.getType() == null) {
@ -603,8 +601,6 @@ public class BinaryProtocol implements BinaryProtocolCommands {
}
public void setRange(byte[] src, int scrPos, int offset, int count) {
synchronized (imageLock) {
System.arraycopy(src, scrPos, controller.getContent(), offset, count);
}
state.setRange(src, scrPos, offset, count);
}
}

View File

@ -0,0 +1,42 @@
package com.rusefi.binaryprotocol;
import com.opensr5.ConfigurationImage;
import com.rusefi.config.generated.Fields;
public class BinaryProtocolState {
private final Object imageLock = new Object();
private ConfigurationImage controller;
/**
* Snapshot of current gauges status
* @see Fields#TS_OUTPUT_COMMAND
*/
private byte[] currentOutputs;
public void setController(ConfigurationImage controller) {
synchronized (imageLock) {
this.controller = controller.clone();
}
}
public byte[] getCurrentOutputs() {
return currentOutputs;
}
public void setCurrentOutputs(byte[] currentOutputs) {
this.currentOutputs = currentOutputs;
}
public ConfigurationImage getControllerConfiguration() {
synchronized (imageLock) {
if (controller == null)
return null;
return controller.clone();
}
}
public void setRange(byte[] src, int scrPos, int offset, int count) {
synchronized (imageLock) {
System.arraycopy(src, scrPos, controller.getContent(), offset, count);
}
}
}

View File

@ -126,7 +126,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
byte[] response = new byte[1 + count];
response[0] = (byte) TS_OK.charAt(0);
BinaryProtocol bp = linkManager.getCurrentStreamState();
byte[] currentOutputs = bp.currentOutputs;
byte[] currentOutputs = bp.getCurrentOutputs();
if (currentOutputs != null)
System.arraycopy(currentOutputs, 1 + offset , response, 1, count);
stream.sendPacket(response, FileLog.LOGGER);

View File

@ -5,7 +5,6 @@ import com.rusefi.FileLog;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.IoStream;
import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager;
import com.rusefi.io.tcp.BinaryProtocolServer;
@ -41,7 +40,7 @@ class BinaryProtocolServerSandbox {
}
});
bp.setController(new ConfigurationImage(new byte[Fields.TOTAL_CONFIG_SIZE]));
bp.currentOutputs = new byte[1 + Fields.TS_OUTPUT_SIZE];
bp.setCurrentOutputs(new byte[1 + Fields.TS_OUTPUT_SIZE]);
BinaryProtocolServer.start(linkManager);
}
}