2003-02-16 Anders Carlsson <andersca@codefactory.se>
[platform/upstream/dbus.git] / bus / dispatch.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dispatch.c  Message dispatcher
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "dispatch.h"
25 #include "connection.h"
26 #include "driver.h"
27 #include "utils.h"
28 #include <dbus/dbus-internals.h>
29 #include <string.h>
30
31 static int message_handler_slot;
32
33 static void
34 send_one_message (DBusConnection *connection, void *data)
35 {
36   /* Only send messages to registered connections */
37   if (bus_connection_get_name (connection) == NULL)
38     return;
39   
40   BUS_HANDLE_OOM (dbus_connection_send_message (connection, data, NULL, NULL));
41 }
42
43 void
44 bus_dispatch_broadcast_message (DBusMessage *message)
45 {
46   _dbus_assert (dbus_message_get_sender (message) != NULL);
47   
48   bus_connection_foreach (send_one_message, message);
49   
50 }
51
52 static DBusHandlerResult
53 bus_dispatch_message_handler (DBusMessageHandler *handler,
54                               DBusConnection     *connection,
55                               DBusMessage        *message,
56                               void               *user_data)
57 {
58   const char *sender, *service_name, *message_name;
59   
60   /* Assign a sender to the message */
61   sender = bus_connection_get_name (connection);
62   BUS_HANDLE_OOM (dbus_message_set_sender (message, sender));
63
64   service_name = dbus_message_get_service (message);
65   message_name = dbus_message_get_name (message);
66   
67   /* TODO: Crashes if service_name == NULL */
68   
69   /* See if the message is to the driver */
70   if (message_name && strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0)
71     {
72       bus_connection_disconnect (connection);
73     }
74   else if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0)
75     {
76       bus_driver_handle_message (connection, message);
77     }
78   else if (sender == NULL)
79     {
80       _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
81       dbus_connection_disconnect (connection);
82     }
83   else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0)
84     {
85       bus_dispatch_broadcast_message (message);
86     }
87   else  
88     {
89       DBusString service_string;
90       BusService *service;
91
92       _dbus_string_init_const (&service_string, service_name);
93       service = bus_service_lookup (&service_string, FALSE);
94
95       if (!service)
96         {
97           DBusMessage *error_reply;
98           DBusString error_message;
99           const char *error_str;
100           
101           /* Trying to send a message to a non-existant service,
102              bounce back an error message. */
103           
104           BUS_HANDLE_OOM (_dbus_string_init (&error_message, _DBUS_INT_MAX));
105
106           BUS_HANDLE_OOM (_dbus_string_append (&error_message, "Service \""));
107           BUS_HANDLE_OOM (_dbus_string_append (&error_message, service_name));
108           BUS_HANDLE_OOM (_dbus_string_append (&error_message, "does not exist"));        
109
110           _dbus_string_get_const_data (&error_message, &error_str);
111           BUS_HANDLE_OOM (error_reply = dbus_message_new_error_reply (message, DBUS_ERROR_SERVICE_DOES_NOT_EXIST,
112                                                                       error_str));
113           _dbus_string_free (&error_message);
114
115           /* Dispatch the message */
116           BUS_HANDLE_OOM (dbus_connection_send_message (connection, error_reply, NULL, NULL));
117           dbus_message_unref (error_reply);
118         }
119       else
120         {
121           _dbus_assert (bus_service_get_primary_owner (service) != NULL);
122       
123           /* Dispatch the message */
124           BUS_HANDLE_OOM (dbus_connection_send_message (bus_service_get_primary_owner (service),
125                                                         message, NULL, NULL));
126         }
127     }
128
129   return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
130 }
131
132 dbus_bool_t
133 bus_dispatch_add_connection (DBusConnection *connection)
134 {
135   DBusMessageHandler *handler;
136   
137   message_handler_slot = dbus_connection_allocate_data_slot ();
138
139   if (message_handler_slot < 0)
140     return FALSE;
141
142   handler = dbus_message_handler_new (bus_dispatch_message_handler, NULL, NULL);  
143
144   if (!dbus_connection_add_filter (connection, handler))
145     {
146       dbus_message_handler_unref (handler);
147
148       return FALSE;
149     }
150
151   if (!dbus_connection_set_data (connection,
152                                  message_handler_slot,
153                                  handler,
154                                  (DBusFreeFunction)dbus_message_handler_unref))
155     {
156       dbus_connection_remove_filter (connection, handler);
157       dbus_message_handler_unref (handler);
158
159       return FALSE;
160     }
161
162   return TRUE;
163 }
164
165 void
166 bus_dispatch_remove_connection (DBusConnection *connection)
167 {
168   /* Here we tell the bus driver that we want to get off. */
169   bus_driver_remove_connection (connection);
170
171   dbus_connection_set_data (connection,
172                             message_handler_slot,
173                             NULL, NULL);
174 }
175