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
23 #define DBUS_API_SUBJECT_TO_CHANGE
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib-lowlevel.h>
27 #include "dbus-protocol.h"
29 static DBusConnection *bus = NULL;
31 static DBusHandlerResult
32 do_register (DBusConnection *conn, DBusMessage *message)
37 dbus_error_init (&error);
39 dbus_message_get_args (message, &error,
43 if (dbus_error_is_set (&error))
45 g_warning ("Error parsing register attempt");
46 dbus_error_free (&error);
48 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
51 g_message ("Register received from: %s", s);
53 return DBUS_HANDLER_RESULT_HANDLED;
56 static DBusHandlerResult
57 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
59 GMainLoop *loop = user_data;
62 dbus_error_init (&error);
64 g_message ("dbus: interface=%s, path=%s, member=%s",
65 dbus_message_get_interface (message),
66 dbus_message_get_path (message),
67 dbus_message_get_member (message));
69 if (dbus_message_is_signal (message,
70 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
73 /* No, we shouldn't quit, but until we get somewhere
74 * usefull such that we can restore our state, we will */
75 g_warning ("Disconnnected from d-bus, terminating...");
77 g_main_loop_quit (loop);
78 return DBUS_HANDLER_RESULT_HANDLED;
79 } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
82 return do_register (conn, message);
83 } else if (dbus_message_is_signal (message,
84 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
89 dbus_message_get_args (message, &error,
90 DBUS_TYPE_STRING, &name,
93 if (dbus_error_is_set (&error))
95 g_warning ("Error parsing NameAcquired message");
96 dbus_error_free (&error);
98 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
101 g_message ("dbus: ServiceAcquired (%s)", name);
103 return DBUS_HANDLER_RESULT_HANDLED;
106 g_message ("dbus: missed event");
108 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
112 dbus_protocol_setup ()
116 dbus_error_init (&error);
118 bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
122 g_warning ("dbus_bus_get(): %s", error.message);
123 dbus_error_free (&error);
128 dbus_connection_setup_with_g_main (bus, NULL);
129 dbus_connection_set_exit_on_disconnect (bus, FALSE);
131 dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
133 if (dbus_error_is_set (&error))
135 g_warning ("dbus_error_is_set (): %s", error.message);
136 dbus_error_free (&error);
141 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
142 dbus_bus_add_match (bus,
143 "type='method_call',interface='org.freedesktop.Avahi'",
146 if (dbus_error_is_set (&error))
148 g_warning ("dbus_bus_add_match (): %s", error.message);
149 dbus_error_free (&error);
156 dbus_protocol_shutdown ()
159 dbus_connection_disconnect(bus);
160 dbus_connection_unref(bus);