Add MSRPC client and fix server code
[moonshot-ui.git] / src / moonshot-msrpc-server.vala
1 using Rpc;
2 using MoonshotRpcInterface;
3
4 /* This class is the closure when we pass execution from the RPC thread
5  * to the GLib main loop thread; we need to be executing inside the main
6  * loop before we can access any state or make any Gtk+ calls.
7  */
8 public class IdentityRequest : Object {
9     private Rpc.AsyncCall call;
10     private MainWindow main_window;
11     private Identity **result;
12
13     public IdentityRequest (Rpc.AsyncCall _call,
14                             Gtk.Window window,
15                             Identity **_result)
16     {
17         call = _call;
18         main_window = (MainWindow)window;
19         result = _result;
20     }
21
22     public bool main_loop_cb ()
23     {
24         // Execution is passed from the RPC get_identity() call to
25         // here, where we are inside the main loop thread.
26         main_window.set_callback (this.id_card_selected_cb);
27         return false;
28     }
29
30     public bool id_card_selected_cb ()
31     {
32         var id_card = this.main_window.selected_id_card_widget.id_card;
33
34         *result = new Identity();
35
36         (*result)->identity = "identity";
37         (*result)->password = id_card.password;
38         (*result)->service = "certificate";
39
40         call.return (null);
41
42         //delete result;
43
44         return false;
45     }
46 }
47
48 /* This class must be a singleton, because we use a global RPC
49  * binding handle. I cannot picture a situation where more than
50  * one instance of the same interface would be needed so this
51  * shouldn't be a problem.
52  *
53  * Shutdown is automatically done by the RPC runtime when the
54  * process ends
55  */
56 public class MoonshotServer : Object {
57     private static int counter;
58     private static MainWindow main_window;
59
60     private static MoonshotServer instance = null;
61
62     public static void start (Gtk.Window window)
63     {
64         main_window = (MainWindow) window;
65         Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot");
66     }
67
68     public static MoonshotServer get_instance ()
69     {
70         if (instance == null)
71             instance = new MoonshotServer ();
72         return instance;
73     }
74
75     /* Note that these RPC callbacks execute outside the GLib main loop,
76      * in threads owned by the RPC runtime
77      */
78
79     [CCode (cname = "moonshot_ping")]
80     public static int ping (string msg)
81     {
82         stdout.printf ("%s\n", msg);
83         return counter ++;
84     }
85
86     [CCode (cname = "moonshot_get_identity")]
87     public static void moonshot_get_identity (Rpc.AsyncCall call,
88                                               string in_identity,
89                                               string in_password,
90                                               string in_service,
91                                               Identity **result)
92     {
93         IdentityRequest request = new IdentityRequest (call, main_window, result);
94
95         // Pass execution to the main loop thread
96         Idle.add (request.main_loop_cb);
97     }
98 }