int main() { std::cout << "\n Demonstrating C++11 Blocking Queue"; std::cout << "\n ===================================="; BlockingQueue<std::string> q; std::thread t(test, &q); for(int i=0; i<15; ++i) { std::ostringstream temp; temp << i; std::string msg = std::string("msg#") + temp.str(); { std::lock_guard<std::mutex> l(ioLock); std::cout << "\n main enQing " << msg.c_str(); } q.enQ(msg); std::this_thread::sleep_for(std::chrono::milliseconds(3)); } q.enQ("quit"); t.join(); std::cout << "\n"; std::cout << "\n Making copy of BlockingQueue"; std::cout << "\n ------------------------------"; std::string msg = "test"; q.enQ(msg); std::cout << "\n q.size() = " << q.size(); BlockingQueue<std::string> q2 = q; // make default copy std::cout << "\n q2.size() = " << q2.size(); std::cout << "\n q element = " << (msg = q.deQ()); q.enQ(msg); std::cout << "\n q2 element = " << q2.deQ() << "\n"; std::cout << "\n Assigning state of BlockingQueue"; std::cout << "\n ----------------------------------"; BlockingQueue<std::string> q3; q3 = q; std::cout << "\n q.size() = " << q.size(); std::cout << "\n q3.size() = " << q3.size(); std::cout << "\n q element = " << q.deQ(); q.enQ(msg); std::cout << "\n q3 element = " << q3.deQ() << "\n"; std::cout << "\n\n"; }
void ClientHandler::operator()(Socket& socket_) { Display *disp = new Display(); BlockingQueue<string> q_; while (true) { Receiver *rs = new Receiver(); std::string command = socket_.recvString(); q_.enQ(command); if (command.size() == 0) break; size_t size= q_.size(); string ackmsg; for (size_t i = 0; i < size; i++) { command = q_.deQ(); ackmsg = rs->start(command, socket_); } Sender *s = new Sender(); if (ackmsg != "") { s->sendAck(socket_, ackmsg); } } disp->show("ClientHandler socket connection closing"); socket_.shutDown(); socket_.close(); disp->show("ClientHandler thread terminating"); }
int main() { ::SetConsoleTitle("ClientHandler"); Show::attach(&std::cout); Show::start(); Show::title("\n ClientHandler Server started"); BlockingQueue<HttpMessage> msgQ; SocketSystem ss; SocketListener sl(8080, Socket::IP6); ClientHandler cp(msgQ); sl.start(cp); try { while (true) { HttpMessage msg = msgQ.deQ(); std::cout << "\n\n clienthandler " + msg.findValue("toAddr") + " recvd message contents:\n " + msg.bodyString() + "\n from client " + msg.findValue("fromAddr") + "\n"; if (msg.bodyString() == "closeServer") { ::Sleep(100); } } } catch (std::exception& exc) { Show::write("\n Exeception caught: "); std::string exMsg = "\n " + std::string(exc.what()) + "\n\n"; Show::write(exMsg); } std::cout << "\n\n"; return 0; }
// ------------------------------------ Starts Receiver for the this component --------------------------- void Server::startReceiverThread() { BlockingQueue<HttpMessage> msgQ; try { SocketSystem ss; SocketListener sl(getServerPort(), Socket::IP6); Receiver cp(msgQ,"Server"); sl.start(cp); /* * Since this is a server the loop below never terminates. * We could easily change that by sending a distinguished * message for shutdown. */ while (true) { HttpMessage msg = msgQ.deQ(); std::string cPort = msg.findValue("From-Port"); if (msg.attributes()[0].first == "GET") { // Checks if its an file extraction request std::string filename = msg.findValue("file"); Show::write("\n\n Client @: "+cPort+ " => requested file :: " + filename); sendServerFiles(msg); } else Show::write("\n\n Message from client @:"+cPort +" => " + msg.bodyString()); } } catch (std::exception& exc) { Show::write("\n Exeception caught: "); std::string exMsg = "\n " + std::string(exc.what()) + "\n\n"; Show::write(exMsg); } }
void PopAndShow(BlockingQueue<double>& q) { long save = cout.flags(); // save format state cout.flags(ios::showpoint); // show decimal point cout.precision(3); // show only 3 digits on right of decimal double item = q.deQ(); cout << "\n " << setw(4) << item << " : "; cout << "Q size = " << q.size(); cout.flags(save); // restore format state }
// Function for reterieving files which contains search text specified in the message string Receiver::performTextSearch(string msg1, string &result) { Display * disp = new Display(); TextSearch *ts = new TextSearch(); Message *m = new Message(); BlockingQueue<std::string> resultfiles; std::vector<string> file_patterns, all_files; string sstr = m->getmsgBody1(msg1), file_path = m->getFileName(msg1), filepattern = m->getPatterns(msg1), token, resultfiles1; disp->show("\n*******************************************\n"); string prtmsg ="Performing Text Search for string: "+ sstr + " on files with patterns " + filepattern+"\n"; disp->show(prtmsg); std::istringstream ss(filepattern); while (std::getline(ss, token, ',')) { file_patterns.push_back(token); } all_files = showFiles(file_path, file_patterns); for each (string file in all_files) { if (ts->search(sstr, file)) resultfiles.enQ(file); } size_t vecsize = resultfiles.size(); for (size_t i = 0; i < vecsize; i++) { resultfiles1 = resultfiles1 + resultfiles.deQ(); resultfiles1.append(";"); } disp->show("Sending Result File list to client \n"); disp->show("*******************************************\n\n"); Message *resmsg = new Message(); Message orgmsg; orgmsg.setMessage(msg1); resmsg->setCommand("textsearch"); resmsg->setFileAttrs(orgmsg.getFileName(msg1)); resmsg->setsendAddr(orgmsg.getrecvAddr(msg1)); resmsg->setrecAddr(orgmsg.getsendAddr(msg1)); resmsg->setmsgBody(resultfiles1); result = resmsg->getMessage(); return result; }
Message RequestProcessor::GetMessage() { return outQ.deQ(); }