diff --git a/.gitignore b/.gitignore index 860229f594a458e80a8c4ca59a90e30ac6c9aeb9..97a38285941ba941f9652d12e1d97f7f920a80ab 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ moc_* .qmake_stash qrc_* rcc/* -node_build \ No newline at end of file +node_build +build diff --git a/CMakeLists.txt b/CMakeLists.txt index dee65667342f0fcead36990e575eba4e9014eab7..020ecce3b501ce5b28f74e37c6ff1d9c8acae3f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ if(UNIX) add_definitions(-DCLI_PATH="./cellframe-node-cli") add_executable(${PROJECT_NAME} ./CellframeNodeDiagtool/DiagnosticWorker.cpp + ./CellframeNodeDiagtool/DiagDataSendingWorker.cpp ./CellframeNodeDiagtool/AbstractDiagnostic.cpp ./CellframeNodeDiagtool/MacDiagnostic.cpp ./CellframeNodeDiagtool/NotifyWorker.cpp @@ -35,6 +36,7 @@ if(UNIX) add_definitions(-DCLI_PATH="/opt/cellframe-node/bin/cellframe-node-cli") add_executable(${PROJECT_NAME} ./CellframeNodeDiagtool/DiagnosticWorker.cpp + ./CellframeNodeDiagtool/DiagDataSendingWorker.cpp ./CellframeNodeDiagtool/AbstractDiagnostic.cpp ./CellframeNodeDiagtool/LinuxDiagnostic.cpp ./CellframeNodeDiagtool/NotifyWorker.cpp @@ -51,6 +53,7 @@ if(WIN32) add_definitions(-DCLI_PATH="cellframe-node-cli.exe") add_executable(${PROJECT_NAME} WIN32 ./CellframeNodeDiagtool/DiagnosticWorker.cpp + ./CellframeNodeDiagtool/DiagDataSendingWorker.cpp ./CellframeNodeDiagtool/AbstractDiagnostic.cpp ./CellframeNodeDiagtool/registry.c ./CellframeNodeDiagtool/WinDiagnostic.cpp diff --git a/CellframeNodeDiagtool/DiagDataSendingWorker.cpp b/CellframeNodeDiagtool/DiagDataSendingWorker.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59fba0c5a4c16d5b66ff3b09f85015258ceb1e5f --- /dev/null +++ b/CellframeNodeDiagtool/DiagDataSendingWorker.cpp @@ -0,0 +1,87 @@ +#include "DiagDataSendingWorker.h" + +static quint64 port = 8040; + +DiagDataSendingWorker::DiagDataSendingWorker(QTcpServer *parent) + : QTcpServer{parent} +{ + s_timer_restart = new QTimer(this); + connect(s_timer_restart, &QTimer::timeout, + this, &DiagDataSendingWorker::startServer, + Qt::QueuedConnection); + + startServer(); + + m_urls = QStringList()<<"https://telemetry.cellframe.net/diag_report"<< "https://engine-minkowski.kelvpn.com/diag_report"; + m_netMngr = new QNetworkAccessManager(this); + connect(m_netMngr, &QNetworkAccessManager::finished, this, [=](QNetworkReply*r) { + qDebug() <<"Data sent. " << r->error(); + }); +} + +void DiagDataSendingWorker::startServer() +{ + if( !listen(QHostAddress::Any, port) ) + { + s_timer_restart->stop(); + s_timer_restart->start(5000); + + qWarning()<<"Unable start diagtool server" << errorString(); + } + else{ + connect(this, &QTcpServer::newConnection, + this, &DiagDataSendingWorker::onNewConnection, + Qt::QueuedConnection); + + serverStatus = true; + qInfo()<<"Diagtool server is started"; + } +} + +void DiagDataSendingWorker::onNewConnection() +{ + qDebug()<<"New connection..."; + QTcpSocket * _socket = nextPendingConnection(); + + if(_socket->state() != QAbstractSocket::ConnectedState) + return; + + s_tcpSocketList.push_back(_socket); + + connect(_socket, &QTcpSocket::disconnected, + this, &DiagDataSendingWorker::onClientDisconnect, + Qt::QueuedConnection); + + qDebug()<<"Client's count:" << s_tcpSocketList.length(); +} + +void DiagDataSendingWorker::onClientDisconnect() +{ + qDebug()<<"Client disconnected"; + QTcpSocket* _socket = dynamic_cast<QTcpSocket*>(sender()); + s_tcpSocketList.removeOne(_socket); + qDebug()<<"Client's count:" << s_tcpSocketList.length(); +} + + +void DiagDataSendingWorker::sendData(QJsonDocument diagdata, QString mac) +{ + if(serverStatus && s_tcpSocketList.length()) + { + for(auto client: qAsConst(s_tcpSocketList)) + client->write(diagdata.toJson()); + } + + for (const auto &url_s : qAsConst(m_urls)) + { + QUrl url = QUrl(url_s); + + auto req = QNetworkRequest(url); + req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); + QJsonObject obj = diagdata.object(); + obj.insert("mac",mac); + diagdata.setObject(obj); + m_netMngr->post(req, diagdata.toJson()); + qDebug()<<"Post request by url: " << url.toString(); + } +} diff --git a/CellframeNodeDiagtool/DiagDataSendingWorker.h b/CellframeNodeDiagtool/DiagDataSendingWorker.h new file mode 100644 index 0000000000000000000000000000000000000000..af0d2af9da5593c673c11ae92c70ef3c599dac43 --- /dev/null +++ b/CellframeNodeDiagtool/DiagDataSendingWorker.h @@ -0,0 +1,45 @@ +#ifndef DIAGDATASENDINGWORKER_H +#define DIAGDATASENDINGWORKER_H + +#include <QObject> +#include <QTcpServer> +#include <QTcpSocket> +#include <QTextCodec> +#include <QUrl> +#include <QTimer> + +#include <QJsonDocument> +#include <QJsonObject> + +#include <QNetworkAccessManager> +#include <QHttpPart> +#include <QHttpMultiPart> +#include <QNetworkReply> + +class DiagDataSendingWorker : public QTcpServer +{ + Q_OBJECT +public: + explicit DiagDataSendingWorker(QTcpServer *parent = nullptr); + + void sendData(QJsonDocument diagdata, QString mac); + +private slots: + void startServer(); + void onNewConnection(); + void onClientDisconnect(); + + +private: + QTimer *s_timer_restart; + QList<QTcpSocket*> s_tcpSocketList; + + QStringList m_urls; + QNetworkAccessManager *m_netMngr; + +public: + bool serverStatus{false}; + +}; + +#endif // DIAGDATASENDINGWORKER_H diff --git a/CellframeNodeDiagtool/DiagnosticWorker.cpp b/CellframeNodeDiagtool/DiagnosticWorker.cpp index 24264257c2af53ad8797bcf5810b2fb715fc0b81..e1c07c93dcab368786db256e26b4fab09f1898d8 100644 --- a/CellframeNodeDiagtool/DiagnosticWorker.cpp +++ b/CellframeNodeDiagtool/DiagnosticWorker.cpp @@ -18,11 +18,7 @@ DiagnosticWorker::~DiagnosticWorker() void DiagnosticWorker::init() { - m_urls = QStringList()<<"https://telemetry.cellframe.net/diag_report"<< "https://engine-minkowski.kelvpn.com/diag_report"; - m_netMngr = new QNetworkAccessManager(this); - connect(m_netMngr, &QNetworkAccessManager::finished, this, [=](QNetworkReply*r) { - qDebug() <<"Data sent. " << r->error(); - }); + s_diagDataWorker = new DiagDataSendingWorker(); s_uptime_timer = new QTimer(this); connect(s_uptime_timer, &QTimer::timeout, @@ -130,27 +126,10 @@ void DiagnosticWorker::slot_diagnostic_data(QJsonDocument data) data.setObject(obj); m_lastSendedPack = data; - write_data(data); + s_diagDataWorker->sendData(data, m_diagnostic->s_mac.toString()); } void DiagnosticWorker::slot_uptime() { s_uptime = m_diagnostic->get_uptime_string(s_elapsed_timer->elapsed()/1000); } - -void DiagnosticWorker::write_data(QJsonDocument data) -{ - for (auto url_s : m_urls) - { - QUrl url = QUrl(url_s); - - auto req = QNetworkRequest(url); - req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); - QString key = m_diagnostic->s_mac.toString(); - QJsonObject obj = data.object(); - obj.insert("mac",key); - data.setObject(obj); - m_netMngr->post(req, data.toJson()); - qDebug()<<"Post request by url: " << url.toString(); - } -} diff --git a/CellframeNodeDiagtool/DiagnosticWorker.h b/CellframeNodeDiagtool/DiagnosticWorker.h index ab027f3a5c3a23ba857c05e97ca5d35946808ac8..7bce4a1952a44070dc8c1083f44b180228b11d38 100644 --- a/CellframeNodeDiagtool/DiagnosticWorker.h +++ b/CellframeNodeDiagtool/DiagnosticWorker.h @@ -9,17 +9,13 @@ #include <QDesktopServices> #include <QSettings> -#include <QNetworkAccessManager> -#include <QHttpPart> -#include <QHttpMultiPart> -#include <QNetworkReply> - #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include "NotifyWorker.h" +#include "DiagDataSendingWorker.h" #ifdef Q_OS_LINUX #include "LinuxDiagnostic.h" @@ -61,8 +57,7 @@ private: QJsonDocument m_lastSendedPack; - QStringList m_urls; - QNetworkAccessManager *m_netMngr; + DiagDataSendingWorker *s_diagDataWorker; bool m_initStatus{false}; bool m_isSendData{true}; diff --git a/CellframeNodeTray/NodeTrayCommandController.cpp b/CellframeNodeTray/NodeTrayCommandController.cpp index 270fc70e31b9560cf4ba260e103cdcc6a914d965..1a8ca89553e1d03dc12146c61e52844de68fedea 100644 --- a/CellframeNodeTray/NodeTrayCommandController.cpp +++ b/CellframeNodeTray/NodeTrayCommandController.cpp @@ -265,7 +265,7 @@ void NodeTrayCommandController::showMessage(const QString msg, const QMessageBox // msgBox.setIcon(icn); // msgBox.exec(); - emit sigShowMessage("Celflrame Node Tray",msg); + emit sigShowMessage("Cellframe Node Tray",msg); } #ifdef WIN32