Skip to content

Commit

Permalink
console: support launching simulator on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
nmschulte committed Mar 9, 2024
1 parent ced3ba4 commit 8da6d1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
31 changes: 24 additions & 7 deletions java_console/io/src/main/java/com/rusefi/SimulatorExecHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class SimulatorExecHelper {
private final static NamedThreadFactory THREAD_FACTORY = new NamedThreadFactory("SimulatorExecHelper", true);

// see also SimulatorHelper
private static final String SIMULATOR_BINARY = "../simulator/build/fome_simulator.exe";
public static final String SIMULATOR_BINARY_NAME = "fome_simulator";
private static final String SIMULATOR_BINARY_PATH = "../simulator/build";
static Process simulatorProcess;

/**
Expand All @@ -25,10 +25,11 @@ private static void runSimulator() {
FileLog.MAIN.logLine("runSimulator...");

try {
FileLog.MAIN.logLine("Binary size: " + new File(SIMULATOR_BINARY).length());
File binary = getSimulatorBinary();
FileLog.MAIN.logLine("Binary size: " + binary.length());

FileLog.MAIN.logLine("Executing " + SIMULATOR_BINARY);
SimulatorExecHelper.simulatorProcess = Runtime.getRuntime().exec(SIMULATOR_BINARY);
FileLog.MAIN.logLine("Executing " + binary.getPath());
SimulatorExecHelper.simulatorProcess = Runtime.getRuntime().exec(binary.getPath());
FileLog.MAIN.logLine("simulatorProcess: " + SimulatorExecHelper.simulatorProcess);

dumpProcessOutput(SimulatorExecHelper.simulatorProcess);
Expand Down Expand Up @@ -93,9 +94,25 @@ static void destroy() {
}

public static void startSimulator() {
if (!new File(SIMULATOR_BINARY).exists())
throw new IllegalStateException(SIMULATOR_BINARY + " not found");
getSimulatorBinary();

FileLog.MAIN.logLine("startSimulator...");
new Thread(SimulatorExecHelper::runSimulator, "simulator process").start();
}

private static File getSimulatorBinary() {
return getSimulatorBinary(SIMULATOR_BINARY_PATH + SIMULATOR_BINARY_NAME);
}

public static File getSimulatorBinary(String binaryPath) {
File binary = new File(binaryPath);

if (!binary.exists()) // try also for Windows/PE executable
binary = new File(binaryPath + ".exe");

if (!binary.exists() || binary.isDirectory() || !binary.canExecute())
throw new IllegalStateException("FOME Simulator program not found");

return binary;
}
}
22 changes: 11 additions & 11 deletions java_console/ui/src/main/java/com/rusefi/SimulatorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,25 @@

public class SimulatorHelper {
private final static ThreadFactory THREAD_FACTORY = new NamedThreadFactory("SimulatorHelper");
public static final String BINARY = "fome_simulator.exe";
private static Process process;

public static boolean isBinaryHere() {
return new File(BINARY).exists();
}

/**
* this code start sumulator for UI console
* todo: unify with the code which starts simulator for auto tests?
*/
private static void startSimulator() {
LinkManager.isSimulationMode = true;

FileLog.MAIN.logLine("Executing " + BINARY);
File binary = SimulatorExecHelper.getSimulatorBinary("./" + SimulatorExecHelper.SIMULATOR_BINARY_NAME);

FileLog.MAIN.logLine("Executing " + binary.getPath());
THREAD_FACTORY.newThread(new Runnable() {
@Override
public void run() {
try {
FileLog.SIMULATOR_CONSOLE.start();
process = Runtime.getRuntime().exec(BINARY);
FileLog.MAIN.logLine("Executing " + BINARY + "=" + process);
process = Runtime.getRuntime().exec(binary.getPath());
FileLog.MAIN.logLine("Executing " + binary.getPath() + "=" + process);
SimulatorExecHelper.dumpProcessOutput(process);
} catch (IOException e) {
throw new IllegalStateException(e);
Expand All @@ -62,8 +59,11 @@ public void run() {
}

public static JComponent createSimulatorComponent(final StartupFrame portSelector) {
if (!SimulatorHelper.isBinaryHere())
return new JLabel(SimulatorHelper.BINARY + " not found");
try {
SimulatorExecHelper.getSimulatorBinary(SimulatorExecHelper.SIMULATOR_BINARY_NAME);
} catch (IllegalStateException e) {
return new JLabel(e.getMessage());
}

if (TcpConnector.isTcpPortOpened())
return new JLabel("Port " + TcpConnector.DEFAULT_PORT + " already busy. Simulator running?");
Expand All @@ -87,4 +87,4 @@ public static void onWindowClosed() {
if (process != null)
process.destroy();
}
}
}

0 comments on commit 8da6d1b

Please sign in to comment.