2 using MoonshotRpcInterface;
4 /* Apologies in advance */
5 [CCode (cname = "g_strdup")]
6 public extern char *strdup (string str);
8 /* This class is the closure when we pass execution from the RPC thread
9 * to the GLib main loop thread; we need to be executing inside the main
10 * loop before we can access any state or make any Gtk+ calls.
12 /* Fixme: can you make *this* an async callback? */
13 public class IdentityRequest : Object {
14 private MainWindow main_window;
15 private unowned Mutex mutex;
16 private unowned Cond cond;
18 internal IdCard? id_card = null;
20 public IdentityRequest (Gtk.Window window,
24 main_window = (MainWindow)window;
29 public bool main_loop_cb ()
31 // Execution is passed from the RPC get_identity() call to
32 // here, where we are inside the main loop thread.
33 main_window.set_callback (this.id_card_selected_cb);
37 public bool id_card_selected_cb ()
39 this.id_card = this.main_window.selected_id_card_widget.id_card;
44 // Block the mainloop until the ID card details have been read and
45 // sent, to prevent races
53 /* This class must be a singleton, because we use a global RPC
54 * binding handle. I cannot picture a situation where more than
55 * one instance of the same interface would be needed so this
56 * shouldn't be a problem.
58 * Shutdown is automatically done by the RPC runtime when the
61 public class MoonshotServer : Object {
62 private static MainWindow main_window;
64 private static MoonshotServer instance = null;
66 public static void start (Gtk.Window window)
68 main_window = (MainWindow) window;
69 Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot");
72 public static MoonshotServer get_instance ()
75 instance = new MoonshotServer ();
79 [CCode (cname = "moonshot_get_identity")]
80 public static void moonshot_get_identity (Rpc.AsyncCall call,
86 char **certificate_out)
88 Mutex mutex = new Mutex ();
89 Cond cond = new Cond ();
94 IdentityRequest request = new IdentityRequest (main_window, mutex, cond);
96 // Pass execution to the main loop thread and wait for
97 // the 'send' action to be signalled.
98 Idle.add (request.main_loop_cb);
99 while (request.id_card == null)
102 // Send back the results. Memory is freed by the RPC runtime.
103 if (request.id_card.nai == nai || request.id_card.password == password)
105 *nai_out = strdup (request.id_card.nai);
106 *password_out = strdup (request.id_card.password);
107 *certificate_out = strdup ("certificate");
113 *password_out = null;
114 *certificate_out = null;
118 call.return (&result);