summaryrefslogtreecommitdiff
path: root/graph/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'graph/src/main/java')
-rw-r--r--graph/src/main/java/com/seaofnodes/graph/GraphCapture.java31
-rw-r--r--graph/src/main/java/com/seaofnodes/graph/GraphViewer.java45
2 files changed, 44 insertions, 32 deletions
diff --git a/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java b/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java
index 0e2f538d..6fc40b5f 100644
--- a/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java
+++ b/graph/src/main/java/com/seaofnodes/graph/GraphCapture.java
@@ -5,23 +5,43 @@ import java.util.ArrayList;
import java.util.BitSet;
import java.util.UUID;
-/** Shared capture, event nesting and neighborhoods. Chapter hooks only read the IR. */
+/** Shared compilation lifecycle, capture, event nesting and neighborhoods. */
public abstract class GraphCapture<N> extends GraphObserver<N> {
private final GraphAdapter<N> _graph;
- private final String _comp = UUID.randomUUID().toString();
+ private String _comp;
+ private GraphSocket _server;
private final ArrayList<Peep> _peeps = new ArrayList<>();
private long _next, _step;
- private boolean _off;
+ private boolean _off = true;
protected GraphCapture(GraphAdapter<N> graph) { _graph = graph; }
+ // Create the chapter's compiler, attach this observer, and run its phases.
+ protected abstract void compile(String src);
+ // The remaining hooks only inspect context, except detach which clears it.
protected abstract String phase();
protected abstract int pos();
protected abstract void roots(ArrayList<N> roots);
protected abstract void detach();
- protected abstract void frame(GraphSnapshot snap, int pos, GraphEvent evt) throws IOException;
+ final void run(String src, GraphSocket server) {
+ _comp = UUID.randomUUID().toString();
+ _next = _step = 0;
+ _server = server;
+ _off = false;
+ try {
+ compile(src);
+ } finally {
+ _off = true;
+ try {
+ detach();
+ } finally {
+ _peeps.clear();
+ _server = null;
+ }
+ }
+ }
private class Peep {
final N node;
@@ -114,11 +134,10 @@ public abstract class GraphCapture<N> extends GraphObserver<N> {
// Assign steps on emission; attempts without progress do not consume frames.
snap = new GraphSnapshot(snap.ver(), _comp, _step++, snap.roots(), snap.nodes());
try {
- frame(snap, pos, evt);
+ _server.put(GraphJson.frame(snap, pos, evt));
} catch( IOException e ) {
// A disconnected display must not interrupt optimization.
_off = true;
- detach();
System.err.println("Graph viewer disconnected: " + e.getMessage());
}
}
diff --git a/graph/src/main/java/com/seaofnodes/graph/GraphViewer.java b/graph/src/main/java/com/seaofnodes/graph/GraphViewer.java
index dcb2e337..e70a12cf 100644
--- a/graph/src/main/java/com/seaofnodes/graph/GraphViewer.java
+++ b/graph/src/main/java/com/seaofnodes/graph/GraphViewer.java
@@ -5,16 +5,9 @@ import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
-/** Shared launcher and source/compile loop. Subclasses supply their compiler. */
-public abstract class GraphViewer implements AutoCloseable {
- private final GraphSocket _server;
-
- protected GraphViewer() throws IOException { _server = new GraphSocket(viewerURI(), 12345); }
- protected abstract void compile(String src);
-
- protected final void frame(GraphSnapshot snap, int pos, GraphEvent evt) throws IOException {
- _server.put(GraphJson.frame(snap, pos, evt));
- }
+/** Shared launcher and source/compile loop. Chapter observers supply their compiler. */
+public final class GraphViewer {
+ private GraphViewer() {}
private static URI viewerURI() throws IOException {
String url = System.getProperty("simple.graph.url");
@@ -27,23 +20,23 @@ public abstract class GraphViewer implements AutoCloseable {
+ "or set -Dsimple.graph.url=<viewer URL>");
}
- public final void run() throws IOException {
- _server.put("!");
- while( true ) {
- String src = _server.get();
- if( src == null || src.equals("null") ) return;
- if( src.equals("+") ) continue; // Frames are currently sent eagerly.
- System.out.println(src);
- try {
- compile(src);
- } catch( RuntimeException e ) {
- System.err.println(e);
- _server.put(GraphJson.error(e.toString()));
- } finally {
- _server.put("#");
+ public static void run(GraphCapture<?> obs) throws IOException {
+ try( var server = new GraphSocket(viewerURI(), 12345) ) {
+ server.put("!");
+ while( true ) {
+ String src = server.get();
+ if( src == null || src.equals("null") ) return;
+ if( src.equals("+") ) continue; // Frames are currently sent eagerly.
+ System.out.println(src);
+ try {
+ obs.run(src, server);
+ } catch( RuntimeException e ) {
+ System.err.println(e);
+ server.put(GraphJson.error(e.toString()));
+ } finally {
+ server.put("#");
+ }
}
}
}
-
- @Override public final void close() throws IOException { _server.close(); }
}