]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-protocol.c
* using AC_GNU_SOURCE instead of -D _GNU_SOURCE
[catta] / avahi-daemon / dbus-protocol.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 #include <glib.h>
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #define DBUS_API_SUBJECT_TO_CHANGE
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
31     
32 #include "dbus-protocol.h"
33
34 static DBusConnection *bus = NULL;
35
36 static DBusHandlerResult
37 do_register (DBusConnection *conn, DBusMessage *message)
38 {
39     DBusError error;
40     char *s;
41
42     dbus_error_init (&error);
43
44     dbus_message_get_args (message, &error,
45                            DBUS_TYPE_STRING, &s,
46                            DBUS_TYPE_INVALID);
47
48     if (dbus_error_is_set (&error))
49     {
50         g_warning ("Error parsing register attempt");
51         dbus_error_free (&error);
52
53         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
54     }
55
56     g_message ("Register received from: %s (dbus: %s)", s, dbus_message_get_sender (message));
57
58     return DBUS_HANDLER_RESULT_HANDLED;
59 }
60
61 static DBusHandlerResult
62 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
63 {
64     GMainLoop *loop = user_data;
65     DBusError error;
66
67     dbus_error_init (&error);
68
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));
73
74     if (dbus_message_is_signal (message,
75 #ifdef DBUS_USE_NEW_API
76                                 DBUS_INTERFACE_LOCAL,
77 #else
78                                 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
79 #endif
80                                 "Disconnected"))
81     {
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, terminating...");
85
86         g_main_loop_quit (loop);
87         return DBUS_HANDLER_RESULT_HANDLED;
88     } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
89                                             "Register"))
90     {
91         return do_register (conn, message);
92     } else if (dbus_message_is_signal (message,
93 #ifdef DBUS_USE_NEW_API
94                                        DBUS_INTERFACE_DBUS,
95                                        "NameAcquired"))
96 #else
97                                        DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
98                                        "ServiceAcquired"))
99 #endif
100     {
101         char *name;
102
103         dbus_message_get_args (message, &error,
104                                DBUS_TYPE_STRING, &name,
105                                DBUS_TYPE_INVALID);
106
107         if (dbus_error_is_set (&error))
108         {
109             g_warning ("Error parsing NameAcquired message");
110             dbus_error_free (&error);
111
112             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
113         }
114
115         g_message ("dbus: ServiceAcquired (%s)", name);
116
117         return DBUS_HANDLER_RESULT_HANDLED;
118     }
119
120     g_message ("dbus: missed event");
121
122     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
123 }
124
125 int
126 dbus_protocol_setup (GMainLoop *loop)
127 {
128     DBusError error;
129
130     dbus_error_init (&error);
131
132     bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
133
134     if (bus == NULL)
135     {
136         g_warning ("dbus_bus_get(): %s", error.message);
137         dbus_error_free (&error);
138
139         return 1;
140     }
141
142     dbus_connection_setup_with_g_main (bus, NULL);
143     dbus_connection_set_exit_on_disconnect (bus, FALSE);
144
145 #ifdef DBUS_USE_NEW_API
146     dbus_bus_request_name (bus, DBUS_SERVICE_AVAHI, 0, &error);
147 #else
148     dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
149 #endif
150
151     if (dbus_error_is_set (&error))
152     {
153         g_warning ("dbus_error_is_set (): %s", error.message);
154         dbus_error_free (&error);
155
156         return 1;
157     }
158
159     dbus_connection_add_filter (bus, signal_filter, loop, NULL);
160     dbus_bus_add_match (bus,
161                         "type='method_call',interface='org.freedesktop.Avahi'",
162                         &error);
163
164     if (dbus_error_is_set (&error))
165     {
166         g_warning ("dbus_bus_add_match (): %s", error.message);
167         dbus_error_free (&error);
168
169         return 1;
170     }
171
172     return 0;
173 }
174
175 void
176 dbus_protocol_shutdown ()
177 {
178     if (bus) {
179         dbus_connection_disconnect(bus);
180         dbus_connection_unref(bus);
181     }
182 }