dbus/glib/examples/example-signal-recipient.c
Colin Walters ab1ae1f4e2 2005-06-29 Colin Walters <walters@verbum.org>
* glib/dbus-gproxy.c (struct _DBusGProxy): Add new member
	name_call for keeping track of any outgoing GetNameOwner call.
	Also add for_owner and associated.
	(struct _DBusGProxyManager): Add owner_names, which is hash table
	that maps a base name to a list of names it owns (that we're
	interested in).  Add pending_nameowner_calls which is a list of
	all outstanding GetNameOwner; avoids us having to iterate over
	every proxy.  Add unassociated_proxies which keeps track of name
	proxies with no associated name owner.
	(dbus_g_proxy_manager_unref): Destroy owner_names.
	(struct DBusGProxyNameOwnerInfo): New struct for keeping track of
	name refcounts.
	(find_name_in_info, name_owner_foreach)
	(dbus_g_proxy_manager_lookup_name_owner, insert_nameinfo)
	(dbus_g_proxy_manager_monitor_name_owner)
	(dbus_g_proxy_manager_unmonitor_name_owner)
	(unassociate_proxies, dbus_g_proxy_manager_replace_name_owner):
	New functions; they manipulate the owner_names mapping.
	(got_name_owner_cb): New function.
	(get_name_owner): New function, extracted from
	dbus_g_proxy_new_for_name_owner.
	(dbus_g_proxy_manager_register): For now we need to keep track of
	all NameOwnerChanged.  Also if the proxy is for a name, if we
	don't already know the name owner, queue a new GetNameOwner
	request and add it to our list of unassociated proxies.  Otherwise
	inc the refcount.
	(dbus_g_proxy_manager_unregister): If this proxy is for a name,
	cancel any pending GetNameOwner call, etc.
	(dbus_g_proxy_manager_filter): Handle NameOwnerChanged.  Also use
	the owner_names mapping to look up the current names for the
	signal source, and dispatch to any proxies for that name.
	(dbus_g_proxy_new): Initialize new members.
	(dbus_g_proxy_new_for_name): Delete unused proxy variable.
	(dbus_g_proxy_new_for_name_owner): Use get_name_owner.
	(dbus_g_pending_call_end_valist): New function, extracted from
	dbus_g_proxy_end_call_internal.  Useful when we don't have a proxy
	but want to use the GLib infrastructure.  Also note how many
	arguments in reply were over.
	(dbus_g_pending_call_end): New function, just call
	dbus_g_pending_call_end_valist.
	(dbus_g_proxy_end_call_internal): Just call
	dbus_g_pending_call_end_valist.

	* glib/dbus-gobject.c (_dbus_gobject_lookup_marshaller): Fix lookup
	of builtin marshaller for STRING_STRING_STRING.

	* test/glib/test-dbus-glib.c:
	* test/glib/test-service-glib.c:
	* test/glib/test-service-glib.xml:
	Extend tests to cover name proxies, destruction of owner proxies,
	etc.

	* glib/examples/example-signal-recipient.c
	(dbus_g_proxy_new_for_name_owner): Create a name proxy.

	* tools/dbus-send.c (main): Print D-BUS error name in addition
	to message.
2005-06-29 16:59:00 +00:00

102 lines
2.7 KiB
C

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
static void
lose (const char *str, ...)
{
va_list args;
va_start (args, str);
vfprintf (stderr, str, args);
fputc ('\n', stderr);
va_end (args);
exit (1);
}
static void
lose_gerror (const char *prefix, GError *error)
{
lose ("%s: %s", prefix, error->message);
}
static gboolean
emit_signal (gpointer arg)
{
DBusGProxy *proxy = arg;
dbus_g_proxy_call_no_reply (proxy, "emitHelloSignal", G_TYPE_INVALID);
return TRUE;
}
static void
hello_signal_handler (DBusGProxy *proxy, const char *hello_string, gpointer user_data)
{
printf ("Received signal and it says: %s\n", hello_string);
}
int
main (int argc, char **argv)
{
DBusGConnection *bus;
DBusGProxy *remote_object;
GError *error = NULL;
GMainLoop *mainloop;
g_type_init ();
mainloop = g_main_loop_new (NULL, FALSE);
bus = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (!bus)
lose_gerror ("Couldn't connect to session bus", error);
/* We use _for_name_owner in order to track this particular service
* instance, which lets us receive signals.
*/
remote_object = dbus_g_proxy_new_for_name (bus,
"org.designfu.TestService",
"/org/designfu/TestService/object",
"org.designfu.TestService");
if (!remote_object)
lose_gerror ("Failed to get name owner", error);
/* IMPORTANT:
*
* Note because this signal's signature is VOID__STRING, we do not
* need to register a marshaller, since there is a builtin one.
* However for other signatures, you must generate a marshaller,
* then call dbus_g_object_register_marshaller. It would look like
* this:
*
* dbus_g_object_register_marshaller (g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INVALID);
*
*/
/* Tell DBus what the type signature of the signal callback is; this
* allows us to sanity-check incoming messages before invoking the
* callback. You need to do this once for each proxy you create,
* not every time you want to connect to the signal.
*/
dbus_g_proxy_add_signal (remote_object, "HelloSignal", G_TYPE_STRING, G_TYPE_INVALID);
/* Actually connect to the signal. Note you can call
* dbus_g_proxy_connect_signal multiple times for one invocation of
* dbus_g_proxy_add_signal.
*/
dbus_g_proxy_connect_signal (remote_object, "HelloSignal", G_CALLBACK (hello_signal_handler),
NULL, NULL);
g_timeout_add (2000, emit_signal, remote_object);
g_main_loop_run (mainloop);
exit (0);
}