Able to send and receive messages, server panicking and not relaying userids yet
This commit is contained in:
34
client/inc/logic/client_reactor.h
Normal file
34
client/inc/logic/client_reactor.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "logic/message_handler.h"
|
||||
#include "messages.pb.h"
|
||||
#include "services.grpc.pb.h"
|
||||
#include <QObject>
|
||||
#include <condition_variable>
|
||||
#include <grpcpp/support/client_callback.h>
|
||||
#include <grpcpp/support/status.h>
|
||||
#include <memory>
|
||||
|
||||
class Reactor : public QObject,
|
||||
public grpc::ClientBidiReactor<chat::chatMsg, chat::chatMsg> {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit Reactor(chat::Chat::Stub *);
|
||||
void OnWriteDone(bool ok) override;
|
||||
void OnReadDone(bool ok) override;
|
||||
void OnDone(const grpc::Status &s) override;
|
||||
grpc::Status Await();
|
||||
void SendMessage(const chat::chatMsg &msg);
|
||||
bool IsConnected() const;
|
||||
std::shared_ptr<MessageHandler> getHandler();
|
||||
|
||||
private:
|
||||
chat::Chat::Stub *m_stub;
|
||||
grpc::ClientContext m_context;
|
||||
chat::chatMsg m_msg;
|
||||
std::shared_ptr<MessageHandler> m_handler;
|
||||
std::mutex m_mutex;
|
||||
std::condition_variable m_cv;
|
||||
bool m_done = false;
|
||||
grpc::Status m_status;
|
||||
};
|
||||
14
client/inc/logic/message_handler.h
Normal file
14
client/inc/logic/message_handler.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "messages.pb.h"
|
||||
#include <QObject>
|
||||
|
||||
class MessageHandler : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
MessageHandler(QObject *parent = nullptr) {}
|
||||
void emitMessageReceived(const chat::chatMsg &message);
|
||||
|
||||
signals:
|
||||
void messageReceived(const chat::chatMsg &message);
|
||||
};
|
||||
@@ -5,13 +5,13 @@
|
||||
|
||||
class Message {
|
||||
public:
|
||||
Message(int, QDateTime, QString);
|
||||
Message(QString, QDateTime, QString);
|
||||
~Message() {}
|
||||
|
||||
QString toString() const;
|
||||
|
||||
private:
|
||||
int m_userId;
|
||||
QString m_userId;
|
||||
QDateTime m_timestamp;
|
||||
QString m_content;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "logic/client_reactor.h"
|
||||
#include "messages.pb.h"
|
||||
#include "models/chatroom.h"
|
||||
#include <QMainWindow>
|
||||
#include <memory>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
namespace Ui {
|
||||
@@ -17,9 +20,12 @@ public:
|
||||
~MainWindow();
|
||||
|
||||
private:
|
||||
void sendMsg(const QString &);
|
||||
|
||||
Ui::MainWindow *ui;
|
||||
Chatroom m_chatroom;
|
||||
std::unique_ptr<Reactor> m_reactor;
|
||||
|
||||
public slots:
|
||||
void receiveMsg(QString &);
|
||||
void receiveMsg(const chat::chatMsg &);
|
||||
};
|
||||
|
||||
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