SimulatorTcpSandbox

This commit is contained in:
rusefillc 2021-12-06 18:55:33 -05:00
parent 546f43b7d0
commit 0a3d42f9c3
5 changed files with 32 additions and 21 deletions

View File

@ -228,7 +228,7 @@ public class BinaryProtocol {
*
* @return true if everything fine
*/
public boolean connectAndReadConfiguration(DataListener listener) {
public boolean connectAndReadConfiguration(Arguments arguments, DataListener listener) {
try {
signature = getSignature(stream);
log.info("Got " + signature + " signature");
@ -236,7 +236,7 @@ public class BinaryProtocol {
} catch (IOException e) {
return false;
}
readImage(Fields.TOTAL_CONFIG_SIZE);
readImage(arguments, Fields.TOTAL_CONFIG_SIZE);
if (isClosed)
return false;
@ -340,11 +340,11 @@ public class BinaryProtocol {
/**
* read complete tune from physical data stream
*/
public void readImage(int size) {
public void readImage(Arguments arguments, int size) {
ConfigurationImage image = getAndValidateLocallyCached();
if (image == null) {
image = readFullImageFromController(size);
image = readFullImageFromController(arguments, size);
if (image == null)
return;
}
@ -362,7 +362,7 @@ public class BinaryProtocol {
}
@Nullable
private ConfigurationImage readFullImageFromController(int size) {
private ConfigurationImage readFullImageFromController(Arguments arguments, int size) {
ConfigurationImage image;
image = new ConfigurationImage(size);
@ -398,12 +398,14 @@ public class BinaryProtocol {
offset += requestSize;
}
try {
ConfigurationImageFile.saveToFile(image, CONFIGURATION_RUSEFI_BINARY);
Msq tune = MsqFactory.valueOf(image);
tune.writeXmlFile(CONFIGURATION_RUSEFI_XML);
} catch (Exception e) {
System.err.println("Ignoring " + e);
if (arguments.saveFile) {
try {
ConfigurationImageFile.saveToFile(image, CONFIGURATION_RUSEFI_BINARY);
Msq tune = MsqFactory.valueOf(image);
tune.writeXmlFile(CONFIGURATION_RUSEFI_XML);
} catch (Exception e) {
System.err.println("Ignoring " + e);
}
}
return image;
}

View File

@ -32,7 +32,7 @@ public class BufferedSerialIoStream extends SerialIoStream {
/**
* Just open physical serial and not much more
* @see PortHolder#connectAndReadConfiguration()
* @see PortHolder#connectAndReadConfiguration(com.rusefi.binaryprotocol.BinaryProtocol.Arguments)
*/
public static IoStream openPort(String port) {
log.info("[BufferedSerialIoStream] openPort " + port);

View File

@ -39,7 +39,7 @@ public class PortHolder {
this.ioStreamFactory = ioStreamFactory;
}
boolean connectAndReadConfiguration() {
boolean connectAndReadConfiguration(BinaryProtocol.Arguments arguments) {
IoStream stream = ioStreamFactory.call();
if (stream == null) {
// error already reported
@ -50,7 +50,7 @@ public class PortHolder {
portLock.notifyAll();
}
boolean result = bp.connectAndReadConfiguration(dataListener);
boolean result = bp.connectAndReadConfiguration( arguments, dataListener);
if (listener != null) {
if (result) {
listener.onConnectionEstablished();

View File

@ -7,7 +7,6 @@ import com.rusefi.io.ConnectionStateListener;
import com.rusefi.io.IoStream;
import com.rusefi.io.LinkConnector;
import com.rusefi.io.LinkManager;
import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments;
import static com.devexperts.logging.Logging.getLogging;
@ -20,6 +19,7 @@ public class StreamConnector implements LinkConnector {
private final PortHolder portHolder;
private final LinkManager linkManager;
private BinaryProtocol.Arguments arguments;
public StreamConnector(LinkManager linkManager, Callable<IoStream> ioStreamCallable) {
this.linkManager = linkManager;
@ -29,12 +29,13 @@ public class StreamConnector implements LinkConnector {
@Override
public void connectAndReadConfiguration(BinaryProtocol.Arguments arguments, ConnectionStateListener listener) {
this.arguments = arguments;
log.info("StreamConnector: connecting");
portHolder.listener = listener;
log.info("scheduleOpening");
linkManager.execute(() -> {
log.info("scheduleOpening>openPort");
portHolder.connectAndReadConfiguration();
portHolder.connectAndReadConfiguration(arguments);
});
}
@ -53,7 +54,7 @@ public class StreamConnector implements LinkConnector {
linkManager.execute(() -> {
linkManager.messageListener.postMessage(StreamConnector.this.getClass(), "Restarting serial IO");
portHolder.close();
portHolder.connectAndReadConfiguration();
portHolder.connectAndReadConfiguration(arguments);
});
}

View File

@ -13,12 +13,14 @@ import jdk.nashorn.internal.runtime.regexp.joni.constants.Arguments;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import static com.rusefi.io.tcp.TcpConnector.DEFAULT_PORT;
import static com.rusefi.io.tcp.TcpConnector.LOCALHOST;
public class SimulatorTcpSandbox {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException, InterruptedException {
BinaryProtocol.DISABLE_LOCAL_CACHE = true;
Socket s = new Socket(LOCALHOST, DEFAULT_PORT);
@ -27,10 +29,12 @@ public class SimulatorTcpSandbox {
LinkManager linkManager = new LinkManager();
verifyCrcNoPending(tsStream, linkManager);
AtomicReference<ConfigurationImage> configurationImageAtomicReference = new AtomicReference<>();
CountDownLatch imageLatch = new CountDownLatch(1);
StreamConnector streamConnector = new StreamConnector(linkManager, () -> tsStream);
linkManager.setConnector(streamConnector);
streamConnector.connectAndReadConfiguration(new BinaryProtocol.Arguments( false), new ConnectionStateListener() {
streamConnector.connectAndReadConfiguration(new BinaryProtocol.Arguments(false), new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
System.out.println("onConnectionEstablished");
@ -41,8 +45,8 @@ public class SimulatorTcpSandbox {
} else {
BinaryProtocolState binaryProtocolState = currentStreamState.getBinaryProtocolState();
ConfigurationImage ci = binaryProtocolState.getControllerConfiguration();
System.out.println("Got ConfigurationImage " + ci);
System.exit(0);
configurationImageAtomicReference.set(ci);
imageLatch.countDown();
}
}
@ -52,6 +56,10 @@ public class SimulatorTcpSandbox {
}
});
imageLatch.await();
ConfigurationImage ci = configurationImageAtomicReference.get();
System.out.println("Got ConfigurationImage " + ci + ", " + ci.getSize());
System.exit(0);
}
private static void verifyCrcNoPending(IoStream tsStream, LinkManager linkManager) {