mongo::DBClientConnection &getInstance() { static mongo::DBClientConnection instance; static bool ran = false; if(!ran) { ran = true; #ifdef __APPLE__ instance.connect("localhost"); #else instance.connect(MONGO_IP); std::string errmsg; instance.auth(MONGO_SOURCE, MONGO_USER, MONGO_PASSWORD, errmsg); #endif } return instance; }
int main(int argc, char *argv[]) { int opt; const char *optstr = "m:f:h"; const char *mongo_server = "localhost:27017"; string errmsg; while ((opt = getopt(argc, argv, optstr)) != -1) { switch (opt) { case 'm': mongo_server = optarg; break; case 'f': dns_server = optarg; break; case 'h': default: print_usage(argv[0]); return 0; } } if (! mongo_conn.connect(mongo_server, errmsg)) { cerr << errmsg << endl; return -1; } resolve_soa(); return 0; }
void dashboard_conn_check(std::string cloud_name) { if(!msattrs) { APIDictionary result(perform("cldshow", cloud_name, "metricstore")); host = APIString(result["hostname"]); port = APIString(result["port"]); msci.connect(host + ":" + port + "/metrics"); result = APIDictionary(perform("cldshow", cloud_name, "time")); username = APIString(result["username"]); msattrs = 1; } }
mongo::DBClientConnection& GetDB() { if(!connected) { std::string host(GetConfig().GetAttribute("Database.host").ToString()); LOGINFO("正在连接到 " + host + " 的 mongodb 服务器"); try { dbconnection.connect(host); connected = true; LOGINFO("已连接"); } catch(mongo::DBException &e) { LOGERROR("连接数据库时发生错误: " + e.toString()); throw FCException("无法连接至数据库"); } } return dbconnection; }
bool MongoDBDriver::getConnection(mongo::DBClientConnection &conn) { Uri u = Uri::Parse(url); int p=-1; // default if (!u.Port.empty()) { try { p = stoi(u.Port,nullptr,10); } catch(...) { } } mongo::HostAndPort hp(u.Host,p); std::string errmsg = url; return(conn.connect(hp,errmsg)); }