2024-05-24 15:28:05 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2024 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "competitivecompenionhandler.h"
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2024-05-24 23:20:11 +08:00
|
|
|
#include <QTcpServer>
|
2024-05-24 15:28:05 +08:00
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QTextDocument>
|
|
|
|
#include "ojproblemset.h"
|
|
|
|
#include "../settings.h"
|
|
|
|
#include "../mainwindow.h"
|
|
|
|
|
|
|
|
CompetitiveCompanionHandler::CompetitiveCompanionHandler(QObject *parent):
|
2024-05-24 17:53:39 +08:00
|
|
|
QObject(parent),
|
|
|
|
mThread(nullptr)
|
2024-05-24 15:28:05 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompetitiveCompanionHandler::start()
|
|
|
|
{
|
2024-05-24 17:53:39 +08:00
|
|
|
if (!pSettings->executor().enableProblemSet())
|
|
|
|
return;
|
|
|
|
if (!pSettings->executor().enableCompetitiveCompanion())
|
|
|
|
return;
|
|
|
|
mThread = new CompetitiveCompanionThread(this);
|
2024-05-24 23:20:11 +08:00
|
|
|
connect(mThread,
|
2024-05-24 17:53:39 +08:00
|
|
|
&CompetitiveCompanionThread::newProblemReceived,
|
|
|
|
this, &CompetitiveCompanionHandler::onNewProblemReceived);
|
2024-05-24 23:20:11 +08:00
|
|
|
mThread->start();
|
|
|
|
if (!mThread->waitStart()) {
|
|
|
|
qDebug()<<"Failed to listen!";
|
|
|
|
delete mThread;
|
|
|
|
mThread = nullptr;
|
2024-05-24 15:28:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompetitiveCompanionHandler::stop()
|
|
|
|
{
|
2024-05-24 17:53:39 +08:00
|
|
|
if (!mThread)
|
|
|
|
return;
|
|
|
|
connect(mThread, &QThread::finished,
|
|
|
|
mThread, &QObject::deleteLater);
|
|
|
|
mThread->stop();
|
|
|
|
mThread=nullptr;
|
2024-05-24 15:28:05 +08:00
|
|
|
}
|
|
|
|
|
2024-05-24 17:53:39 +08:00
|
|
|
void CompetitiveCompanionHandler::onNewProblemReceived(int num, int total, POJProblem newProblem)
|
2024-05-24 15:28:05 +08:00
|
|
|
{
|
2024-05-24 17:53:39 +08:00
|
|
|
emit newProblemReceived(num, total, newProblem);
|
|
|
|
}
|
2024-05-24 15:28:05 +08:00
|
|
|
|
2024-05-24 23:20:11 +08:00
|
|
|
void CompetitiveCompanionThread::onNewProblemConnection(QTcpSocket* clientConnection)
|
2024-05-24 17:53:39 +08:00
|
|
|
{
|
2024-05-24 15:28:05 +08:00
|
|
|
QByteArray content;
|
|
|
|
int unreadCount = 0;
|
|
|
|
while (clientConnection->state() == QTcpSocket::ConnectedState) {
|
|
|
|
clientConnection->waitForReadyRead(100);
|
|
|
|
QByteArray readed = clientConnection->readAll();
|
|
|
|
if (readed.isEmpty()) {
|
|
|
|
unreadCount ++;
|
|
|
|
if (!content.isEmpty() || unreadCount>30)
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
unreadCount = 0;
|
|
|
|
}
|
|
|
|
content += readed;
|
|
|
|
}
|
|
|
|
content += clientConnection->readAll();
|
|
|
|
clientConnection->write("HTTP/1.1 200 OK");
|
|
|
|
clientConnection->disconnectFromHost();
|
|
|
|
// qDebug()<<"---------";
|
|
|
|
// qDebug()<<content;
|
|
|
|
content = getHTTPBody(content);
|
|
|
|
// qDebug()<<"*********";
|
|
|
|
// qDebug()<<content;
|
|
|
|
if (content.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(content,&error);
|
|
|
|
if (error.error!=QJsonParseError::NoError) {
|
|
|
|
qDebug()<<"Read http content failed!";
|
|
|
|
qDebug()<<error.errorString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonObject obj=doc.object();
|
2024-05-24 17:53:39 +08:00
|
|
|
//qDebug()<<obj;
|
|
|
|
QJsonObject batchObj = obj["batch"].toObject();
|
|
|
|
QString batchId = batchObj["id"].toString();
|
|
|
|
if (mBatchId!=batchId) {
|
|
|
|
mBatchId = batchId;
|
|
|
|
mBatchCount = batchObj["size"].toInt();
|
|
|
|
mBatchProblemsRecieved = 0;
|
|
|
|
//emit newBatchReceived(mBatchCount);
|
|
|
|
}
|
|
|
|
|
2024-05-24 15:28:05 +08:00
|
|
|
QString name = obj["name"].toString();
|
|
|
|
|
|
|
|
POJProblem problem = std::make_shared<OJProblem>();
|
|
|
|
problem->name = name;
|
|
|
|
problem->url = obj["url"].toString();
|
2024-05-24 17:53:39 +08:00
|
|
|
if (obj.contains("timeLimit")) {
|
|
|
|
problem->timeLimit = obj["timeLimit"].toInt();
|
|
|
|
problem->timeLimitUnit = ProblemTimeLimitUnit::Milliseconds;
|
|
|
|
}
|
|
|
|
if (obj.contains("memoryLimit")) {
|
|
|
|
problem->memoryLimit = obj["memoryLimit"].toInt();
|
|
|
|
problem->memoryLimitUnit = ProblemMemoryLimitUnit::MB;
|
|
|
|
}
|
2024-05-24 15:28:05 +08:00
|
|
|
QJsonArray caseArray = obj["tests"].toArray();
|
|
|
|
foreach ( const QJsonValue& val, caseArray) {
|
|
|
|
QJsonObject caseObj = val.toObject();
|
|
|
|
POJProblemCase problemCase = std::make_shared<OJProblemCase>();
|
|
|
|
problemCase->testState = ProblemCaseTestState::NotTested;
|
|
|
|
problemCase->name = tr("Problem Case %1").arg(problem->cases.count()+1);
|
|
|
|
if (pSettings->executor().convertHTMLToTextForInput()) {
|
|
|
|
QTextDocument doc;
|
|
|
|
doc.setHtml(caseObj["input"].toString());
|
|
|
|
problemCase->input = doc.toPlainText();
|
|
|
|
} else
|
|
|
|
problemCase->input = caseObj["input"].toString();
|
|
|
|
if (pSettings->executor().convertHTMLToTextForExpected()) {
|
|
|
|
QTextDocument doc;
|
|
|
|
doc.setHtml(caseObj["output"].toString());
|
|
|
|
problemCase->expected = doc.toPlainText();
|
|
|
|
} else
|
|
|
|
problemCase->expected = caseObj["output"].toString();
|
|
|
|
problem->cases.append(problemCase);
|
|
|
|
}
|
2024-05-24 17:53:39 +08:00
|
|
|
mBatchProblemsRecieved++;
|
|
|
|
emit newProblemReceived(mBatchProblemsRecieved, mBatchCount, problem);
|
|
|
|
// if (mBatchProblemsRecieved == mBatchCount) {
|
|
|
|
// emit batchFinished(mBatchCount);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
CompetitiveCompanionThread::CompetitiveCompanionThread(QObject *parent):
|
|
|
|
QThread{parent},
|
|
|
|
mStop{false},
|
2024-05-24 23:20:11 +08:00
|
|
|
mBatchProblemsRecieved{0},
|
|
|
|
mStartSemaphore{0},
|
|
|
|
mStartOk{false}
|
2024-05-24 17:53:39 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompetitiveCompanionThread::stop()
|
|
|
|
{
|
|
|
|
mStop = true;
|
|
|
|
}
|
|
|
|
|
2024-05-24 23:20:11 +08:00
|
|
|
bool CompetitiveCompanionThread::waitStart()
|
2024-05-24 17:53:39 +08:00
|
|
|
{
|
2024-05-24 23:20:11 +08:00
|
|
|
mStartSemaphore.acquire(1);
|
|
|
|
return mStartOk;
|
2024-05-24 17:53:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompetitiveCompanionThread::run()
|
|
|
|
{
|
2024-05-24 23:20:11 +08:00
|
|
|
QTcpServer tcpServer;
|
|
|
|
mStartOk = tcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort());
|
|
|
|
if (!mStartOk) {
|
|
|
|
mStop=true;
|
|
|
|
}
|
|
|
|
mStartSemaphore.release(1);
|
2024-05-24 17:53:39 +08:00
|
|
|
while(!mStop) {
|
2024-05-24 23:20:11 +08:00
|
|
|
tcpServer.waitForNewConnection(100);
|
|
|
|
while (tcpServer.hasPendingConnections()) {
|
|
|
|
QTcpSocket* clientConnection = tcpServer.nextPendingConnection();
|
|
|
|
onNewProblemConnection(clientConnection);
|
|
|
|
delete clientConnection;
|
|
|
|
}
|
2024-05-24 17:53:39 +08:00
|
|
|
}
|
2024-05-24 23:20:11 +08:00
|
|
|
tcpServer.close();
|
2024-05-24 15:28:05 +08:00
|
|
|
}
|