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"
28 #include <dbus/dbus-internals.h>
31 static int message_handler_slot;
34 send_one_message (DBusConnection *connection, void *data)
36 BUS_HANDLE_OOM (dbus_connection_send_message (connection, data, NULL, NULL));
40 bus_dispatch_broadcast_message (DBusMessage *message)
42 _dbus_assert (dbus_message_get_sender (message) != NULL);
44 bus_connection_foreach (send_one_message, message);
48 static DBusHandlerResult
49 bus_dispatch_message_handler (DBusMessageHandler *handler,
50 DBusConnection *connection,
54 const char *sender, *service_name;
56 /* Assign a sender to the message */
57 sender = bus_connection_get_name (connection);
58 BUS_HANDLE_OOM (dbus_message_set_sender (message, sender));
60 service_name = dbus_message_get_service (message);
62 /* See if the message is to the driver */
63 if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0)
65 bus_driver_handle_message (connection, message);
67 else if (sender == NULL)
69 _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
70 dbus_connection_disconnect (connection);
72 else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0)
74 bus_dispatch_broadcast_message (message);
78 DBusString service_string;
81 _dbus_string_init_const (&service_string, service_name);
82 service = bus_service_lookup (&service_string, FALSE);
84 _dbus_assert (bus_service_get_primary_owner (service) != NULL);
86 /* Dispatch the message */
87 BUS_HANDLE_OOM (dbus_connection_send_message (bus_service_get_primary_owner (service),
88 message, NULL, NULL));
91 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
95 bus_dispatch_add_connection (DBusConnection *connection)
97 DBusMessageHandler *handler;
99 message_handler_slot = dbus_connection_allocate_data_slot ();
101 if (message_handler_slot < 0)
104 handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);
106 if (!dbus_connection_add_filter (connection, handler))
108 dbus_message_handler_unref (handler);
113 if (!dbus_connection_set_data (connection,
114 message_handler_slot,
116 (DBusFreeFunction)dbus_message_handler_unref))
118 dbus_connection_remove_filter (connection, handler);
119 dbus_message_handler_unref (handler);
128 bus_dispatch_remove_connection (DBusConnection *connection)
130 /* Here we tell the bus driver that we want to get off. */
131 bus_driver_remove_connection (connection);
133 dbus_connection_set_data (connection,
134 message_handler_slot,