make CompetitiveCompanionThread really work.
This commit is contained in:
parent
a6da52980d
commit
2a536f6d22
|
@ -19,6 +19,7 @@
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
#include <QTcpServer>
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
#include "ojproblemset.h"
|
#include "ojproblemset.h"
|
||||||
|
@ -38,14 +39,14 @@ void CompetitiveCompanionHandler::start()
|
||||||
if (!pSettings->executor().enableCompetitiveCompanion())
|
if (!pSettings->executor().enableCompetitiveCompanion())
|
||||||
return;
|
return;
|
||||||
mThread = new CompetitiveCompanionThread(this);
|
mThread = new CompetitiveCompanionThread(this);
|
||||||
if (!mThread->listen()) {
|
connect(mThread,
|
||||||
delete mThread;
|
|
||||||
mThread = nullptr;
|
|
||||||
} else {
|
|
||||||
connect(mThread,
|
|
||||||
&CompetitiveCompanionThread::newProblemReceived,
|
&CompetitiveCompanionThread::newProblemReceived,
|
||||||
this, &CompetitiveCompanionHandler::onNewProblemReceived);
|
this, &CompetitiveCompanionHandler::onNewProblemReceived);
|
||||||
mThread->start();
|
mThread->start();
|
||||||
|
if (!mThread->waitStart()) {
|
||||||
|
qDebug()<<"Failed to listen!";
|
||||||
|
delete mThread;
|
||||||
|
mThread = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,12 +65,8 @@ void CompetitiveCompanionHandler::onNewProblemReceived(int num, int total, POJPr
|
||||||
emit newProblemReceived(num, total, newProblem);
|
emit newProblemReceived(num, total, newProblem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompetitiveCompanionThread::onNewProblemConnection()
|
void CompetitiveCompanionThread::onNewProblemConnection(QTcpSocket* clientConnection)
|
||||||
{
|
{
|
||||||
QTcpSocket* clientConnection = mTcpServer.nextPendingConnection();
|
|
||||||
connect(clientConnection, &QAbstractSocket::disconnected,
|
|
||||||
clientConnection, &QObject::deleteLater);
|
|
||||||
|
|
||||||
QByteArray content;
|
QByteArray content;
|
||||||
int unreadCount = 0;
|
int unreadCount = 0;
|
||||||
while (clientConnection->state() == QTcpSocket::ConnectedState) {
|
while (clientConnection->state() == QTcpSocket::ConnectedState) {
|
||||||
|
@ -156,10 +153,10 @@ void CompetitiveCompanionThread::onNewProblemConnection()
|
||||||
CompetitiveCompanionThread::CompetitiveCompanionThread(QObject *parent):
|
CompetitiveCompanionThread::CompetitiveCompanionThread(QObject *parent):
|
||||||
QThread{parent},
|
QThread{parent},
|
||||||
mStop{false},
|
mStop{false},
|
||||||
mBatchProblemsRecieved{0}
|
mBatchProblemsRecieved{0},
|
||||||
|
mStartSemaphore{0},
|
||||||
|
mStartOk{false}
|
||||||
{
|
{
|
||||||
connect(&mTcpServer,&QTcpServer::newConnection,
|
|
||||||
this, &CompetitiveCompanionThread::onNewProblemConnection);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompetitiveCompanionThread::stop()
|
void CompetitiveCompanionThread::stop()
|
||||||
|
@ -167,18 +164,27 @@ void CompetitiveCompanionThread::stop()
|
||||||
mStop = true;
|
mStop = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CompetitiveCompanionThread::listen()
|
bool CompetitiveCompanionThread::waitStart()
|
||||||
{
|
{
|
||||||
if (mTcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort())) {
|
mStartSemaphore.acquire(1);
|
||||||
return true;
|
return mStartOk;
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CompetitiveCompanionThread::run()
|
void CompetitiveCompanionThread::run()
|
||||||
{
|
{
|
||||||
while(!mStop) {
|
QTcpServer tcpServer;
|
||||||
QThread::msleep(100);
|
mStartOk = tcpServer.listen(QHostAddress::LocalHost,pSettings->executor().competivieCompanionPort());
|
||||||
|
if (!mStartOk) {
|
||||||
|
mStop=true;
|
||||||
}
|
}
|
||||||
mTcpServer.close();
|
mStartSemaphore.release(1);
|
||||||
|
while(!mStop) {
|
||||||
|
tcpServer.waitForNewConnection(100);
|
||||||
|
while (tcpServer.hasPendingConnections()) {
|
||||||
|
QTcpSocket* clientConnection = tcpServer.nextPendingConnection();
|
||||||
|
onNewProblemConnection(clientConnection);
|
||||||
|
delete clientConnection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tcpServer.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,9 +17,9 @@
|
||||||
#ifndef COMPETITIVECOMPANIONHANDLER_H
|
#ifndef COMPETITIVECOMPANIONHANDLER_H
|
||||||
#define COMPETITIVECOMPANIONHANDLER_H
|
#define COMPETITIVECOMPANIONHANDLER_H
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QTcpServer>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QSemaphore>
|
||||||
|
|
||||||
class OJProblem;
|
class OJProblem;
|
||||||
using POJProblem = std::shared_ptr<OJProblem>;
|
using POJProblem = std::shared_ptr<OJProblem>;
|
||||||
|
@ -32,22 +32,23 @@ public:
|
||||||
CompetitiveCompanionThread(const CompetitiveCompanionThread&) = delete;
|
CompetitiveCompanionThread(const CompetitiveCompanionThread&) = delete;
|
||||||
CompetitiveCompanionThread &operator=(const CompetitiveCompanionThread&) = delete;
|
CompetitiveCompanionThread &operator=(const CompetitiveCompanionThread&) = delete;
|
||||||
void stop();
|
void stop();
|
||||||
bool listen();
|
bool waitStart();
|
||||||
signals:
|
signals:
|
||||||
void newProblemReceived(int num, int total, POJProblem newProblem);
|
void newProblemReceived(int num, int total, POJProblem newProblem);
|
||||||
// void newBatchReceived(int total);
|
// void newBatchReceived(int total);
|
||||||
// void batchFinished(int total);
|
// void batchFinished(int total);
|
||||||
private slots:
|
private slots:
|
||||||
void onNewProblemConnection();
|
void onNewProblemConnection(QTcpSocket* connection);
|
||||||
// QThread interface
|
// QThread interface
|
||||||
protected:
|
protected:
|
||||||
void run() override;
|
void run() override;
|
||||||
private:
|
private:
|
||||||
QTcpServer mTcpServer;
|
|
||||||
bool mStop;
|
bool mStop;
|
||||||
QString mBatchId;
|
QString mBatchId;
|
||||||
int mBatchCount;
|
int mBatchCount;
|
||||||
int mBatchProblemsRecieved;
|
int mBatchProblemsRecieved;
|
||||||
|
QSemaphore mStartSemaphore;
|
||||||
|
bool mStartOk;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue