int mongo_replset_connect(mongo_connection* conn) {

    int res = 0;
    mongo_host_port* node;

    conn->sock = 0;
    conn->connected = 0;

    /* First iterate over the seed nodes to get the canonical list of hosts
     * from the replica set. Break out once we have a host list.
     */
    node = conn->replset->seeds;
    while( node != NULL ) {
        res = mongo_socket_connect( conn, (const char*)&node->host, node->port );
        if( res != MONGO_OK )
            return MONGO_ERROR;

        mongo_replset_check_seed( conn );

        if( conn->replset->hosts )
            break;

        node = node->next;
    }

    /* Iterate over the host list, checking for the primary node. */
    if( !conn->replset->hosts ) {
        conn->err = MONGO_CONN_CANNOT_FIND_PRIMARY;
        return MONGO_ERROR;
    }
    else {
        node = conn->replset->hosts;

        while( node != NULL ) {
            res = mongo_socket_connect( conn, (const char*)&node->host, node->port );

            if( res == MONGO_OK ) {
                if( mongo_replset_check_host( conn ) != MONGO_OK )
                    return MONGO_ERROR;

                /* Primary found, so return. */
                else if( conn->replset->primary_connected )
                     return MONGO_OK;

                /* No primary, so close the connection. */
                else {
                    mongo_close_socket( conn->sock );
                    conn->sock = 0;
                    conn->connected = 0;
                }
            }

            node = node->next;
        }
    }


    conn->err = MONGO_CONN_CANNOT_FIND_PRIMARY;
    return MONGO_ERROR;
}
Exemple #2
0
mongo_conn_return mongo_replset_connect(mongo_connection* conn) {

    int connect_error = 0;
    mongo_host_port* node;

    conn->sock = 0;
    conn->connected = 0;

    /* First iterate over the seed nodes to get the canonical list of hosts
     * from the replica set. Break out once we have a host list.
     */
    node = conn->replset->seeds;
    while( node != NULL ) {
        connect_error = mongo_socket_connect( conn, (const char*)&node->host, node->port );

        if( connect_error == 0 ) {
            if ( (connect_error = mongo_replset_check_seed( conn )) )
                return connect_error;
        }

        if( conn->replset->hosts )
            break;

        node = node->next;
    }

    /* Iterate over the host list, checking for the primary node. */
    if( !conn->replset->hosts )
        return mongo_conn_cannot_find_primary;
    else {
        node = conn->replset->hosts;

        while( node != NULL ) {
            connect_error = mongo_socket_connect( conn, (const char*)&node->host, node->port );

            if( connect_error == 0 ) {
                if ( (connect_error = mongo_replset_check_host( conn )) )
                    return connect_error;

                /* Primary found, so return. */
                else if( conn->replset->primary_connected )
                     return 0;

                /* No primary, so close the connection. */
                else {
                    mongo_close_socket( conn->sock );
                    conn->sock = 0;
                    conn->connected = 0;
                }
            }

            node = node->next;
        }
    }

    return mongo_conn_cannot_find_primary;
}