From 53d5c8aa38e134661db9dc451799132372a4841d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 19 Aug 2005 11:12:14 +0000 Subject: [PATCH] * implement avahi_client_free * issue initial GetState() call synchronously git-svn-id: file:///home/lennart/svn/public/avahi/trunk@352 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-client/client-test.c | 2 +- avahi-client/client.c | 144 ++++++++++++++++++------------------- avahi-client/client.h | 17 +++-- avahi-client/internal.h | 11 ++- 4 files changed, 86 insertions(+), 88 deletions(-) diff --git a/avahi-client/client-test.c b/avahi-client/client-test.c index f8f81af..0dd1707 100644 --- a/avahi-client/client-test.c +++ b/avahi-client/client-test.c @@ -165,7 +165,7 @@ fail: avahi_simple_poll_free(simple_poll); if (avahi) - avahi_free (avahi); + avahi_client_free (avahi); return 0; } diff --git a/avahi-client/client.c b/avahi-client/client.c index e99956a..90a816f 100644 --- a/avahi-client/client.c +++ b/avahi-client/client.c @@ -40,17 +40,13 @@ #include "client.h" #include "internal.h" -int -avahi_client_set_errno (AvahiClient *client, int error) -{ +int avahi_client_set_errno (AvahiClient *client, int error) { assert(client); - client->error = error; - - return error; + return client->error = error; } -static void avahi_client_set_state (AvahiClient *client, AvahiServerState state) { +static void client_set_state (AvahiClient *client, AvahiServerState state) { assert(state); if (client->state == state) @@ -62,54 +58,6 @@ static void avahi_client_set_state (AvahiClient *client, AvahiServerState state) client->callback (client, state, client->userdata); } -static void -avahi_client_state_request_callback (DBusPendingCall *call, void *data) -{ - AvahiClient *client = data; - DBusError error; - DBusMessage *reply; - int state, type; - - dbus_error_init (&error); - - reply = dbus_pending_call_steal_reply (call); - - type = dbus_message_get_type (reply); - - if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN) - { - dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID); - - if (dbus_error_is_set (&error)) - return; - - avahi_client_set_state (client, state); - } else if (type == DBUS_MESSAGE_TYPE_ERROR) { - dbus_set_error_from_message (&error, reply); - } - - dbus_pending_call_unref (call); -} - -static void -avahi_client_schedule_state_request (AvahiClient *client) -{ - DBusMessage *message; - DBusPendingCall *pcall; - - /*** Lennart says that this can't happen this way since it will - * never be called if no main loop is used. This call has to - * happen synchronously */ - - if (client == NULL) return; - - message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState"); - - dbus_connection_send_with_reply (client->bus, message, &pcall, -1); - - dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL); -} - static DBusHandlerResult filter_func (DBusConnection *bus, DBusMessage *message, void *data) { @@ -135,9 +83,9 @@ filter_func (DBusConnection *bus, DBusMessage *message, void *data) if (strcmp (name, AVAHI_DBUS_NAME) == 0) { if (old == NULL && new != NULL) { - avahi_client_set_state (client, AVAHI_CLIENT_RECONNECTED); + client_set_state (client, AVAHI_CLIENT_RECONNECTED); } else if (old != NULL && new == NULL) { - avahi_client_set_state (client, AVAHI_CLIENT_DISCONNECTED); + client_set_state (client, AVAHI_CLIENT_DISCONNECTED); /* XXX: we really need to expire all entry groups */ } } @@ -194,9 +142,46 @@ static int translate_dbus_error(const DBusError *error) { return AVAHI_ERR_DBUS_ERROR; } -AvahiClient * -avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) -{ +static int get_server_state(AvahiClient *client, int *ret_error) { + DBusMessage *message, *reply; + DBusError error; + int32_t state; + int e; + + assert(client); + + dbus_error_init(&error); + + if (!(message = dbus_message_new_method_call(AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState"))) + goto fail; + + reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error); + dbus_message_unref(message); + + if (!reply) + goto fail; + + if (!(dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID))) + goto fail; + + client_set_state(client, (AvahiServerState) state); + + return AVAHI_OK; + +fail: + if (dbus_error_is_set(&error)) { + e = translate_dbus_error(&error); + dbus_error_free(&error); + } else + e = AVAHI_ERR_NO_MEMORY; + + if (ret_error) + *ret_error = e; + + return e; +} + +AvahiClient *avahi_client_new(const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *ret_error) { AvahiClient *client = NULL; DBusError error; @@ -279,23 +264,15 @@ avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void goto fail; } - /* This can't happen asynchronously, since it is not guaranteed that a main loop is used */ - - /*client_get_server_state (client);*/ + if (get_server_state(client, ret_error) < 0) + goto fail; return client; fail: - if (client) { - - if (client->bus) { - dbus_connection_disconnect(client->bus); - dbus_connection_unref(client->bus); - } - - avahi_free(client); - } + if (client) + avahi_client_free(client); if (dbus_error_is_set(&error)) dbus_error_free(&error); @@ -303,6 +280,29 @@ fail: return NULL; } +void avahi_client_free(AvahiClient *client) { + assert(client); + + if (client->bus) { + dbus_connection_disconnect(client->bus); + dbus_connection_unref(client->bus); + } + + while (client->groups) + avahi_entry_group_free(client->groups); + + while (client->domain_browsers) + avahi_domain_browser_free(client->domain_browsers); + + while (client->service_browsers) + avahi_service_browser_free(client->service_browsers); + + while (client->service_type_browsers) + avahi_service_type_browser_free(client->service_type_browsers); + + avahi_free(client); +} + static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) { diff --git a/avahi-client/client.h b/avahi-client/client.h index 99f27dd..5aa3efc 100644 --- a/avahi-client/client.h +++ b/avahi-client/client.h @@ -36,15 +36,11 @@ AVAHI_C_DECL_BEGIN #endif -typedef struct _AvahiClient AvahiClient; - -typedef struct _AvahiEntryGroup AvahiEntryGroup; - -typedef struct _AvahiDomainBrowser AvahiDomainBrowser; - -typedef struct _AvahiServiceBrowser AvahiServiceBrowser; - -typedef struct _AvahiServiceTypeBrowser AvahiServiceTypeBrowser; +typedef struct AvahiClient AvahiClient; +typedef struct AvahiEntryGroup AvahiEntryGroup; +typedef struct AvahiDomainBrowser AvahiDomainBrowser; +typedef struct AvahiServiceBrowser AvahiServiceBrowser; +typedef struct AvahiServiceTypeBrowser AvahiServiceTypeBrowser; /** States of a client object, note that AvahiServerStates are also emitted */ typedef enum { @@ -70,6 +66,9 @@ typedef void (*AvahiServiceTypeBrowserCallback) (AvahiServiceTypeBrowser *b, Ava /** Creates a new client instance */ AvahiClient* avahi_client_new (const AvahiPoll *poll_api, AvahiClientCallback callback, void *userdata, int *error); +/** Free a client instance */ +void avahi_client_free(AvahiClient *client); + /** Get the version of the server */ char* avahi_client_get_version_string (AvahiClient*); diff --git a/avahi-client/internal.h b/avahi-client/internal.h index 4aaca39..92a5fbc 100644 --- a/avahi-client/internal.h +++ b/avahi-client/internal.h @@ -25,8 +25,7 @@ #include #include "client.h" -struct _AvahiClient -{ +struct AvahiClient { const AvahiPoll *poll_api; DBusConnection *bus; int error; @@ -41,7 +40,7 @@ struct _AvahiClient AVAHI_LLIST_HEAD(AvahiServiceTypeBrowser, service_type_browsers); }; -struct _AvahiEntryGroup { +struct AvahiEntryGroup { char *path; AvahiClient *client; AvahiEntryGroupCallback callback; @@ -49,7 +48,7 @@ struct _AvahiEntryGroup { AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups); }; -struct _AvahiDomainBrowser { +struct AvahiDomainBrowser { char *path; AvahiClient *client; AvahiDomainBrowserCallback callback; @@ -57,7 +56,7 @@ struct _AvahiDomainBrowser { AVAHI_LLIST_FIELDS(AvahiDomainBrowser, domain_browsers); }; -struct _AvahiServiceBrowser { +struct AvahiServiceBrowser { char *path; AvahiClient *client; AvahiServiceBrowserCallback callback; @@ -65,7 +64,7 @@ struct _AvahiServiceBrowser { AVAHI_LLIST_FIELDS(AvahiServiceBrowser, service_browsers); }; -struct _AvahiServiceTypeBrowser { +struct AvahiServiceTypeBrowser { char *path; AvahiClient *client; AvahiServiceTypeBrowserCallback callback; -- 2.39.5