]> git.meshlink.io Git - catta/blob - avahi-daemon/main.c
* Split out the dbus stuff in the daemon to a separate file so it can be built condi...
[catta] / avahi-daemon / main.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-core/core.h>
27
28 #include "main.h"
29 #include "simple-protocol.h"
30 #include "static-services.h"
31
32 #define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi"
33
34 AvahiServer *avahi_server = NULL;
35
36 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
37     g_assert(s);
38
39     if (state == AVAHI_SERVER_RUNNING) {
40         g_message("Server startup complete.  Host name is <%s>", avahi_server_get_host_name_fqdn(s));
41         static_service_add_to_server();
42     } else if (state == AVAHI_SERVER_COLLISION) {
43         gchar *n;
44
45         static_service_remove_from_server();
46         
47         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
48         g_message("Host name conflict, retrying with <%s>", n);
49         avahi_server_set_host_name(s, n);
50         g_free(n);
51     }
52 }
53
54 int main(int argc, char *argv[]) {
55     GMainLoop *loop = NULL;
56     gint r = 255;
57     AvahiServerConfig config;
58
59     avahi_server_config_init(&config);
60
61     loop = g_main_loop_new(NULL, FALSE);
62
63     if (simple_protocol_setup(NULL) < 0)
64         goto finish;
65
66 #ifdef ENABLE_DBUS
67     if (dbus_protocol_setup () < 0)
68         goto finish;
69 #endif
70
71     if (!(avahi_server = avahi_server_new(NULL, &config, server_callback, NULL)))
72         goto finish;
73
74     static_service_load();
75
76     g_main_loop_run(loop);
77
78     r = 0;
79     
80 finish:
81
82     static_service_remove_from_server();
83     static_service_free_all();
84     
85     simple_protocol_shutdown();
86
87 #ifdef ENABLE_DBUS
88     dbus_protocol_shutdown();
89 #endif
90
91     if (avahi_server)
92         avahi_server_free(avahi_server);
93     
94     if (loop)
95         g_main_loop_unref(loop);
96
97     avahi_server_config_free(&config);
98
99     return r;
100 }