summaryrefslogtreecommitdiffstats
path: root/gui/CoreConnector.vala
blob: a3f09109bbfec47e2991c0c03b09ce68963bb16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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");
  }
}