3 [DBus (name = "org.janet.Moonshot")]
4 public class MoonshotServer : Object {
6 private MainWindow main_window;
8 public MoonshotServer (Gtk.Window window)
10 this.main_window = (MainWindow) window;
14 * This is the function used by the GSS mechanism to get the NAI,
15 * password and certificate of the ID card for the specificated service.
17 * The function will block until the user choose the ID card.
19 * @param nai NAI of the ID Card (optional)
20 * @param password Password of the ID Card (optional)
21 * @param service Service application request an ID Card for
22 * @param nai_out NAI stored in the ID Card
23 * @param password_out Password stored in the ID Card
24 * @param certificate Certificate stored in th ID Card
26 * @return true if the user choose a correct ID card for that service,
29 public async bool get_identity (string nai,
33 out string password_out,
34 out string certificate_out)
36 bool has_service = false;
38 var request = new IdentityRequest (main_window,
42 request.set_callback ((IdentityRequest) => get_identity.callback());
50 var id_card = request.id_card;
52 if (id_card != null) {
53 foreach (string id_card_service in id_card.services)
55 if (id_card_service == service)
61 nai_out = id_card.nai;
62 password_out = id_card.password;
63 certificate_out = "certificate";
76 using MoonshotRpcInterface;
78 /* This class must be a singleton, because we use a global RPC
79 * binding handle. I cannot picture a situation where more than
80 * one instance of the same interface would be needed so this
81 * shouldn't be a problem.
83 * Shutdown is automatically done by the RPC runtime when the
86 public class MoonshotServer : Object {
87 private static MainWindow main_window;
89 private static MoonshotServer instance = null;
91 public static void start (Gtk.Window window)
93 main_window = (MainWindow) window;
94 Rpc.server_start (MoonshotRpcInterface.spec, "/org/janet/Moonshot", Rpc.Flags.PER_USER);
97 public static MoonshotServer get_instance ()
100 instance = new MoonshotServer ();
104 [CCode (cname = "moonshot_get_identity")]
105 public static void moonshot_get_identity (Rpc.AsyncCall call,
110 ref string password_out,
111 ref string certificate_out)
115 var request = new IdentityRequest (main_window,
120 // Pass execution to the main loop and block the RPC thread
121 request.mutex = new Mutex ();
122 request.cond = new Cond ();
123 request.set_callback (return_identity_cb);
125 request.mutex.lock ();
126 Idle.add (request.execute);
128 while (request.complete == false)
129 request.cond.wait (request.mutex);
133 certificate_out = "";
135 var id_card = request.id_card;
136 bool has_service = false;
138 if (id_card == null) {
139 foreach (string id_card_service in id_card.services)
141 if (id_card_service == service)
147 // The strings are freed by the RPC runtime
148 nai_out = id_card.nai;
149 password_out = id_card.password;
150 certificate_out = "certificate";
156 // The outputs must be set before this function is called. For this
157 // reason they are 'ref' not 'out' parameters - Vala assigns to the
158 // 'out' parameters only at the end of the function, which is too
160 call.return (&result);
162 request.cond.signal ();
163 request.mutex.unlock ();
166 [CCode (cname = "moonshot_get_default_identity")]
167 public static void get_default_identity (Rpc.AsyncCall call,
169 ref string password_out)
173 var request = new IdentityRequest.default (main_window);
175 // This is really a lot of work to get a default identity.
176 // However, it's really best to do so in the main loop to avoid
177 // races (the alternative is add a mutex to the ID card data,
178 // but this is the only place it would be used).
179 request.mutex = new Mutex ();
180 request.cond = new Cond ();
181 request.set_callback (return_identity_cb);
183 request.mutex.lock ();
184 Idle.add (request.execute);
186 while (request.complete == false)
187 request.cond.wait (request.mutex);
191 certificate_out = "";
193 var id_card = request.id_card;
194 bool has_service = false;
196 if (id_card == null) {
197 foreach (string id_card_service in id_card.services)
199 if (id_card_service == service)
205 // The strings are freed by the RPC runtime
206 nai_out = id_card.nai;
207 password_out = id_card.password;
208 certificate_out = "certificate";
214 // The outputs must be set before this function is called. For this
215 // reason they are 'ref' not 'out' parameters - Vala assigns to the
216 // 'out' parameters only at the end of the function, which is too
218 call.return (&result);
220 request.cond.signal ();
221 request.mutex.unlock ();
224 // Called from the main loop thread when an identity has
226 static void return_identity_cb (IdentityRequest request) {
227 // Notify the RPC thread that the request is complete
228 request.mutex.lock ();
229 request.cond.signal ();
231 // Block the main loop until the RPC call has returned
232 // to avoid any races
233 request.cond.wait (request.mutex);
234 request.mutex.unlock ();