/** * \brief This function tries to destroy connection with MongoDB server */ void vs_mongo_conn_destroy(struct VS_CTX *vs_ctx) { if(vs_ctx->mongo_conn != NULL) { mongo_destroy(vs_ctx->mongo_conn); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; v_print_log(VRS_PRINT_DEBUG_MSG, "Connection to MongoDB server %s:%d destroyed\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); } }
/** * \brief This function tries to connect to MongoDB server */ int vs_mongo_conn_init(struct VS_CTX *vs_ctx) { int status; vs_ctx->mongo_conn = mongo_alloc(); mongo_init(vs_ctx->mongo_conn); /* Set connection timeout */ mongo_set_op_timeout(vs_ctx->mongo_conn, 1000); /* Connect to MongoDB server */ status = mongo_client(vs_ctx->mongo_conn, vs_ctx->mongodb_server, vs_ctx->mongodb_port); if( status != MONGO_OK ) { switch ( vs_ctx->mongo_conn->err ) { case MONGO_CONN_NO_SOCKET: v_print_log(VRS_PRINT_ERROR, "No MongoDB server %s:%d socket\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_FAIL: v_print_log(VRS_PRINT_ERROR, "Connection to MongoDB server %s:%d failed\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; case MONGO_CONN_NOT_MASTER: v_print_log(VRS_PRINT_ERROR, "MongoDB server %s:%d is not master\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); break; default: v_print_log(VRS_PRINT_ERROR, "Failed to connect to MongoDB server %s:%d , error: %d\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port, vs_ctx->mongo_conn->err); break; } mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Connection to MongoDB server %s:%d succeeded\n", vs_ctx->mongodb_server, vs_ctx->mongodb_port); /* There has to be some db name defined */ if(vs_ctx->mongodb_db_name == NULL) { v_print_log(VRS_PRINT_ERROR, "No database name defined\n"); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } /* Try to do when authentication is configured */ if(vs_ctx->mongodb_user != NULL && vs_ctx->mongodb_pass != NULL) { status = mongo_cmd_authenticate(vs_ctx->mongo_conn, vs_ctx->mongodb_db_name, vs_ctx->mongodb_user, vs_ctx->mongodb_pass); if(status != MONGO_OK) { v_print_log(VRS_PRINT_ERROR, "Authentication to %s database failed, error: %s\n", vs_ctx->mongodb_db_name, mongo_get_server_err_string(vs_ctx->mongo_conn)); mongo_dealloc(vs_ctx->mongo_conn); vs_ctx->mongo_conn = NULL; return 0; } v_print_log(VRS_PRINT_DEBUG_MSG, "Authentication to %s database succeeded\n", vs_ctx->mongodb_db_name); } else { v_print_log(VRS_PRINT_DEBUG_MSG, "No MongoDB authentication required\n"); } /* Namespace used for storing nodes */ vs_ctx->mongo_node_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 6 + 1)); strcpy(vs_ctx->mongo_node_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_node_ns, ".nodes"); /* Namespace used for storing tag groups and tags */ vs_ctx->mongo_tg_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 11 + 1)); strcpy(vs_ctx->mongo_tg_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_tg_ns, ".tag_groups"); /* Namespace used for storing layers */ vs_ctx->mongo_layer_ns = (char*)malloc(sizeof(char) * (strlen(vs_ctx->mongodb_db_name) + 7 + 1)); strcpy(vs_ctx->mongo_layer_ns, vs_ctx->mongodb_db_name); strcat(vs_ctx->mongo_layer_ns, ".layers"); return 1; }