Better boot code

This commit is contained in:
Andrea Cavalli 2019-04-09 18:19:06 +02:00
parent ff7dd21788
commit 60f91fbc2e
8 changed files with 137 additions and 138 deletions

View File

@ -18,8 +18,9 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

@ -1,6 +1,8 @@
package it.cavallium.warppi; package it.cavallium.warppi;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import it.cavallium.warppi.Platform.ConsoleUtils; import it.cavallium.warppi.Platform.ConsoleUtils;
import it.cavallium.warppi.boot.StartupArguments; import it.cavallium.warppi.boot.StartupArguments;
@ -15,15 +17,14 @@ import it.cavallium.warppi.flow.BehaviorSubject;
import it.cavallium.warppi.flow.Observable; import it.cavallium.warppi.flow.Observable;
import it.cavallium.warppi.gui.DisplayManager; import it.cavallium.warppi.gui.DisplayManager;
import it.cavallium.warppi.gui.HUD; import it.cavallium.warppi.gui.HUD;
import it.cavallium.warppi.gui.HardwareDisplay;
import it.cavallium.warppi.gui.screens.Screen; import it.cavallium.warppi.gui.screens.Screen;
import it.cavallium.warppi.util.ClassUtils; import it.cavallium.warppi.util.ClassUtils;
import it.cavallium.warppi.util.RunnableWithException;
public class WarpPI { public class WarpPI {
public static final WarpPI INSTANCE = new WarpPI(); public static final WarpPI INSTANCE = new WarpPI();
private static Platform platform; private static Platform platform;
private static boolean running = false; private static boolean running = false;
private static BehaviorSubject<LoadingStatus> loadPhase = BehaviorSubject.create();
private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false); private final BehaviorSubject<Boolean> loaded = BehaviorSubject.create(false);
private Device device; private Device device;
@ -43,19 +44,18 @@ public class WarpPI {
* @throws InterruptedException * @throws InterruptedException
* @throws IOException * @throws IOException
*/ */
public static void start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args) public static Future<Void> start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args, final RunnableWithException onLoading) throws IOException {
throws InterruptedException, IOException {
if (WarpPI.running) { if (WarpPI.running) {
throw new RuntimeException("Already running!"); throw new RuntimeException("Already running!");
} else { } else {
WarpPI.running = true; WarpPI.running = true;
WarpPI.INSTANCE.startEngine(platform, screen, hud, args); return WarpPI.INSTANCE.startInstance(platform, screen, hud, args, onLoading);
} }
} }
private void startEngine(final Platform platform, final Screen screen, private Future<Void> startInstance(final Platform platform, final Screen screen,
final HUD hud, final StartupArguments args) final HUD hud, final StartupArguments args, final RunnableWithException onLoading)
throws InterruptedException, IOException { throws IOException {
WarpPI.platform = platform; WarpPI.platform = platform;
platform.getConsoleUtils().out().println("WarpPI Calculator"); platform.getConsoleUtils().out().println("WarpPI Calculator");
initializeEnvironment(args); initializeEnvironment(args);
@ -63,6 +63,9 @@ public class WarpPI {
final Thread currentThread = Thread.currentThread(); final Thread currentThread = Thread.currentThread();
currentThread.setPriority(Thread.MAX_PRIORITY); currentThread.setPriority(Thread.MAX_PRIORITY);
WarpPI.getPlatform().setThreadName(currentThread, "Main thread"); WarpPI.getPlatform().setThreadName(currentThread, "Main thread");
return CompletableFuture.runAsync(() -> {
try {
final DisplayOutputDevice display = platform.getDisplayOutputDevice(); final DisplayOutputDevice display = platform.getDisplayOutputDevice();
final BacklightOutputDevice backlight = platform.getBacklightOutputDevice(); final BacklightOutputDevice backlight = platform.getBacklightOutputDevice();
final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)"); final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
@ -70,8 +73,13 @@ public class WarpPI {
final TouchInputDevice touchscreen = platform.getTouchInputDevice(); final TouchInputDevice touchscreen = platform.getTouchInputDevice();
final InputManager im = new InputManager(keyboard, touchscreen); final InputManager im = new InputManager(keyboard, touchscreen);
device = new Device(dm, im); device = new Device(dm, im);
device.setup();
device.setup(() -> WarpPI.loadPhase.onNext(new LoadingStatus())); onLoading.run();
this.loadingCompleted();
} catch (Exception ex) {
this.loadingFailed(ex);
}
}).thenRun(this::onShutdown);
} }
private void onShutdown() { private void onShutdown() {
@ -117,10 +125,6 @@ public class WarpPI {
return loaded; return loaded;
} }
public Observable<LoadingStatus> getLoadPhase() {
return WarpPI.loadPhase;
}
public Device getHardwareDevice() { public Device getHardwareDevice() {
return device; return device;
} }
@ -129,19 +133,13 @@ public class WarpPI {
return WarpPI.platform; return WarpPI.platform;
} }
public static class LoadingStatus {
protected LoadingStatus() {
} private void loadingCompleted() {
public void done() {
WarpPI.INSTANCE.loaded.onNext(true); WarpPI.INSTANCE.loaded.onNext(true);
WarpPI.INSTANCE.device.getDisplayManager().waitForExit(); WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
WarpPI.INSTANCE.onShutdown();
} }
public void failed() { private void loadingFailed(Exception e) {
WarpPI.INSTANCE.onShutdown(); e.printStackTrace();
}
} }
} }

View File

@ -1,35 +1,32 @@
package it.cavallium.warppi.boot; package it.cavallium.warppi.boot;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.Future;
import it.cavallium.warppi.WarpPI; import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.WarpPI.LoadingStatus;
import it.cavallium.warppi.Platform; import it.cavallium.warppi.Platform;
import it.cavallium.warppi.device.PIHardwareDisplay;
import it.cavallium.warppi.device.input.PIHardwareTouchDevice; import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
import it.cavallium.warppi.gui.CalculatorHUD; import it.cavallium.warppi.gui.CalculatorHUD;
import it.cavallium.warppi.gui.screens.LoadingScreen; import it.cavallium.warppi.gui.screens.LoadingScreen;
import it.cavallium.warppi.math.rules.RulesManager; import it.cavallium.warppi.math.rules.RulesManager;
import it.cavallium.warppi.util.Error;
public class Boot { public class Boot {
public static void boot(final Platform platform, final String[] args) throws Exception { public static void boot(final Platform platform, final String[] args) throws Exception {
WarpPI.start(platform, new LoadingScreen(), new CalculatorHUD(), Boot.parseStartupArguments(args)); Future<Void> execution = WarpPI.start(
WarpPI.INSTANCE.getLoadPhase().subscribe(Boot::loadCalculator); platform,
new LoadingScreen(),
new CalculatorHUD(),
Boot.parseStartupArguments(args),
Boot::loadCalculator);
execution.get();
} }
private static void loadCalculator(final LoadingStatus loading) { private static void loadCalculator() throws Exception {
try {
WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f); WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
//TODO: plugins system: PluginsManager.initialize(); //TODO: plugins system: PluginsManager.initialize();
RulesManager.initialize(); RulesManager.initialize();
RulesManager.warmUp(); RulesManager.warmUp();
loading.done();
} catch (InterruptedException | Error e) {
e.printStackTrace();
}
loading.failed();
} }
public static StartupArguments parseStartupArguments(final String[] a) { public static StartupArguments parseStartupArguments(final String[] a) {

View File

@ -21,12 +21,8 @@ public class Device {
return inputManager; return inputManager;
} }
public void setup(final Runnable r) { public void setup() {
displayManager.initialize(); displayManager.initialize();
final Thread t = new Thread(r);
WarpPI.getPlatform().setThreadDaemon(t, false);
WarpPI.getPlatform().setThreadName(t, "Main thread (after setup)");
t.start();
} }
} }

View File

@ -0,0 +1,5 @@
package it.cavallium.warppi.util;
public interface RunnableWithException {
public void run() throws Exception;
}

View File

@ -23,8 +23,9 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

@ -8,13 +8,14 @@
</classpathentry> </classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> <classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes> <attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes> <attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>

View File

@ -1,6 +1,6 @@
package it.cavallium.warppi.util; package it.cavallium.warppi.util;
public class Error extends java.lang.Throwable { public class Error extends java.lang.Exception {
/** /**
* *