feat: отделение общих рассылок по клиентам в отдельный сервис

This commit is contained in:
semenov
2025-01-14 11:16:16 +03:00
parent 2f58227e11
commit ce82fb32e5
11 changed files with 299 additions and 233 deletions

View File

@@ -47,6 +47,8 @@ add_library(ServerLMS SHARED
Systems/recognizesystem.h
Systems/updatecontroller.cpp
Systems/updatecontroller.h
Systems/commonclienthandler.cpp
Systems/commonclienthandler.h
Systems/logger.cpp
Systems/logger.h
Systems/dataparser.cpp

View File

@@ -0,0 +1,140 @@
#include "commonclienthandler.h"
CommonClientHandler::CommonClientHandler(QObject *parent) : QObject(parent)
{
}
void CommonClientHandler::initialize(QMap<int, ClientHandler *> *clientsMap, ProcessingSystem *processingSystem, DataParser *dataParser, Logger *logger)
{
this->clientsMap = clientsMap;
this->processingSystem = processingSystem;
this->dataParser = dataParser;
this->logger = logger;
}
void CommonClientHandler::sendNewVersionListToAllClient()
{
foreach(int idSocket,clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendVersionList();
}
}
void CommonClientHandler::sendCurrentVersionToAllClient()
{
foreach(int idSocket,clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendVersion();
}
}
void CommonClientHandler::slot_AuthChanged()
{
//Проходим все открытые сокеты
foreach(int idSocket, clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
//Проверяем, есть ли клиенты TYPE_GUI
if(handler->getClient()->getTypeClient() == TypeClientAutorization::TYPE_GUI)
{//Отправляем этому клиенту обновление списков
ClientQueryToDB queryToDB;
queryToDB.typeQuery = TypeQueryToDB::TYPE_QUERY_GET_ALL_LISTS;
processingSystem->processingClientQueryToDB(handler, queryToDB);
}
}
}
void CommonClientHandler::slot_sendPacketToAllClients(PacketType packetType)
{
foreach(int idSocket, clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendPacketType(packetType);
}
}
void CommonClientHandler::slot_msgToClientFromGUI(QString login, QString text)
{
QString textMsg = text;
QByteArray byteArrayMsg = dataParser->xmlAnswer_message(textMsg);
//Проходим все открытые сокеты, ищем нужный
foreach(int idSocket, clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
if(handler->getClient()->getLogin() == login)
{//Отправляем ему
handler->sendXmlAnswer(byteArrayMsg);
QString peerAddress = handler->getSocket()->peerAddress().toString();
QString peerPort = QString::number(handler->getSocket()->peerPort());
QString str = "Msg To Client [" + peerAddress + ":" + peerPort + "] : " + textMsg;
logger->addTextToLogger(str);
break;
}
}
}
void CommonClientHandler::slot_msgToGUIfromClient(QString login, QString text)
{
QString textMsg = text;
QByteArray byteArrayMsg = dataParser->xmlAnswer_message(textMsg, login);
//Проходим все открытые сокеты, ищем нужный
foreach(int idSocket, clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);;
if(handler->getClient()->getTypeClient() == TypeClientAutorization::TYPE_GUI)
{//Отправляем GUI-клиенту для отображения в Мессенджере
handler->sendXmlAnswer(byteArrayMsg, PacketType::TYPE_XMLANSWER);
QString peerAddress = handler->getSocket()->peerAddress().toString();
QString peerPort = QString::number(handler->getSocket()->peerPort());
QString str = "Msg From Client [" + peerAddress + ":" + peerPort + "] : " + textMsg;
logger->addTextToLogger(str);
break;
}
}
}
void CommonClientHandler::slot_sendTaskToClient(QString fullNameClient,QString textTask)
{
QByteArray byteArrayTask = dataParser->xmlAnswer_task(textTask);
//Проходим все открытые сокеты
foreach(int idSocket, clientsMap->keys())
{
ClientHandler *handler = clientsMap->value(idSocket);
if(handler->getClient()->getFullName() == fullNameClient)
{//Отправляем ему
handler->getSocket()->write(byteArrayTask);
QString peerAddress = handler->getSocket()->peerAddress().toString();
QString peerPort = QString::number(handler->getSocket()->peerPort());
QString str = "Task To Client [" + peerAddress + ":" + peerPort + "] : " + textTask;
logger->addTextToLogger(str);
}
}
}

View File

@@ -0,0 +1,39 @@
#ifndef COMMONCLIENTHANDLER_H
#define COMMONCLIENTHANDLER_H
#include <QObject>
#include <clienthandler.h>
class ProcessingSystem;
class DataParser;
class Logger;
class CommonClientHandler : public QObject
{
Q_OBJECT
public:
explicit CommonClientHandler(QObject *parent = nullptr);
void initialize(QMap<int, ClientHandler*> *clientsMap,
ProcessingSystem *processingSystem,
DataParser *dataParser,
Logger *logger);
void sendNewVersionListToAllClient();
void sendCurrentVersionToAllClient();
void slot_AuthChanged();
void slot_sendPacketToAllClients(PacketType packetType);
//слот обработки сигнала о готовности нового сообщения на отправку клиенту от мессенджера
void slot_msgToClientFromGUI(QString login, QString text);
void slot_msgToGUIfromClient(QString login, QString text);
void slot_sendTaskToClient(QString fullNameClient, QString textTask);
signals:
private:
QMap<int, ClientHandler*> *clientsMap;
ProcessingSystem *processingSystem;
DataParser *dataParser;
Logger *logger;
};
#endif // COMMONCLIENTHANDLER_H

View File

@@ -8,16 +8,17 @@ ProcessingSystem::ProcessingSystem(ProviderDBLMS* providerDBLMS, QObject *parent
this->providerDBLMS = providerDBLMS;
}
void ProcessingSystem::initialize(DataParser *dataParser, ServerLMSWidget *server)
void ProcessingSystem::initialize(ServerLMSWidget *server, DataParser *dataParser, CommonClientHandler *commonClientHandler,Logger *logger)
{
this->server = server;
this->commonClientServer = commonClientHandler;
this->dataParser = dataParser;
this->server = server;
connect(this,&ProcessingSystem::sigAuthChanged,server, &ServerLMSWidget::slot_AuthChanged,Qt::AutoConnection);
connect(this,&ProcessingSystem::sigAuthChanged,commonClientHandler, &CommonClientHandler::slot_AuthChanged,Qt::AutoConnection);
connect(this,&ProcessingSystem::sigUpdateListClients,server, &ServerLMSWidget::slotUpdateListClients,Qt::AutoConnection);
connect(this,&ProcessingSystem::sigLogMessage,server->getLogger(),&Logger::addTextToLogger,Qt::QueuedConnection);
connect(this,&ProcessingSystem::signal_msgToClientReady,server, &ServerLMSWidget::slot_msgToClientFromGUI);
connect(this,&ProcessingSystem::signal_msgFromClientReady,server, &ServerLMSWidget::slot_msgToGUIfromClient);
connect(this,&ProcessingSystem::signal_msgToClientReady,commonClientHandler, &CommonClientHandler::slot_msgToClientFromGUI);
connect(this,&ProcessingSystem::signal_msgFromClientReady,commonClientHandler, &CommonClientHandler::slot_msgToGUIfromClient);
connect(this,&ProcessingSystem::sigLogMessage,logger,&Logger::addTextToLogger,Qt::QueuedConnection);
}
void ProcessingSystem::processingClientAutorization(ClientHandler *client, ClientAutorization clientAutorization)

View File

@@ -15,6 +15,7 @@ class InstructorsAndTrainees;
class Logger;
class DataParser;
class ClientHandler;
class CommonClientHandler;
class ProcessingSystem : public QObject
{
@@ -22,7 +23,7 @@ class ProcessingSystem : public QObject
public:
explicit ProcessingSystem(ProviderDBLMS* providerDBLMS, QObject *parent = nullptr);
void initialize(DataParser* dataParser,ServerLMSWidget *server); //TODO: проверить необходимость зависимости на ServerLMS
void initialize(ServerLMSWidget *server,DataParser* dataParser,CommonClientHandler *commonClientServer,Logger *logger);
void processingClientAutorization(ClientHandler *client, ClientAutorization clientAutorization);
void processingClientDeAutorization(ClientHandler *client, ClientDeAutorization clientDeAutorization);
void processingClientQueryToDB(ClientHandler *client, ClientQueryToDB clientQueryToDB, int id = 0, void* data = nullptr);
@@ -40,6 +41,7 @@ signals:
void signal_msgFromClientReady(QString login, QString text);
private:
CommonClientHandler *commonClientServer;
ServerLMSWidget *server;
DataParser *dataParser;
//InstructorsAndTraineesWidget *pInstructorsAndTrainees;

View File

@@ -2,7 +2,7 @@
UpdateController::UpdateController(QObject *parent) :
QObject(parent),
server(nullptr)
commonClientHandler(nullptr)
{
buildPath = QDir::currentPath() + "/" + applicationFolderName;
sharedDataPath = QDir::currentPath() + "/" + sharedDataFolderName;
@@ -10,9 +10,9 @@ UpdateController::UpdateController(QObject *parent) :
qDebug() << hashFileName;
}
void UpdateController::initialize(ServerLMSWidget *server,DataParser *dataParser,AssetsManager *assetManager)
void UpdateController::initialize(CommonClientHandler *commonClientHandler,DataParser *dataParser,AssetsManager *assetManager)
{
this->server = server;
this->commonClientHandler = commonClientHandler;
this->dataParser = dataParser;
this->assetManager = assetManager;
@@ -29,21 +29,21 @@ void UpdateController::initialize(ServerLMSWidget *server,DataParser *dataParser
void UpdateController::changeAssetVersion(QString versionName)
{
server->slot_sendPacketToAllClients(PacketType::BUSY);
commonClientHandler->slot_sendPacketToAllClients(PacketType::BUSY);
qDebug() << "UpdateController thread ID " << QThread::currentThreadId();
currentStreamingPath = assetManager->setVersion(versionName);
setUpCurrentServerHash();
server->slot_sendPacketToAllClients(PacketType::HASH_READY);
server->sendCurrentVersionToAllClient();
commonClientHandler->slot_sendPacketToAllClients(PacketType::HASH_READY);
commonClientHandler->sendCurrentVersionToAllClient();
server->slot_sendPacketToAllClients(PacketType::FREE);
commonClientHandler->slot_sendPacketToAllClients(PacketType::FREE);
}
void UpdateController::createCopyVersion(QString versionName,QString newVersionName)
{
server->slot_sendPacketToAllClients(PacketType::BUSY);
commonClientHandler->slot_sendPacketToAllClients(PacketType::BUSY);
assetManager->createCopyVersion(versionName,newVersionName);
server->slot_sendPacketToAllClients(PacketType::FREE);
commonClientHandler->slot_sendPacketToAllClients(PacketType::FREE);
}
void UpdateController::deleteAssetVersion(QString versionName)
@@ -72,12 +72,12 @@ void UpdateController::showHash()
void UpdateController::calculateFullHash()
{
server->slot_sendPacketToAllClients(PacketType::BUSY);
commonClientHandler->slot_sendPacketToAllClients(PacketType::BUSY);
auto *list = calculateHash(buildPath);
saveHash(buildHashName,list);
calculateSharedHash();
emit sigLogMessage("Calculate hash complete");
server->slot_sendPacketToAllClients(PacketType::FREE);
commonClientHandler->slot_sendPacketToAllClients(PacketType::FREE);
}
void UpdateController::saveHash(QString fileName,QList<FileData> *fileList)
@@ -426,7 +426,7 @@ void UpdateController::calculateSharedHash()
void UpdateController::sendNewVersionList()
{
server->sendNewVersionListToAllClient();
commonClientHandler->sendNewVersionListToAllClient();
}
bool UpdateController::checkDuplicate(QString versionName)

View File

@@ -19,6 +19,7 @@ class DataParser;
class ClientHandler;
class AssetsManager;
class ServerLMSWidget;
class CommonClientHandler;
class UpdateController : public QObject
{
@@ -27,7 +28,7 @@ class UpdateController : public QObject
public:
explicit UpdateController(QObject *parent = 0);
void initialize(ServerLMSWidget* server,DataParser *dataParser,AssetsManager *assetManager);
void initialize(CommonClientHandler* commonClientHandler,DataParser *dataParser,AssetsManager *assetManager);
void compareFiles(ClientHandler* handler, QByteArray array);
void showHash();
void calculateFullHash();
@@ -65,7 +66,7 @@ private:
QString buildPath;
QString currentStreamingPath;
QString sharedDataPath;
ServerLMSWidget *server;
CommonClientHandler *commonClientHandler;
DataParser *dataParser;
AssetsManager *assetManager;
quint64 sizeToSend;

View File

@@ -56,16 +56,19 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) :
processingSystem = new ProcessingSystem(providerDBLMS);
processingSystem->moveToThread(updateThread);
dataParser = new DataParser(assetsManager,processingSystem);
updateController = new UpdateController;
updateController->moveToThread(updateThread);
commonClientHandler = new CommonClientHandler;
loggerThread->start();
updateThread->start();
processingSystem->initialize(dataParser,this);
commonClientHandler->initialize(&clientsMap,processingSystem,dataParser,logger);
processingSystem->initialize(this,dataParser,commonClientHandler,logger);
logger->setTypeLog("widget");
@@ -76,7 +79,7 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) :
connect(this,&ServerLMSWidget::sigLog,logger,&Logger::addTextToLogger,Qt::AutoConnection);
connect(this,&ServerLMSWidget::sigCalculateFullHash,updateController,&UpdateController::calculateFullHash,Qt::AutoConnection);
emit sigUpdateController(this,dataParser,assetsManager);
emit sigUpdateController(commonClientHandler,dataParser,assetsManager);
on_btnStartServer_clicked();
first = true;
@@ -97,30 +100,6 @@ void ServerLMSWidget::autorizationHandler(QString login)
}
}
void ServerLMSWidget::sendNewVersionListToAllClient() //Выделить в сервис отправки сервера(не конкретный клиент)
{
foreach(int idSocket,clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendVersionList();
}
}
void ServerLMSWidget::sendCurrentVersionToAllClient() //Выделить в сервис отправки сервера(не конкретный клиент)
{
foreach(int idSocket,clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendVersion();
}
}
ServerLMSWidget::~ServerLMSWidget()
{
stopServer();
@@ -226,33 +205,6 @@ void ServerLMSWidget::slot_BlockAutorization(bool block)
unBlockAutorization();
}
void ServerLMSWidget::slot_AuthChanged() //Выделить в сервис отправки сервера(не конкретный клиент)
{
//Проходим все открытые сокеты
foreach(int idSocket, clientsMap.keys())
{
//Проверяем, есть ли клиенты TYPE_GUI
if(clientsMap[idSocket]->getClient()->getTypeClient() == TypeClientAutorization::TYPE_GUI)
{//Отправляем этому клиенту обновление списков
ClientQueryToDB queryToDB;
queryToDB.typeQuery = TypeQueryToDB::TYPE_QUERY_GET_ALL_LISTS;
processingSystem->processingClientQueryToDB(clientsMap[idSocket], queryToDB);
}
}
}
void ServerLMSWidget::slot_sendPacketToAllClients(PacketType packetType) //Выделить в сервис отправки сервера(не конкретный клиент)
{
foreach(int idSocket, clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if (!handler->getClient()->getIsLoggedIn()) continue;
handler->sendPacketType(packetType);
}
}
void ServerLMSWidget::removeClient(int idSocket)
{
clientsMap.remove(idSocket);
@@ -272,56 +224,12 @@ void ServerLMSWidget::slot_LanguageChanged(QString language)
}
void ServerLMSWidget::slot_msgToClientFromGUI(QString login, QString text) //Выделить в сервис отправки сервера(не конкретный клиент)
void ServerLMSWidget::on_btnTaskSet_clicked()
{
QString textMsg = text;
QString fullNameClient = ui->listWidget_Clients->currentItem()->text();
QString textTask = ui->comboTasks->currentText();
QByteArray byteArrayMsg = dataParser->xmlAnswer_message(textMsg);
//Проходим все открытые сокеты, ищем нужный
foreach(int idSocket, clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if(handler->getClient()->getLogin() == login)
{//Отправляем ему
handler->sendXmlAnswer(byteArrayMsg);
QString peerAddress = clientsMap[idSocket]->getSocket()->peerAddress().toString();
QString peerPort = QString::number(clientsMap[idSocket]->getSocket()->peerPort());
QString str = "Msg To Client [" + peerAddress + ":" + peerPort + "] : " + textMsg;
logger->addTextToLogger(str);
break;
}
}
}
void ServerLMSWidget::slot_msgToGUIfromClient(QString login, QString text) //Выделить в сервис отправки сервера(не конкретный клиент)
{
QString textMsg = text;
QByteArray byteArrayMsg = dataParser->xmlAnswer_message(textMsg, login);
//Проходим все открытые сокеты, ищем нужный
foreach(int idSocket, clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if(handler->getClient()->getTypeClient() == TypeClientAutorization::TYPE_GUI)
{//Отправляем GUI-клиенту для отображения в Мессенджере
handler->sendXmlAnswer(byteArrayMsg, PacketType::TYPE_XMLANSWER);
QString peerAddress = clientsMap[idSocket]->getSocket()->peerAddress().toString();
QString peerPort = QString::number(clientsMap[idSocket]->getSocket()->peerPort());
QString str = "Msg From Client [" + peerAddress + ":" + peerPort + "] : " + textMsg;
logger->addTextToLogger(str);
break;
}
}
commonClientHandler->slot_sendTaskToClient(fullNameClient,textTask);
}
void ServerLMSWidget::slotAddToLog(QString msg)
@@ -355,30 +263,6 @@ void ServerLMSWidget::on_btnStopServer_clicked()
}
}
void ServerLMSWidget::on_btnTaskSet_clicked() //Выделить в сервис отправки сервера(не конкретный клиент)
{
QString fullNameClient = ui->listWidget_Clients->currentItem()->text();
QString textTask = ui->comboTasks->currentText();
QByteArray byteArrayTask = dataParser->xmlAnswer_task(textTask);
//Проходим все открытые сокеты
foreach(int idSocket, clientsMap.keys())
{
if(clientsMap[idSocket]->getClient()->getFullName() == fullNameClient)
{//Отправляем ему
clientsMap[idSocket]->getSocket()->write(byteArrayTask);
QString peerAddress = clientsMap[idSocket]->getSocket()->peerAddress().toString();
QString peerPort = QString::number(clientsMap[idSocket]->getSocket()->peerPort());
QString str = "Task To Client [" + peerAddress + ":" + peerPort + "] : " + textTask;
logger->addTextToLogger(str);
}
}
}
void ServerLMSWidget::changeEvent(QEvent *event)
{
// В случае получения события изменения языка приложения

View File

@@ -18,6 +18,7 @@
#include <Systems/assetsmanager.h>
#include <Systems/recognizesystem.h>
#include <Systems/logger.h>
#include <Systems/commonclienthandler.h>
#include <Data/typesDataServerClient.h>
#include <Data/Client.h>
@@ -47,8 +48,7 @@ public:
~ServerLMSWidget();
void autorizationHandler(QString login);
void sendNewVersionListToAllClient();
void sendCurrentVersionToAllClient();
protected:
// Метод получения событий
@@ -59,7 +59,7 @@ signals:
void sigRecognize();
void sigLog(QString log);
void sigCalculateFullHash();
void sigUpdateController(ServerLMSWidget* server,DataParser *dataParser,AssetsManager *assetManager);
void sigUpdateController(CommonClientHandler* commonClientHandler,DataParser *dataParser,AssetsManager *assetManager);
QTcpSocket* sigGetSocket();
@@ -68,11 +68,6 @@ public slots:
void addClient(qintptr descriptor, ClientHandler *client);
void slotUpdateListClients();
void slot_BlockAutorization(bool block);
void slot_AuthChanged();
void slot_sendPacketToAllClients(PacketType packetType);
//слот обработки сигнала о готовности нового сообщения на отправку клиенту от мессенджера
void slot_msgToClientFromGUI(QString login, QString text);
void slot_msgToGUIfromClient(QString login, QString text);
void slotAddToLog(QString msg);
public:
@@ -127,6 +122,7 @@ private:
UpdateController *updateController;
AssetsManager *assetsManager;
Logger *logger;
CommonClientHandler *commonClientHandler;
ProviderDBLMS* providerDBLMS;