mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJClient.git
synced 2026-03-28 05:25:39 +03:00
321 lines
10 KiB
C++
321 lines
10 KiB
C++
#include "Core/recognizesystem.h"
|
||
#include <QMessageBox>
|
||
|
||
RecognizeSystem::RecognizeSystem(QObject *parent):
|
||
QObject(parent),
|
||
versionContainer(nullptr),
|
||
dataParser(nullptr),
|
||
postProcessorSystem(nullptr),
|
||
client(nullptr)
|
||
{
|
||
packetType = PacketType::TYPE_NONE;
|
||
filePath = "";
|
||
fileSize = 0;
|
||
sizeReceiveData = 0;
|
||
tmpBlock.clear();
|
||
countSend = 0;
|
||
}
|
||
|
||
void RecognizeSystem::initialize(DataParser *dataParser, VersionContainer *versionContainer,PostProcessorSystem *postProcessorSystem,Client *client)
|
||
{
|
||
this->versionContainer = versionContainer;
|
||
this->dataParser = dataParser;
|
||
this->postProcessorSystem = postProcessorSystem;
|
||
this->client = client;
|
||
}
|
||
|
||
void RecognizeSystem::recognize(QTcpSocket *socket)
|
||
{
|
||
//qDebug() << "RecognizeThreadId " << QThread::currentThreadId();
|
||
|
||
QDataStream stream(socket);
|
||
stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
|
||
|
||
while(socket->bytesAvailable())
|
||
{
|
||
if (socket->state() != QTcpSocket::ConnectedState)
|
||
{
|
||
qDebug() << "RecognizeSystem::recognize socket->state() != QTcpSocket::ConnectedState";
|
||
return;
|
||
}
|
||
|
||
/*
|
||
if(!socket->waitForReadyRead(TCP_READ_TIMEOUT))
|
||
{
|
||
continue;
|
||
}
|
||
*/
|
||
|
||
switch ((int)packetType)
|
||
{
|
||
case PacketType::TYPE_NONE: //определение первичного пакета
|
||
{
|
||
stream.startTransaction();
|
||
stream >> packetType;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
socket->waitForReadyRead(TCP_READ_TIMEOUT);
|
||
continue;
|
||
}
|
||
//qDebug() << Tools::GetTime() << "CLIENT: type: " << packetType;
|
||
}
|
||
continue;
|
||
|
||
case PacketType::TYPE_FOLDER: //создание папок
|
||
{
|
||
stream.startTransaction();
|
||
stream >> filePath;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
continue;
|
||
}
|
||
|
||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||
|
||
QDir dir(filePath);
|
||
if(!dir.exists())
|
||
{
|
||
if(dir.mkpath(filePath))
|
||
{
|
||
qDebug() << "Dir Created: " << filePath;
|
||
}
|
||
}
|
||
|
||
emit sigUpdateBytesAvailable();
|
||
}
|
||
break;
|
||
|
||
case PacketType::TYPE_FILE: //загрузка файлов
|
||
{
|
||
//ПОЛУЧЕНИЕ ПУТИ
|
||
//ПОЛУЧЕНИЕ РАЗМЕРА ФАЙЛА
|
||
forever
|
||
{
|
||
stream.startTransaction();
|
||
stream >> filePath;
|
||
stream >> fileSize;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
emit sigSendDebugLog(Tools::getTime() + "CLIENT: filePath, fileSize - FAIL commitTransaction");
|
||
continue;
|
||
}
|
||
|
||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||
|
||
emit sigSendDebugLog("CLIENT: filesize: " + QString::number(fileSize));
|
||
emit sigSendDebugLog("CLIENT: filePath: " + filePath);
|
||
|
||
//socket->waitForReadyRead(100);
|
||
break;
|
||
}
|
||
|
||
//ПОЛУЧЕНИЕ САМОГО ФАЙЛА
|
||
emit sigSendDebugLog(Tools::getTime() + "AfterRead size and path BytesAvailable: " + socket->bytesAvailable());
|
||
|
||
//УКАЗАНИЕ ПУТИ ФАЙЛА
|
||
QFile file(filePath);
|
||
|
||
if (file.exists())
|
||
{
|
||
file.remove(); //удаление файла, если он уже есть, но необходимо обновить
|
||
emit sigSendDebugLog(Tools::getTime() + "Delete exist file: " + filePath);
|
||
|
||
//socket->waitForReadyRead(100);
|
||
}
|
||
|
||
tmpBlock.clear();
|
||
sizeReceiveData = 0;
|
||
countSend = 0;
|
||
|
||
file.open(QFile::WriteOnly);
|
||
|
||
if(! file.isOpen())
|
||
QMessageBox::critical(nullptr, "P1", "isOpen false. File: " + file.fileName());
|
||
|
||
if(! file.isWritable())
|
||
QMessageBox::critical(nullptr, "P2", "isWritable false. File: " + file.fileName());
|
||
|
||
while(true)
|
||
{
|
||
if(socket->bytesAvailable() <= 0)
|
||
{
|
||
socket->waitForReadyRead(TCP_READ_TIMEOUT);
|
||
continue;
|
||
}
|
||
|
||
if(fileSize - sizeReceiveData >= BLOCK_SIZE)
|
||
tmpBlock = socket->read(BLOCK_SIZE);
|
||
else
|
||
tmpBlock = socket->read(fileSize - sizeReceiveData);
|
||
|
||
qint64 bytesReceived = tmpBlock.length();
|
||
|
||
if(bytesReceived <= 0)
|
||
{
|
||
QMessageBox::critical(nullptr, "P3", "bytesReceived <= 0. File: " + file.fileName());
|
||
continue;
|
||
}
|
||
|
||
sizeReceiveData += bytesReceived;
|
||
countSend++;
|
||
|
||
qint64 toFile = file.write(tmpBlock);
|
||
|
||
if(toFile <= 0)
|
||
{
|
||
//emit sigUpdateBytesAvailable();
|
||
QMessageBox::critical(nullptr, "P4", "write toFile <= 0. File: " + file.fileName());
|
||
continue;
|
||
}
|
||
|
||
tmpBlock.clear();
|
||
|
||
if(sizeReceiveData == fileSize)
|
||
{
|
||
emit sigSendDebugLog(Tools::getTime() + "FINAL Count send: " + QString::number(countSend));
|
||
emit sigSendDebugLog(Tools::getTime() + "FINAL Size received: " + QString::number(sizeReceiveData));
|
||
emit sigSendDebugLog(Tools::getTime() + "FINAL File size" + QString::number(fileSize));
|
||
emit sigUpdateBytesAvailable();
|
||
break;
|
||
}
|
||
else if(sizeReceiveData > fileSize)
|
||
{
|
||
QMessageBox::critical(nullptr, "P5", "sizeReceiveData > fileSize");
|
||
}
|
||
}
|
||
|
||
file.close();
|
||
emit sigSendDebugLog(Tools::getTime() + "File loaded");
|
||
|
||
//ОЧИСТКА ПОСЛЕ ПЕРЕДАЧИ
|
||
|
||
filePath.clear();
|
||
fileSize = 0;
|
||
tmpBlock.clear();
|
||
sizeReceiveData = 0;
|
||
countSend = 0;
|
||
}
|
||
break;
|
||
|
||
case PacketType::TYPE_DELETE: //удаление лишних файлов (рекурсивно удаляет все содежимое)
|
||
{
|
||
stream.startTransaction();
|
||
stream >> filePath;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
continue;
|
||
}
|
||
|
||
filePath = Tools::createReceiveFullPath(filePath,versionContainer->getServerVersionData());
|
||
|
||
QFileInfo fileInfo(filePath);
|
||
|
||
if(fileInfo.exists())
|
||
{
|
||
if(fileInfo.isFile())
|
||
{
|
||
QFile file(filePath);
|
||
file.remove();
|
||
}
|
||
|
||
if(fileInfo.isDir())
|
||
{
|
||
QDir dir(filePath);
|
||
dir.removeRecursively();
|
||
}
|
||
|
||
qDebug() << Tools::getTime() << "Deleted: " << filePath;
|
||
}
|
||
}
|
||
break;
|
||
|
||
case PacketType::TYPE_NEEDUPDATE: //нужно обновление
|
||
{
|
||
bool flag = false;
|
||
quint64 size = 0;
|
||
quint64 fileCount = 0;
|
||
quint64 fileDelete = 0;
|
||
|
||
stream.startTransaction();
|
||
stream >> flag;
|
||
stream >> size;
|
||
stream >> fileCount;
|
||
stream >> fileDelete;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
continue;
|
||
}
|
||
|
||
emit sigNeedUpdate(flag,size,fileCount,fileDelete);
|
||
}
|
||
break;
|
||
|
||
case PacketType::TYPE_XMLANSWER: //ответы формата XML
|
||
{
|
||
QByteArray array;
|
||
stream.startTransaction();
|
||
stream >> array;
|
||
|
||
if(!stream.commitTransaction())
|
||
{
|
||
continue;
|
||
}
|
||
|
||
dataParser->xmlParser(array);
|
||
}
|
||
break;
|
||
|
||
case PacketType::HASH_READY:
|
||
{
|
||
emit sigCheckUpdate();
|
||
}
|
||
break;
|
||
|
||
case PacketType::RECALCULATE_HASH:
|
||
{
|
||
emit sigdRecalculateHashOnServerState();
|
||
}
|
||
break;
|
||
|
||
case PacketType::BUSY:
|
||
{
|
||
emit sigAnimationActivated(true);
|
||
}
|
||
break;
|
||
|
||
case PacketType::FREE:
|
||
{
|
||
emit sigAnimationActivated(false);
|
||
}
|
||
break;
|
||
|
||
case PacketType::TYPE_XMLANSWER_DOCS_CHANGED: //на случай общего обновления
|
||
{
|
||
if (client->getIsLoggedIn())
|
||
{
|
||
emit sigSendPacketType(PacketType::GET_DOCS);
|
||
}
|
||
}
|
||
break;
|
||
|
||
case PacketType::UPDATE_FILE_COMPLETE:
|
||
{
|
||
postProcessorSystem->calculateCommonHash();
|
||
emit sigLoadComplete();
|
||
emit sigSendPacketType(PacketType::GET_DOCS);
|
||
}
|
||
break;
|
||
|
||
default:
|
||
qCritical() << "RecognizeSystem::recognize packetType unknown!";
|
||
}
|
||
|
||
packetType = PacketType::TYPE_NONE;
|
||
}
|
||
}
|