for sending messages to oneself (TODO item).
* python/service.py (class Object): Swap ordering of bus_name
and object_path parameters to better support inheritance.
* doc/dbus-tutorial.xml: change Python docs to reflect change
in parameter ordering and fix the inheritance section.
* doc/TODO: remove sending message to oneself TODO item
+2005-07-14 John (J5) Palmieri <johnp@redhat.com>
+
+ * bus/dispatch.c, test/test-service.c: Add testcase
+ for sending messages to oneself (TODO item).
+
+ * python/service.py (class Object): Swap ordering of bus_name
+ and object_path parameters to better support inheritance.
+
+ * doc/dbus-tutorial.xml: change Python docs to reflect change
+ in parameter ordering and fix the inheritance section.
+
+ * doc/TODO: remove sending message to oneself TODO item
+
2005-07-15 Ross Burton <ross@openedhand.com>
* glib/dbus-gproxy.c:
}
#define TEST_ECHO_MESSAGE "Test echo message"
+#define TEST_RUN_HELLO_FROM_SELF_MESSAGE "Test sending message to self"
+
+/* returns TRUE if the correct thing happens,
+ * but the correct thing may include OOM errors.
+ */
+static dbus_bool_t
+check_existent_hello_from_self (BusContext *context,
+ DBusConnection *connection)
+{
+ DBusMessage *message;
+ dbus_uint32_t serial;
+ dbus_bool_t retval;
+ const char *base_service;
+ const char *text;
+
+ message = dbus_message_new_method_call (EXISTENT_SERVICE_NAME,
+ "/org/freedesktop/TestSuite",
+ "org.freedesktop.TestSuite",
+ "RunHelloFromSelf");
+
+ if (message == NULL)
+ return TRUE;
+
+ text = TEST_RUN_HELLO_FROM_SELF_MESSAGE;
+ if (!dbus_message_append_args (message,
+ DBUS_TYPE_STRING, &text,
+ DBUS_TYPE_INVALID))
+ {
+ dbus_message_unref (message);
+ return TRUE;
+ }
+
+ if (!dbus_connection_send (connection, message, &serial))
+ {
+ dbus_message_unref (message);
+ return TRUE;
+ }
+
+ dbus_message_unref (message);
+ message = NULL;
+
+ bus_test_run_everything (context);
+
+ /* Note: if this test is run in OOM mode, it will block when the bus
+ * doesn't send a reply due to OOM.
+ */
+ block_connection_until_message_from_bus (context, connection, "reply from running hello from self");
+
+ message = pop_message_waiting_for_memory (connection);
+ if (message == NULL)
+ {
+ _dbus_warn ("Failed to pop message! Should have been reply from RunHelloFromSelf message\n");
+ goto out;
+ }
+
+ if (dbus_message_get_reply_serial (message) != serial)
+ {
+ _dbus_warn ("Wrong reply serial\n");
+ goto out;
+ }
+
+ dbus_message_unref (message);
+ message = NULL;
+
+ retval = TRUE;
+
+ out:
+ if (message)
+ dbus_message_unref (message);
+
+ return retval;
+}
+
/* returns TRUE if the correct thing happens,
* but the correct thing may include OOM errors.
dbus_message_unref (message);
message = NULL;
-
+
+ if (!check_existent_hello_from_self (context, connection))
+ goto out;
+
if (!check_send_exit_to_service (context, connection,
EXISTENT_SERVICE_NAME,
base_service))
dbus-marshal-recursive.c (this is mostly done now, just needs some
cleanup)
- - need to define bus behavior if you send a message to
- yourself; is it an error, or allowed? If allowed,
- we need to have a test for it in the test suite.
-
- just before 1.0, try a HAVE_INT64=0 build and be sure it runs
- dbus-pending-call.c has some API and thread safety issues to review
<article id="index">
<articleinfo>
<title>D-BUS Tutorial</title>
- <releaseinfo>Version 0.4</releaseinfo>
- <date>14 July 2005</date>
+ <releaseinfo>Version 0.4.1</releaseinfo>
+ <date>15 July 2005</date>
<authorgroup>
<author>
<firstname>Havoc</firstname>
import dbus.glib
class HelloWorldObject(dbus.service.Object):
- def __init__(self, bus_name):
- dbus.service.Object.__init__(self, '/org/freedesktop/HelloWorldObject', bus_name)
+ def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
+ dbus.service.Object.__init__(self, bus_name, object_path)
session_bus = dbus.SessionBus()
bus_name = dbus.service.BusName('org.freedesktop.HelloWorld', bus=session_bus)
import dbus.glib
class HelloWorldObject(dbus.service.Object):
- def __init__(self, bus_name):
- dbus.service.Object.__init__(self, '/org/freedesktop/HelloWorldObject', bus_name)
+ def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
+ dbus.service.Object.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def hello(self):
import dbus.glib
class HelloWorldObject(dbus.service.Object):
- def __init__(self, bus_name):
- dbus.service.Object.__init__(self, '/org/freedesktop/HelloWorldObject', bus_name)
+ def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
+ dbus.service.Object.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def hello(self):
Let's inherit from the HelloWorldObject example above and overide the hello method to say goodbye.
<programlisting>
class HelloWorldGoodbyeObject(HelloWorldObject):
- def __init__(self, bus_name):
- HelloWorldObject.__init__(self, '/org/freedesktop/HelloWorldGoodbyeObject', bus_name)
+ def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldGoodbyeObject'):
+ HelloWorldObject.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldGoodbyeIFace')
def hello(self):
"""
__metaclass__ = ObjectType
- def __init__(self, object_path, name):
+ def __init__(self, bus_name, object_path):
self._object_path = object_path
- self._name = name
+ self._name = bus_name
self._bus = name.get_bus()
self._connection = self._bus.get_connection()
static DBusLoop *loop;
static dbus_bool_t already_quit = FALSE;
+static dbus_bool_t hello_from_self_reply_recived = FALSE;
static void
quit (void)
exit (1);
}
+static void
+check_hello_from_self_reply (DBusPendingCall *pcall,
+ void *user_data)
+{
+ DBusMessage *reply;
+ DBusMessage *echo_message, *echo_reply;
+ DBusError error;
+ DBusConnection *connection;
+
+ int type;
+
+ dbus_error_init (&error);
+
+ connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
+ if (connection == NULL)
+ {
+ fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
+ error.message);
+ dbus_error_free (&error);
+ die("no memory");
+ }
+
+
+ echo_message = (DBusMessage *)user_data;
+
+ reply = dbus_pending_call_steal_reply (pcall);
+
+ type = dbus_message_get_type (reply);
+
+ if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
+ {
+ const char *s;
+ printf ("Reply from HelloFromSelf recived\n");
+
+ if (!dbus_message_get_args (echo_message,
+ &error,
+ DBUS_TYPE_STRING, &s,
+ DBUS_TYPE_INVALID))
+ {
+ echo_reply = dbus_message_new_error (echo_message,
+ error.name,
+ error.message);
+
+ if (echo_reply == NULL)
+ die ("No memory\n");
+
+ }
+ else
+ {
+ echo_reply = dbus_message_new_method_return (echo_message);
+ if (echo_reply == NULL)
+ die ("No memory\n");
+
+ if (!dbus_message_append_args (echo_reply,
+ DBUS_TYPE_STRING, &s,
+ DBUS_TYPE_INVALID))
+ die ("No memory");
+ }
+
+ if (!dbus_connection_send (connection, echo_reply, NULL))
+ die ("No memory\n");
+
+ dbus_message_unref (echo_reply);
+ }
+ else if (type == DBUS_MESSAGE_TYPE_ERROR)
+ {
+ dbus_set_error_from_message (&error, reply);
+ printf ("Error type in reply: %s\n", error.message);
+
+ if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
+ {
+ echo_reply = dbus_message_new_error (echo_reply,
+ error.name,
+ error.message);
+
+ if (echo_reply == NULL)
+ die ("No memory\n");
+
+ if (!dbus_connection_send (connection, echo_reply, NULL))
+ die ("No memory\n");
+
+ dbus_message_unref (echo_reply);
+ }
+ dbus_error_free (&error);
+ }
+ else
+ _dbus_assert_not_reached ("Unexpected message recived\n");
+
+ hello_from_self_reply_recived = TRUE;
+
+ dbus_message_unref (reply);
+ dbus_message_unref (echo_message);
+ dbus_pending_call_unref (pcall);
+}
+
+static DBusHandlerResult
+handle_run_hello_from_self (DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusError error;
+ DBusMessage *reply, *self_message;
+ DBusPendingCall *pcall;
+ char *s;
+
+ _dbus_verbose ("sending reply to Echo method\n");
+
+ dbus_error_init (&error);
+
+ if (!dbus_message_get_args (message,
+ &error,
+ DBUS_TYPE_STRING, &s,
+ DBUS_TYPE_INVALID))
+ {
+ reply = dbus_message_new_error (message,
+ error.name,
+ error.message);
+
+ if (reply == NULL)
+ die ("No memory\n");
+
+ if (!dbus_connection_send (connection, reply, NULL))
+ die ("No memory\n");
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+ printf ("Sending HelloFromSelf\n");
+
+ _dbus_verbose ("*** Sending message to self\n");
+ self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
+ "/org/freedesktop/TestSuite",
+ "org.freedesktop.TestSuite",
+ "HelloFromSelf");
+
+ if (self_message == NULL)
+ die ("No memory");
+
+ if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
+ die("No memory");
+
+ dbus_message_ref (message);
+ if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
+ die("No memory");
+
+ printf ("Sent HelloFromSelf\n");
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
static DBusHandlerResult
handle_echo (DBusConnection *connection,
DBusMessage *message)
return DBUS_HANDLER_RESULT_HANDLED;
}
+
+ else if (dbus_message_is_method_call (message,
+ "org.freedesktop.TestSuite",
+ "RunHelloFromSelf"))
+ {
+ return handle_run_hello_from_self (connection, message);
+ }
+ else if (dbus_message_is_method_call (message,
+ "org.freedesktop.TestSuite",
+ "HelloFromSelf"))
+ {
+ DBusMessage *reply;
+ printf ("Recived the HelloFromSelf message\n");
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ die ("No memory");
+
+ if (!dbus_connection_send (connection, reply, NULL))
+ die ("No memory");
+ }
else
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
main (int argc,
char **argv)
{
- DBusConnection *connection;
DBusError error;
int result;
+ DBusConnection *connection;
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
dbus_error_free (&error);
exit (1);
}
-
+
_dbus_verbose ("*** Test service entering main loop\n");
_dbus_loop_run (loop);
-
+
test_connection_shutdown (loop, connection);
dbus_connection_remove_filter (connection, filter_func, NULL);