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; }
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; }
MONGO_EXPORT int mongo_reconnect( mongo *conn ) { int res; mongo_disconnect( conn ); if( conn->replset ) { conn->replset->primary_connected = 0; mongo_replset_free_list( &conn->replset->hosts ); conn->replset->hosts = NULL; res = mongo_replset_connect( conn ); return res; } else return mongo_socket_connect( conn, conn->primary->host, conn->primary->port ); }
int mongo_connect( mongo *conn , const char *host, int port ) { conn->primary = bson_malloc( sizeof( mongo_host_port ) ); strncpy( conn->primary->host, host, strlen( host ) + 1 ); conn->primary->port = port; conn->primary->next = NULL; mongo_init( conn ); if( mongo_socket_connect( conn, host, port ) != MONGO_OK ) return MONGO_ERROR; if( mongo_check_is_master( conn ) != MONGO_OK ) return MONGO_ERROR; else return MONGO_OK; }
mongo_conn_return mongo_connect( mongo_connection * conn , const char * host, int port ){ MONGO_INIT_EXCEPTION(&conn->exception); conn->replset = NULL; conn->primary = bson_malloc( sizeof( mongo_host_port ) ); strncpy( conn->primary->host, host, strlen( host ) + 1 ); conn->primary->port = port; conn->primary->next = NULL; conn->err = 0; conn->errstr = NULL; return mongo_socket_connect(conn, host, port); }
int mongo_connect( mongo_connection * conn , const char * host, int port ){ conn->replset = NULL; conn->primary = bson_malloc( sizeof( mongo_host_port ) ); strncpy( conn->primary->host, host, strlen( host ) + 1 ); conn->primary->port = port; conn->primary->next = NULL; conn->conn_timeout_ms = 0; conn->err = 0; conn->errstr = NULL; conn->lasterrcode = 0; conn->lasterrstr = NULL; return mongo_socket_connect(conn, host, port); }