mongoc_client_t * mongoc_client_new (const char *uri_string) { const mongoc_write_concern_t *write_concern; mongoc_client_t *client; mongoc_uri_t *uri; const bson_t *options; bson_iter_t iter; bool has_ssl = false; if (!uri_string) { uri_string = "mongodb://127.0.0.1/"; } if (!(uri = mongoc_uri_new(uri_string))) { return NULL; } options = mongoc_uri_get_options (uri); if (bson_iter_init_find (&iter, options, "ssl") && BSON_ITER_HOLDS_BOOL (&iter) && bson_iter_bool (&iter)) { has_ssl = true; } client = bson_malloc0(sizeof *client); client->uri = uri; client->request_id = rand (); client->initiator = mongoc_client_default_stream_initiator; client->initiator_data = client; write_concern = mongoc_uri_get_write_concern (uri); client->write_concern = mongoc_write_concern_copy (write_concern); client->read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); _mongoc_cluster_init (&client->cluster, client->uri, client); #ifdef MONGOC_ENABLE_SSL if (has_ssl) { mongoc_client_set_ssl_opts (client, mongoc_ssl_opt_get_default ()); } #endif mongoc_counter_clients_active_inc (); return client; }
mongoc_client_t * _mongoc_client_new_from_uri (const mongoc_uri_t *uri, mongoc_topology_t *topology) { mongoc_client_t *client; const mongoc_read_prefs_t *read_prefs; const mongoc_read_concern_t *read_concern; const mongoc_write_concern_t *write_concern; BSON_ASSERT (uri); #ifndef MONGOC_ENABLE_SSL if (mongoc_uri_get_ssl (uri)) { MONGOC_ERROR ("Can't create SSL client, SSL not enabled in this build."); return NULL; } #endif client = (mongoc_client_t *)bson_malloc0(sizeof *client); client->uri = mongoc_uri_copy (uri); client->request_id = rand (); client->initiator = mongoc_client_default_stream_initiator; client->initiator_data = client; client->topology = topology; write_concern = mongoc_uri_get_write_concern (client->uri); client->write_concern = mongoc_write_concern_copy (write_concern); read_concern = mongoc_uri_get_read_concern (client->uri); client->read_concern = mongoc_read_concern_copy (read_concern); read_prefs = mongoc_uri_get_read_prefs_t (client->uri); client->read_prefs = mongoc_read_prefs_copy (read_prefs); mongoc_cluster_init (&client->cluster, client->uri, client); #ifdef MONGOC_ENABLE_SSL client->use_ssl = false; if (mongoc_uri_get_ssl (client->uri)) { /* sets use_ssl = true */ mongoc_client_set_ssl_opts (client, mongoc_ssl_opt_get_default ()); } #endif mongoc_counter_clients_active_inc (); return client; }
mongoc_client_t * _mongoc_client_new_from_uri (const mongoc_uri_t *uri, mongoc_topology_t *topology) { mongoc_client_t *client; const mongoc_read_prefs_t *read_prefs; const mongoc_write_concern_t *write_concern; #ifdef MONGOC_ENABLE_SSL const bson_t *options; bson_iter_t iter; #endif BSON_ASSERT (uri); client = (mongoc_client_t *)bson_malloc0(sizeof *client); client->uri = mongoc_uri_copy (uri); client->request_id = rand (); client->initiator = mongoc_client_default_stream_initiator; client->initiator_data = client; client->topology = topology; write_concern = mongoc_uri_get_write_concern (client->uri); client->write_concern = mongoc_write_concern_copy (write_concern); read_prefs = mongoc_uri_get_read_prefs_t (client->uri); client->read_prefs = mongoc_read_prefs_copy (read_prefs); mongoc_cluster_init (&client->cluster, client->uri, client); #ifdef MONGOC_ENABLE_SSL options = mongoc_uri_get_options (client->uri); if (bson_iter_init_find (&iter, options, "ssl") && BSON_ITER_HOLDS_BOOL (&iter) && bson_iter_bool (&iter)) { mongoc_client_set_ssl_opts (client, mongoc_ssl_opt_get_default ()); } #endif mongoc_counter_clients_active_inc (); return client; }
SEXP R_mongo_client_new(SEXP uri_string) { mongoc_client_t *client = mongoc_client_new (translateCharUTF8(asChar(uri_string))); if(!client) stop("Invalid uri_string. Try mongodb://localhost"); //set ssl certificates here if (mongoc_uri_get_ssl (mongoc_client_get_uri(client))) { mongoc_client_set_ssl_opts(client, mongoc_ssl_opt_get_default()); } //verify that server is online //can fail if user has limited priv /* bson_error_t err; if(!mongoc_client_get_server_status(client, NULL, NULL, &err)){ mongoc_client_destroy(client); stop(err.message); } */ return client2r(client); }