1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c Message dispatcher
4 * Copyright (C) 2003 CodeFactory AB
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
25 #include "connection.h"
31 #include <dbus/dbus-internals.h>
34 static int message_handler_slot;
39 BusTransaction *transaction;
44 send_one_message (DBusConnection *connection, void *data)
46 SendMessageData *d = data;
48 if (!bus_connection_is_active (connection))
51 if (!bus_transaction_send_message (d->transaction,
55 BUS_SET_OOM (d->error);
63 bus_dispatch_broadcast_message (BusTransaction *transaction,
69 BusConnections *connections;
71 _dbus_assert (dbus_message_get_sender (message) != NULL);
73 connections = bus_transaction_get_connections (transaction);
75 dbus_error_init (&tmp_error);
77 d.transaction = transaction;
80 bus_connections_foreach (connections, send_one_message, &d);
82 if (dbus_error_is_set (&tmp_error))
84 dbus_move_error (&tmp_error, error);
92 send_service_nonexistent_error (BusTransaction *transaction,
93 DBusConnection *connection,
94 const char *service_name,
95 DBusMessage *in_reply_to,
98 DBusMessage *error_reply;
99 DBusString error_message;
100 const char *error_str;
102 /* Trying to send a message to a non-existant service,
103 * bounce back an error message.
106 if (!_dbus_string_init (&error_message, _DBUS_INT_MAX))
112 if (!_dbus_string_append (&error_message, "Service \"") ||
113 !_dbus_string_append (&error_message, service_name) ||
114 !_dbus_string_append (&error_message, "\" does not exist"))
116 _dbus_string_free (&error_message);
121 _dbus_string_get_const_data (&error_message, &error_str);
122 error_reply = dbus_message_new_error_reply (in_reply_to,
123 DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
126 _dbus_string_free (&error_message);
128 if (error_reply == NULL)
134 if (!bus_transaction_send_message (transaction, connection, error_reply))
136 dbus_message_unref (error_reply);
141 dbus_message_unref (error_reply);
147 bus_dispatch (DBusConnection *connection,
148 DBusMessage *message)
150 const char *sender, *service_name, *message_name;
152 BusTransaction *transaction;
156 dbus_error_init (&error);
158 context = bus_connection_get_context (connection);
159 _dbus_assert (context != NULL);
161 /* If we can't even allocate an OOM error, we just go to sleep
164 while (!bus_connection_preallocate_oom_error (connection))
165 bus_wait_for_memory ();
167 /* Ref connection in case we disconnect it at some point in here */
168 dbus_connection_ref (connection);
170 service_name = dbus_message_get_service (message);
171 message_name = dbus_message_get_name (message);
173 _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */
175 /* If service_name is NULL, this is a message to the bus daemon, not intended
176 * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these
177 * immediately, especially disconnection messages.
179 if (service_name == NULL)
181 if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
182 bus_connection_disconnected (connection);
184 /* DBusConnection also handles some of these automatically, we leave
190 _dbus_assert (service_name != NULL); /* this message is intended for bus routing */
192 /* Create our transaction */
193 transaction = bus_transaction_new (context);
194 if (transaction == NULL)
196 BUS_SET_OOM (&error);
200 /* Assign a sender to the message */
201 if (bus_connection_is_active (connection))
203 sender = bus_connection_get_name (connection);
204 _dbus_assert (sender != NULL);
206 if (!dbus_message_set_sender (message, sender))
208 BUS_SET_OOM (&error);
213 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
215 if (!bus_driver_handle_message (connection, transaction, message, &error))
218 else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
220 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
221 dbus_connection_disconnect (connection);
223 /* FIXME what if we un-special-case this service and just have a flag
224 * on services that all service owners will get messages to it, not just
227 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */
229 if (!bus_dispatch_broadcast_message (transaction, message, &error))
232 else /* route to named service */
234 DBusString service_string;
236 BusRegistry *registry;
238 registry = bus_connection_get_registry (connection);
240 _dbus_string_init_const (&service_string, service_name);
241 service = bus_registry_lookup (registry, &service_string);
245 if (!send_service_nonexistent_error (transaction, connection,
252 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
254 /* Dispatch the message */
255 if (!bus_transaction_send_message (transaction,
256 bus_service_get_primary_owner (service),
259 BUS_SET_OOM (&error);
266 if (dbus_error_is_set (&error))
268 if (!dbus_connection_get_is_connected (connection))
270 /* If we disconnected it, we won't bother to send it any error
274 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
276 bus_connection_send_oom_error (connection, message);
278 /* cancel transaction due to OOM */
279 if (transaction != NULL)
281 bus_transaction_cancel_and_free (transaction);
287 /* Try to send the real error, if no mem to do that, send
290 _dbus_assert (transaction != NULL);
292 if (!bus_transaction_send_error_reply (transaction, connection,
295 bus_connection_send_oom_error (connection, message);
297 /* cancel transaction due to OOM */
298 if (transaction != NULL)
300 bus_transaction_cancel_and_free (transaction);
306 dbus_error_free (&error);
309 if (transaction != NULL)
311 bus_transaction_execute_and_free (transaction);
314 dbus_connection_unref (connection);
317 static DBusHandlerResult
318 bus_dispatch_message_handler (DBusMessageHandler *handler,
319 DBusConnection *connection,
320 DBusMessage *message,
323 bus_dispatch (connection, message);
325 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
329 bus_dispatch_add_connection (DBusConnection *connection)
331 DBusMessageHandler *handler;
333 message_handler_slot = dbus_connection_allocate_data_slot ();
335 if (message_handler_slot < 0)
338 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
340 if (!dbus_connection_add_filter (connection, handler))
342 dbus_message_handler_unref (handler);
347 if (!dbus_connection_set_data (connection,
348 message_handler_slot,
350 (DBusFreeFunction)dbus_message_handler_unref))
352 dbus_connection_remove_filter (connection, handler);
353 dbus_message_handler_unref (handler);
362 bus_dispatch_remove_connection (DBusConnection *connection)
364 /* Here we tell the bus driver that we want to get off. */
365 bus_driver_remove_connection (connection);
367 dbus_connection_set_data (connection,
368 message_handler_slot,
374 #ifdef DBUS_BUILD_TESTS
377 run_test_bus (BusContext *context)
384 check_hello_message (BusContext *context,
385 DBusConnection *connection)
387 DBusMessage *message;
390 message = dbus_message_new (DBUS_SERVICE_DBUS,
394 _dbus_assert_not_reached ("no memory");
396 if (!dbus_connection_send (connection, message, &serial))
397 _dbus_assert_not_reached ("no memory");
403 bus_dispatch_test (const DBusString *test_data_dir)
407 const char *activation_dirs[] = { NULL, NULL };
411 DBusResultCode result;
413 return TRUE; /* FIXME */
415 dbus_error_init (&error);
416 context = bus_context_new ("debug:name=test-server",
420 _dbus_assert_not_reached ("could not alloc context");
422 foo = dbus_connection_open ("debug:name=test-server", &result);
424 _dbus_assert_not_reached ("could not alloc connection");
426 bar = dbus_connection_open ("debug:name=test-server", &result);
428 _dbus_assert_not_reached ("could not alloc connection");
430 baz = dbus_connection_open ("debug:name=test-server", &result);
432 _dbus_assert_not_reached ("could not alloc connection");
438 #endif /* DBUS_BUILD_TESTS */