Browse Source

Add capture dialog and connect network provider

basic-view
Ali Hatami Tajik 5 months ago
parent
commit
7b9b18e98f
  1. 3
      gui/gui.pro
  2. 26
      gui/main.cpp
  3. 26
      gui/view/DialogStartCaptur.cpp
  4. 24
      gui/view/DialogStartCaptur.h
  5. 105
      gui/view/DialogStartCaptur.ui
  6. 100
      gui/view/MainWindow.cpp
  7. 16
      gui/view/MainWindow.h
  8. 42
      gui/viewmodel/table/LogTableModel.cpp
  9. 11
      gui/viewmodel/table/LogTableModel.h
  10. 9
      gui/viewmodel/tree/LogTreeModel.cpp
  11. 1
      gui/viewmodel/tree/LogTreeModel.h
  12. 10
      model/include/LogWarehouse.h
  13. 38
      model/include/data/DataManager.h
  14. 18
      model/include/data/filter/MessageFilter.h
  15. 3
      model/include/data/filter/PrefixFilterFormatCreator.h
  16. 2
      model/include/type/Log.h
  17. 4
      model/model.pro
  18. 11
      model/src/LogWarehouse.cpp
  19. 58
      model/src/data/DataManager.cpp
  20. 27
      model/src/data/filter/MessageFilter.cpp
  21. 28
      model/src/data/filter/PrefixFilterFormatCreator.cpp
  22. 50
      model/src/provider/NetworkProvider.cpp

3
gui/gui.pro

@ -17,6 +17,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
view/DialogStartCaptur.cpp \
view/MainWindow.cpp \
viewmodel/displayString/FilePathDisplay.cpp \
viewmodel/displayString/FilenameDisplay.cpp \
@ -39,6 +40,7 @@ SOURCES += \
viewmodel/tree/util/Log2TreeItemConverter.cpp
HEADERS += \
view/DialogStartCaptur.h \
view/MainWindow.h \
viewmodel/displayString/DisplayProviderCreator.h \
viewmodel/displayString/FilePathDisplay.h \
@ -65,6 +67,7 @@ HEADERS += \
viewmodel/tree/util/Log2TreeItemConverter.h
FORMS += \
view/DialogStartCaptur.ui \
view/MainWindow.ui
LIBS += -L$$OUT_PWD/../model/ -lmodel

26
gui/main.cpp

@ -1,35 +1,13 @@
#include "view/MainWindow.h"
#include <QApplication>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QVector>
#include "parser/JsonParser.h"
#include "viewmodel/table/LogTableModel.h"
#include "viewmodel/displayString/SimpleDisplayProviderCreator.h"
#include "provider/NetworkProvider.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
DataProvider_ptr testProvider =
std::make_shared<NetworkProvider>(QHostAddress::LocalHost, 12345);
auto warehouse = std::make_shared<LogWarehouse>();
qRegisterMetaType<Log_ptr>();
QObject::connect(dynamic_cast<QObject*>(testProvider.get()),
SIGNAL(provided(const Log_ptr)),
warehouse.get(),
SLOT(addData(const Log_ptr)),
Qt::DirectConnection);
auto model = new LogTableModel(warehouse);
model->setDisplayProviderCreator(std::make_shared<SimpleDisplayProviderCreator>());
w.setTable(model);
MainWindow w;
w.show();
return a.exec();

26
gui/view/DialogStartCaptur.cpp

@ -0,0 +1,26 @@
#include "DialogStartCaptur.h"
#include "ui_DialogStartCaptur.h"
DialogStartCaptur::DialogStartCaptur(QWidget* parent) :
QDialog(parent),
ui(new Ui::DialogStartCaptur)
{
ui->setupUi(this);
}
/*************************************************************************************************/
quint16 DialogStartCaptur::getPort()
{
return static_cast<quint16>(ui->spinBox->value());
}
/*************************************************************************************************/
QString DialogStartCaptur::getAddress()
{
return ui->lineEdit->text();
}
DialogStartCaptur::~DialogStartCaptur()
{
delete ui;
}

24
gui/view/DialogStartCaptur.h

@ -0,0 +1,24 @@
#ifndef DIALOGSTARTCAPTUR_H
#define DIALOGSTARTCAPTUR_H
#include <QDialog>
namespace Ui {
class DialogStartCaptur;
}
class DialogStartCaptur : public QDialog
{
Q_OBJECT
public:
explicit DialogStartCaptur(QWidget* parent = nullptr);
quint16 getPort();
QString getAddress();
~DialogStartCaptur();
private:
Ui::DialogStartCaptur* ui;
};
#endif //DIALOGSTARTCAPTUR_H

105
gui/view/DialogStartCaptur.ui

@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DialogStartCaptur</class>
<widget class="QDialog" name="DialogStartCaptur">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>270</width>
<height>170</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="placeholderText">
<string>Address</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox">
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>12345</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogStartCaptur</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>DialogStartCaptur</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

100
gui/view/MainWindow.cpp

@ -1,20 +1,27 @@
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include "view/DialogStartCaptur.h"
#include "viewmodel/table/ELogColumn.h"
#include "viewmodel/tree/LogTreeModel.h"
#include "viewmodel/table/LogTableModel.h"
#include "viewmodel/displayString/SimpleDisplayProviderCreator.h"
#include "data/filter/PrefixFilterFormatCreator.h"
#include "provider/NetworkProvider.h"
#include "parser/JsonParser.h"
/*************************************************************************************************/
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
: QMainWindow(parent), _dataManager(std::make_shared<PrefixFilterFormatCreator>())
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
prepareTableView();
prepareTreeView();
auto tm = new TreeModel();
tm->showSelectedLog(nullptr);
ui->treeView->setModel(tm);
createConnections();
}
/*************************************************************************************************/
@ -24,13 +31,59 @@ MainWindow::~MainWindow()
}
/*************************************************************************************************/
void MainWindow::setTable(QAbstractTableModel* model)
void MainWindow::setTable(QAbstractItemModel* model)
{
ui->tableView->setModel(model);
//ui->tableView->setColumnHidden(ELogColumn::id, true);
//ui->tableView->setColumnHidden(ELogColumn::scope, true);
//ui->tableView->setColumnHidden(ELogColumn::lineno, true);
connect(ui->tableView->selectionModel(),
&QItemSelectionModel::currentChanged,
this,
&MainWindow::onTableRowClicked);
}
/*************************************************************************************************/
void MainWindow::onTableRowClicked(const QModelIndex& current, const QModelIndex& previous)
{
Q_UNUSED(previous)
QAbstractItemModel* abstractModel = ui->tableView->model();
LogTableModel* tablemodel = dynamic_cast<LogTableModel*>(abstractModel);
if(current.isValid())
{
auto log = tablemodel->data(current, Qt::UserRole).value<Log_ptr>();
QAbstractItemModel* treeAbstractModel = ui->treeView->model();
TreeModel* treemodel = dynamic_cast<TreeModel*>(treeAbstractModel);
treemodel->showSelectedLog(log);
}
}
/*************************************************************************************************/
void MainWindow::onTableDeselected()
{
QAbstractItemModel* treeAbstractModel = ui->treeView->model();
TreeModel* treemodel = dynamic_cast<TreeModel*>(treeAbstractModel);
treemodel->clearView();
}
/*************************************************************************************************/
void MainWindow::onActionStartCapture(bool trigered)
{
Q_UNUSED(trigered)
DialogStartCaptur capture;
capture.setModal(true);
int isOk = capture.exec();
if(isOk)
{
auto port = capture.getPort();
auto address = capture.getAddress();
qDebug() << address << port;
useNetworkProvider(address, port);
}
}
/*************************************************************************************************/
@ -39,11 +92,42 @@ void MainWindow::prepareTableView()
ui->tableView->verticalHeader()->setVisible(false);
ui->tableView->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeMode::ResizeToContents);
ui->tableView->horizontalHeader()->setStretchLastSection(true);
ui->tableView->horizontalHeader()->setSectionsMovable(true);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
LogTableModel* tableModel = new LogTableModel();
tableModel->setDisplayProviderCreator(std::make_shared<SimpleDisplayProviderCreator>());
setTable(tableModel);
connect(&_dataManager, &DataManager::logAdded, tableModel, &LogTableModel::onLogAdded);
connect(&_dataManager, &DataManager::logsReseted, tableModel, &LogTableModel::onLogsChanged);
}
/*************************************************************************************************/
void MainWindow::prepareTreeView()
{
ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
auto tm = new TreeModel();
ui->treeView->setModel(tm);
}
/*************************************************************************************************/
void MainWindow::createConnections()
{
connect(ui->actionStart_Capture, &QAction::triggered, this, &MainWindow::onActionStartCapture);
}
/*************************************************************************************************/
void MainWindow::useNetworkProvider(QString address, quint16 port)
{
auto netProv = std::make_shared<NetworkProvider>(QHostAddress(address), port);
netProv->setParser(std::make_shared<JsonParser>());
_dataProvider = netProv;
connect(dynamic_cast<QObject*>(_dataProvider.get()),
SIGNAL(provided(const Log_ptr)),
&_dataManager,
SLOT(onLogProvided(const Log_ptr)));
}

16
gui/view/MainWindow.h

@ -3,6 +3,8 @@
#include <QMainWindow>
#include <QAbstractTableModel>
#include <data/DataManager.h>
#include "provider/IProvider.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow;
@ -16,12 +18,24 @@ class MainWindow : public QMainWindow
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
void setTable(QAbstractTableModel* model);
void setTable(QAbstractItemModel* model);
//uncrustify off
private slots:
void onTableRowClicked(const QModelIndex &current, const QModelIndex &previous);
void onTableDeselected();
void onActionStartCapture(bool trigered);
//uncrustify on
private:
void prepareTableView();
void prepareTreeView();
void createConnections();
void useNetworkProvider(QString address, quint16 port);
DataManager _dataManager;
DataProvider_ptr _dataProvider;
Ui::MainWindow* ui;
};

42
gui/viewmodel/table/LogTableModel.cpp

@ -3,11 +3,9 @@
#include "viewmodel/displayString/util/DisplayStringProviderMapGenerator.h"
/*************************************************************************************************/
LogTableModel::LogTableModel(LogWarehouse_ptr warehouse) : _warehouse(warehouse)
LogTableModel::LogTableModel()
{
initColumnMap();
connect(warehouse.get(), &LogWarehouse::dataAdded, this, &LogTableModel::rowsAdded);
_lastModelSize = _warehouse->getLength();
}
/*************************************************************************************************/
@ -19,7 +17,7 @@ LogTableModel::~LogTableModel()
int LogTableModel::rowCount(const QModelIndex& parent) const {
Q_UNUSED(parent)
auto size = _warehouse->getLength();
auto size = _logs.size();
return size;
}
@ -33,13 +31,23 @@ int LogTableModel::columnCount(const QModelIndex& parent) const {
/*************************************************************************************************/
QVariant LogTableModel::data(const QModelIndex& index, int role) const {
if(!index.isValid() || role != Qt::DisplayRole)
if(!index.isValid() || (role != Qt::DisplayRole and role != Qt::UserRole))
{
return QVariant();
}
return providerDisplayRepresentation(_warehouse->getItem(index.row()),
static_cast<ELogColumn>(index.column()));
if(role == Qt::DisplayRole)
{
return providerDisplayRepresentation(_logs[index.row()],
static_cast<ELogColumn>(index.column()));
}
if(role == Qt::UserRole)
{
return QVariant::fromValue(_logs[index.row()]);
}
return QVariant();
}
/*************************************************************************************************/
@ -62,15 +70,19 @@ void LogTableModel::setDisplayProviderCreator(DisplayProviderCreator_ptr creator
}
/*************************************************************************************************/
void LogTableModel::rowsAdded()
void LogTableModel::onLogAdded(Log_ptr log)
{
auto warehouseSize = _warehouse->getLength();
if(_lastModelSize < warehouseSize)
{
beginInsertRows(QModelIndex(), _lastModelSize, warehouseSize - 1);
endInsertRows();
_lastModelSize = warehouseSize;
}
beginInsertRows(QModelIndex(), _logs.size(), _logs.size());
_logs.append(log);
endInsertRows();
}
/*************************************************************************************************/
void LogTableModel::onLogsChanged(QList<Log_ptr> logs)
{
beginResetModel();
_logs = logs;
endResetModel();
}
/*************************************************************************************************/

11
gui/viewmodel/table/LogTableModel.h

@ -8,7 +8,6 @@
#include <QVector>
#include <QAbstractTableModel>
#include "LogWarehouse.h"
#include "viewmodel/table/ELogColumn.h"
#include "viewmodel/displayString/DisplayProviderCreator.h"
@ -17,7 +16,7 @@ class LogTableModel : public QAbstractTableModel
Q_OBJECT
public:
LogTableModel(LogWarehouse_ptr warehouse);
LogTableModel();
~LogTableModel() override;
@ -35,15 +34,15 @@ public:
//uncrustify off
public slots:
//uncrustify on
void rowsAdded();
void onLogAdded(Log_ptr log);
void onLogsChanged(QList<Log_ptr> logs);
private:
int _lastModelSize;
QMap<int, QString> _columnMap;
LogWarehouse_ptr _warehouse;
QList<Log_ptr> _logs;
QMap<ELogColumn, DisplayStringProvider_ptr> _textProviders;
void initColumnMap();
QMap<ELogColumn, DisplayStringProvider_ptr> _textProviders;
QVariant providerDisplayRepresentation(const Log_ptr& log, ELogColumn column) const;
};

9
gui/viewmodel/tree/LogTreeModel.cpp

@ -10,6 +10,7 @@
TreeModel::TreeModel(QObject* parent)
: QAbstractItemModel(parent)
{
clearView();
}
/*************************************************************************************************/
@ -107,15 +108,9 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
/*************************************************************************************************/
void TreeModel::showSelectedLog(const Log_ptr log)
{
QString jsonString =
R"(
{"id":"stderr","level":1,"location":{"file":"../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp","func":"init","lineno":69},"msg":["Lanat", {"type": "int", "summary":"bish tar az bish", "content":{"a":"b", "arr" : ["a", "b", "c", {"a":"b"}], "c":{"a":44}}}],"scope":"global","threadId":124330260010752,"timestamp":1716911725703}
)";
Log_ptr logf = JsonParser().parse(jsonString);
beginResetModel();
_rootItem = SimpleLog2TreeItemConverter().convert(
logf,
log,
DisplayStringProviderMapGenerator::generate(
std::make_shared<FullFileDisplayProviderCreator>()));
endResetModel();

1
gui/viewmodel/tree/LogTreeModel.h

@ -32,6 +32,7 @@ public slots:
//uncrustify on
private:
bool _valid = false;
std::unique_ptr<TreeItem> _rootItem;
};

10
model/include/LogWarehouse.h

@ -12,14 +12,8 @@ class LogWarehouse : public QObject
public:
Log_ptr getItem(int index);
int getLength();
//uncrustify off
public slots:
//uncrustify on
void addData(const Log_ptr data);
signals:
void dataAdded();
int addData(const Log_ptr data);
void clear();
private:
QList<Log_ptr> _data;

38
model/include/data/DataManager.h

@ -0,0 +1,38 @@
#ifndef DATAMANAGER_H
#define DATAMANAGER_H
#include <QObject>
#include "data/filter/FilterParser.h"
#include "data/filter/IFilter.h"
#include "LogWarehouse.h"
class DataManager : public QObject
{
Q_OBJECT
public:
DataManager(FilterCreator_ptr<Log_ptr> filterCreator);
void clearLogs();
void setFilter(QString filter);
signals:
void badFilterRequested(QString message);
void logAdded(const Log_ptr log);
void logsReseted(QList<Log_ptr> logs);
//uncrustify off
public slots:
//uncrustify on
void onLogProvided(Log_ptr log);
private:
void resetView();
LogWarehouse_ptr _warehouse;
std::shared_ptr<IFilter<Log_ptr> > _filter;
FilterParser<Log_ptr> _parser;
};
#endif //DATAMANAGER_H

18
model/include/data/filter/MessageFilter.h

@ -0,0 +1,18 @@
#ifndef MESSAGEFILTER
#define MESSAGEFILTER
#include "type/Log.h"
#include "data/filter/IFilter.h"
class MessageFilter : public IFilter<Log_ptr>
{
public:
MessageFilter(QString filter);
bool isAccepted(Log_ptr input) const override;
private:
QString _filter;
};
#endif //MESSAGEFILTER

3
model/include/data/filter/PrefixFilterFormatCreator.h

@ -8,6 +8,9 @@ class PrefixFilterFormatCreator : public IFilterCreator<Log_ptr>
{
public:
std::shared_ptr<IFilter<Log_ptr> > create(const QString& input) const override;
private:
std::shared_ptr<IFilter<Log_ptr> > createMessageFilter(const QString& input) const;
};
#endif //PREFIXFILTERFORMATCREATOR

2
model/include/type/Log.h

@ -25,4 +25,6 @@ struct Log
using Log_ptr = std::shared_ptr<Log>;
Q_DECLARE_METATYPE(Log_ptr);
#endif //LOG

4
model/model.pro

@ -8,12 +8,14 @@ TARGET = model
HEADERS += \
include/LogWarehouse.h \
include/conf/Strings.h \
include/data/DataManager.h \
include/data/filter/BypassFilter.h \
include/data/filter/FilterAnd.h \
include/data/filter/FilterOr.h \
include/data/filter/FilterParser.h \
include/data/filter/IFilter.h \
include/data/filter/IFilterCreator.h \
include/data/filter/MessageFilter.h \
include/data/filter/PrefixFilterFormatCreator.h \
include/parser/JsonParseConfig.h \
include/parser/JsonParser.h \
@ -28,10 +30,12 @@ HEADERS += \
SOURCES += \
src/LogWarehouse.cpp \
src/data/DataManager.cpp \
src/data/filter/BypassFilter.cpp \
src/data/filter/FilterAnd.cpp \
src/data/filter/FilterOr.cpp \
src/data/filter/FilterParser.cpp \
src/data/filter/MessageFilter.cpp \
src/data/filter/PrefixFilterFormatCreator.cpp \
src/parser/JsonParser.cpp \
src/provider/NetworkProvider.cpp

11
model/src/LogWarehouse.cpp

@ -1,6 +1,7 @@
#include "LogWarehouse.h"
#include <QDebug>
#include <QRandomGenerator>
/*************************************************************************************************/
Log_ptr LogWarehouse::getItem(int index)
@ -15,11 +16,15 @@ int LogWarehouse::getLength()
}
/*************************************************************************************************/
void LogWarehouse::addData(const Log_ptr data)
int LogWarehouse::addData(const Log_ptr data)
{
_data.append(data);
qDebug() << data->scope.value();
return _data.size() - 1; //index of where data is added
}
emit dataAdded();
/*************************************************************************************************/
void LogWarehouse::clear()
{
_data.clear();
}

58
model/src/data/DataManager.cpp

@ -0,0 +1,58 @@
#include "data/DataManager.h"
#include "data/filter/BypassFilter.h"
#include "data/filter/PrefixFilterFormatCreator.h"
/*************************************************************************************************/
DataManager::DataManager(FilterCreator_ptr<Log_ptr> filterCreator) : _parser(filterCreator)
{
_filter = std::make_shared<BypassFilter>();
_warehouse = std::make_shared<LogWarehouse>();
}
/*************************************************************************************************/
void DataManager::clearLogs()
{
emit logsReseted({});
_warehouse->clear();
}
/*************************************************************************************************/
void DataManager::setFilter(QString filter)
{
try {
_filter = _parser.parse(filter);
resetView();
}
catch(std::runtime_error e)
{
_filter = std::make_shared<BypassFilter>();
emit badFilterRequested(e.what());
}
}
/*************************************************************************************************/
void DataManager::onLogProvided(const Log_ptr log)
{
_warehouse->addData(log);
if(_filter->isAccepted(log))
{
emit logAdded(log);
}
}
/*************************************************************************************************/
void DataManager::resetView()
{
QList<Log_ptr> logs;
for(int i = 0; i < _warehouse->getLength(); i++)
{
auto log = _warehouse->getItem(i);
if(_filter->isAccepted(log))
{
logs.append(logs);
}
}
emit logsReseted(logs);
}

27
model/src/data/filter/MessageFilter.cpp

@ -0,0 +1,27 @@
#include "data/filter/MessageFilter.h"
/*************************************************************************************************/
MessageFilter::MessageFilter(QString filter)
{
_filter = filter;
}
/*************************************************************************************************/
bool MessageFilter::isAccepted(Log_ptr input) const
{
if(input->msg.has_value())
{
for(auto& part : input->msg.value())
{
if(part.string.has_value())
{
if(part.string.value().contains(_filter))
{
return true;
}
}
}
}
return false;
}

28
model/src/data/filter/PrefixFilterFormatCreator.cpp

@ -1,5 +1,8 @@
#include "data/filter/PrefixFilterFormatCreator.h"
#include <QRegularExpression>
#include "data/filter/MessageFilter.h"
#include "data/filter/BypassFilter.h"
/*************************************************************************************************/
@ -7,5 +10,28 @@ std::shared_ptr<IFilter<Log_ptr> > PrefixFilterFormatCreator::create(const QStri
{
Q_UNUSED(input)
return std::make_shared<BypassFilter>();
if(input.startsWith("[MSG"))
{
return createMessageFilter(input);
}
else
{
return std::make_shared<BypassFilter>();
}
}
/*************************************************************************************************/
std::shared_ptr<IFilter<Log_ptr> > PrefixFilterFormatCreator::createMessageFilter(
const QString& input) const
{
QRegularExpression re(R"(\[MSG (.*)\])");
auto match = re.match(input);
if(match.hasMatch())
{
return std::make_shared<MessageFilter>(match.captured(1));
}
else
{
return std::make_shared<BypassFilter>();
}
}

50
model/src/provider/NetworkProvider.cpp

@ -3,6 +3,8 @@
#include <QThread>
#include <QTimer>
#include "parser/JsonParser.h"
#include <QTcpSocket>
#include <QtConcurrent/QtConcurrent>
/*************************************************************************************************/
NetworkProvider::NetworkProvider(QHostAddress hostAddress, quint16 port)
@ -12,20 +14,20 @@ NetworkProvider::NetworkProvider(QHostAddress hostAddress, quint16 port)
_isRunning = false;
initServer();
connect(&_timer, &QTimer::timeout,
[this](){
QString jsonString =
R"(
{"id":"stderr","level":1,"location":{"file":"../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp","func":"init","lineno":69},"msg":["Lanat\n"],"scope":"global","threadId":124330260010752,"timestamp":1716911725703}
)";
Log_ptr log = JsonParser().parse(jsonString);
static int a = 69;
log->src->lineNumner = a++;
emit provided(log);
});
//connect(&_timer, &QTimer::timeout,
//[this](){
//QString jsonString =
//R"(
//{"id":"stderr","level":1,"location":{"file":"../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp","func":"init","lineno":69},"msg":["Lanat\n"],"scope":"global","threadId":124330260010752,"timestamp":1716911725703}
//)";
//Log_ptr log = JsonParser().parse(jsonString);
//static int a = 69;
//log->src->lineNumner = a++;
//emit provided(log);
//});
_timer.start(1000);
//_timer.start(1000);
}
/*************************************************************************************************/
@ -64,14 +66,30 @@ void NetworkProvider::onErrorOccured(QAbstractSocket::SocketError error)
/*************************************************************************************************/
void NetworkProvider::createNewConnection()
{
//TODO
QTcpSocket* socket = _server.nextPendingConnection();
QtConcurrent::run([this, socket](){
while(socket->isOpen())
{
if(socket->waitForReadyRead())
{
auto bytes = socket->readLine();
try {
auto log = _parser->parse(bytes);
emit provided(log);
}
catch(std::exception e)
{
}
}
}
});
}
/*************************************************************************************************/
void NetworkProvider::initServer()
{
QThread* thread = new QThread();
_server.moveToThread(thread);
//QThread* thread = new QThread();
//_server.moveToThread(thread);
QObject::connect(&_server,
&QTcpServer::newConnection,

Loading…
Cancel
Save