CDRIVER-21 use getaddrinfo for replica sets; test fixes

This commit is contained in:
Kyle Banker 2011-04-05 17:58:29 -04:00
Родитель 2045c312d5
Коммит b08b5f18e7
12 изменённых файлов: 32 добавлений и 77 удалений

Просмотреть файл

@ -28,7 +28,7 @@ import sys
env = Environment( ENV=os.environ )
if os.sys.platform in ["darwin", "linux2"]:
env.Append( CPPFLAGS=" -pedantic -Wall -ggdb " )
env.Append( CPPFLAGS=" -pedantic -Wall -ggdb" )
env.Append( CPPPATH=["/opt/local/include/"] )
env.Append( LIBPATH=["/opt/local/lib/"] )
@ -38,12 +38,14 @@ if os.sys.platform in ["darwin", "linux2"]:
else:
env.Append( CFLAGS=" -ansi " )
env.Append( CFLAGS=" -D_POSIX_SOURCE")
if GetOption('optimize'):
env.Append( CPPFLAGS=" -O3 " )
# -O3 benchmarks *significantly* faster than -O2 when disabling networking
elif 'win32' == os.sys.platform:
env.Append( LIBS='ws2_32' )
#we shouldn't need these options in c99 mode
if not GetOption('use_c99'):

Просмотреть файл

@ -29,8 +29,6 @@
#include <unistd.h>
#endif
#include <strings.h>
/* only need one of these */
static const int zero = 0;
static const int one = 1;
@ -111,34 +109,41 @@ mongo_message * mongo_message_create( int len , int id , int responseTo , int op
connection stuff
------------------------------ */
static int mongo_socket_connect( mongo_connection * conn, const char * host, int port ){
/* setup */
struct addrinfo* addrs = NULL;
struct addrinfo hints;
char port_str[12];
int ret;
conn->sock = 0;
conn->connected = 0;
memset( conn->sa.sin_zero , 0 , sizeof(conn->sa.sin_zero) );
conn->sa.sin_family = AF_INET;
conn->sa.sin_port = htons(port);
conn->sa.sin_addr.s_addr = inet_addr(host);
conn->addressSize = sizeof(conn->sa);
memset( &hints, 0, sizeof( hints ) );
hints.ai_family = AF_INET;
sprintf( port_str, "%d", port );
/* connect */
conn->sock = socket( AF_INET, SOCK_STREAM, 0 );
if ( conn->sock <= 0 ){
mongo_close_socket( conn->sock );
return mongo_conn_no_socket;
}
if ( connect( conn->sock , (struct sockaddr*)&conn->sa , conn->addressSize ) ){
mongo_close_socket( conn->sock );
ret = getaddrinfo( host, port_str, &hints, &addrs );
if(ret) {
fprintf( stderr, "getaddrinfo failed: %s", gai_strerror( ret ) );
return mongo_conn_fail;
}
if ( connect( conn->sock, addrs->ai_addr, addrs->ai_addrlen ) ){
mongo_close_socket( conn->sock );
freeaddrinfo( addrs );
return mongo_conn_fail;
}
/* nagle */
setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one) );
/* TODO signals */
conn->connected = 1;
freeaddrinfo( addrs );
return 0;
}
@ -290,7 +295,6 @@ mongo_conn_return mongo_replset_connect(mongo_connection* conn) {
int connect_error = 0;
mongo_host_port* node;
mongo_host_port* p;
conn->sock = 0;
conn->connected = 0;
@ -306,7 +310,6 @@ mongo_conn_return mongo_replset_connect(mongo_connection* conn) {
if ( (connect_error = mongo_replset_check_seed( conn )) )
return connect_error;
}
printf("%s:%d", node->host, node->port);
if( conn->hosts )
break;
@ -314,22 +317,12 @@ mongo_conn_return mongo_replset_connect(mongo_connection* conn) {
node = node->next;
}
p = conn->hosts;
printf("2");
while(p != NULL) {
printf("\nLIST: %s:%d", p->host, p->port);
p = p->next;
}
/* Iterate over the host list, checking for the primary node. */
if( !conn->hosts )
return mongo_conn_cannot_find_primary;
else {
printf("Made it!");
node = conn->hosts;
printf("%s:%d", node->host, node->port );
while( node != NULL ) {
connect_error = mongo_socket_connect( conn, (const char*)&node->host, node->port );

Просмотреть файл

@ -30,6 +30,7 @@ typedef int socklen_t;
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#define mongo_close_socket(sock) ( close(sock) )

Просмотреть файл

@ -42,7 +42,6 @@ int main(){
bson_append_symbol(&bb, "symbol", "SYMBOL");
{
char hex[30];
bson_buffer scope_buf;
bson scope;
bson_buffer_init(&scope_buf);

Просмотреть файл

@ -11,7 +11,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER, 27017 )){
if (mongo_connect( conn , TEST_SERVER, 27017 )){
printf("failed to connect\n");
exit(1);
}

Просмотреть файл

@ -18,7 +18,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER , 27017 )){
if (mongo_connect( conn , TEST_SERVER , 27017 )){
printf("failed to connect\n");
exit(1);
}

Просмотреть файл

@ -13,7 +13,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER, 27017 )){
if (mongo_connect( conn , TEST_SERVER, 27017 )){
printf("failed to connect\n");
exit(1);
}

Просмотреть файл

@ -8,7 +8,6 @@ int main(){
bson_buffer bb;
bson b, sub;
bson_iterator it;
bson_type type;
/* Create a rich document like this one:
*
@ -74,41 +73,3 @@ int main(){
return 0;
}
/*
void bson_iterate_object( bson* b, int depth ) {
bson_iterator it;
bson_iterator_init( &it, b->data );
while ( bson_iterator_next( &it ) ){
bson_type t = bson_iterator_type( &it );
if ( t == 0 )
break;
key = bson_iterator_key( &i );
for ( temp=0; temp<=depth; temp++ )
printf( "\t" );
printf( "%s : %d \t " , key , t );
switch ( t ){
case bson_int: printf( "%d" , bson_iterator_int( &i ) ); break;
case bson_double: printf( "%f" , bson_iterator_double( &i ) ); break;
case bson_bool: printf( "%s" , bson_iterator_bool( &i ) ? "true" : "false" ); break;
case bson_string: printf( "%s" , bson_iterator_string( &i ) ); break;
case bson_null: printf( "null" ); break;
case bson_oid: bson_oid_to_string(bson_iterator_oid(&i), oidhex); printf( "%s" , oidhex ); break;
case bson_timestamp:
ts = bson_iterator_timestamp( &i );
printf("i: %d, t: %d", ts.i, ts.t);
break;
case bson_object:
case bson_array:
printf( "\n" );
bson_print_raw( bson_iterator_value( &i ) , depth + 1 );
break;
default:
fprintf( stderr , "can't print type : %d\n" , t );
}
printf( "\n" );
}
} */

Просмотреть файл

@ -84,7 +84,7 @@ void test_basic() {
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn, &TEST_SERVER, 27017 )){
if (mongo_connect( conn, TEST_SERVER, 27017 )){
printf("failed to connect 2\n");
exit(1);
}
@ -123,7 +123,7 @@ void test_streaming() {
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER, 27017 )){
if (mongo_connect( conn , TEST_SERVER, 27017 )){
printf("failed to connect 3\n");
exit(1);
}
@ -162,7 +162,7 @@ void test_large() {
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn, &TEST_SERVER, 27017 )){
if (mongo_connect( conn, TEST_SERVER, 27017 )){
printf("failed to connect 1\n");
exit(1);
}

Просмотреть файл

@ -9,7 +9,6 @@
int test_connect( const char* set_name ) {
mongo_connection conn[1];
int error = 0;
INIT_SOCKETS_FOR_WINDOWS;

Просмотреть файл

@ -20,7 +20,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER, 27017 )){
if (mongo_connect( conn , TEST_SERVER, 27017 )){
printf("failed to connect\n");
exit(1);
}

Просмотреть файл

@ -16,7 +16,7 @@ int main(){
INIT_SOCKETS_FOR_WINDOWS;
if (mongo_connect( conn , &TEST_SERVER, 27017 )){
if (mongo_connect( conn , TEST_SERVER, 27017 )){
printf("failed to connect\n");
exit(1);
}