Files
RRJClient/Core/tcpclient.cpp
2024-09-19 11:11:20 +03:00

126 lines
3.1 KiB
C++

#include "tcpclient.h"
#include "UpdateController.h"
#include "externalexecuter.h"
#include <QDir>
TCPClient::TCPClient(QObject *parent) :
QObject(parent)
{
}
void TCPClient::initialize(RecognizeSystem *recognize,ExternalExecuter *externalExecuter,SendSystem *sendSystem)
{
this->recognizeSystem = recognize;
this->externalExecuter = externalExecuter;
this->sendSystem = sendSystem;
emit sigSendDebugLog(Tools::getTime() + " Client started");
}
void TCPClient::setConnect(ServerSettings *serverSettings,QThread *th)
{
socket = new QTcpSocket();
qDebug() << "TCPCLient thread: " << thread();
if (socket != NULL && socket->state() == QTcpSocket::ConnectedState)
{
emit sigSendDebugLog("already connected");
return;
}
socket->connectToHost(serverSettings->Address,serverSettings->Port.toShort());
connect(socket,&QTcpSocket::readyRead,this,&TCPClient::slotReadyRead,Qt::DirectConnection);
connect(socket,&QTcpSocket::disconnected,this,&TCPClient::setDisconnect);
connect(socket,&QTcpSocket::connected,this,&TCPClient::slotConnectNotify);
connect(this,&TCPClient::sigRecognize,recognizeSystem,&RecognizeSystem::recognize,Qt::DirectConnection);
connect(this,&TCPClient::sigSetSocket,sendSystem,&SendSystem::setSocket);
emit sigSetSocket(socket);
emit sigSendDebugLog("Try connect...");
}
void TCPClient::setDisconnect()
{
socket->disconnect();
emit sigServerDisconnect();
emit sigSendDebugLog("Server disabled");
}
void TCPClient:: waitRead(int time)
{
socket->waitForReadyRead(time);
}
QTcpSocket *TCPClient::getSocket()
{
return socket;
}
void TCPClient::slotSendCommand(QString command)
{
QDataStream stream(socket);
QByteArray data;
stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
if(!command.isEmpty() && socket->state() == QTcpSocket::ConnectedState){
if(command == "check")
{
stream << PacketType::TYPE_COMMAND;
stream << command;
socket->waitForBytesWritten();
sendSystem->sendFileBlock("/" + hashFilename);
emit sigSendDebugLog(Tools::getTime() + " Local checkFile sended");
socket->waitForReadyRead(1000);
}
else if(command == "update"){
emit sigSendDebugLog("Update started");
stream << PacketType::TYPE_COMMAND;
stream << command;
socket->waitForBytesWritten();
}
else if(command == "run"){
externalExecuter->callApp();
}
}else{
emit sigSendDebugLog("WRONG SOCKET AFTER ENTERED");
}
}
void TCPClient::slotConnectNotify()
{
if(socket->state() != QTcpSocket::ConnectedState)
{
emit sigSendDebugLog("Connect invalid");
emit sigConnectionState(false);
return;
}
else
{
emit sigSendDebugLog("Connect complete");
emit sigConnectionState(true);
sendSystem->sendQTConnect();
}
}
void TCPClient::slotReadyRead()
{
if(!socket){
emit sigSendDebugLog("WRONG SOCKET");
return;
}
emit sigRecognize(socket);
}
TCPClient::~TCPClient()
{
}