コード例 #1
0
static void mongo_replset_check_seed( mongo_connection* conn ) {
    bson out;
    bson hosts;
    const char* data;
    bson_iterator it;
    bson_iterator it_sub;
    const char* host_string;
    mongo_host_port *host_port = NULL;

    out.data = NULL;
    out.owned = 1;

    hosts.data = NULL;
    hosts.owned = 1;

    if( mongo_simple_int_command(conn, "admin", "ismaster", 1, &out) == MONGO_OK ) {

        if( bson_find( &it, &out, "hosts" ) ) {
            data = bson_iterator_value( &it );
            bson_iterator_init( &it_sub, data );

            /* Iterate over host list, adding each host to the
             * connection's host list. */
            while( bson_iterator_next( &it_sub ) ) {
                host_string = bson_iterator_string( &it_sub );

                host_port = bson_malloc( sizeof( mongo_host_port ) );
                mongo_parse_host( host_string, host_port );

                if( host_port ) {
                    mongo_replset_add_node( &conn->replset->hosts,
                        host_port->host, host_port->port );

                    free( host_port );
                    host_port = NULL;
                }
            }
        }
    }

    bson_destroy( &out );
    bson_destroy( &hosts );
    mongo_close_socket( conn->sock );
    conn->sock = 0;
    conn->connected = 0;

}
コード例 #2
0
ファイル: mongo.c プロジェクト: chitr/mongo-c-driver
MONGO_EXPORT void mongo_replset_add_seed( mongo *conn, const char *host, int port ) {
    mongo_replset_add_node( &conn->replset->seeds, host, port );
}
コード例 #3
0
void mongo_replset_add_seed(mongo_connection* conn, const char* host, int port) {
    mongo_replset_add_node( &conn->replset->seeds, host, port );
}
コード例 #4
0
ファイル: mongo.c プロジェクト: alex-python/restpose
static int mongo_replset_check_seed( mongo_connection* conn ) {
    bson out;
    bson hosts;
    const char* data;
    bson_iterator it;
    bson_iterator it_sub;
    const char* host_string;
    char* host;
    int len, idx, port, split;

    out.data = NULL;
    out.owned = 1;

    hosts.data = NULL;
    hosts.owned = 1;

    if (mongo_simple_int_command(conn, "admin", "ismaster", 1, &out)) {

        if( bson_find( &it, &out, "hosts" ) ) {
            data = bson_iterator_value( &it );
            bson_iterator_init( &it_sub, data );

            /* Iterate over host list, adding each host to the
             * connection's host list.
             */
            while( bson_iterator_next( &it_sub ) ) {
                host_string = bson_iterator_string( &it_sub );
                len = split = idx = 0;

                /* Split the host_port string at the ':' */
                while(1) {
                    if( *(host_string + len) == 0)
                      break;
                    if( *(host_string + len) == ':' )
                      split = len;
                    len++;
                }

                /* If 'split' is set, we know the that port exists;
                 * Otherwise, we set the default port.
                 */
                if( len > 0 ) {
                    idx = split ? split : len;
                    host = (char *)bson_malloc( idx + 1 );
                    memcpy( host, host_string, idx );
                    memcpy( host + idx, "\0", 1 );
                    if( split )
                        port = atoi( host_string + idx + 1 );
                    else
                        port = 27017;

                    mongo_replset_add_node( &conn->replset->hosts, host, port );
                }
            }
        }
    }

    bson_destroy( &out );
    bson_destroy( &hosts );
    mongo_close_socket( conn->sock );
    conn->sock = 0;
    conn->connected = 0;

    return 0;
}