[PATCH] dbus-1: Adds helpers to create/parse a request name message/reply.

Mathias Berchtold mberchtold at gmail.com
Fri Jan 10 07:07:06 PST 2014


The general idea is to separate the message building/parsing from the
transport mechanism. The proposed patch gives a client of libdbus more
freedom in this regard without the need to duplicate the code.
In my specific case I request the name asynchronously. The two functions
allow me to build the message outside of libdbus, send it through my
transport and then later parse the reply in the callback.

Signed-off-by: Mat Berchtold <mberchtold at gmail.com>
---
 dbus/dbus-bus.c | 83
++++++++++++++++++++++++++++++++++++++++++++-------------
 dbus/dbus-bus.h |  8 ++++++
 2 files changed, 73 insertions(+), 18 deletions(-)

diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c
index 0fd4831..9fd2d9e 100644
--- a/dbus/dbus-bus.c
+++ b/dbus/dbus-bus.c
@@ -1007,6 +1007,69 @@ dbus_bus_get_id (DBusConnection *connection,
 }

 /**
+ * Creates a request name message. For a detailed discussion refer to
+ * dbus_bus_get_result_from_request_name_reply.
+ *
+ * @param name the name to request
+ * @param flags flags
+ * @param error location to store the error
+ * @returns the message, NULL if error is set
+ */
+DBusMessage *
+dbus_bus_create_request_name_message(const char     *name,
+unsigned int    flags,
+DBusError      *error)
+{
+ DBusMessage *message;
+
+ _dbus_return_val_if_fail(name != NULL, 0);
+ _dbus_return_val_if_fail(_dbus_check_is_valid_bus_name(name), 0);
+ _dbus_return_val_if_error_is_set(error, 0);
+
+ message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
+ DBUS_PATH_DBUS,
+ DBUS_INTERFACE_DBUS,
+ "RequestName");
+
+ if (message == NULL)
+ {
+ _DBUS_SET_OOM(error);
+ return NULL;
+ }
+
+ if (!dbus_message_append_args(message,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_UINT32, &flags,
+ DBUS_TYPE_INVALID))
+ {
+ dbus_message_unref(message);
+ _DBUS_SET_OOM(error);
+ return NULL;
+ }
+ return message;
+}
+
+/**
+ * Gets the result from a request name reply.
+ * The result codes are discussed in dbus_bus_request_name.
+ *
+ * @param message the message
+ * @param error error to be filled in on failure
+ * @param result the result code
+ * @returns #FALSE if the error was set
+ */
+dbus_bool_t
+dbus_bus_get_result_from_request_name_reply(DBusMessage *reply,
+DBusError      *error,
+dbus_uint32_t  *result
+)
+{
+ return dbus_message_get_args(reply, error,
+ DBUS_TYPE_UINT32, result,
+ DBUS_TYPE_INVALID);
+}
+
+/**
  * Asks the bus to assign the given name to this connection by invoking
  * the RequestName method on the bus. This method is fully documented
  * in the D-Bus specification. For quick reference, the flags and
@@ -1122,26 +1185,12 @@ dbus_bus_request_name (DBusConnection *connection,
   _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
   _dbus_return_val_if_error_is_set (error, 0);

-  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                          DBUS_PATH_DBUS,
-                                          DBUS_INTERFACE_DBUS,
-                                          "RequestName");
-
+  message = dbus_bus_create_request_name_message(name, flags, error);
   if (message == NULL)
     {
       _DBUS_SET_OOM (error);
       return -1;
     }
-
-  if (!dbus_message_append_args (message,
-  DBUS_TYPE_STRING, &name,
-  DBUS_TYPE_UINT32, &flags,
-  DBUS_TYPE_INVALID))
-    {
-      dbus_message_unref (message);
-      _DBUS_SET_OOM (error);
-      return -1;
-    }

   reply = dbus_connection_send_with_reply_and_block (connection, message,
-1,
                                                      error);
@@ -1161,9 +1210,7 @@ dbus_bus_request_name (DBusConnection *connection,
       return -1;
     }

-  if (!dbus_message_get_args (reply, error,
-                              DBUS_TYPE_UINT32, &result,
-                              DBUS_TYPE_INVALID))
+  if (!dbus_bus_get_result_from_request_name_reply(reply, error, &result))
     {
       _DBUS_ASSERT_ERROR_IS_SET (error);
       dbus_message_unref (reply);
diff --git a/dbus/dbus-bus.h b/dbus/dbus-bus.h
index 02a9571..6bad442 100644
--- a/dbus/dbus-bus.h
+++ b/dbus/dbus-bus.h
@@ -64,6 +64,14 @@ int             dbus_bus_request_name
(DBusConnection *connection,
     unsigned int    flags,
     DBusError      *error);
 DBUS_EXPORT
+DBusMessage    *dbus_bus_create_request_name_message(const char     *name,
+    unsigned int    flags,
+    DBusError      *error);
+DBUS_EXPORT
+dbus_bool_t     dbus_bus_get_result_from_request_name_reply(DBusMessage
*reply,
+    DBusError      *error,
+    dbus_uint32_t  *result);
+DBUS_EXPORT
 int             dbus_bus_release_name     (DBusConnection *connection,
     const char     *name,
     DBusError      *error);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20140110/ea450309/attachment.html>


More information about the dbus mailing list