summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTrent Lloyd <lathiat@bur.st>2005-06-17 12:37:47 +0000
committerTrent Lloyd <lathiat@bur.st>2005-06-17 12:37:47 +0000
commit652139e9eeb4e2c50b9d1b71de01721a81a239e6 (patch)
tree2ce22cc7df3794b52d7db932eaabb5c9260780b7
parent16b19ffe5fa3b111bedf5342bc6c08de5bf6d4ad (diff)
* Split out the dbus stuff in the daemon to a separate file so it can be built conditionally
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@118 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
-rw-r--r--avahi-daemon/Makefile.am8
-rw-r--r--avahi-daemon/dbus-protocol.c161
-rw-r--r--avahi-daemon/dbus-protocol.h28
-rw-r--r--avahi-daemon/main.c134
4 files changed, 202 insertions, 129 deletions
diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am
index 3d01d5b..f90b16d 100644
--- a/avahi-daemon/Makefile.am
+++ b/avahi-daemon/Makefile.am
@@ -24,14 +24,15 @@ AM_LDADD=-lexpat
AM_CFLAGS+=$(GLIB20_CFLAGS)
AM_LDADD+=$(GLIB20_LIBS)
+if ENABLE_DBUS
# DBUS
AM_CFLAGS+=$(DBUS_CFLAGS)
AM_LDADD+=$(DBUS_LIBS)
+endif
# This cool debug trap works on i386/gcc only
AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
-if ENABLE_DBUS
bin_PROGRAMS = \
avahi
@@ -40,9 +41,12 @@ avahi_SOURCES = \
simple-protocol.c simple-protocol.h \
static-services.c static-services.h
+if ENABLE_DBUS
+avahi_SOURCES += dbus-protocol.c dbus-protocol.h
+endif
+
avahi_CFLAGS = $(AM_CFLAGS)
avahi_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la
-endif
xmllint:
xmllint --noout --valid example.service
diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c
new file mode 100644
index 0000000..e8d30ac
--- /dev/null
+++ b/avahi-daemon/dbus-protocol.c
@@ -0,0 +1,161 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi 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 Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <glib.h>
+
+#define DBUS_API_SUBJECT_TO_CHANGE
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+static DBusConnection *bus = NULL;
+
+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_ORG_FREEDESKTOP_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, terminating...");
+
+ 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_INTERFACE_ORG_FREEDESKTOP_DBUS,
+ "ServiceAcquired"))
+ {
+ 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: ServiceAcquired (%s)", name);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ g_message ("dbus: missed event");
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int
+dbus_protocol_setup ()
+{
+ DBusError error;
+
+ 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);
+
+ goto finish;
+ }
+
+ dbus_connection_setup_with_g_main (bus, NULL);
+ dbus_connection_set_exit_on_disconnect (bus, FALSE);
+
+ dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
+
+ if (dbus_error_is_set (&error))
+ {
+ g_warning ("dbus_error_is_set (): %s", error.message);
+ dbus_error_free (&error);
+
+ goto finish;
+ }
+
+ 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);
+
+ goto finish;
+ }
+}
+
+void
+dbus_protocol_shutdown ()
+{
+ if (bus) {
+ dbus_connection_disconnect(bus);
+ dbus_connection_unref(bus);
+ }
+}
diff --git a/avahi-daemon/dbus-protocol.h b/avahi-daemon/dbus-protocol.h
new file mode 100644
index 0000000..c82f7fd
--- /dev/null
+++ b/avahi-daemon/dbus-protocol.h
@@ -0,0 +1,28 @@
+#ifndef foodbusprotocolhfoo
+#define foodbusprotocolhfoo
+
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi 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 Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+int dbus_protocol_setup ();
+int dbus_protocol_shutdown ();
+
+#endif
diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c
index ecc8c83..4639ba8 100644
--- a/avahi-daemon/main.c
+++ b/avahi-daemon/main.c
@@ -25,10 +25,6 @@
#include <avahi-core/core.h>
-#define DBUS_API_SUBJECT_TO_CHANGE
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
-
#include "main.h"
#include "simple-protocol.h"
#include "static-services.h"
@@ -37,86 +33,6 @@
AvahiServer *avahi_server = NULL;
-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_ORG_FREEDESKTOP_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_INTERFACE_ORG_FREEDESKTOP_DBUS,
- "ServiceAcquired"))
- {
- 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: ServiceAcquired (%s)", name);
-
- return DBUS_HANDLER_RESULT_HANDLED;
- }
-
- g_message ("dbus: missed event");
-
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
g_assert(s);
@@ -137,8 +53,6 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use
int main(int argc, char *argv[]) {
GMainLoop *loop = NULL;
- DBusConnection *bus = NULL;
- DBusError error;
gint r = 255;
AvahiServerConfig config;
@@ -146,46 +60,13 @@ int main(int argc, char *argv[]) {
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);
-
- goto finish;
- }
-
- dbus_connection_setup_with_g_main (bus, NULL);
- dbus_connection_set_exit_on_disconnect (bus, FALSE);
-
- dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
-
- if (dbus_error_is_set (&error))
- {
- g_warning ("dbus_error_is_set (): %s", error.message);
- dbus_error_free (&error);
-
- goto finish;
- }
-
- 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);
-
+ if (simple_protocol_setup(NULL) < 0)
goto finish;
- }
- if (simple_protocol_setup(NULL) < 0)
+#ifdef ENABLE_DBUS
+ if (dbus_protocol_setup () < 0)
goto finish;
+#endif
if (!(avahi_server = avahi_server_new(NULL, &config, server_callback, NULL)))
goto finish;
@@ -203,10 +84,9 @@ finish:
simple_protocol_shutdown();
- if (bus) {
- dbus_connection_disconnect(bus);
- dbus_connection_unref(bus);
- }
+#ifdef ENABLE_DBUS
+ dbus_protocol_shutdown();
+#endif
if (avahi_server)
avahi_server_free(avahi_server);