From 42c5fff2020d36e23c8720bc02a87aa12a592dd6 Mon Sep 17 00:00:00 2001 From: Trent Lloyd Date: Wed, 18 May 2005 10:34:44 +0000 Subject: [PATCH] import basic dbus code into the daemon, with auto-fu magic, etc git-svn-id: file:///home/lennart/svn/public/avahi/trunk@75 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- configure.ac | 5 ++ daemon/Makefile.am | 4 ++ daemon/main.c | 129 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index a6e0462..4e58fb0 100644 --- a/configure.ac +++ b/configure.ac @@ -62,6 +62,11 @@ PKG_CHECK_MODULES(GLIB20, [ glib-2.0 >= 2.4.0 ]) AC_SUBST(GLIB20_CFLAGS) AC_SUBST(GLIB20_LIBS) +# Check for DBUS +PKG_CHECK_MODULES(DBUS, [ dbus-1 >= 0.23, dbus-glib-1 >= 0.23 ]) +AC_SUBST(DBUS_CFLAGS) +AC_SUBST(DBUS_LIBS) + # If using GCC specify some additional parameters if test "x$GCC" = "xyes" ; then CFLAGS="$CFLAGS -pipe -W -Wall -pedantic" diff --git a/daemon/Makefile.am b/daemon/Makefile.am index fb3eeb8..7f2bc9b 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -23,6 +23,10 @@ AM_CFLAGS=-I$(top_srcdir) -D_GNU_SOURCE AM_CFLAGS+=$(GLIB20_CFLAGS) AM_LDADD=$(GLIB20_LIBS) +# DBUS +AM_CFLAGS+=$(DBUS_CFLAGS) +AM_LDADD+=$(DBUS_LIBS) + # This cool debug trap works on i386/gcc only AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' diff --git a/daemon/main.c b/daemon/main.c index c986de0..60caed7 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -25,17 +25,136 @@ #include +#define DBUS_API_SUBJECT_TO_CHANGE +#include +#include + +#define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi" + +static DBusHandlerResult +do_register (DBusConnection *conn, DBusMessage *message) +{ + DBusError error; + char *s; + + dbus_error_init (&error); + + dbus_message_get_args (message, &error, + DBUS_TYPE_STRING, &s, + DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + g_warning ("Error parsing register attempt"); + dbus_error_free (&error); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + g_message ("Register received from: %s", s); + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static DBusHandlerResult +signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data) +{ + GMainLoop *loop = user_data; + DBusError error; + + dbus_error_init (&error); + + g_message ("dbus: interface=%s, path=%s, member=%s", + dbus_message_get_interface (message), + dbus_message_get_path (message), + dbus_message_get_member (message)); + + if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, + "Disconnected")) + { + /* No, we shouldn't quit, but until we get somewhere + * usefull such that we can restore our state, we will */ + g_warning ("Disconnnected from d-bus"); + + g_main_loop_quit (loop); + return DBUS_HANDLER_RESULT_HANDLED; + } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI, + "Register")) + { + return do_register (conn, message); + } else if (dbus_message_is_signal (message, DBUS_SERVICE_DBUS, + "NameAcquired")) + { + char *name; + + dbus_message_get_args (message, &error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_INVALID); + + if (dbus_error_is_set (&error)) + { + g_warning ("Error parsing NameAcquired message"); + dbus_error_free (&error); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + g_message ("dbus: NameAcquired (%s)", name); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + g_message ("dbus: missed event"); + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + int main(int argc, char *argv[]) { - AvahiServer *avahi; GMainLoop *loop = NULL; - - avahi = avahi_server_new(NULL); + DBusConnection *bus; + DBusError error; loop = g_main_loop_new(NULL, FALSE); + + dbus_error_init (&error); + + bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); + + if (bus == NULL) + { + g_warning ("dbus_bus_get(): %s", error.message); + dbus_error_free (&error); + + return -1; + } + + dbus_connection_setup_with_g_main (bus, NULL); + + dbus_bus_request_name (bus, DBUS_SERVICE_AVAHI, 0, &error); + + if (dbus_error_is_set (&error)) + { + g_warning ("dbus_bus_request_name (): %s", error.message); + dbus_error_free (&error); + + return -1; + } + + dbus_connection_add_filter (bus, signal_filter, loop, NULL); + dbus_bus_add_match (bus, + "type='method_call',interface='org.freedesktop.Avahi'", + &error); + + if (dbus_error_is_set (&error)) + { + g_warning ("dbus_bus_add_match (): %s", error.message); + dbus_error_free (&error); + + return -1; + } + g_main_loop_run(loop); g_main_loop_unref(loop); - avahi_server_free(avahi); - return 0; } -- 2.39.5