1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* bus.c message bus context object
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "activation.h"
27 #include "connection.h"
30 #include <dbus/dbus-internals.h>
37 BusConnections *connections;
38 BusActivation *activation;
39 BusRegistry *registry;
43 server_watch_callback (DBusWatch *watch,
44 unsigned int condition,
47 BusContext *context = data;
49 dbus_server_handle_watch (context->server, watch, condition);
53 add_server_watch (DBusWatch *watch,
56 return bus_loop_add_watch (watch, server_watch_callback, context,
61 remove_server_watch (DBusWatch *watch,
64 bus_loop_remove_watch (watch, server_watch_callback, context);
69 server_timeout_callback (DBusTimeout *timeout,
72 dbus_timeout_handle (timeout);
76 add_server_timeout (DBusTimeout *timeout,
79 return bus_loop_add_timeout (timeout, server_timeout_callback, context, NULL);
83 remove_server_timeout (DBusTimeout *timeout,
86 bus_loop_remove_timeout (timeout, server_timeout_callback, context);
90 new_connection_callback (DBusServer *server,
91 DBusConnection *new_connection,
94 BusContext *context = data;
96 if (!bus_connections_setup_connection (context->connections, new_connection))
98 _dbus_verbose ("No memory to setup new connection\n");
100 /* if we don't do this, it will get unref'd without
101 * being disconnected... kind of strange really
102 * that we have to do this, people won't get it right
105 dbus_connection_disconnect (new_connection);
108 /* on OOM, we won't have ref'd the connection so it will die. */
112 bus_context_new (const char *address,
113 const char **service_dirs,
117 DBusResultCode result;
119 context = dbus_new0 (BusContext, 1);
126 context->refcount = 1;
128 context->address = _dbus_strdup (address);
129 if (context->address == NULL)
135 context->server = dbus_server_listen (address, &result);
136 if (context->server == NULL)
138 dbus_set_error (error, DBUS_ERROR_FAILED,
139 "Failed to start server on %s: %s\n",
140 address, dbus_result_to_string (result));
144 context->activation = bus_activation_new (context, address, service_dirs,
146 if (context->activation == NULL)
148 _DBUS_ASSERT_ERROR_IS_SET (error);
152 context->connections = bus_connections_new (context);
153 if (context->connections == NULL)
159 context->registry = bus_registry_new (context);
160 if (context->registry == NULL)
166 dbus_server_set_new_connection_function (context->server,
167 new_connection_callback,
170 if (!dbus_server_set_watch_functions (context->server,
171 (DBusAddWatchFunction) add_server_watch,
172 (DBusRemoveWatchFunction) remove_server_watch,
181 if (!dbus_server_set_timeout_functions (context->server,
182 (DBusAddTimeoutFunction) add_server_timeout,
183 (DBusRemoveTimeoutFunction) remove_server_timeout,
194 bus_context_unref (context);
199 bus_context_shutdown (BusContext *context)
201 if (context->server == NULL ||
202 !dbus_server_get_is_connected (context->server))
205 if (!dbus_server_set_watch_functions (context->server,
209 _dbus_assert_not_reached ("setting watch functions to NULL failed");
211 if (!dbus_server_set_timeout_functions (context->server,
215 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
217 dbus_server_disconnect (context->server);
221 bus_context_ref (BusContext *context)
223 _dbus_assert (context->refcount > 0);
224 context->refcount += 1;
228 bus_context_unref (BusContext *context)
230 _dbus_assert (context->refcount > 0);
231 context->refcount -= 1;
233 if (context->refcount == 0)
235 _dbus_verbose ("Finalizing bus context %p\n", context);
237 bus_context_shutdown (context);
239 if (context->connections)
241 bus_connections_unref (context->connections);
242 context->connections = NULL;
245 if (context->registry)
247 bus_registry_unref (context->registry);
248 context->registry = NULL;
251 if (context->activation)
253 bus_activation_unref (context->activation);
254 context->activation = NULL;
259 dbus_server_unref (context->server);
260 context->server = NULL;
263 dbus_free (context->address);
269 bus_context_get_registry (BusContext *context)
271 return context->registry;
275 bus_context_get_connections (BusContext *context)
277 return context->connections;
281 bus_context_get_activation (BusContext *context)
283 return context->activation;