]> git.meshlink.io Git - catta/blob - avahi-client/entrygroup.c
* Revert revision 303 (Name space changes) as avahi-core has now been changed.
[catta] / avahi-client / entrygroup.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-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <avahi-common/error.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #define DBUS_API_SUBJECT_TO_CHANGE
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
37
38 #include <stdlib.h>
39
40 #include "client.h"
41 #include "internal.h"
42
43 void avahi_entry_group_state_change (AvahiEntryGroup *group, int state)
44 {
45     if (group == NULL || group->callback == NULL)
46         return;
47
48     group->callback (group, state, group->user_data);
49 }
50
51 AvahiEntryGroup*
52 avahi_entry_group_new (AvahiClient *client, AvahiEntryGroupCallback callback, void *user_data)
53 {
54     AvahiEntryGroup *tmp = NULL;
55     DBusMessage *message = NULL, *reply;
56     DBusError error;
57     char *path;
58
59     if (client == NULL)
60         return NULL;
61     
62     dbus_error_init (&error);
63
64     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER,
65             AVAHI_DBUS_INTERFACE_SERVER, "EntryGroupNew");
66
67     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
68
69     if (dbus_error_is_set (&error))
70     {
71         fprintf (stderr, "Error sending EntryGroupNew message: %s\n", error.message);
72         dbus_error_free (&error);
73
74         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
75         goto fail;
76     }
77
78     if (reply == NULL)
79     {
80         fprintf (stderr, "Got NULL reply from EntryGroupNew\n");
81
82         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
83         goto fail;
84     }
85
86     dbus_message_get_args (reply, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID);
87
88     if (dbus_error_is_set (&error))
89     {
90         fprintf (stderr, "Failure parsing EntryGroupNew reply: %s\n", error.message);
91         avahi_client_set_errno (client, AVAHI_ERR_DBUS_ERROR);
92         goto fail;
93     }
94
95     tmp = malloc (sizeof (AvahiEntryGroup));
96
97     tmp->client = client;
98
99     tmp->path = strdup (path);
100     tmp->callback = callback;
101     tmp->user_data = user_data;
102
103     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
104
105     dbus_message_unref (message);
106
107     avahi_client_set_errno (client, AVAHI_OK);
108     return tmp;
109
110 fail:
111     if (tmp) free (tmp);
112     if (message) dbus_message_unref (message);
113     return NULL;
114 }
115
116 int
117 avahi_entry_group_commit (AvahiEntryGroup *group)
118 {
119     DBusMessage *message;
120     DBusError error;
121
122     dbus_error_init (&error);
123
124     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
125             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Commit");
126
127     dbus_connection_send (group->client->bus, message, NULL);
128
129     return avahi_client_set_errno (group->client, AVAHI_OK);
130 }
131
132 int
133 avahi_entry_group_reset (AvahiEntryGroup *group)
134 {
135     DBusMessage *message;
136
137     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
138             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "Reset");
139
140     dbus_connection_send (group->client->bus, message, NULL);
141
142     return avahi_client_set_errno (group->client, AVAHI_OK);
143 }
144
145 int
146 avahi_entry_group_get_state (AvahiEntryGroup *group)
147 {
148     DBusMessage *message, *reply;
149     DBusError error;
150     int state;
151
152     dbus_error_init (&error);
153
154     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
155             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState");
156
157     reply = dbus_connection_send_with_reply_and_block (group->client->bus, message, -1, &error);
158
159     if (dbus_error_is_set (&error))
160     {
161         fprintf (stderr, "Error sending GetState message for %s EntryGroup: %s\n", group->path, error.message);
162         dbus_error_free (&error);
163
164         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
165     }
166
167     dbus_message_get_args(message, &error, DBUS_TYPE_BOOLEAN, &state, DBUS_TYPE_INVALID);
168
169     if (dbus_error_is_set (&error))
170     {
171         fprintf (stderr, "Error parsing GetState reply for %s EntryGroup: %s\n", group->path, error.message);
172         dbus_error_free (&error);
173
174         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
175     }
176
177     avahi_client_set_errno (group->client, AVAHI_OK);
178     return state;
179 }
180
181 int
182 avahi_client_errno (AvahiClient *client)
183 {
184     return client->error;
185 }
186
187 AvahiClient*
188 avahi_entry_group_get_client (AvahiEntryGroup *group)
189 {
190     return group->client;
191 }
192
193 int
194 avahi_entry_group_is_empty (AvahiEntryGroup *group)
195 {
196     return AVAHI_OK;
197 }
198
199 int
200 avahi_entry_group_add_service (AvahiEntryGroup *group,
201                                AvahiIfIndex interface,
202                                AvahiProtocol protocol,
203                                const char *name,
204                                const char *type,
205                                const char *domain,
206                                const char *host,
207                                uint16_t port,
208                                AvahiStringList *txt)
209 {
210     DBusMessage *message;
211     DBusMessageIter iter, sub;
212     AvahiStringList *p;
213
214     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path,
215             AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService");
216
217     if (!message)
218     {
219         dbus_message_unref (message);
220         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
221     }
222
223     if (!dbus_message_append_args (message, DBUS_TYPE_INT32, &interface, DBUS_TYPE_INT32, &protocol,
224                 DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &type, DBUS_TYPE_STRING, &domain,
225                 DBUS_TYPE_STRING, &host, DBUS_TYPE_UINT16, &port, DBUS_TYPE_INVALID))
226     {
227         dbus_message_unref (message);
228         return avahi_client_set_errno (group->client, AVAHI_ERR_DBUS_ERROR);
229     }
230     
231     dbus_message_iter_init_append(message, &iter);
232     dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING, &sub);
233
234     /* Assemble the AvahiStringList into an Array of Array of Bytes to send over dbus */
235     for (p = txt; p != NULL; p = p->next) {
236         DBusMessageIter sub2;
237         const guint8 *data = p->text;
238
239         dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
240         dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
241         dbus_message_iter_close_container(&sub, &sub2);
242     }
243
244     dbus_message_iter_close_container(&iter, &sub);
245
246     dbus_connection_send (group->client->bus, message, NULL);
247
248     return avahi_client_set_errno (group->client, AVAHI_OK);
249 }
250
251 /* XXX: debug function */
252 char* avahi_entry_group_path (AvahiEntryGroup *group)
253 {
254     if (group != NULL) return group->path;
255     else return NULL;
256 }