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>
33 #include "simple-protocol.h"
34 #include "static-services.h"
36 #define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi"
38 AvahiServer *avahi_server = NULL;
40 static DBusHandlerResult
41 do_register (DBusConnection *conn, DBusMessage *message)
46 dbus_error_init (&error);
48 dbus_message_get_args (message, &error,
52 if (dbus_error_is_set (&error))
54 g_warning ("Error parsing register attempt");
55 dbus_error_free (&error);
57 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
60 g_message ("Register received from: %s", s);
62 return DBUS_HANDLER_RESULT_HANDLED;
65 static DBusHandlerResult
66 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
68 GMainLoop *loop = user_data;
71 dbus_error_init (&error);
73 g_message ("dbus: interface=%s, path=%s, member=%s",
74 dbus_message_get_interface (message),
75 dbus_message_get_path (message),
76 dbus_message_get_member (message));
78 if (dbus_message_is_signal (message,
79 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
82 /* No, we shouldn't quit, but until we get somewhere
83 * usefull such that we can restore our state, we will */
84 g_warning ("Disconnnected from d-bus");
86 g_main_loop_quit (loop);
87 return DBUS_HANDLER_RESULT_HANDLED;
88 } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
91 return do_register (conn, message);
92 } else if (dbus_message_is_signal (message,
93 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
98 dbus_message_get_args (message, &error,
99 DBUS_TYPE_STRING, &name,
102 if (dbus_error_is_set (&error))
104 g_warning ("Error parsing NameAcquired message");
105 dbus_error_free (&error);
107 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
110 g_message ("dbus: ServiceAcquired (%s)", name);
112 return DBUS_HANDLER_RESULT_HANDLED;
115 g_message ("dbus: missed event");
117 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
120 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
123 if (state == AVAHI_SERVER_RUNNING) {
124 g_message("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
125 static_service_add_to_server();
126 } else if (state == AVAHI_SERVER_COLLISION) {
129 static_service_remove_from_server();
131 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
132 g_message("Host name conflict, retrying with <%s>", n);
133 avahi_server_set_host_name(s, n);
138 int main(int argc, char *argv[]) {
139 GMainLoop *loop = NULL;
140 DBusConnection *bus = NULL;
143 AvahiServerConfig config;
145 avahi_server_config_init(&config);
147 loop = g_main_loop_new(NULL, FALSE);
149 dbus_error_init (&error);
151 bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
155 g_warning ("dbus_bus_get(): %s", error.message);
156 dbus_error_free (&error);
161 dbus_connection_setup_with_g_main (bus, NULL);
162 dbus_connection_set_exit_on_disconnect (bus, FALSE);
164 dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
166 if (dbus_error_is_set (&error))
168 g_warning ("dbus_error_is_set (): %s", error.message);
169 dbus_error_free (&error);
174 dbus_connection_add_filter (bus, signal_filter, loop, NULL);
175 dbus_bus_add_match (bus,
176 "type='method_call',interface='org.freedesktop.Avahi'",
179 if (dbus_error_is_set (&error))
181 g_warning ("dbus_bus_add_match (): %s", error.message);
182 dbus_error_free (&error);
187 if (simple_protocol_setup(NULL) < 0)
190 if (!(avahi_server = avahi_server_new(NULL, &config, server_callback, NULL)))
193 static_service_load();
195 g_main_loop_run(loop);
201 static_service_remove_from_server();
202 static_service_free_all();
204 simple_protocol_shutdown();
207 dbus_connection_disconnect(bus);
208 dbus_connection_unref(bus);
212 avahi_server_free(avahi_server);
215 g_main_loop_unref(loop);
217 avahi_server_config_free(&config);