Browse Source

Fix network error

fix-network-erro
Ali Hatami Tajik 3 weeks ago
parent
commit
25de8cb3a1
  1. 2
      gui/view/DialogStartCaptur.cpp
  2. 50
      gui/view/MainWindow.cpp
  3. 9
      gui/view/MainWindow.h
  4. 6
      gui/view/MainWindow.ui
  5. 7
      model/include/provider/NetworkProvider.h
  6. 43
      model/include/provider/TcpServerWorker.h
  7. 71
      model/src/provider/NetworkProvider.cpp

2
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;

50
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<NetworkProvider>(QHostAddress(address), port);
auto networkProviderDeleter = [](NetworkProvider* p) {
p->deleteLater();
};
auto netProv = std::shared_ptr<NetworkProvider>(
new NetworkProvider(QHostAddress(
address), port),
networkProviderDeleter);
netProv->setParser(std::make_shared<JsonParser>());
_dataProvider = netProv;
connect(dynamic_cast<QObject*>(_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);
}

9
gui/view/MainWindow.h

@ -26,7 +26,8 @@ public:
//uncrustify off
private slots:
void onTableRowClicked(const QModelIndex &current, 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();

6
gui/view/MainWindow.ui

@ -104,6 +104,7 @@
<addaction name="actionStart_Capture"/>
<addaction name="actionNew_Capture"/>
<addaction name="actionEnd_Capture"/>
<addaction name="actionClear"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuCapture"/>
@ -216,6 +217,11 @@
<string>Ctrl+W</string>
</property>
</action>
<action name="actionClear">
<property name="text">
<string>Clear</string>
</property>
</action>
</widget>
<resources/>
<connections/>

7
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;

43
model/include/provider/TcpServerWorker.h

@ -0,0 +1,43 @@
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#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;
};

71
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<QAbstractSocket::SocketError>::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<QTcpSocket*>(sender());
if(socket)
{
auto bytes = socket->readLine();
auto log = _parser->parse(bytes);
emit provided(log);
}
}
/*************************************************************************************************/
void NetworkProvider::onClientDisconnected()
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if(socket)
{
socket->deleteLater();
emit clientDisconnected();
}
}
/*************************************************************************************************/
void NetworkProvider::onSocketError(QAbstractSocket::SocketError socketError)
{
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
if(socket)
{
emit clientError("Error occured in client socket because " + socket->errorString());
socket->deleteLater(); //Clean up
}
}
/*************************************************************************************************/

Loading…
Cancel
Save