版權聲明:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/liulihuo_gyh/article/details/78425763
類似BS模式,客戶端發送任務請求給服務端,服務端將處理結果返回給客戶端。 redis負責消息的存儲和轉發。
仿真病人掛號看病,Patient進程進行掛號,Doctor進程進行看病 ,程序代碼如下:
////////////////////////////////////////////Patient////////////////////////////////////////////
Patient.h:
-
-
-
class QRedis;
-
-
class Patient : public QObject
-
{
-
Q_OBJECT
-
-
public:
-
Patient(QObject *parent = nullptr);
-
~Patient();
-
-
public slots:
-
void pushTask(); //push任務
-
-
private:
-
void popResult(); //pop結果
-
QRedis * m_redis;
-
};
-
-
-
-
-
-
-
static const QString KEYTASK = "MARKTASK";
-
static const QString KEYRESULT = "MARKRESULT";
-
-
Patient::Patient(QObject *parent)
-
: QObject(parent)
-
{
-
//初始化通道
-
m_redis = new QRedis(this);
-
m_redis->connectHost( "127.0.0.1", 6379);
-
m_redis->auth( "1234");
-
-
qDebug() << "client thread id :" << int(QThread::currentThreadId());
-
-
//輪詢任務
-
QTimer * timer = new QTimer(this);
-
connect(timer, &QTimer::timeout, this, &Patient::popResult);
-
timer->start( 20);
-
-
m_redis->del(KEYRESULT);
-
m_redis->del(KEYTASK);
-
pushTask();
-
}
-
-
Patient::~Patient()
-
{
-
}
-
-
void Patient::pushTask()
-
{
-
static int i = 0;
-
QString task = QStringLiteral( "%1號,姓名:%2,狀態:%3").arg(++i).arg(QStringLiteral("病人%1").arg(i)).arg(QStringLiteral("掛號"));
-
qDebug() << "========================================================\n\n"<< task;
-
qDebug() << "thread id :" << int(QThread::currentThreadId());
-
qint64 ret = m_redis->rpush(KEYTASK, task);
-
}
-
-
void Patient::popResult()
-
{
-
QString state;
-
QString taskData = m_redis->lpop(KEYRESULT);
-
if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
-
{
-
return;
-
}
-
QEventLoop loop;
-
QTimer::singleShot( 5000, &loop, &QEventLoop::quit);
-
loop.exec();
-
pushTask();
-
}
-
-
-
-
-
-
-
int main(int argc, char *argv[])
-
{
-
QCoreApplication a(argc, argv);
-
-
qDebug() << QString( "main thread id = : %1").arg(int(QThread::currentThreadId()));
-
-
QThread patientThread;
-
Patient patient;
-
patient.moveToThread(&patientThread);
-
patientThread.start();
-
return a.exec();
-
}
/////////////////////////////////////////////////Docktor/////////////////////////////////////////////////
Docktor.h
-
-
-
-
-
class QRedis;
-
-
class Docktor : public QObject
-
{
-
Q_OBJECT
-
-
public:
-
Docktor(QObject *parent = nullptr);
-
~Docktor();
-
-
public slots:
-
void popTask(); //pop任務
-
-
private:
-
void pushResult(const QString &task); //push結果
-
-
QRedis * m_redis;
-
};
Docktor.cpp
-
-
-
-
-
-
-
static const QString KEYTASK = "MARKTASK";
-
static const QString KEYRESULT = "MARKRESULT";
-
-
Docktor::Docktor(QObject *parent)
-
: QObject(parent)
-
{
-
//初始化通道
-
m_redis = new QRedis(this);
-
m_redis->connectHost( "127.0.0.1", 6379);
-
m_redis->auth( "1234");
-
-
QTimer * timer = new QTimer(this);
-
connect(timer, &QTimer::timeout, this, &Docktor::popTask);
-
timer->start( 20);
-
}
-
-
Docktor::~Docktor()
-
{
-
}
-
-
void Docktor::popTask()
-
{
-
//獲取任務
-
QString taskData = m_redis->lpop(KEYTASK);
-
if (taskData.compare("nil", Qt::CaseInsensitive) == 0 || taskData.isEmpty())
-
{
-
//qDebug() << QString("wait..............................");
-
return;
-
}
-
//處理任務
-
pushResult(taskData);
-
}
-
-
void Docktor::pushResult(const QString &task)
-
{
-
QStringList taskDatas = task.split( ",");
-
QString state = taskDatas.at( 2);
-
taskDatas.removeLast();
-
taskDatas.append(QStringLiteral( "狀態:看病"));
-
//push處理結果
-
qDebug() << "========================================================\n\n" << taskDatas.join(",");
-
qDebug() << "thread id :" << int(QThread::currentThreadId());
-
qint64 ret = m_redis->rpush(KEYRESULT, taskDatas.join( ","));
-
}
-
-
-
-
-
-
-
int main(int argc, char *argv[])
-
{
-
QCoreApplication a(argc, argv);
-
qDebug() << QString( "main thread id = : %1").arg(int(QThread::currentThreadId()));
-
-
QThread docktorThread;
-
Docktor docktor;
-
docktor.moveToThread(&docktorThread);
-
docktorThread.start();
-
return a.exec();
-
}
/////////////////截圖/////////////