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 "DialogStartCaptur.h"
#include "ui_DialogStartCaptur.h" #include "ui_DialogStartCaptur.h"
/*************************************************************************************************/
DialogStartCaptur::DialogStartCaptur(QWidget* parent) : DialogStartCaptur::DialogStartCaptur(QWidget* parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::DialogStartCaptur) ui(new Ui::DialogStartCaptur)
@ -20,6 +21,7 @@ QString DialogStartCaptur::getAddress()
return ui->lineEdit->text(); return ui->lineEdit->text();
} }
/*************************************************************************************************/
DialogStartCaptur::~DialogStartCaptur() DialogStartCaptur::~DialogStartCaptur()
{ {
delete ui; delete ui;

50
gui/view/MainWindow.cpp

@ -130,6 +130,30 @@ void MainWindow::onFilterTextChanged()
ui->filterEdit->setStyleSheet(""); 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() void MainWindow::prepareTableView()
{ {
@ -169,6 +193,8 @@ void MainWindow::createConnections()
connect(ui->advancedButton, &QPushButton::clicked, this, connect(ui->advancedButton, &QPushButton::clicked, this,
&MainWindow::onAdvancedFilterRequested); &MainWindow::onAdvancedFilterRequested);
connect(ui->actionClear, &QAction::triggered, this, &MainWindow::onClear);
connect(&_dataManager, &DataManager::badFilterRequested, this, &MainWindow::onFilterError); connect(&_dataManager, &DataManager::badFilterRequested, this, &MainWindow::onFilterError);
} }
@ -201,14 +227,26 @@ void MainWindow::handleFilterChange()
/*************************************************************************************************/ /*************************************************************************************************/
void MainWindow::useNetworkProvider(QString address, quint16 port) 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>()); netProv->setParser(std::make_shared<JsonParser>());
_dataProvider = netProv; _dataProvider = netProv;
connect(dynamic_cast<QObject*>(_dataProvider.get()), connect(netProv.get(),
SIGNAL(provided(const Log_ptr)), &NetworkProvider::provided,
&_dataManager, &_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 //uncrustify off
private slots: private slots:
void onTableRowClicked(const QModelIndex &current, const QModelIndex &previous); //uncrustify on
void onTableRowClicked(const QModelIndex& current, const QModelIndex& previous);
void onTableDeselected(); void onTableDeselected();
void onActionStartCapture(bool trigered); void onActionStartCapture(bool trigered);
@ -34,7 +35,11 @@ private slots:
void onAdvancedFilterRequested(); void onAdvancedFilterRequested();
void onFilterError(QString msg); void onFilterError(QString msg);
void onFilterTextChanged(); void onFilterTextChanged();
//uncrustify on void onClear();
void onClientConnected();
void onClientDisconnected();
void onClientSocketError(const QString& error);
private: private:
void prepareTableView(); void prepareTableView();

6
gui/view/MainWindow.ui

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

7
model/include/provider/NetworkProvider.h

@ -21,6 +21,9 @@ public:
signals: signals:
void provided(const Log_ptr log) override; void provided(const Log_ptr log) override;
void clientConnected();
void clientDisconnected();
void clientError(QString error);
//uncrustify off //uncrustify off
private slots: private slots:
@ -28,6 +31,10 @@ private slots:
void onErrorOccured(QAbstractSocket::SocketError error); void onErrorOccured(QAbstractSocket::SocketError error);
void createNewConnection(); void createNewConnection();
void onReadyRead();
void onClientDisconnected();
void onSocketError(QAbstractSocket::SocketError socketError);
private: private:
bool _isRunning; bool _isRunning;
quint16 _port; 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() void NetworkProvider::createNewConnection()
{ {
QTcpSocket* socket = _server.nextPendingConnection(); QTcpSocket* socket = _server.nextPendingConnection();
QtConcurrent::run([this, socket](){ connect(socket, &QTcpSocket::readyRead, this, &NetworkProvider::onReadyRead);
while(socket->isOpen()) connect(socket, &QTcpSocket::disconnected, this, &NetworkProvider::onClientDisconnected);
{ connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred),
try { this, &NetworkProvider::onSocketError);
if(socket->waitForReadyRead()) emit clientConnected();
{ //QtConcurrent::run([this, socket](){
auto bytes = socket->readLine(); //while(socket->isOpen())
auto log = _parser->parse(bytes); //{
emit provided(log); //try {
} //if(socket->waitForReadyRead())
} //{
catch(std::exception& e) //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