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