From 305fcb2fad41b191d77045460ff06af1a251e00f Mon Sep 17 00:00:00 2001 From: semenov Date: Wed, 6 Aug 2025 09:55:14 +0300 Subject: [PATCH] ref: clientMap to MultithreadServer --- ServerLMS/Systems/processingsystem.cpp | 8 +- ServerLMS/Systems/processingsystem.h | 6 +- ServerLMS/clienthandler.cpp | 1 - ServerLMS/multithreadserver.cpp | 108 ++++++++++++++++++++++-- ServerLMS/multithreadserver.h | 38 +++++++-- ServerLMS/serverlmswidget.cpp | 112 +++---------------------- ServerLMS/serverlmswidget.h | 26 +----- 7 files changed, 153 insertions(+), 146 deletions(-) diff --git a/ServerLMS/Systems/processingsystem.cpp b/ServerLMS/Systems/processingsystem.cpp index cc674c0..e9b466e 100644 --- a/ServerLMS/Systems/processingsystem.cpp +++ b/ServerLMS/Systems/processingsystem.cpp @@ -11,8 +11,7 @@ ProcessingSystem::ProcessingSystem(ProviderDBLMS* providerDBLMS, UpdateControlle this->updateController = updateController; } -void ProcessingSystem::initialize(ServerLMSWidget *server, - DataParser *dataParser, +void ProcessingSystem::initialize(MultiThreadServer *server, DataParser *dataParser, CommonClientHandler *commonClientHandler, Logger *logger, UpdateController *updateController, @@ -29,7 +28,6 @@ void ProcessingSystem::initialize(ServerLMSWidget *server, connect(this,&ProcessingSystem::sigStatusTasksAMMofTraineeChanged,commonClientHandler, &CommonClientHandler::slot_StatusTasksAMMofTraineeChanged,Qt::AutoConnection); connect(this,&ProcessingSystem::sigStatusTasksFIMofTraineeChanged,commonClientHandler, &CommonClientHandler::slot_StatusTasksFIMofTraineeChanged,Qt::AutoConnection); - connect(this,&ProcessingSystem::sigUpdateListClients,server, &ServerLMSWidget::slotUpdateListClients,Qt::AutoConnection); connect(this,&ProcessingSystem::sigSetData,updateController,&UpdateController::setDataInfo,Qt::AutoConnection); connect(this,&ProcessingSystem::sigLogMessage,logger,&Logger::addTextToLogger,Qt::QueuedConnection); } @@ -692,9 +690,9 @@ ClientHandler *ProcessingSystem::getUnityClientById(int id) QString login = providerDBLMS->getLoginTraineeById(id); //Проходим все открытые сокеты, ищем нужный - foreach(int idSocket, server->getClientsMap().keys()) + foreach(int idSocket, server->getClientsMap()->keys()) { - ClientHandler *handler = server->getClientsMap().value(idSocket); + ClientHandler *handler = server->getClientsMap()->value(idSocket); if(handler->getClient()->getLogin() == login) { if(handler->getClient()->GETTYPE() == TypeClientAutorization::TYPE_UNITY_CLIENT) diff --git a/ServerLMS/Systems/processingsystem.h b/ServerLMS/Systems/processingsystem.h index 82affed..7e360cd 100644 --- a/ServerLMS/Systems/processingsystem.h +++ b/ServerLMS/Systems/processingsystem.h @@ -6,6 +6,7 @@ #include #include +#include "multithreadserver.h" //#include "instructorsandtraineeswidget.h" #include "chatsystem.h" #include "providerdblms.h" @@ -17,6 +18,7 @@ class Logger; class DataParser; class ClientHandler; class CommonClientHandler; +class MultiThreadServer; class ProcessingSystem : public QObject { @@ -24,7 +26,7 @@ class ProcessingSystem : public QObject public: explicit ProcessingSystem(ProviderDBLMS* providerDBLMS, UpdateController* updateController, QObject *parent = nullptr); - void initialize(ServerLMSWidget *server, + void initialize(MultiThreadServer *server, DataParser* dataParser, CommonClientHandler *commonClientServer, Logger *logger, @@ -66,7 +68,7 @@ signals: private: CommonClientHandler *commonClientServer; - ServerLMSWidget *server; + MultiThreadServer *server; DataParser *dataParser; UpdateController *updateController; ProviderDBLMS* providerDBLMS; diff --git a/ServerLMS/clienthandler.cpp b/ServerLMS/clienthandler.cpp index c01d390..5e1f576 100644 --- a/ServerLMS/clienthandler.cpp +++ b/ServerLMS/clienthandler.cpp @@ -45,7 +45,6 @@ void ClientHandler::initialize(int descriptor,ServerLMSWidget *serverWidget, connect(this,&ClientHandler::sigFolderBlock,sendSystem,&SendSystem::sendFolderBlock,Qt::AutoConnection); connect(this,&ClientHandler::sigGetIsSendStopped,sendSystem,&SendSystem::getIsSendStopped,Qt::AutoConnection); connect(this,&ClientHandler::sigSendDeleteBlock,sendSystem,&SendSystem::sendDeleteBlock,Qt::AutoConnection); - //connect(this,&ClientHandler::sigSendFinish,sendSystem,&SendSystem::sendPacketType,Qt::AutoConnection); connect(this,&ClientHandler::sigSendMessageBlock,sendSystem,&SendSystem::sendMessageBlock,Qt::AutoConnection); connect(this,&ClientHandler::sigNeedUpdate,sendSystem,&SendSystem::sendNeedUpdate,Qt::AutoConnection); connect(this,&ClientHandler::sigSendNotify,sendSystem,&SendSystem::sendNotify,Qt::AutoConnection); diff --git a/ServerLMS/multithreadserver.cpp b/ServerLMS/multithreadserver.cpp index 3e8a5fb..90f68dc 100644 --- a/ServerLMS/multithreadserver.cpp +++ b/ServerLMS/multithreadserver.cpp @@ -1,14 +1,19 @@ #include "multithreadserver.h" -MultiThreadServer::MultiThreadServer(ServerLMSWidget *widget,UpdateController *updateController, - DataParser *dataParser,Logger *logger,QObject *parent): +MultiThreadServer::MultiThreadServer(ServerLMSWidget *widget,UpdateController *updateController,ProcessingSystem *processingSystem, + DataParser *dataParser,Logger *logger,qint16 hostPort, QObject *parent ): QTcpServer(parent), serverLmsWidget(widget), + hostPort(hostPort), + processingSystem(processingSystem), updateController(updateController), dataParser(dataParser), - logger(logger) + logger(logger), + stateServer(stoped), + stateBlockAutorization(unblocked) { connect(this,&MultiThreadServer::sigSendToLogger,logger,&Logger::addTextToLogger); + clientsMap = new QMap; } void MultiThreadServer::incomingConnection(qintptr socketDesriptor) @@ -21,21 +26,94 @@ void MultiThreadServer::incomingConnection(qintptr socketDesriptor) emit sigInitClient(socketDesriptor,serverLmsWidget,updateController,dataParser,logger); disconnect(this,&MultiThreadServer::sigInitClient,newClient,&ClientHandler::initialize); - serverLmsWidget->addClient(socketDesriptor,newClient); + addClient(socketDesriptor,newClient); emit sigSendToLogger("To Client: " + QString(SERVER_HELLO)); } +bool MultiThreadServer::startServer() +{ + if(stateServer == stoped) + { + //connect(tcpServer, &QTcpServer::newConnection, this, &ServerLMSWidget::slotNewConnection,Qt::AutoConnection); + + if(!listen(QHostAddress::Any, hostPort)) + { + logger->addTextToLogger("SERVER: start ERROR"); + stateServer = stoped; + return false; + } + else + { + logger->addTextToLogger("SERVER: start OK"); + stateServer = started; + return true; + } + } + else + return false; +} + +bool MultiThreadServer::stopServer() +{ + if(stateServer == started) + { + disableClients(); + + //Закрываем сервер + close(); + stateServer = stoped; + return true; + } + else + return false; +} + +QMap *MultiThreadServer::getClientsMap() const +{ + return clientsMap; +} + +void MultiThreadServer::updateClientList() +{ + serverLmsWidget->slotUpdateListClients(); +} + +void MultiThreadServer::disableClients() +{ + QByteArray arrayAnswer = dataParser->ClientAnswer()->notify(NOTIFY_SERVER_END); + + //Закрываем все открытые сокеты + foreach(int idSocket, clientsMap->keys()) + { + ClientHandler* handler = (*clientsMap)[idSocket]; + + //Фиксируем время выхода Юнити-клиента + if(handler->getClient()->GETTYPE() == TypeClientAutorization::TYPE_UNITY_CLIENT) + { + processingSystem->processingExitUnityClient(handler); + } + + handler->sigSendXmlAnswer(arrayAnswer, PacketType::TYPE_XMLANSWER); + QString str = QString(arrayAnswer); + processingSystem->processingClientDeAutorization(handler->getClient()->getLogin()); + + handler->sigSocketClose(); + //clientsMap.remove(idSocket); + removeClient(idSocket); + } + +} + void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPort) { - auto clientsMap = serverLmsWidget->getClientsMap(); QString login = ""; qDebug() << peerAddress << " " << peerPort << " " << "disconnect"; //Проходим все открытые сокеты, ищем нужный - foreach(int idSocket, clientsMap.keys()) + foreach(int idSocket, clientsMap->keys()) { - ClientHandler *client = clientsMap[idSocket]; + ClientHandler *client = (*clientsMap)[idSocket]; if(client->getClient()->getAddress() == peerAddress && client->getClient()->getPort() == peerPort) { @@ -43,9 +121,9 @@ void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPo ClientDeAutorization clientDeAutorization; clientDeAutorization.Login = login; - serverLmsWidget->getProcessingSystem()->processingClientDeAutorization(client, clientDeAutorization); + processingSystem->processingClientDeAutorization(client, clientDeAutorization); - serverLmsWidget->removeClient(idSocket); + removeClient(idSocket); delete client; continue; } @@ -59,3 +137,15 @@ void MultiThreadServer::slotDisconnectClient(QString peerAddress, QString peerPo serverLmsWidget->getProcessingSystem()->processingClientDeAutorization(login); } +void MultiThreadServer::removeClient(int idSocket) +{ + clientsMap->remove(idSocket); + serverLmsWidget->slotUpdateListClients(); +} + +void MultiThreadServer::addClient(qintptr descriptor, ClientHandler *client) +{ + (*clientsMap)[descriptor] = client; + serverLmsWidget->slotUpdateListClients(); +} + diff --git a/ServerLMS/multithreadserver.h b/ServerLMS/multithreadserver.h index eb12f80..761b61f 100644 --- a/ServerLMS/multithreadserver.h +++ b/ServerLMS/multithreadserver.h @@ -2,21 +2,40 @@ #define MULTITHREADSERVER_H #include "serverlmswidget.h" - +#include "Systems/processingsystem.h" #include - +class ProcessingSystem; class MultiThreadServer : public QTcpServer { Q_OBJECT public: - MultiThreadServer( ServerLMSWidget *widget,UpdateController *updateController, - DataParser *dataParser,Logger *logger,QObject *parent = nullptr); + MultiThreadServer(ServerLMSWidget *widget,UpdateController *updateController,ProcessingSystem *processingSystem, + DataParser *dataParser,Logger *logger, qint16 hostPort, QObject *parent = nullptr); + + QMap *getClientsMap() const; + void updateClientList(); + void disableClients(); + + bool startServer(); + bool stopServer(); + void blockAutorization() + { + stateBlockAutorization = blocked; + } + void unBlockAutorization() + { + stateBlockAutorization = unblocked; + } + EStateBlockAutorization getStateBlockAutorization() const + { + return stateBlockAutorization; + } signals: void sigInitClient(int descriptor, ServerLMSWidget *serverWidget, - UpdateController *updateController, DataParser *dataParser,Logger *logger); + UpdateController *updateController, DataParser *dataParser,Logger *logger); void signalStopSendFile(); void sigSendToLogger(QString text); @@ -27,9 +46,18 @@ protected: private: ServerLMSWidget *serverLmsWidget; + QMap *clientsMap; + qint16 hostPort; + ProcessingSystem *processingSystem; UpdateController *updateController; DataParser *dataParser; Logger *logger; + + EStateServer stateServer; + EStateBlockAutorization stateBlockAutorization; + + void removeClient(int idSocket); + void addClient(qintptr descriptor, ClientHandler *client); }; #endif // MULTITHREADSERVER_H diff --git a/ServerLMS/serverlmswidget.cpp b/ServerLMS/serverlmswidget.cpp index c636804..f083b1b 100644 --- a/ServerLMS/serverlmswidget.cpp +++ b/ServerLMS/serverlmswidget.cpp @@ -11,10 +11,7 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ServerLMSWidget), - tcpServer(nullptr), - hostPort(6000), - stateServer(stoped), - stateBlockAutorization(unblocked), + server(nullptr), updateThread(nullptr), loggerThread(nullptr), dataParser(nullptr), @@ -63,13 +60,14 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) : dataParser = new DataParser(assetsManager,processingSystem); commonClientHandler = new CommonClientHandler; + server = new MultiThreadServer(this,updateController,processingSystem,dataParser,logger,6000); loggerThread->start(); updateThread->start(); - commonClientHandler->initialize(&clientsMap,processingSystem,dataParser,logger); - processingSystem->initialize(this,dataParser,commonClientHandler,logger,updateController,chatSystem); - chatSystem->initialize(commonClientHandler,dataParser,&clientsMap); + commonClientHandler->initialize(server->getClientsMap(),processingSystem,dataParser,logger); + processingSystem->initialize(server,dataParser,commonClientHandler,logger,updateController,chatSystem); + chatSystem->initialize(commonClientHandler,dataParser,server->getClientsMap()); logger->setTypeLog("widget"); connect(dataParser,&DataParser::sigLogMessage,logger,&Logger::addTextToLogger); @@ -93,127 +91,43 @@ void ServerLMSWidget::setError(int code) ServerLMSWidget::~ServerLMSWidget() { - stopServer(); + server->stopServer(); updateThread->exit(); loggerThread->exit(); delete ui; } -bool ServerLMSWidget::startServer() -{ - if(stateServer == stoped) - { - tcpServer = new MultiThreadServer(this,updateController,dataParser,logger); - //connect(tcpServer, &QTcpServer::newConnection, this, &ServerLMSWidget::slotNewConnection,Qt::AutoConnection); - - if(!tcpServer->listen(QHostAddress::Any, hostPort)) - { - logger->addTextToLogger("SERVER: start ERROR"); - stateServer = stoped; - return false; - } - else - { - logger->addTextToLogger("SERVER: start OK"); - stateServer = started; - return true; - } - } - else - return false; -} - -bool ServerLMSWidget::stopServer() -{ - if(stateServer == started) - { - QByteArray arrayAnswer = dataParser->ClientAnswer()->notify(NOTIFY_SERVER_END); - - //Закрываем все открытые сокеты - foreach(int idSocket, clientsMap.keys()) - { - ClientHandler* clientHandlerOpen = clientsMap[idSocket]; - - //Фиксируем время выхода Юнити-клиента - if(clientHandlerOpen->getClient()->GETTYPE() == TypeClientAutorization::TYPE_UNITY_CLIENT) - { - processingSystem->processingExitUnityClient(clientHandlerOpen); - } - - clientHandlerOpen->sigSendXmlAnswer(arrayAnswer, PacketType::TYPE_XMLANSWER); - //while (!clientsMap[idSocket]->sigSocketFlush()) {} - - QString str = QString(arrayAnswer); - emit sigLog("To Client: " + str); - - //slotDisconnectClient(clientsMap[idSocket]->get, QString peerPort) - processingSystem->processingClientDeAutorization(clientHandlerOpen->getClient()->getLogin()); - - clientHandlerOpen->sigSocketClose(); - //clientsMap.remove(idSocket); - removeClient(idSocket); - } - - //Закрываем сервер - tcpServer->close(); - stateServer = stoped; - delete tcpServer; - emit sigLog("Server is stopped"); - return true; - } - else - return false; -} Logger *ServerLMSWidget::getLogger() const { return logger; } -QMap ServerLMSWidget::getClientsMap() const -{ - return clientsMap; -} - void ServerLMSWidget::slotUpdateListClients() { //Очищаем список ui->listWidget_Clients->clear(); - //Проходим все открытые сокеты - foreach(int idSocket, clientsMap.keys()) + for (const ClientHandler *handler : server->getClientsMap()->values()) { - //Добавляем в список Клиентов - QString strClient = clientsMap[idSocket]->getClient()->getFullName(); + QString strClient = handler->getClient()->getFullName(); ui->listWidget_Clients->addItem(strClient); ui->listWidget_Clients->scrollToBottom(); ui->listWidget_Clients->setCurrentRow(ui->listWidget_Clients->count() - 1); } - int countClients = clientsMap.count(); + int countClients = (*server->getClientsMap()).count(); emit sigLog("SERVER: countClients = " + QString::number(countClients)); } void ServerLMSWidget::slot_BlockAutorization(bool block) { if(block) - blockAutorization(); + server->blockAutorization(); else - unBlockAutorization(); -} - -void ServerLMSWidget::removeClient(int idSocket) -{ - clientsMap.remove(idSocket); - slotUpdateListClients(); -} - -void ServerLMSWidget::addClient(qintptr descriptor, ClientHandler *client) -{ - clientsMap[descriptor] = client; - slotUpdateListClients(); + server->unBlockAutorization(); } void ServerLMSWidget::slot_LanguageChanged(QString language) @@ -230,7 +144,7 @@ void ServerLMSWidget::slotAddToLog(QString msg) void ServerLMSWidget::on_btnStartServer_clicked() { - if(startServer()) + if(server->startServer()) { QApplication::setOverrideCursor(Qt::WaitCursor); QApplication::restoreOverrideCursor(); @@ -243,7 +157,7 @@ void ServerLMSWidget::on_btnStartServer_clicked() void ServerLMSWidget::on_btnStopServer_clicked() { - if(stopServer()) + if(server->stopServer()) { ui->btnStopServer->setEnabled(false); ui->btnStartServer->setEnabled(true); diff --git a/ServerLMS/serverlmswidget.h b/ServerLMS/serverlmswidget.h index e3ac3ab..cae2965 100644 --- a/ServerLMS/serverlmswidget.h +++ b/ServerLMS/serverlmswidget.h @@ -66,7 +66,6 @@ signals: public slots: void slot_LanguageChanged(QString language); - void addClient(qintptr descriptor, ClientHandler *client); void slotUpdateListClients(); void slot_BlockAutorization(bool block); void slotAddToLog(QString msg); @@ -74,50 +73,27 @@ public slots: public: void removeClient(int socketId); - void blockAutorization() - { - stateBlockAutorization = blocked; - } - void unBlockAutorization() - { - stateBlockAutorization = unblocked; - } int hasError() const { return errorCode; } - EStateBlockAutorization getStateBlockAutorization() const - { - return stateBlockAutorization; - } ProcessingSystem* getProcessingSystem() { return processingSystem; } - QMap getClientsMap() const; - Logger *getLogger() const; private slots: void on_btnStartServer_clicked(); void on_btnStopServer_clicked(); -private: - bool startServer(); - bool stopServer(); - private: Ui::ServerLMSWidget *ui; private: - MultiThreadServer * tcpServer; - qint16 hostPort; - QMap clientsMap; - EStateServer stateServer; - EStateBlockAutorization stateBlockAutorization; - + MultiThreadServer *server; QThread *updateThread; QThread *loggerThread;