Logo Search packages:      
Sourcecode: avahi version File versions

AvahiClient* avahi_client_new ( const AvahiPoll poll_api,
AvahiClientFlags  flags,
AvahiClientCallback  callback,
void *  userdata,
int *  error 
)

Creates a new client instance

Parameters:
poll_api  The abstract event loop API to use
flags  Some flags to modify the behaviour of the client library
callback  A callback that is called whenever the state of the client changes. This may be NULL. Please note that this function is called for the first time from within the avahi_client_new() context! Thus, in the callback you should not make use of global variables that are initialized only after your call to avahi_client_new(). A common mistake is to store the AvahiClient pointer returned by avahi_client_new() in a global variable and assume that this global variable already contains the valid pointer when the callback is called for the first time. A work-around for this is to always use the AvahiClient pointer passed to the callback function instead of the global pointer.
userdata  Some arbitrary user data pointer that will be passed to the callback function
error  If creation of the client fails, this integer will contain the error cause. May be NULL if you aren't interested in the reason why avahi_client_new() failed.

Definition at line 478 of file client.c.

References AVAHI_CLIENT_CONNECTING, AVAHI_CLIENT_NO_FAIL, AVAHI_ERR_DBUS_ERROR, AVAHI_ERR_NO_DAEMON, AVAHI_ERR_NO_MEMORY, AVAHI_LLIST_HEAD_INIT, avahi_new, and AVAHI_OK.

                                                                                                                                               {
    AvahiClient *client = NULL;
    DBusError error;

    dbus_error_init(&error);

    if (!(client = avahi_new(AvahiClient, 1))) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY;
        goto fail;
    }

    client->poll_api = poll_api;
    client->error = AVAHI_OK;
    client->callback = callback;
    client->userdata = userdata;
    client->state = (AvahiClientState) -1;
    client->flags = flags;
    
    client->host_name = NULL;
    client->host_name_fqdn = NULL;
    client->domain_name = NULL;
    client->version_string = NULL;
    client->local_service_cookie_valid = 0;
    
    AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, client->groups);
    AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, client->domain_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, client->service_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, client->service_type_browsers);
    AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, client->service_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, client->host_name_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, client->address_resolvers);
    AVAHI_LLIST_HEAD_INIT(AvahiRecordBrowser, client->record_browsers);

    if (!(client->bus = avahi_dbus_bus_get(&error)) || dbus_error_is_set(&error)) {
        if (ret_error)
            *ret_error = AVAHI_ERR_DBUS_ERROR;
        goto fail;
    }

    if (avahi_dbus_connection_glue(client->bus, poll_api) < 0) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY; /* Not optimal */
        goto fail;
    }

    if (!dbus_connection_add_filter (client->bus, filter_func, client, NULL)) {
        if (ret_error)
            *ret_error = AVAHI_ERR_NO_MEMORY; 
        goto fail;
    }
        
    dbus_bus_add_match(
        client->bus,
        "type='signal', "
        "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
        "sender='" AVAHI_DBUS_NAME "', "
        "path='" AVAHI_DBUS_PATH_SERVER "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;

    dbus_bus_add_match (
        client->bus,
        "type='signal', "
        "interface='" DBUS_INTERFACE_DBUS "', "
        "sender='" DBUS_SERVICE_DBUS "', "
        "path='" DBUS_PATH_DBUS "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;

    dbus_bus_add_match (
        client->bus,
        "type='signal', "
        "interface='" DBUS_INTERFACE_LOCAL "'",
        &error);

    if (dbus_error_is_set(&error))
        goto fail;


    if (!(dbus_bus_name_has_owner(client->bus, AVAHI_DBUS_NAME, &error)) ||
        dbus_error_is_set(&error)) {

        /* We free the error so its not set, that way the fail target
         * will return the NO_DAEMON error rather than a DBUS error */
        dbus_error_free(&error);

        if (!(flags & AVAHI_CLIENT_NO_FAIL)) {
            
            if (ret_error)
                *ret_error = AVAHI_ERR_NO_DAEMON;
        
            goto fail;
        }

        /* The user doesn't want this call to fail if the daemon is not
         * available, so let's return succesfully */
        client_set_state(client, AVAHI_CLIENT_CONNECTING);
        
    } else {

        if (init_server(client, ret_error) < 0)
            goto fail;
    }

    return client;

fail:

    if (client)
        avahi_client_free(client);

    if (dbus_error_is_set(&error)) {

        if (ret_error) {
            if (strcmp(error.name, DBUS_ERROR_FILE_NOT_FOUND) == 0)
                /* DBUS returns this error when the DBUS daemon is not running */
                *ret_error = AVAHI_ERR_NO_DAEMON;
            else
                *ret_error = avahi_error_dbus_to_number(error.name);
        }
        
        dbus_error_free(&error);
    }
        
    return NULL;
}


Generated by  Doxygen 1.6.0   Back to index