4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #define DBUS_API_SUBJECT_TO_CHANGE
25 #include <dbus/dbus.h>
26 #include <dbus/dbus-glib-lowlevel.h>
28 static DBusConnection *bus = NULL;
30 static DBusHandlerResult
31 do_register (DBusConnection *conn, DBusMessage *message)
36 dbus_error_init (&error);
38 dbus_message_get_args (message, &error,
42 if (dbus_error_is_set (&error))
44 g_warning ("Error parsing register attempt");
45 dbus_error_free (&error);
47 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
50 g_message ("Register received from: %s", s);
52 return DBUS_HANDLER_RESULT_HANDLED;
55 static DBusHandlerResult
56 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
58 GMainLoop *loop = user_data;
61 dbus_error_init (&error);
63 g_message ("dbus: interface=%s, path=%s, member=%s",
64 dbus_message_get_interface (message),
65 dbus_message_get_path (message),
66 dbus_message_get_member (message));
68 if (dbus_message_is_signal (message,
69 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
72 /* No, we shouldn't quit, but until we get somewhere
73 * usefull such that we can restore our state, we will */
74 g_warning ("Disconnnected from d-bus, terminating...");
76 g_main_loop_quit (loop);
77 return DBUS_HANDLER_RESULT_HANDLED;
78 } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
81 return do_register (conn, message);
82 } else if (dbus_message_is_signal (message,
83 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
88 dbus_message_get_args (message, &error,
89 DBUS_TYPE_STRING, &name,
92 if (dbus_error_is_set (&error))
94 g_warning ("Error parsing NameAcquired message");
95 dbus_error_free (&error);
97 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
100 g_message ("dbus: ServiceAcquired (%s)", name);
102 return DBUS_HANDLER_RESULT_HANDLED;
105 g_message ("dbus: missed event");
107 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
111 dbus_protocol_setup ()
115 dbus_error_init (&error);
117 bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
121 g_warning ("dbus_bus_get(): %s", error.message);
122 dbus_error_free (&error);
127 dbus_connection_setup_with_g_main (bus, NULL);
128 dbus_connection_set_exit_on_disconnect (bus, FALSE);
130 dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
132 if (dbus_error_is_set (&error))
134 g_warning ("dbus_error_is_set (): %s", error.message);
135 dbus_error_free (&error);
140 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
141 dbus_bus_add_match (bus,
142 "type='method_call',interface='org.freedesktop.Avahi'",
145 if (dbus_error_is_set (&error))
147 g_warning ("dbus_bus_add_match (): %s", error.message);
148 dbus_error_free (&error);
155 dbus_protocol_shutdown ()
158 dbus_connection_disconnect(bus);
159 dbus_connection_unref(bus);