summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-message-handler.c
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2002-12-24 06:37:33 +0000
committerHavoc Pennington <hp@redhat.com>2002-12-24 06:37:33 +0000
commit17fbe2b702cdc880abd6cbe117e620b6432f42e0 (patch)
tree9dc357f6d6c5cd7dd4bfa2bc0dee1760d4ac366a /dbus/dbus-message-handler.c
parent7af22a5ef9460af0f6afc2f1704d44b2e4c18ead (diff)
2002-12-24 Havoc Pennington <hp@pobox.com>
* glib/dbus-gthread.c: fix include * glib/dbus-glib.h: rename DBusMessageHandler for now. I think glib API needs to change, though, as you don't want to use DBusMessageFunction, you want to use the DBusMessageHandler object. Probably dbus_connection_open_with_g_main_loop() and dbus_connection_setup_g_main_loop() or something like that (but think of better names...) that just create a connection that has watch/timeout functions etc. already set up. * dbus/dbus-connection.c (dbus_connection_send_message_with_reply): new function just to show how the message handler helps us deal with replies. * dbus/dbus-list.c (_dbus_list_remove_last): new function * dbus/dbus-string.c (_dbus_string_test): free a string that wasn't * dbus/dbus-hash.c: use memory pools for the hash entries (rebuild_table): be more paranoid about overflow, and shrink table when we can (_dbus_hash_test): reduce number of sprintfs and write valid C89. Add tests for case where we grow and then shrink the hash table. * dbus/dbus-mempool.h, dbus/dbus-mempool.c: memory pools * dbus/dbus-connection.c (dbus_connection_register_handler) (dbus_connection_unregister_handler): new functions * dbus/dbus-message.c (dbus_message_get_name): new * dbus/dbus-list.c: fix docs typo * dbus/dbus-message-handler.h, dbus/dbus-message-handler.c: an object representing a handler for messages.
Diffstat (limited to 'dbus/dbus-message-handler.c')
-rw-r--r--dbus/dbus-message-handler.c265
1 files changed, 265 insertions, 0 deletions
diff --git a/dbus/dbus-message-handler.c b/dbus/dbus-message-handler.c
new file mode 100644
index 00000000..5f12d238
--- /dev/null
+++ b/dbus/dbus-message-handler.c
@@ -0,0 +1,265 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-message-handler.c Sender/receiver of messages.
+ *
+ * Copyright (C) 2002 Red Hat Inc.
+ *
+ * Licensed under the Academic Free License version 1.2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "dbus-internals.h"
+#include "dbus-message-handler.h"
+#include "dbus-list.h"
+#include "dbus-connection-internal.h"
+
+/**
+ * @defgroup DBusMessageHandlerInternals DBusMessageHandler implementation details
+ * @ingroup DBusInternals
+ * @brief DBusMessageHandler private implementation details.
+ *
+ * The guts of DBusMessageHandler and its methods.
+ *
+ * @{
+ */
+
+
+/**
+ * @brief Internals of DBusMessageHandler
+ *
+ * Object that can send and receive messages.
+ */
+struct DBusMessageHandler
+{
+ int refcount; /**< reference count */
+
+ DBusHandleMessageFunction function; /**< handler function */
+ void *user_data; /**< user data for function */
+ DBusFreeFunction free_user_data; /**< free the user data */
+
+ DBusList *connections; /**< connections we're registered with */
+};
+
+/**
+ * Add this connection to the list used by this message handler.
+ * When the message handler goes away, the connection
+ * will be notified.
+ *
+ * @param handler the message handler
+ * @param connection the connection
+ * @returns #FALSE if not enough memory
+ */
+dbus_bool_t
+_dbus_message_handler_add_connection (DBusMessageHandler *handler,
+ DBusConnection *connection)
+{
+ /* This is a bit wasteful - we just put the connection in the list
+ * once per time it's added. :-/
+ */
+ if (!_dbus_list_prepend (&handler->connections, connection))
+ return FALSE;
+
+ return TRUE;
+}
+
+/**
+ * Reverses the effect of _dbus_message_handler_add_connection().
+ * @param handler the message handler
+ * @param connection the connection
+ */
+void
+_dbus_message_handler_remove_connection (DBusMessageHandler *handler,
+ DBusConnection *connection)
+{
+ if (!_dbus_list_remove (&handler->connections, connection))
+ _dbus_warn ("Function _dbus_message_handler_remove_connection() called when the connection hadn't been added\n");
+}
+
+
+/**
+ * Handles the given message, by dispatching the handler function
+ * for this DBusMessageHandler, if any.
+ *
+ * @param handler the handler
+ * @param connection the connection that received the message
+ * @param message the message
+ *
+ * @returns what to do with the message
+ */
+DBusHandlerResult
+_dbus_message_handler_handle_message (DBusMessageHandler *handler,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ /* This function doesn't ref handler/connection/message
+ * since that's done in dbus_connection_dispatch_message().
+ */
+ if (handler->function != NULL)
+ return (* handler->function) (handler, connection, message, handler->user_data);
+ else
+ return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
+}
+
+/** @} */
+
+/**
+ * @defgroup DBusMessageHandler DBusMessageHandler
+ * @ingroup DBus
+ * @brief Message processor
+ *
+ * A DBusMessageHandler is an object that can send and receive
+ * messages. Typically the handler is registered with one or
+ * more DBusConnection objects and processes some types of
+ * messages received from the connection.
+ *
+ * @{
+ */
+
+/**
+ * @typedef DBusMessageHandler
+ *
+ * Opaque data type representing a message handler.
+ */
+
+/**
+ * Creates a new message handler. The handler function
+ * may be #NULL for a no-op handler or a handler to
+ * be assigned a function later.
+ *
+ * @param function function to call to handle a message
+ * @param user_data data to pass to the function
+ * @param free_user_data function to call to free the user data
+ * @returns a new DBusMessageHandler or #NULL if no memory.
+ */
+DBusMessageHandler*
+dbus_message_handler_new (DBusHandleMessageFunction function,
+ void *user_data,
+ DBusFreeFunction free_user_data)
+{
+ DBusMessageHandler *handler;
+
+ handler = dbus_new (DBusMessageHandler, 1);
+
+ if (handler == NULL)
+ return NULL;
+
+ handler->refcount = 1;
+ handler->function = function;
+ handler->user_data = user_data;
+ handler->free_user_data = free_user_data;
+ handler->connections = NULL;
+
+ return handler;
+}
+
+/**
+ * Increments the reference count on a message handler.
+ *
+ * @param handler the handler
+ */
+void
+dbus_message_handler_ref (DBusMessageHandler *handler)
+{
+ _dbus_assert (handler != NULL);
+
+ handler->refcount += 1;
+}
+
+/**
+ * Decrements the reference count on a message handler,
+ * freeing the handler if the count reaches 0.
+ *
+ * @param handler the handler
+ */
+void
+dbus_message_handler_unref (DBusMessageHandler *handler)
+{
+ _dbus_assert (handler != NULL);
+ _dbus_assert (handler->refcount > 0);
+
+ handler->refcount -= 1;
+ if (handler->refcount == 0)
+ {
+ DBusList *link;
+
+ if (handler->free_user_data)
+ (* handler->free_user_data) (handler->user_data);
+
+ link = _dbus_list_get_first_link (&handler->connections);
+ while (link != NULL)
+ {
+ DBusConnection *connection = link->data;
+
+ _dbus_connection_handler_destroyed (connection, handler);
+
+ link = _dbus_list_get_next_link (&handler->connections, link);
+ }
+
+ _dbus_list_clear (&handler->connections);
+
+ dbus_free (handler);
+ }
+}
+
+/**
+ * Gets the user data for the handler (the same user data
+ * passed to the handler function.)
+ *
+ * @param handler the handler
+ * @returns the user data
+ */
+void*
+dbus_message_handler_get_data (DBusMessageHandler *handler)
+{
+ return handler->user_data;
+}
+
+/**
+ * Sets the user data for the handler (the same user data
+ * to be passed to the handler function). Frees any previously-existing
+ * user data with the previous free_user_data function.
+ *
+ * @param handler the handler
+ * @param user_data the user data
+ * @param free_user_data free function for the data
+ */
+void
+dbus_message_handler_set_data (DBusMessageHandler *handler,
+ void *user_data,
+ DBusFreeFunction free_user_data)
+{
+ if (handler->free_user_data)
+ (* handler->free_user_data) (handler->user_data);
+
+ handler->user_data = user_data;
+ handler->free_user_data = free_user_data;
+}
+
+/**
+ * Sets the handler function. Call dbus_message_handler_set_data()
+ * to set the user data for the function.
+ *
+ * @param handler the handler
+ * @param function the function
+ */
+void
+dbus_message_handler_set_function (DBusMessageHandler *handler,
+ DBusHandleMessageFunction function)
+{
+ handler->function = function;
+}
+
+/** @} */