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
26 #include <avahi-core/core.h>
28 #define DBUS_API_SUBJECT_TO_CHANGE
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
32 #define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi"
34 static DBusHandlerResult
35 do_register (DBusConnection *conn, DBusMessage *message)
40 dbus_error_init (&error);
42 dbus_message_get_args (message, &error,
46 if (dbus_error_is_set (&error))
48 g_warning ("Error parsing register attempt");
49 dbus_error_free (&error);
51 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
54 g_message ("Register received from: %s", s);
56 return DBUS_HANDLER_RESULT_HANDLED;
59 static DBusHandlerResult
60 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
62 GMainLoop *loop = user_data;
65 dbus_error_init (&error);
67 g_message ("dbus: interface=%s, path=%s, member=%s",
68 dbus_message_get_interface (message),
69 dbus_message_get_path (message),
70 dbus_message_get_member (message));
72 if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL,
75 /* No, we shouldn't quit, but until we get somewhere
76 * usefull such that we can restore our state, we will */
77 g_warning ("Disconnnected from d-bus");
79 g_main_loop_quit (loop);
80 return DBUS_HANDLER_RESULT_HANDLED;
81 } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
84 return do_register (conn, message);
85 } else if (dbus_message_is_signal (message, DBUS_SERVICE_DBUS,
90 dbus_message_get_args (message, &error,
91 DBUS_TYPE_STRING, &name,
94 if (dbus_error_is_set (&error))
96 g_warning ("Error parsing NameAcquired message");
97 dbus_error_free (&error);
99 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
102 g_message ("dbus: NameAcquired (%s)", name);
104 return DBUS_HANDLER_RESULT_HANDLED;
107 g_message ("dbus: missed event");
109 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
112 int main(int argc, char *argv[]) {
113 GMainLoop *loop = NULL;
117 loop = g_main_loop_new(NULL, FALSE);
119 dbus_error_init (&error);
121 bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
125 g_warning ("dbus_bus_get(): %s", error.message);
126 dbus_error_free (&error);
131 dbus_connection_setup_with_g_main (bus, NULL);
133 dbus_bus_request_name (bus, DBUS_SERVICE_AVAHI, 0, &error);
135 if (dbus_error_is_set (&error))
137 g_warning ("dbus_bus_request_name (): %s", error.message);
138 dbus_error_free (&error);
143 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
144 dbus_bus_add_match (bus,
145 "type='method_call',interface='org.freedesktop.Avahi'",
148 if (dbus_error_is_set (&error))
150 g_warning ("dbus_bus_add_match (): %s", error.message);
151 dbus_error_free (&error);
156 g_main_loop_run(loop);
157 g_main_loop_unref(loop);