void MongoDBTest::testCursorRequest() { Poco::MongoDB::Database db("team"); Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("numbers"); for(int i = 0; i < 10000; ++i) { Document::Ptr doc = new Document(); doc->add("number", i); insertRequest->documents().push_back(doc); } _mongo->sendRequest(*insertRequest); Poco::Int64 count = db.count(*_mongo, "numbers"); assert(count == 10000); Poco::MongoDB::Cursor cursor("team", "numbers"); int n = 0; Poco::MongoDB::ResponseMessage& response = cursor.next(*_mongo); while(1) { n += response.documents().size(); if ( response.cursorID() == 0 ) break; response = cursor.next(*_mongo); } assert(n == 10000); Poco::MongoDB::QueryRequest drop("team.$cmd"); drop.setNumberToReturn(1); drop.selector().add("drop", std::string("numbers")); Poco::MongoDB::ResponseMessage responseDrop; _mongo->sendRequest(drop, responseDrop); }
void MongoDBTest::testCursorRequest() { if (!_connected) { std::cout << "Not connected, test skipped." << std::endl; return; } Poco::MongoDB::Database db("team"); Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("numbers"); for(int i = 0; i < 10000; ++i) { Document::Ptr doc = new Document(); doc->add("number", i); insertRequest->documents().push_back(doc); } _mongo.sendRequest(*insertRequest); double count = db.count(_mongo, "numbers"); assert(count == 10000); Poco::MongoDB::Cursor cursor("team", "numbers"); int n = 0; Poco::MongoDB::ResponseMessage& response = cursor.next(_mongo); while(1) { n += response.documents().size(); if ( response.cursorID() == 0 ) break; response = cursor.next(_mongo); } std::cout << "n= " << n << std::endl; assert(n == 10000); Poco::MongoDB::QueryRequest drop("team.$cmd"); drop.setNumberToReturn(1); drop.selector().add("drop", std::string("numbers")); Poco::MongoDB::ResponseMessage responseDrop; _mongo.sendRequest(drop, responseDrop); if ( responseDrop.documents().size() > 0 ) { std::cout << responseDrop.documents()[0]->toString(2) << std::endl; } }
int main(int argc, char *argv[]) { int sockfd, newsockfd, port_no, bindfd, listenfd, bytes_sent, bytes_recvd; char sbuffer[512], cli_ip[16], sname[64], cname[64]; char *ptr_buff, *ptr_port; const char *ptr_cli_ip; struct sockaddr_in serv_addr, cli_addr; socklen_t serv_size, cli_size; int inp_true = 0, count = 0, inp, ni, x, y, toss; char serv_choice, cli_choice, nc; char choice_buffer[2], co_ordinates_buffer[2], toss_buffer; system("clear"); ptr_buff = &sbuffer[0]; ptr_port = (char *)&PORT; //creating sever side socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("Server side listening Socket could not be created!"); return 1; } memset(&serv_addr, 0, sizeof(serv_addr)); port_no = atoi(ptr_port); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port_no); serv_addr.sin_addr.s_addr = INADDR_ANY; //binding socket bindfd = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (bindfd == -1) { perror("Failed to bind!"); return 1; } Repo repo(MONGODB_HOST, MONGODB_PORT); cout<<"--Player History--"<<endl; repo.Read(); cout<<endl<<"Enter your Name : "; cin>>sname; Player *playerExist = Exist(sname); if (playerExist->name != "") { cout<<endl<<"Welcome back "<<sname; cout<<endl<<"Your history : "; cout<<endl<<"win = "<<playerExist->win; cout<<endl<<"lose = "<<playerExist->lose; cout<<endl<<"draw = "<<playerExist->draw; } else { Document::Ptr person = new Document(); person->add("name", string(sname)); person->add("win", 0); person->add("lose", 0); person->add("draw", 0); repo.Create(person); } char ans; cout<<endl<<"Reset Game History ? (y/N)"; cin>>ans; if (ans == 'y') { repo.Delete("all"); cout<<"--Player History--"<<endl; repo.Read(); } /*-----*/ cout<<"Server created!"<<endl<<"Waiting for a Player..."<<endl; //listening for incoming connections listenfd = listen(sockfd, 5); if (listenfd == -1) { perror("Failed to listen!"); return 1; } serv_size = sizeof(serv_addr); cli_size = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_size); if (newsockfd == -1) { perror("Failed to accept from client!"); return 1; } ptr_cli_ip = inet_ntop(AF_INET, &cli_addr.sin_addr, cli_ip, cli_size); cout<<"Server received connections from "<<cli_ip<<endl; memset(&cname, 0, sizeof(cname)); do { static int flag = 0; bytes_recvd = recv(newsockfd, &cname, sizeof(cname), 0); if (bytes_recvd == -1 && flag == 0) { memset(&cname, 0, sizeof(cname)); cout<<"Could not ACQUIRE Player Information!"<<endl<<"Trying again..."<<endl; continue; } else { flag = 1; bytes_sent = send(newsockfd, &sname, sizeof(sname), 0); if (bytes_sent == -1) cout<<"Could not SEND Player Data!"<<"Trying Again..."<<endl; else cout<<cname<<" has joined the game."<<endl; } }while(bytes_recvd == -1 || bytes_sent == -1); cout<<"Creating Game. Please wait..."<<endl; sleep(2); cout<<endl<<"Game created!"<<endl<<endl<<"Doing a toss..."; srand(time(NULL)); toss = rand() % 2; sleep(1); sprintf(&toss_buffer, "%d", toss); bytes_sent = send(newsockfd, &toss_buffer, sizeof(toss_buffer), 0); if (bytes_sent == -1) { perror("TOSS BUFFER not sent!"); return 1; } if (toss == 0) { cout<<endl<<"You WON the toss!"<<endl; do { cout<<sname<<" Enter Your Choice (X or O): "; cin>>serv_choice; if (serv_choice == 'X' || serv_choice == 'x') { serv_choice = 'X'; cli_choice = 'O'; inp_true = 1; cout<<endl<<cname<<" gets O."<<endl<<endl<<"Lets Play!"<<endl<<endl; } else if (serv_choice == 'O' || serv_choice == 'o' || serv_choice == '0') { serv_choice = 'O'; cli_choice = 'X'; inp_true = 1; cout<<endl<<cname<<" gets X."<<endl<<endl<<"Lets Play!"<<endl<<endl; } else { cout<<"\nInvalid Choice! Please Choose Again..."<<endl; inp_true = 0; } }while(inp_true == 0); memset(&choice_buffer, 0, sizeof(choice_buffer)); choice_buffer[0] = serv_choice; choice_buffer[1] = cli_choice; bytes_sent = send(newsockfd, &choice_buffer, sizeof(choice_buffer), 0); if (bytes_sent == -1) { perror("CHOICE BUFFER could not be sent!"); return 1; } }