1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-message-handler.c Sender/receiver of messages.
4 * Copyright (C) 2002 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
24 #include "dbus-internals.h"
25 #include "dbus-message-handler.h"
26 #include "dbus-list.h"
27 #include "dbus-connection-internal.h"
30 * @defgroup DBusMessageHandlerInternals DBusMessageHandler implementation details
31 * @ingroup DBusInternals
32 * @brief DBusMessageHandler private implementation details.
34 * The guts of DBusMessageHandler and its methods.
41 * @brief Internals of DBusMessageHandler
43 * Object that can send and receive messages.
45 struct DBusMessageHandler
47 int refcount; /**< reference count */
49 DBusHandleMessageFunction function; /**< handler function */
50 void *user_data; /**< user data for function */
51 DBusFreeFunction free_user_data; /**< free the user data */
53 DBusList *connections; /**< connections we're registered with */
57 * Add this connection to the list used by this message handler.
58 * When the message handler goes away, the connection
61 * @param handler the message handler
62 * @param connection the connection
63 * @returns #FALSE if not enough memory
66 _dbus_message_handler_add_connection (DBusMessageHandler *handler,
67 DBusConnection *connection)
69 /* This is a bit wasteful - we just put the connection in the list
70 * once per time it's added. :-/
72 if (!_dbus_list_prepend (&handler->connections, connection))
79 * Reverses the effect of _dbus_message_handler_add_connection().
80 * @param handler the message handler
81 * @param connection the connection
84 _dbus_message_handler_remove_connection (DBusMessageHandler *handler,
85 DBusConnection *connection)
87 if (!_dbus_list_remove (&handler->connections, connection))
88 _dbus_warn ("Function _dbus_message_handler_remove_connection() called when the connection hadn't been added\n");
93 * Handles the given message, by dispatching the handler function
94 * for this DBusMessageHandler, if any.
96 * @param handler the handler
97 * @param connection the connection that received the message
98 * @param message the message
100 * @returns what to do with the message
103 _dbus_message_handler_handle_message (DBusMessageHandler *handler,
104 DBusConnection *connection,
105 DBusMessage *message)
107 /* This function doesn't ref handler/connection/message
108 * since that's done in dbus_connection_dispatch_message().
110 if (handler->function != NULL)
111 return (* handler->function) (handler, connection, message, handler->user_data);
113 return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
119 * @defgroup DBusMessageHandler DBusMessageHandler
121 * @brief Message processor
123 * A DBusMessageHandler is an object that can send and receive
124 * messages. Typically the handler is registered with one or
125 * more DBusConnection objects and processes some types of
126 * messages received from the connection.
132 * @typedef DBusMessageHandler
134 * Opaque data type representing a message handler.
138 * Creates a new message handler. The handler function
139 * may be #NULL for a no-op handler or a handler to
140 * be assigned a function later.
142 * @param function function to call to handle a message
143 * @param user_data data to pass to the function
144 * @param free_user_data function to call to free the user data
145 * @returns a new DBusMessageHandler or #NULL if no memory.
148 dbus_message_handler_new (DBusHandleMessageFunction function,
150 DBusFreeFunction free_user_data)
152 DBusMessageHandler *handler;
154 handler = dbus_new (DBusMessageHandler, 1);
159 handler->refcount = 1;
160 handler->function = function;
161 handler->user_data = user_data;
162 handler->free_user_data = free_user_data;
163 handler->connections = NULL;
169 * Increments the reference count on a message handler.
171 * @param handler the handler
174 dbus_message_handler_ref (DBusMessageHandler *handler)
176 _dbus_assert (handler != NULL);
178 handler->refcount += 1;
182 * Decrements the reference count on a message handler,
183 * freeing the handler if the count reaches 0.
185 * @param handler the handler
188 dbus_message_handler_unref (DBusMessageHandler *handler)
190 _dbus_assert (handler != NULL);
191 _dbus_assert (handler->refcount > 0);
193 handler->refcount -= 1;
194 if (handler->refcount == 0)
198 if (handler->free_user_data)
199 (* handler->free_user_data) (handler->user_data);
201 link = _dbus_list_get_first_link (&handler->connections);
204 DBusConnection *connection = link->data;
206 _dbus_connection_handler_destroyed (connection, handler);
208 link = _dbus_list_get_next_link (&handler->connections, link);
211 _dbus_list_clear (&handler->connections);
218 * Gets the user data for the handler (the same user data
219 * passed to the handler function.)
221 * @param handler the handler
222 * @returns the user data
225 dbus_message_handler_get_data (DBusMessageHandler *handler)
227 return handler->user_data;
231 * Sets the user data for the handler (the same user data
232 * to be passed to the handler function). Frees any previously-existing
233 * user data with the previous free_user_data function.
235 * @param handler the handler
236 * @param user_data the user data
237 * @param free_user_data free function for the data
240 dbus_message_handler_set_data (DBusMessageHandler *handler,
242 DBusFreeFunction free_user_data)
244 if (handler->free_user_data)
245 (* handler->free_user_data) (handler->user_data);
247 handler->user_data = user_data;
248 handler->free_user_data = free_user_data;
252 * Sets the handler function. Call dbus_message_handler_set_data()
253 * to set the user data for the function.
255 * @param handler the handler
256 * @param function the function
259 dbus_message_handler_set_function (DBusMessageHandler *handler,
260 DBusHandleMessageFunction function)
262 handler->function = function;