Able to send and receive messages, server panicking and not relaying userids yet
This commit is contained in:
61
client/src/logic/client_reactor.cpp
Normal file
61
client/src/logic/client_reactor.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "logic/client_reactor.h"
|
||||
#include "messages.pb.h"
|
||||
#include "services.grpc.pb.h"
|
||||
#include <QObject>
|
||||
#include <memory>
|
||||
|
||||
Reactor::Reactor(chat::Chat::Stub *stub) : m_stub(stub) {
|
||||
m_stub->async()->sendMsg(&m_context, this);
|
||||
|
||||
m_handler = std::make_shared<MessageHandler>();
|
||||
|
||||
StartRead(&m_msg);
|
||||
StartCall();
|
||||
}
|
||||
|
||||
void Reactor::OnWriteDone(bool ok) {
|
||||
if (!ok) {
|
||||
std::cerr << "Error: write failed" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Reactor::OnReadDone(bool ok) {
|
||||
if (ok) {
|
||||
m_handler->emitMessageReceived(m_msg);
|
||||
std::cout << "Received message from: " << m_msg.userid()
|
||||
<< " content: " << m_msg.message() << std::endl;
|
||||
StartRead(&m_msg);
|
||||
} else {
|
||||
std::cout << "Server finished sending" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Reactor::OnDone(const grpc::Status &status) {
|
||||
m_status = status;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_done = true;
|
||||
}
|
||||
m_cv.notify_one();
|
||||
if (!status.ok()) {
|
||||
std::cerr << "Error: " << status.error_message() << std::endl;
|
||||
}
|
||||
std::cout << "Server finished sending" << std::endl;
|
||||
}
|
||||
|
||||
grpc::Status Reactor::Await() {
|
||||
std::unique_lock<std::mutex> lock(m_mutex);
|
||||
m_cv.wait(lock, [this] { return m_done; });
|
||||
return m_status;
|
||||
}
|
||||
|
||||
void Reactor::SendMessage(const chat::chatMsg &msg) {
|
||||
static chat::chatMsg writeMsg;
|
||||
writeMsg.CopyFrom(msg);
|
||||
StartWrite(&writeMsg);
|
||||
std::cout << "Sent message: " << msg.message();
|
||||
}
|
||||
|
||||
bool Reactor::IsConnected() const { return !m_done; }
|
||||
|
||||
std::shared_ptr<MessageHandler> Reactor::getHandler() { return m_handler; }
|
||||
5
client/src/logic/message_handler.cpp
Normal file
5
client/src/logic/message_handler.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "logic/message_handler.h"
|
||||
|
||||
void MessageHandler::emitMessageReceived(const chat::chatMsg &msg) {
|
||||
emit messageReceived(msg);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "models/message.h"
|
||||
|
||||
Message::Message(int userId, QDateTime timestamp, QString content)
|
||||
Message::Message(QString userId, QDateTime timestamp, QString content)
|
||||
: m_userId(userId), m_timestamp(timestamp), m_content(content) {}
|
||||
|
||||
QString Message::toString() const {
|
||||
|
||||
@@ -1,20 +1,45 @@
|
||||
#include "ui/mainwindow.h"
|
||||
#include "logic/client_reactor.h"
|
||||
#include "messages.pb.h"
|
||||
#include "ui_mainwindow.h"
|
||||
#include <grpcpp/create_channel.h>
|
||||
#include <grpcpp/security/credentials.h>
|
||||
#include <memory>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
auto channel = grpc::CreateChannel("localhost:50051",
|
||||
grpc::InsecureChannelCredentials());
|
||||
auto stub = chat::Chat::NewStub(channel);
|
||||
|
||||
m_reactor = std::make_unique<Reactor>(stub.get());
|
||||
|
||||
connect(ui->sendButton, &QPushButton::clicked, this, [this]() {
|
||||
auto msg = ui->inputText->toPlainText();
|
||||
receiveMsg(msg);
|
||||
if (!msg.isEmpty()) {
|
||||
sendMsg(msg);
|
||||
ui->inputText->clear();
|
||||
}
|
||||
});
|
||||
connect(m_reactor->getHandler().get(), &MessageHandler::messageReceived, this,
|
||||
&MainWindow::receiveMsg);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow() { delete ui; }
|
||||
|
||||
void MainWindow::receiveMsg(QString &msg) {
|
||||
Message message(0, QDateTime::currentDateTime(), msg);
|
||||
void MainWindow::sendMsg(const QString &msg) {
|
||||
if (m_reactor && m_reactor->IsConnected()) {
|
||||
chat::chatMsg chatMsg;
|
||||
chatMsg.set_message(msg.toStdString());
|
||||
m_reactor->SendMessage(chatMsg);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::receiveMsg(const chat::chatMsg &chatMsg) {
|
||||
auto userId = QString::fromStdString(chatMsg.userid());
|
||||
auto content = QString::fromStdString(chatMsg.message());
|
||||
Message message(userId, QDateTime::currentDateTime(), content);
|
||||
m_chatroom.addMessage(message);
|
||||
ui->outputText->setText(m_chatroom.getMessagesString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user