summaryrefslogtreecommitdiffstats
path: root/gui/CoreConnector.vala
diff options
context:
space:
mode:
Diffstat (limited to 'gui/CoreConnector.vala')
-rw-r--r--gui/CoreConnector.vala72
1 files changed, 72 insertions, 0 deletions
diff --git a/gui/CoreConnector.vala b/gui/CoreConnector.vala
new file mode 100644
index 0000000..a3f0910
--- /dev/null
+++ b/gui/CoreConnector.vala
@@ -0,0 +1,72 @@
+using Erl;
+
+
+public class CoreConnector {
+ unowned Thread thread;
+ bool running;
+
+ private class TermStore {
+ public Erl.Term term;
+ }
+
+ public CoreConnector() {
+ running = false;
+ }
+
+ public bool start() {
+ if(running)
+ return true;
+
+ running = true;
+
+ try {
+ thread = Thread.create(receive, true);
+ return true;
+ } catch(ThreadError e) {
+ return false;
+ }
+ }
+
+ public void stop() {
+ if(!running)
+ return;
+
+ running = false;
+ thread.join();
+ }
+
+ private void* receive() {
+ Erl.Node node = Erl.Node("ephraim-gtk", "magiccookie", 0);
+ Erl.Connection con = node.connect("ephraim-core@avalon.local");
+
+ con.reg_send("ephraim", Erl.mk_self_pid(node));
+
+ while(running) {
+ TermStore response = new TermStore();
+ Erl.ReceiveType ret = con.receive(out response.term, 1000);
+
+ switch(ret) {
+ case Erl.ReceiveType.ERROR:
+ if(Erl.errno == Erl.Error.TIMEDOUT)
+ break;
+
+ running = false;
+ break;
+ case Erl.ReceiveType.TICK:
+ // Do nothing
+ break;
+ case Erl.ReceiveType.MSG:
+ Idle.add(() => {handleTerm(response); return false;});
+ break;
+ }
+ }
+
+ return null;
+ }
+
+ private void handleTerm(TermStore store) {
+ unowned Term term = store.term;
+ Erl.print_term(stdout, term);
+ stdout.printf("\n");
+ }
+}