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"); } }