До трансляции message на ГУИ от Клиента

This commit is contained in:
krivoshein
2024-12-16 17:47:33 +03:00
parent b4e30429f7
commit e393244bf7
60 changed files with 877 additions and 551 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2024-12-13T16:41:17. -->
<!-- Written by QtCreator 4.11.1, 2024-12-16T17:46:50. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@@ -70,6 +70,28 @@ void DataParser::xmlParser(ClientHandler *client, QByteArray array)
processingSystem->processingClientDeAutorization(client, clientDeAutorization);
}
else if(xmlReader.name() == "ToClientMessage")
{//Отправка сообщения Клиенту
ToClientMessage toClientMessage;
/*Перебираем все атрибуты тега*/
foreach(const QXmlStreamAttribute &attr, xmlReader.attributes())
{
QString name = attr.name().toString();
QString value = attr.value().toString();
//addTextToLogger(name + ": " + value);
if(name == "id")
toClientMessage.id = value.toInt();
else if(name == "Text")
toClientMessage.Text = value;
else if(name == "Login")
toClientMessage.Login = value;
}
processingSystem->processingToClientMessage(client, toClientMessage);
}
else if(xmlReader.name() == "QueryToDB")
{//Запрос к базе данных от клиента
@@ -182,7 +204,7 @@ void DataParser::xmlParser(ClientHandler *client, QByteArray array)
clientMessage.Text = value;
}
processingSystem->processingClientMessage(client, clientMessage);
processingSystem->processingFromClientMessage(client, clientMessage);
}
else if(xmlReader.name() == "ClientNotify")
{//Уведомление от клиента
@@ -369,80 +391,6 @@ QByteArray DataParser::xmlAnswer_deAuthorization(bool result, QString login)
return xmlAnswer(listTag);
}
QByteArray DataParser::xmlAnswer_ClientQueryToDB(bool result, QList<Instructor>* listInstructors,
QList<Trainee>* listTrainees, QList<Group>* listGroups)
{
QDomDocument groupsTraineesDOM;
QFile blankFile(":/blankXML/groupsTrainees.xml");
if (! blankFile.open(QFile::ReadOnly | QFile::Text)) {
qDebug() << "SaveTraineesGroupsXML: Не удалось считать файл :/blankXML/groupsTrainees.xml";
return QByteArray();
}
groupsTraineesDOM.setContent(blankFile.readAll());
blankFile.close();
QDomNode allListsNode = groupsTraineesDOM.namedItem("AllLists");
QDomNode groupsTraineesNode = allListsNode.firstChildElement("GroupsTrainees");
QDomNode allInstructorsNode = allListsNode.firstChildElement("Instructors");
for(Group group : *listGroups)
{
//Группа
QDomNode groupNode = groupsTraineesDOM.createElement("group");
groupsTraineesNode.appendChild(groupNode);
groupNode.toElement().setAttribute("group_id", group.getID());
groupNode.toElement().setAttribute("name", group.getName());
//Обучаемые
for(Trainee trainee : *listTrainees)
{
if(group.getID() != trainee.getGroup().getID())
continue;
QDomNode traineeNode = groupsTraineesDOM.createElement("trainee");
groupNode.appendChild(traineeNode);
traineeNode.toElement().setAttribute("trainee_id", QString::number(trainee.getID()));
traineeNode.toElement().setAttribute("name", trainee.getName());
traineeNode.toElement().setAttribute("login", trainee.getLogin());
traineeNode.toElement().setAttribute("password", trainee.getPassword());
traineeNode.toElement().setAttribute("archived", trainee.getArchived() ? QStringLiteral("true") : QStringLiteral("false"));
traineeNode.toElement().setAttribute("logged_in", trainee.getLoggedIn() ? QStringLiteral("true") : QStringLiteral("false"));
traineeNode.toElement().setAttribute("group_trainee", QString::number(trainee.getGroup().getID()));
traineeNode.toElement().setAttribute("computer_trainee", QString::number(trainee.getComputer().getID()));
}
}
for(Instructor instructor : *listInstructors)
{
//Инструктор
QDomNode instructorNode = groupsTraineesDOM.createElement("instructor");
allInstructorsNode.appendChild(instructorNode);
instructorNode.toElement().setAttribute("instructor_id", QString::number(instructor.getID()));
instructorNode.toElement().setAttribute("name", instructor.getName());
instructorNode.toElement().setAttribute("login", instructor.getLogin());
instructorNode.toElement().setAttribute("password", instructor.getPassword());
instructorNode.toElement().setAttribute("is_admin", instructor.getIsAdmin() ? QStringLiteral("true") : QStringLiteral("false"));
instructorNode.toElement().setAttribute("archived", instructor.getArchived() ? QStringLiteral("true") : QStringLiteral("false"));
instructorNode.toElement().setAttribute("logged_in", instructor.getLoggedIn() ? QStringLiteral("true") : QStringLiteral("false"));
}
QString xmlFileName = /*appDirPath +*/ "GroupsTrainees.xml";
QFile xmlOutFile(xmlFileName);
if (!xmlOutFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "SaveTraineesGroupsXML: Не удалось записать файл " + xmlFileName;
return QByteArray();
}
QTextStream outFile(&xmlOutFile);
groupsTraineesDOM.save(outFile, 4);
xmlOutFile.close();
return groupsTraineesDOM.toByteArray();
}
bool DataParser::loadBlankXML(QString nameFile, QDomDocument *commonDOM)
{
QFile blankFile(":/blankXML/" + nameFile);

View File

@@ -29,9 +29,6 @@ public:
QByteArray xmlAnswer_authorization(bool result, QString instructorName, QString clientName, QString accessType, QString login);
QByteArray xmlAnswer_deAuthorization(bool result, QString login);
QByteArray xmlAnswer_ClientQueryToDB(bool result, QList<Instructor>* listInstructors = nullptr,
QList<Trainee>* listTrainees = nullptr, QList<Group>* listGroups = nullptr);
bool loadBlankXML(QString nameFile, QDomDocument* commonDOM);
bool saveDOMtoXML(QString nameFile, QDomDocument* commonDOM);
QByteArray xmlAnswer_ClientQueryToDB_ListInstructors(bool result, QList<Instructor>* listInstructors);

View File

@@ -119,12 +119,6 @@ void ProcessingSystem::processingClientQueryToDB(ClientHandler *client, ClientQu
switch (clientQueryToDB.typeQuery)
{
case TypeQueryToDB::TYPE_QUERY_GET_LIST_INSTRUCTORS:
{
QList<Instructor> listInstructors = providerDBLMS->GetListAllInstructors();
arrayAnswer = dataParser->xmlAnswer_ClientQueryToDB(true, &listInstructors);
break;
}
case TypeQueryToDB::TYPE_QUERY_GET_ALL_LISTS:
{
QList<Instructor> listInstructors = providerDBLMS->GetListAllInstructors();
@@ -223,7 +217,12 @@ void ProcessingSystem::processingClientQueryToDB(ClientHandler *client, ClientQu
//logger->addTextToLogger("To Client: " + str);
}
void ProcessingSystem::processingClientMessage(ClientHandler *client, ClientMessage clientMessage)
void ProcessingSystem::processingToClientMessage(ClientHandler *client, ToClientMessage toClientMessage)
{
signal_msgToClientReady(toClientMessage.Login, toClientMessage.Text);
}
void ProcessingSystem::processingFromClientMessage(ClientHandler *client, ClientMessage clientMessage)
{
QString peerAddress = client->getSocket()->peerAddress().toString();
QString peerPort = QString::number(client->getSocket()->peerPort());
@@ -231,7 +230,9 @@ void ProcessingSystem::processingClientMessage(ClientHandler *client, ClientMess
QString str = "Msg From Client [" + peerAddress + ":" + peerPort + "] : " + clientMessage.Text;
emit sigLogMessage(str);
emit sigAddToMessanger(client->getClient()->getLogin(), clientMessage.Text);
//Здесь нужно оттранслировать на ГУИ!
//emit sigAddToMessanger(client->getClient()->getLogin(), clientMessage.Text);
}
void ProcessingSystem::processingClientNotify(ClientHandler *client, ClientNotify clientNotify)

View File

@@ -26,7 +26,9 @@ public:
void processingClientAutorization(ClientHandler *client, ClientAutorization clientAutorization);
void processingClientDeAutorization(ClientHandler *client, ClientDeAutorization clientDeAutorization);
void processingClientQueryToDB(ClientHandler *client, ClientQueryToDB clientQueryToDB, int id = 0, void* data = nullptr);
void processingClientMessage(ClientHandler *client, ClientMessage clientMessage);
void processingToClientMessage(ClientHandler *client, ToClientMessage toClientMessage);
void processingFromClientMessage(ClientHandler *client, ClientMessage clientMessage);
void processingClientNotify(ClientHandler *client, ClientNotify clientNotify);
signals:
@@ -34,6 +36,7 @@ signals:
void sigAuthChanged();
void sigLogMessage(QString log);
void sigAddToMessanger(QString login,QString text);
void signal_msgToClientReady(QString login, QString text);
private:
ServerLMSWidget *server;

View File

@@ -72,6 +72,7 @@ ServerLMSWidget::ServerLMSWidget(QWidget *parent) :
connect(this,&ServerLMSWidget::sigLog,logger,&Logger::addTextToLogger);
connect(processingSystem,&ProcessingSystem::sigAuthChanged,this, &ServerLMSWidget::slot_AuthChanged);
connect(processingSystem,&ProcessingSystem::signal_msgToClientReady,this, &ServerLMSWidget::slot_msgToClientFromGUI);
on_btnStartServer_clicked();
@@ -227,7 +228,7 @@ void ServerLMSWidget::slot_LanguageChanged(QString language)
}
void ServerLMSWidget::slot_msgToClientReady(QString login, QString text)
void ServerLMSWidget::slot_msgToClientFromGUI(QString login, QString text)
{
QString textMsg = text;
@@ -253,6 +254,32 @@ void ServerLMSWidget::slot_msgToClientReady(QString login, QString text)
}
}
void ServerLMSWidget::slot_msgToGUIfromClient(QString login, QString text)
{
QString textMsg = text;
QByteArray byteArrayMsg = dataParser->xmlAnswer_message(textMsg);
//Проходим все открытые сокеты, ищем нужный
foreach(int idSocket, clientsMap.keys())
{
ClientHandler *handler = clientsMap[idSocket];
if(handler->getClient()->getTypeClient() == TypeClientAutorization::TYPE_GUI)
{//Отправляем GUI-клиенту для отображения в Мессенджере
handler->sendXmlAnswer(byteArrayMsg);
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;
}
}
}
void ServerLMSWidget::slotAddToLog(QString msg)
{
ui->listWidgetLogger->addItem(msg);

View File

@@ -63,7 +63,8 @@ public slots:
private slots:
//слот обработки сигнала о готовности нового сообщения на отправку клиенту от мессенджера
void slot_msgToClientReady(QString login, QString text);
void slot_msgToClientFromGUI(QString login, QString text);
void slot_msgToGUIfromClient(QString login, QString text);
void slotAddToLog(QString msg);
public:

View File

@@ -57,7 +57,6 @@ public:
};
enum TypeQueryToDB{
TYPE_QUERY_GET_LIST_INSTRUCTORS,
TYPE_QUERY_GET_ALL_LISTS,
TYPE_QUERY_NEW_INSTRUCTOR,
TYPE_QUERY_DEL_INSTRUCTOR,
@@ -87,6 +86,14 @@ public:
QString Text;
};
class ToClientMessage
{
public:
int id;
QString Login;
QString Text;
};
class ServerTask
{
public: