Skip to content

Commit

Permalink
Project system & auto recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
serivesmejia committed Sep 19, 2024
1 parent d03ffdf commit 78ece97
Show file tree
Hide file tree
Showing 60 changed files with 1,832 additions and 180 deletions.
7 changes: 6 additions & 1 deletion EOCVSimPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ dependencies {
implementation project(":LwjglPlatform")

// compileOnly 'com.github.deltacv.EOCV-Sim:EOCV-Sim:3.6.0'
compileOnly 'com.github.deltacv:EOCV-Sim:4.0.0-dev-240912-0046'
compileOnly 'com.github.deltacv:EOCV-Sim:4.0.0-dev-240917-1612'

compileOnly "io.github.spair:imgui-java-app:$imgui_version"

implementation 'org.codehaus.janino:janino:3.1.12'

implementation "org.slf4j:slf4j-api:$slf4j_version"

implementation "io.javalin:javalin:6.3.0"
implementation 'com.google.code.gson:gson:2.8.9'

implementation 'org.java-websocket:Java-WebSocket:1.5.7'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.deltacv.papervision.plugin.project;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

public class PaperVisionProject {

public long timestamp;
public String path;
public String name;
public JsonElement json;

private static final Gson gson = new Gson();

public PaperVisionProject(long timestamp, String path, String name, JsonElement json) {
this.timestamp = timestamp;
this.path = path;
this.name = name;
this.json = json;
}

public static PaperVisionProject fromJson(String jsonString) {
return gson.fromJson(jsonString, PaperVisionProject.class);
}

public String toJson() {
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.deltacv.papervision.plugin.project.recovery;

import com.google.gson.Gson;
import io.github.deltacv.papervision.plugin.project.PaperVisionProject;

public class RecoveredProject {
public String originalProjectPath;
public long date;
public String hash;
public PaperVisionProject project;

private static final Gson gson = new Gson();

public RecoveredProject(String originalProjectPath, long date, String hash, PaperVisionProject project) {
this.originalProjectPath = originalProjectPath;
this.date = date;
this.hash = hash;
this.project = project;
}

public String toJson() {
return gson.toJson(this);
}

@Override
public String toString() {
return "RecoveredProject{" +
"path='" + originalProjectPath + '\'' +
", date=" + date +
", hash='" + hash + '\'' +
'}';
}

public static RecoveredProject fromJson(String json) {
return gson.fromJson(json, RecoveredProject.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.github.deltacv.papervision.plugin.project.recovery;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RecoveryDaemonClient extends WebSocketClient {

public static final int MAX_CONNECTION_ATTEMPTS_BEFORE_EXITING = 3;

public RecoveryDaemonClient(int port) throws URISyntaxException {
super(new URI("ws://localhost:" + port));
}

@Override
public void onOpen(ServerHandshake handshakedata) {
System.out.println("Connected to recovery daemon server at port " + uri.getPort());
}

@Override
public void onMessage(String message) {
try {
RecoveryData recoveryData = RecoveryData.deserialize(message);
Path recoveryPath = Paths.get(recoveryData.recoveryFolderPath);

if (!Files.exists(recoveryPath)) {
Files.createDirectories(recoveryPath);
}

Path recoveryFilePath = recoveryPath.resolve(recoveryData.recoveryFileName);
Files.write(recoveryFilePath, recoveryData.projectData.toJson().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onClose(int code, String reason, boolean remote) {
}

@Override
public void onError(Exception ex) {
}

public static void main(String[] args) {
int port = args.length > 0 ? Integer.parseInt(args[0]) : 17112;

RecoveryDaemonClient client = null;
int connectionAttempts = 0;

try {
while(!Thread.interrupted()) {
if(connectionAttempts >= MAX_CONNECTION_ATTEMPTS_BEFORE_EXITING) {
System.out.println("Failed to connect to recovery daemon after " + MAX_CONNECTION_ATTEMPTS_BEFORE_EXITING + " attempts. Exiting...");
System.exit(0);
}

if(client == null || client.isClosed()) {
client = new RecoveryDaemonClient(port);
client.connect();
connectionAttempts += 1;
}

Thread.sleep(100);
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch(InterruptedException ignored) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.deltacv.papervision.plugin.project.recovery;

import com.google.gson.Gson;

public class RecoveryData {

public String recoveryFolderPath;
public String recoveryFileName;
public RecoveredProject projectData;

private static final Gson gson = new Gson();

public RecoveryData(String recoveryFolderPath, String recoveryFileName, RecoveredProject projectData) {
this.recoveryFolderPath = recoveryFolderPath;
this.recoveryFileName = recoveryFileName;
this.projectData = projectData;
}

public static String serialize(RecoveryData data) {
return gson.toJson(data);
}

public static RecoveryData deserialize(String json) {
return gson.fromJson(json, RecoveryData.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.deltacv.papervision.plugin

import com.github.serivesmejia.eocvsim.util.exception.handling.EOCVSimUncaughtExceptionHandler
import com.google.gson.JsonElement
import io.github.deltacv.papervision.platform.lwjgl.PaperVisionApp
import io.github.deltacv.papervision.serialization.PaperVisionSerializer
import io.github.deltacv.papervision.util.event.EventHandler
import io.github.deltacv.papervision.util.event.EventListener
import java.util.concurrent.Executors
import java.util.concurrent.Future
Expand All @@ -22,34 +24,65 @@ object PaperVisionDaemon {
private lateinit var app: PaperVisionApp
val paperVision get() = app.paperVision

val onAppInstantiate = EventHandler("PaperVisionDaemon-onAppInstantiate")

lateinit var paperVisionFuture: Future<*>
private set

var paperVisionWatchdogGrowled = false
private set

fun launchDaemonPaperVision(instantiator: () -> PaperVisionApp) {
paperVisionFuture = executorService.submit {
app = instantiator()
onAppInstantiate.run()
app.start()
}
}

executorService.submit {
while(!paperVisionFuture.isDone) {
Thread.sleep(100)
fun openProject(json: JsonElement) {
paperVision.onUpdate.doOnce {
PaperVisionSerializer.deserializeAndApply(json, paperVision)

if(!app.glfwWindow.visible) {
app.glfwWindow.visible = true
}
}
}

fun currentProjectJson() = PaperVisionSerializer.serialize(
paperVision.nodes.inmutable, paperVision.links.inmutable
)


fun currentProjectJsonTree() = PaperVisionSerializer.serializeToTree(
paperVision.nodes.inmutable, paperVision.links.inmutable
)

fun showPaperVision() {
app.glfwWindow.visible = true
}

fun hidePaperVision() {
app.glfwWindow.visible = false
}

fun invokeOnMainLoop(runnable: Runnable) =
paperVision.onUpdate.doOnce(runnable)

fun attachToMainLoop(listener: EventListener) =
paperVision.onUpdate(listener)

fun attachToEditorChange(listener: EventListener) =
paperVision.nodeEditor.onEditorChange(listener)

fun watchdog() {
if(paperVisionFuture.isDone) paperVisionFuture.get()
if(paperVisionFuture.isDone && !paperVisionWatchdogGrowled) {
paperVisionWatchdogGrowled = true
paperVisionFuture.get()
}
}

fun invokeLater(runnable: Runnable) = executorService.submit(runnable)

/**
* Stops the PaperVision process by shutting down the executor service.
*/
Expand Down
Loading

0 comments on commit 78ece97

Please sign in to comment.