void DataServer::onClientConnection(const muduo::net::TcpConnectionPtr& conn) { LOG_INFO << conn->peerAddress().toIpPort() << (conn->connected() ? " is UP" : " is DOWN"); }
void processRequest(const muduo::net::TcpConnectionPtr& conn, const QueenTask& task) { std::list<std::vector<int>> queens; if(task.getPos().size() == 0) { queens = solve_queens(task.getSize()); } else { queens = complete_queens(task.getPos(), task.getSize()); } int number = queens.size(); // solutions <clientId> <id> <subid> <number>\r\n std::string prefix = task.getClientId() + " " + task.getTaskId() + " " + task.getSubTaskId(); std::string res = "solutions " + prefix + " " + std::to_string(number) + "\r\n"; conn->send(res); LOG_INFO << "send " << res.substr(0, res.size()-2) << " to server"; if(task.isComputeSolutions()) { for(auto &queen : queens) { // one_solution <client_id> <id> <subid> <solution>\r\n std::string line = "one_solution " + prefix; for(auto &n : queen) { line += " " + std::to_string(n); } line += "\r\n"; conn->send(line); } } }
void MedianExecutor::onMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buffer, muduo::Timestamp time) { while(buffer->findCRLF()) { const char* crlf = buffer->findCRLF(); std::string response(buffer->peek(), crlf); buffer->retrieveUntil(crlf+2); std::string id(conn->peerAddress().toIpPort().c_str()); LOG_INFO << "MedianExecutor receive: [" << response << "] from " << id; std::vector<std::string> tokens; boost::split(tokens, response, boost::is_any_of(" ")); if(tokens[0] == "random" || tokens[0] == "split") { for(size_t i = 1; i < tokens.size(); ++i) { int64_t n = std::stol(tokens[i]); workerBuffers[id].push_back(n); } } else { LOG_ERROR << "MedianExecutor receive unknown response: [" << response << "] from " << id; conn->shutdown(); } { std::unique_lock<std::mutex> lock(mt); ++workingSize; cond.notify_all(); } } }
void EchoServer::onMessage(const muduo::net::TcpConnectionPtr &conn, muduo::net::Buffer *buf, muduo::Timestamp time) { muduo::string msg(buf->retrieveAllAsString()); LOG_INFO << conn->name() << " echo " << msg.size() << " bytes, " << " data received at " << time.toString(); conn->send(msg); }
void onConnection(const muduo::net::TcpConnectionPtr& conn) { if(conn->connected()) { conn_ = conn; sendRequest(14, false); } else { LOG_INFO << conn->peerAddress().toIpPort() << " is down"; conn->shutdown(); } }
void onConnection(const muduo::net::TcpConnectionPtr& conn) { if(conn->connected()) { LOG_INFO << "connect to " << conn->peerAddress().toIpPort(); conn->setTcpNoDelay(true); conn->send("ready\r\n"); } else { LOG_INFO << conn->peerAddress().toIpPort() << " is down"; client.disconnect(); } }
void onMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buffer, muduo::Timestamp time) { //[solutions] <clientId> <id> <subid> <size> <pos>\r\n while(buffer->findCRLF()) { const char* crlf = buffer->findCRLF(); std::string request(buffer->peek(), crlf); buffer->retrieveUntil(crlf+2); LOG_INFO << "worker receive " << request; bool computeSolutions = (request.find("solutions") == 0); if(computeSolutions) request = request.substr(request.find("solutions"), request.size()); std::vector<std::string> tokens; boost::split(tokens, request, boost::is_any_of(" ")); if(tokens.size() >= 4) { int size = std::stoi(tokens[3]); std::vector<int> pos; for(size_t i = 4; i < tokens.size(); ++i) pos.push_back(std::stoi(tokens[i])); QueenTask task(tokens[0], tokens[1], tokens[2], size, pos, computeSolutions); threadPool.run(boost::bind(&QueenWorker::processRequest, this, conn, task)); } else { LOG_ERROR << "receive bad request " << request << " from " << conn->peerAddress().toIpPort(); } } }
void DataServer::onWorkerConnection(const muduo::net::TcpConnectionPtr& conn) { std::string peer(conn->peerAddress().toIpPort().c_str()); if(conn->connected()) { LOG_INFO << "connected to worker: " << peer; connections.insert(std::make_pair(peer, conn)); { std::unique_lock<std::mutex> lock(mt); size -= 1; cond.notify_all(); } } else { LOG_INFO << "worker: " << peer << " is down"; workers.erase(peer); connections.erase(peer); } }
void SudoKuServer::onMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buf, muduo::Timestamp time) { LOG_DEBUG << conn->name(); size_t len = buf->readableBytes(); while (len >= kCell + 2) { const char* crlf = buf->findCRLF(); if(crlf) { string request(buf->peek(), crlf); string id; buf->retrieveUntil(crlf + 2); string::iterator colon = find(request.begin(), request.end(), ':'); if (colon != request.end()) { id.assign(request.begin(), colon); request.erase(request.begin(), colon + 1); } if (request.size() == boost::implicit_cast<size_t>(kCell)) { string result = solveSudoku(request); if (id.empty()) { conn->send(result + "\r\n"); } else { conn->send(id + ":" + result + "\r\n"); } } else { conn->send("Bad Request!\r\n"); } } else { break; } } }
void QueryServer::onMessage(const muduo::net::TcpConnectionPtr &conn, muduo::net::Buffer *buf, muduo::Timestamp) { muduo::string s(buf->retrieveAllAsString()); LOG_INFO << "receive php msg: " << s; std::string msg(s.c_str()); std::string result = query_.queryPage(msg); conn->send(result.c_str()); }
void MsgManager::broadcast(const muduo::net::TcpConnectionPtr& conn) { /* * { "type" : "new_msg", //消息类型,客户端向其他人发送消息,如梁杰发消息问晚上谁开黑 "data" : [ { "id":"101", "content":"晚上谁开黑", "sender":"liangjie" "receiver":"group202" } ] } * */ conn->send(tempMessage_); LOG_INFO<<"broadcast send:"<<tempMessage_; }
void SudoKuServer::onConnection(const muduo::net::TcpConnectionPtr& conn) { LOG_INFO<< "SudoKuServer - " << conn->peerAddress().toIpPort() << " -> " << conn->localAddress().toIpPort() << " is " << (conn->connected() ? "UP" : "DOWN"); }
// command: // 1. genNumber n response: ok // 2. average response: number<double> // 3. median response: number<int64_t> // 4. sort response: ok // 5. freq n response: <n1, freq1> <n2, freq2> ... <n, freq> void DataServer::onClientMessage(const muduo::net::TcpConnectionPtr& conn, muduo::net::Buffer* buf, muduo::Timestamp time) { while(buf->findCRLF()) { const char* crlf = buf->findCRLF(); std::string command(buf->peek(), crlf); buf->retrieveUntil(crlf+2); std::vector<std::string> tokens; boost::split(tokens, command, boost::is_any_of(" ")); if(command.find("genNumber") == 0) { if(tokens.size() == 3) { GenNumberExecutor executor(connections); dataExecutor = &executor; int64_t number = std::stol(tokens[1]); char mode = tokens[2][0]; executor.execute(number, mode); conn->send("OK\r\n"); } else { conn->send("usage: genNumber [n] [n/u/z]\r\n"); } } else if(command == "average") { AverageExecutor executor(connections); dataExecutor = &executor; double average = executor.execute(); conn->send("average: " + std::to_string(average) + "\r\n"); } else if(command == "median") { MedianExecutor executor(connections); dataExecutor = &executor; int64_t median = executor.execute(); conn->send("median: " + std::to_string(median) + "\r\n"); } else if(command == "sort") { SortExecutor executor(connections); dataExecutor = &executor; executor.execute(); conn->send("OK\r\n"); } else if(command.find("freq") == 0) { size_t freqNumber = std::stoi(tokens[1]); FreqExecutor executor(connections); dataExecutor = &executor; std::vector<std::pair<int64_t, int64_t>> freqs = executor.execute(freqNumber); std::string response = "freqs:"; for(auto& freq : freqs) { response += " " + std::to_string(freq.first) + " " + std::to_string(freq.second); } response += "\r\n"; conn->send(response); } else { LOG_ERROR << "receive unknown command [" << command << "] from " << conn->peerAddress().toIpPort(); conn->shutdown(); } } }