// this method is executed in a thread to simulate multiple clients void* commandClients(void* arg) { NetworkOutputStream* nos = new NetworkOutputStream(); int socket = nos->open("localhost", _port); NetworkInputStream* nis = new NetworkInputStream(socket); Logger* log = getLogger(NULL); CommandWriter* writer = new CommandWriter(nos); for (int x = 0; x < 10; x++) { log->info("client: preparing insert"); InsertCommand* cmd = new InsertCommand(); cmd->setDB("db1"); cmd->setNameSpace("ns"); BSONObj* o = new BSONObj(); std::string* id = uuid(); o->add("_id", id->c_str()); delete id; std::string* rev = uuid(); o->add("_revision", rev->c_str()); delete rev; o->add("name", "John"); cmd->setBSON(o); BSONObj* options = new BSONObj(); cmd->setOptions(options); log->info("client: writing insert command"); writer->writeCommand(cmd); log->info("client: insert command sent"); delete cmd; log->info("client: preparing showdbs command"); ShowdbsCommand* showCmd = new ShowdbsCommand(); BSONObj* options2 = new BSONObj(); showCmd->setOptions(options2); log->info("client: sending showCmd"); writer->writeCommand(showCmd); log->info("client: waiting showDbs answer"); int dbs = nis->readInt(); EXPECT_EQ(dbs, 3); char* db1 = nis->readChars(); EXPECT_TRUE(strcmp(db1, "db1") == 0); char* db2 = nis->readChars(); EXPECT_TRUE(strcmp(db2, "db2") == 0); char* db3 = nis->readChars(); EXPECT_TRUE(strcmp(db3, "db3") == 0); log->info("client: showDbs received"); free(db1); free(db2); free(db3); } log->info("client: sending ShutdownCommand"); ShutdownCommand* shut = new ShutdownCommand(); writer->writeCommand(shut); log->info("client: shutdown sent"); nis->close(); }
std::vector<std::string>* Connection::dbs() const { if (_logger->isDebug()) _logger->debug(2, "dbs command."); ShowdbsCommand cmd; _commandWriter->writeCommand(&cmd); cmd.readResult(_inputStream); std::vector<std::string>* result = (std::vector<std::string>*)cmd.result(); return result; }
std::vector<char*>* DjondbConnection::dbs() const { if (_logger->isDebug()) _logger->debug(2, "dbs command."); if (!isOpen()) { throw DjondbException(D_ERROR_CONNECTION, "Not connected to any server"); } ShowdbsCommand cmd; _commandWriter->writeCommand(&cmd); cmd.readResult(_inputStream); std::vector<std::string>* temp = (std::vector<std::string>*)cmd.result(); std::vector<char*>* result = new std::vector<char*>(); for (std::vector<std::string>::const_iterator i = temp->begin(); i != temp->end(); i++) { std::string db = *i; result->push_back(strcpy(db.c_str(), db.length())); } delete temp; return result; }