]> git.meshlink.io Git - catta/blob - avahi-daemon/main.c
* rename daemon to avahi-daemon
[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 #define DBUS_API_SUBJECT_TO_CHANGE
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
31
32 #define DBUS_SERVICE_AVAHI "org.freedesktop.Avahi"
33
34 static DBusHandlerResult
35 do_register (DBusConnection *conn, DBusMessage *message)
36 {
37         DBusError error;
38         char *s;
39
40         dbus_error_init (&error);
41
42         dbus_message_get_args (message, &error,
43                         DBUS_TYPE_STRING, &s,
44                         DBUS_TYPE_INVALID);
45
46         if (dbus_error_is_set (&error))
47         {
48                 g_warning ("Error parsing register attempt");
49                 dbus_error_free (&error);
50
51                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
52         }
53
54         g_message ("Register received from: %s", s);
55
56         return DBUS_HANDLER_RESULT_HANDLED;
57 }
58
59 static DBusHandlerResult
60 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
61 {
62         GMainLoop *loop = user_data;
63         DBusError error;
64
65         dbus_error_init (&error);
66
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));
71
72         if (dbus_message_is_signal (message,
73                                 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
74                                 "Disconnected"))
75         {
76                 /* No, we shouldn't quit, but until we get somewhere
77                  * usefull such that we can restore our state, we will */
78                 g_warning ("Disconnnected from d-bus");
79
80                 g_main_loop_quit (loop);
81                 return DBUS_HANDLER_RESULT_HANDLED;
82         } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
83                                 "Register"))
84         {
85                 return do_register (conn, message);
86         } else if (dbus_message_is_signal (message,
87                                 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
88                                 "ServiceAcquired"))
89         {
90                 char *name;
91
92                 dbus_message_get_args (message, &error,
93                                 DBUS_TYPE_STRING, &name,
94                                 DBUS_TYPE_INVALID);
95
96                 if (dbus_error_is_set (&error))
97                 {
98                         g_warning ("Error parsing NameAcquired message");
99                         dbus_error_free (&error);
100
101                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
102                 }
103
104                 g_message ("dbus: ServiceAcquired (%s)", name);
105
106                 return DBUS_HANDLER_RESULT_HANDLED;
107         }
108
109         g_message ("dbus: missed event");
110
111         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
112 }
113                 
114 int main(int argc, char *argv[]) {
115     GMainLoop *loop = NULL;
116     DBusConnection *bus;
117     DBusError error;
118
119     loop = g_main_loop_new(NULL, FALSE);
120
121     dbus_error_init (&error);
122
123
124     bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
125
126     if (bus == NULL)
127     {
128             g_warning ("dbus_bus_get(): %s", error.message);
129             dbus_error_free (&error);
130
131             return -1;
132     }
133
134     dbus_connection_setup_with_g_main (bus, NULL);
135     dbus_connection_set_exit_on_disconnect (bus, FALSE);
136
137     dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
138
139     if (dbus_error_is_set (&error))
140     {
141             g_warning ("dbus_error_is_set (): %s", error.message);
142             dbus_error_free (&error);
143
144             return -1;
145     }
146
147     dbus_connection_add_filter (bus, signal_filter, loop, NULL);
148     dbus_bus_add_match (bus,
149                     "type='method_call',interface='org.freedesktop.Avahi'",
150                     &error);
151
152     if (dbus_error_is_set (&error))
153     {
154             g_warning ("dbus_bus_add_match (): %s", error.message);
155             dbus_error_free (&error);
156
157             return -1;
158     }
159
160     g_main_loop_run(loop);
161     g_main_loop_unref(loop);
162     
163     return 0;
164 }