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