143
remove_connection (GDBusConnection *connection, gboolean remote_peer_vanished,
144
GError *error, GDBusServer *server)
148
trace_object (server, "Removing [%p] from list of active connections",
150
list = (GSList*) g_object_steal_data (G_OBJECT (server),
151
ACTIVE_CONNECTIONS_KEY);
153
list = g_slist_remove (list, connection);
155
g_object_set_data_full (G_OBJECT (server), ACTIVE_CONNECTIONS_KEY,
156
list, (GDestroyNotify) g_slist_free);
160
connection_finalized (gpointer server, GObject *connection)
162
remove_connection ((GDBusConnection*) connection, FALSE, NULL,
163
(GDBusServer*) server);
138
initialize_server (DeeServer *self)
140
DeeServerPrivate *priv;
167
add_new_connection (GDBusServer *server, GDBusConnection *connection,
173
data = g_object_steal_data (G_OBJECT (connection), CONNECTION_ACCEPTED_KEY);
177
list = (GSList*) g_object_steal_data (G_OBJECT (server),
178
ACTIVE_CONNECTIONS_KEY);
179
list = g_slist_prepend (list, connection);
180
g_object_set_data_full (G_OBJECT (server), ACTIVE_CONNECTIONS_KEY,
181
list, (GDestroyNotify) g_slist_free);
183
g_signal_connect (connection, "closed",
184
G_CALLBACK (remove_connection), server);
185
/* the connections in our list are weak references, need to make sure
186
* they're not used after they're freed */
187
g_object_weak_ref (G_OBJECT (connection), connection_finalized, server);
190
/* accept the connection if any of the DeeServers accepted it */
195
server_toggle_cb (gpointer data, GObject *object, gboolean is_last_ref)
198
if (!is_last_ref) return;
200
g_hash_table_remove (active_servers, data);
202
g_dbus_server_stop (G_DBUS_SERVER (object));
204
list = (GSList*) g_object_get_data (object, ACTIVE_CONNECTIONS_KEY);
205
for (iter = list; iter != NULL; iter = iter->next)
207
g_object_weak_unref (iter->data, connection_finalized, object);
208
g_signal_handlers_disconnect_by_func (iter->data,
209
remove_connection, object);
212
/* and this will finalize the object */
213
g_object_remove_toggle_ref (object, server_toggle_cb, data);
217
get_server_for_address (const gchar *bus_address, GError **error)
142
222
GDBusServerFlags server_flags;
224
server = g_hash_table_lookup (active_servers, bus_address);
227
return g_object_ref (server);
230
/* create new GDBusServer instance */
231
guid = g_dbus_generate_guid ();
232
server_flags = G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
234
server = g_dbus_server_new_sync (bus_address, server_flags, guid,
237
if (error && *error) return NULL;
239
/* need to keep a list of all connections for this GDBusServer */
240
g_signal_connect_after (server, "new-connection",
241
G_CALLBACK (add_new_connection), NULL);
243
address = g_strdup (bus_address);
244
/* transfer ownership of address to the hash table */
245
g_hash_table_insert (active_servers, address, server);
246
/* we need to stop the server before unreffing it the last time, so we'll
247
* use toggle ref. FIXME: toggle ref is odd, use just weak ref (to remove
248
* it from the active_servers hash table) once it properly stops
249
* the listener on finalize. */
250
g_object_add_toggle_ref (G_OBJECT (server), server_toggle_cb, address);
258
initialize_server (DeeServer *self)
260
DeeServerPrivate *priv;
143
262
GError *error = NULL;
145
264
priv = self->priv;
147
266
priv->initialize_server_timer_id = 0;
149
guid = g_dbus_generate_guid ();
150
server_flags = G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
152
priv->server = g_dbus_server_new_sync (priv->bus_address,
268
/* create new server or get the existing instance for this bus_address */
269
priv->server = get_server_for_address (priv->bus_address, &error);
161
273
g_critical ("Unable to set up DBusServer: %s", error->message);
163
275
g_error_free (error);
166
277
g_object_notify (G_OBJECT (self), "swarm-leader");
357
488
* dee_server_new_for_address:
359
489
* @swarm_name: Name of swarm to join.
360
490
* @bus_address: D-Bus address to use for the connection.
362
492
* Creates a new instance of #DeeServer and tries to bind to @bus_address.
363
493
* The #DeePeer:swarm-leader property will be set when the binding succeeds.
495
* If there is already a #DeeServer instance bound to @bus_address,
496
* the connection will be shared with the newly constructed instance.
500
* This function is primarily meant for sharing of one connection (socket)
501
* between multiple DeeServers, so that you can create #DeeServer instances
502
* with varying swarm names, but the same bus address, which will cause
503
* them to share the connection (the sharing is possible only within
504
* the same process though).
365
508
* Return value: (transfer full): A newly constructed #DeeServer.
395
537
g_dbus_server_get_client_address (priv->server) : NULL;
541
* dee_server_bus_address_for_name:
542
* @name: A name to create bus address for.
543
* @include_username: Include current user name as part of the bus address.
545
* Helper method which creates bus address string for the given name, which
546
* should have the same format as a DBus unique name.
548
* Return value: (transfer full): Newly allocated string with bus address.
549
* Use g_free() to free.
552
dee_server_bus_address_for_name (const gchar *name, gboolean include_username)
556
g_return_val_if_fail (name != NULL, NULL);
558
if (g_unix_socket_address_abstract_names_supported ())
560
result = include_username ?
561
g_strdup_printf ("unix:abstract=%s-%s", g_get_user_name (), name) :
562
g_strdup_printf ("unix:abstract=%s", name);
566
result = include_username ?
567
g_strdup_printf ("unix:path=%s/%s-%s", g_get_tmp_dir (),
568
g_get_user_name (), name) :
569
g_strdup_printf ("unix:path=%s/%s", g_get_tmp_dir (), name);
398
575
/* Private Methods */
400
577
on_new_connection (GDBusServer *server,