From 25de8cb3a1643b41dfe7d7e7a52430882d713e51 Mon Sep 17 00:00:00 2001 From: Ali Hatami Date: Tue, 5 Nov 2024 21:50:25 +0330 Subject: [PATCH] Fix network error --- gui/view/DialogStartCaptur.cpp | 2 + gui/view/MainWindow.cpp | 50 +++++++++++++++-- gui/view/MainWindow.h | 9 ++- gui/view/MainWindow.ui | 6 ++ model/include/provider/NetworkProvider.h | 7 +++ model/include/provider/TcpServerWorker.h | 43 ++++++++++++++ model/src/provider/NetworkProvider.cpp | 71 ++++++++++++++++++------ 7 files changed, 164 insertions(+), 24 deletions(-) create mode 100644 model/include/provider/TcpServerWorker.h diff --git a/gui/view/DialogStartCaptur.cpp b/gui/view/DialogStartCaptur.cpp index 590d53e..7b8c160 100644 --- a/gui/view/DialogStartCaptur.cpp +++ b/gui/view/DialogStartCaptur.cpp @@ -1,6 +1,7 @@ #include "DialogStartCaptur.h" #include "ui_DialogStartCaptur.h" +/*************************************************************************************************/ DialogStartCaptur::DialogStartCaptur(QWidget* parent) : QDialog(parent), ui(new Ui::DialogStartCaptur) @@ -20,6 +21,7 @@ QString DialogStartCaptur::getAddress() return ui->lineEdit->text(); } +/*************************************************************************************************/ DialogStartCaptur::~DialogStartCaptur() { delete ui; diff --git a/gui/view/MainWindow.cpp b/gui/view/MainWindow.cpp index 70f53fe..c7551ad 100644 --- a/gui/view/MainWindow.cpp +++ b/gui/view/MainWindow.cpp @@ -130,6 +130,30 @@ void MainWindow::onFilterTextChanged() ui->filterEdit->setStyleSheet(""); } +/*************************************************************************************************/ +void MainWindow::onClear() +{ + _dataManager.clearLogs(); +} + +/*************************************************************************************************/ +void MainWindow::onClientConnected() +{ + ui->statusbar->showMessage("Client Connected", 5000); +} + +/*************************************************************************************************/ +void MainWindow::onClientDisconnected() +{ + ui->statusbar->showMessage("Client Disconnected", 5000); +} + +/*************************************************************************************************/ +void MainWindow::onClientSocketError(const QString& error) +{ + ui->statusbar->showMessage("Client Error: " + error, 10000); +} + /*************************************************************************************************/ void MainWindow::prepareTableView() { @@ -169,6 +193,8 @@ void MainWindow::createConnections() connect(ui->advancedButton, &QPushButton::clicked, this, &MainWindow::onAdvancedFilterRequested); + connect(ui->actionClear, &QAction::triggered, this, &MainWindow::onClear); + connect(&_dataManager, &DataManager::badFilterRequested, this, &MainWindow::onFilterError); } @@ -201,14 +227,26 @@ void MainWindow::handleFilterChange() /*************************************************************************************************/ void MainWindow::useNetworkProvider(QString address, quint16 port) { - auto netProv = std::make_shared(QHostAddress(address), port); + auto networkProviderDeleter = [](NetworkProvider* p) { + p->deleteLater(); + }; + auto netProv = std::shared_ptr( + new NetworkProvider(QHostAddress( + address), port), + networkProviderDeleter); + netProv->setParser(std::make_shared()); _dataProvider = netProv; - connect(dynamic_cast(_dataProvider.get()), - SIGNAL(provided(const Log_ptr)), + connect(netProv.get(), + &NetworkProvider::provided, &_dataManager, - SLOT(onLogProvided(const Log_ptr))); -} + &DataManager::onLogProvided); -/*************************************************************************************************/ + connect(netProv.get(), &NetworkProvider::clientConnected, this, &MainWindow::onClientConnected); + connect(netProv.get(), + &NetworkProvider::clientDisconnected, + this, + &MainWindow::onClientDisconnected); + connect(netProv.get(), &NetworkProvider::clientError, this, &MainWindow::onClientSocketError); +} diff --git a/gui/view/MainWindow.h b/gui/view/MainWindow.h index 242ac37..1d7cf11 100644 --- a/gui/view/MainWindow.h +++ b/gui/view/MainWindow.h @@ -26,7 +26,8 @@ public: //uncrustify off private slots: - void onTableRowClicked(const QModelIndex ¤t, const QModelIndex &previous); +//uncrustify on + void onTableRowClicked(const QModelIndex& current, const QModelIndex& previous); void onTableDeselected(); void onActionStartCapture(bool trigered); @@ -34,7 +35,11 @@ private slots: void onAdvancedFilterRequested(); void onFilterError(QString msg); void onFilterTextChanged(); -//uncrustify on + void onClear(); + + void onClientConnected(); + void onClientDisconnected(); + void onClientSocketError(const QString& error); private: void prepareTableView(); diff --git a/gui/view/MainWindow.ui b/gui/view/MainWindow.ui index e2a8d0d..01f3364 100644 --- a/gui/view/MainWindow.ui +++ b/gui/view/MainWindow.ui @@ -104,6 +104,7 @@ + @@ -216,6 +217,11 @@ Ctrl+W + + + Clear + + diff --git a/model/include/provider/NetworkProvider.h b/model/include/provider/NetworkProvider.h index a786c08..b0e1de6 100644 --- a/model/include/provider/NetworkProvider.h +++ b/model/include/provider/NetworkProvider.h @@ -21,6 +21,9 @@ public: signals: void provided(const Log_ptr log) override; + void clientConnected(); + void clientDisconnected(); + void clientError(QString error); //uncrustify off private slots: @@ -28,6 +31,10 @@ private slots: void onErrorOccured(QAbstractSocket::SocketError error); void createNewConnection(); + void onReadyRead(); + void onClientDisconnected(); + void onSocketError(QAbstractSocket::SocketError socketError); + private: bool _isRunning; quint16 _port; diff --git a/model/include/provider/TcpServerWorker.h b/model/include/provider/TcpServerWorker.h new file mode 100644 index 0000000..ccd2a59 --- /dev/null +++ b/model/include/provider/TcpServerWorker.h @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "parser/IParser.h" + +class TcpServerWorker : public QObject +{ + Q_OBJECT + +public: + TcpServerWorker(Parser_ptr parser, QObject* parent = nullptr) : QObject(parent) + { + _parser = std::move(parser); + //Initialize the server + server = new QTcpServer(this); + connect(server, &QTcpServer::newConnection, this, &TcpServerWorker::handleNewConnection); + } + + bool startServer(quint16 port) { + return server->listen(QHostAddress::Any, port); + } + +signals: + void log(Log_ptr log); + +//uncrustify off +private slots: +//uncrustify on + void handleNewConnection() { + QTcpSocket* clientSocket = server->nextPendingConnection(); + connect(clientSocket, &QTcpSocket::readyRead, this, [this, clientSocket]() { + while(clientSocket->canReadLine()) + { + emit log(_parser->parse(clientSocket->readLine())); + } + }); + } + +private: + QTcpServer* server; + Parser_ptr _parser; +}; diff --git a/model/src/provider/NetworkProvider.cpp b/model/src/provider/NetworkProvider.cpp index c66a7be..39f1952 100644 --- a/model/src/provider/NetworkProvider.cpp +++ b/model/src/provider/NetworkProvider.cpp @@ -52,22 +52,61 @@ void NetworkProvider::onErrorOccured(QAbstractSocket::SocketError error) void NetworkProvider::createNewConnection() { QTcpSocket* socket = _server.nextPendingConnection(); - QtConcurrent::run([this, socket](){ - while(socket->isOpen()) - { - try { - if(socket->waitForReadyRead()) - { - auto bytes = socket->readLine(); - auto log = _parser->parse(bytes); - emit provided(log); - } - } - catch(std::exception& e) - { - } - } - }); + connect(socket, &QTcpSocket::readyRead, this, &NetworkProvider::onReadyRead); + connect(socket, &QTcpSocket::disconnected, this, &NetworkProvider::onClientDisconnected); + connect(socket, QOverload::of(&QTcpSocket::errorOccurred), + this, &NetworkProvider::onSocketError); + emit clientConnected(); + //QtConcurrent::run([this, socket](){ + //while(socket->isOpen()) + //{ + //try { + //if(socket->waitForReadyRead()) + //{ + //auto bytes = socket->readLine(); + //auto log = _parser->parse(bytes); + //emit provided(log); + //} + //} + //catch(std::exception& e) + //{ + //} + //} + //}); +} + +/*************************************************************************************************/ +void NetworkProvider::onReadyRead() +{ + QTcpSocket* socket = qobject_cast(sender()); + if(socket) + { + auto bytes = socket->readLine(); + auto log = _parser->parse(bytes); + emit provided(log); + } +} + +/*************************************************************************************************/ +void NetworkProvider::onClientDisconnected() +{ + QTcpSocket* socket = qobject_cast(sender()); + if(socket) + { + socket->deleteLater(); + emit clientDisconnected(); + } +} + +/*************************************************************************************************/ +void NetworkProvider::onSocketError(QAbstractSocket::SocketError socketError) +{ + QTcpSocket* socket = qobject_cast(sender()); + if(socket) + { + emit clientError("Error occured in client socket because " + socket->errorString()); + socket->deleteLater(); //Clean up + } } /*************************************************************************************************/