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 #include "simple-protocol.h"
34 #define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi"
36 static DBusHandlerResult
37 do_register (DBusConnection *conn, DBusMessage *message)
42 dbus_error_init (&error);
44 dbus_message_get_args (message, &error,
48 if (dbus_error_is_set (&error))
50 g_warning ("Error parsing register attempt");
51 dbus_error_free (&error);
53 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
56 g_message ("Register received from: %s", s);
58 return DBUS_HANDLER_RESULT_HANDLED;
61 static DBusHandlerResult
62 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
64 GMainLoop *loop = user_data;
67 dbus_error_init (&error);
69 g_message ("dbus: interface=%s, path=%s, member=%s",
70 dbus_message_get_interface (message),
71 dbus_message_get_path (message),
72 dbus_message_get_member (message));
74 if (dbus_message_is_signal (message,
75 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
78 /* No, we shouldn't quit, but until we get somewhere
79 * usefull such that we can restore our state, we will */
80 g_warning ("Disconnnected from d-bus");
82 g_main_loop_quit (loop);
83 return DBUS_HANDLER_RESULT_HANDLED;
84 } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
87 return do_register (conn, message);
88 } else if (dbus_message_is_signal (message,
89 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
94 dbus_message_get_args (message, &error,
95 DBUS_TYPE_STRING, &name,
98 if (dbus_error_is_set (&error))
100 g_warning ("Error parsing NameAcquired message");
101 dbus_error_free (&error);
103 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
106 g_message ("dbus: ServiceAcquired (%s)", name);
108 return DBUS_HANDLER_RESULT_HANDLED;
111 g_message ("dbus: missed event");
113 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
116 int main(int argc, char *argv[]) {
117 GMainLoop *loop = NULL;
118 DBusConnection *bus = NULL;
122 loop = g_main_loop_new(NULL, FALSE);
124 dbus_error_init (&error);
126 bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
130 g_warning ("dbus_bus_get(): %s", error.message);
131 dbus_error_free (&error);
136 dbus_connection_setup_with_g_main (bus, NULL);
137 dbus_connection_set_exit_on_disconnect (bus, FALSE);
139 dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
141 if (dbus_error_is_set (&error))
143 g_warning ("dbus_error_is_set (): %s", error.message);
144 dbus_error_free (&error);
149 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
150 dbus_bus_add_match (bus,
151 "type='method_call',interface='org.freedesktop.Avahi'",
154 if (dbus_error_is_set (&error))
156 g_warning ("dbus_bus_add_match (): %s", error.message);
157 dbus_error_free (&error);
162 if (simple_protocol_setup(NULL) < 0)
165 g_main_loop_run(loop);
171 simple_protocol_shutdown();
174 dbus_connection_disconnect(bus);
175 dbus_connection_unref(bus);
179 g_main_loop_unref(loop);